From 1452cade272d876277e65215e91b476b4cce4a11 Mon Sep 17 00:00:00 2001 From: Max Shishkin Date: Wed, 3 Jul 2013 00:03:42 +0400 Subject: [PATCH] Add progress tracking --- dist/spin.js | 41 ++++++++++++++++++++++++++++++++++++++++- dist/spin.min.js | 2 +- index.html | 30 +++++++++++++++++++++++++++++- spin.js | 41 ++++++++++++++++++++++++++++++++++++++++- 4 files changed, 110 insertions(+), 4 deletions(-) diff --git a/dist/spin.js b/dist/spin.js index c66c607..c974edf 100644 --- a/dist/spin.js +++ b/dist/spin.js @@ -130,6 +130,26 @@ return o } + /** + * Returns progress tracker display. + */ + function createProgressTracker(radius, length, offsetTop, offsetLeft, color) { + var dim = 2 * (radius + length) + + if (dim < 50) dim = 50 + + return css(createEl('div', {innerHTML: '0%'}), { + textAlign : 'center', + width : dim + 'px', + height: dim + 'px', + lineHeight: dim + 'px', + position : 'absolute', + top : (offsetTop - dim / 2) + 'px', + left : (offsetLeft - dim / 2) + 'px', + color : color + }) + } + // Built-in defaults var defaults = { @@ -149,7 +169,10 @@ className: 'spinner', // CSS class to assign to the element top: 'auto', // center vertically left: 'auto', // center horizontally - position: 'relative' // element position + position: 'relative', // element position + progress: false, // show progress tracker + progressTop: 0, // offset top for progress tracker + progressLeft: 0 // offset left for progress tracker } /** The constructor */ @@ -191,6 +214,11 @@ el.setAttribute('role', 'progressbar') self.lines(el, self.opts) + if (o.progress) { + self.progressTracker = createProgressTracker(o.radius, o.length, o.progressTop, o.progressLeft, o.color) + ins(el, self.progressTracker) + } + if (!useCssAnimations) { // No CSS animation support, use setTimeout() instead var i = 0 @@ -208,6 +236,7 @@ self.opacity(el, j * o.direction + start, alpha, o) } + self.timeout = self.el && setTimeout(anim, ~~(1000/fps)) })() } @@ -227,6 +256,16 @@ return this }, + /** + * Update progress status. + */ + setProgress: function(completed, total) { + if (this.progressTracker) { + total = total || 100 + this.progressTracker.innerHTML = Math.round((completed / total) * 100) + '%' + } + }, + /** * Internal method that draws the individual lines. Will be overwritten * in VML fallback mode below. diff --git a/dist/spin.min.js b/dist/spin.min.js index 7fc8fa6..d2d3aeb 100644 --- a/dist/spin.min.js +++ b/dist/spin.min.js @@ -1 +1 @@ -(function(t,e){if(typeof exports=="object")module.exports=e();else if(typeof define=="function"&&define.amd)define(e);else t.Spinner=e()})(this,function(){"use strict";var t=["webkit","Moz","ms","O"],e={},i;function o(t,e){var i=document.createElement(t||"div"),o;for(o in e)i[o]=e[o];return i}function n(t){for(var e=1,i=arguments.length;e>1):parseInt(n.left,10)+s)+"px",top:(n.top=="auto"?l.y-a.y+(t.offsetHeight>>1):parseInt(n.top,10)+s)+"px"})}r.setAttribute("role","progressbar");e.lines(r,e.opts);if(!i){var u=0,p=(n.lines-1)*(1-n.direction)/2,c,h=n.fps,m=h/n.speed,y=(1-n.opacity)/(m*n.trail/100),g=m/n.lines;(function v(){u++;for(var t=0;t>1)+"px"})}for(;r',e)}r.addRule(".spin-vml","behavior:url(#default#VML)");p.prototype.lines=function(e,i){var o=i.length+i.width,r=2*o;function s(){return f(t("group",{coordsize:r+" "+r,coordorigin:-o+" "+-o}),{width:r,height:r})}var a=-(i.width+i.length)*2+"px",l=f(s(),{position:"absolute",top:a,left:a}),d;function u(e,r,a){n(l,n(f(s(),{rotation:360/i.lines*e+"deg",left:~~r}),n(f(t("roundrect",{arcsize:i.corners}),{width:o,height:i.width,left:i.radius,top:-i.width>>1,filter:a}),t("fill",{color:i.color,opacity:i.opacity}),t("stroke",{opacity:0}))))}if(i.shadow)for(d=1;d<=i.lines;d++)u(d,-2,"progid:DXImageTransform.Microsoft.Blur(pixelradius=2,makeshadow=1,shadowopacity=.3)");for(d=1;d<=i.lines;d++)u(d);return n(e,l)};p.prototype.opacity=function(t,e,i,o){var n=t.firstChild;o=o.shadow&&o.lines||0;if(n&&e+o>1):parseInt(n.left,10)+a)+"px",top:(n.top=="auto"?c.y-l.y+(t.offsetHeight>>1):parseInt(n.top,10)+a)+"px"})}s.setAttribute("role","progressbar");e.lines(s,e.opts);if(n.progress){e.progressTracker=u(n.radius,n.length,n.progressTop,n.progressLeft,n.color);o(s,e.progressTracker)}if(!i){var d=0,h=(n.lines-1)*(1-n.direction)/2,g,m=n.fps,y=m/n.speed,x=(1-n.opacity)/(y*n.trail/100),v=y/n.lines;!function w(){d++;for(var t=0;t>1)+"px"})}for(;n',e)}n.addRule(".spin-vml","behavior:url(#default#VML)");d.prototype.lines=function(e,i){var r=i.length+i.width,n=2*r;function s(){return f(t("group",{coordsize:n+" "+n,coordorigin:-r+" "+-r}),{width:n,height:n})}var a=-(i.width+i.length)*2+"px",l=f(s(),{position:"absolute",top:a,left:a}),p;function u(e,n,a){o(l,o(f(s(),{rotation:360/i.lines*e+"deg",left:~~n}),o(f(t("roundrect",{arcsize:i.corners}),{width:r,height:i.width,left:i.radius,top:-i.width>>1,filter:a}),t("fill",{color:i.color,opacity:i.opacity}),t("stroke",{opacity:0}))))}if(i.shadow)for(p=1;p<=i.lines;p++)u(p,-2,"progid:DXImageTransform.Microsoft.Blur(pixelradius=2,makeshadow=1,shadowopacity=.3)");for(p=1;p<=i.lines;p++)u(p);return o(e,l)};d.prototype.opacity=function(t,e,i,r){var o=t.firstChild;r=r.shadow&&r.lines||0;if(o&&e+rExample


+ Progress
+
+
+
+