You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

4 lines
4.8 KiB

/*! jQuery asProgress - v0.1.1 - 2014-12-09
* https://github.com/amazingSurge/jquery-asProgress
* Copyright (c) 2014 amazingSurge; Licensed GPL */
!function(a,b,c){"use strict";function d(a){return"string"==typeof a&&-1!=a.indexOf("%")}function e(){return"undefined"!=typeof c.performance&&c.performance.now?c.performance.now():Date.now()}Date.now||(Date.now=function(){return(new Date).getTime()});for(var f=["webkit","moz"],g=0;g<f.length&&!c.requestAnimationFrame;++g){var h=f[g];c.requestAnimationFrame=c[h+"RequestAnimationFrame"],c.cancelAnimationFrame=c[h+"CancelAnimationFrame"]||c[h+"CancelRequestAnimationFrame"]}if(/iP(ad|hone|od).*OS (6|7)/.test(c.navigator.userAgent)||!c.requestAnimationFrame||!c.cancelAnimationFrame){var i=0;c.requestAnimationFrame=function(a){var b=Date.now(),c=Math.max(i+16,b);return setTimeout(function(){a(i=c)},c-b)},c.cancelAnimationFrame=clearTimeout}var j="asProgress",k=a[j]=function(b,c){this.element=b,this.$element=a(b),this.options=a.extend({},k.defaults,c,this.$element.data()),this.options.bootstrap?(this.namespace="progress",this.$target=this.$element.find(".progress-bar"),this.classes={label:this.namespace+"-label",bar:this.namespace+"-bar"}):(this.namespace=this.options.namespace,this.classes={label:this.namespace+"__label",bar:this.namespace+"__bar"},this.$target=this.$element,this.$element.addClass(this.namespace)),this.easing=k.easing[this.options.easing]||k.easing.ease,this.min=this.$target.attr("aria-valuemin"),this.max=this.$target.attr("aria-valuemax"),this.min=this.min?parseInt(this.min,10):this.options.min,this.max=this.max?parseInt(this.max,10):this.options.max,this.first=this.$target.attr("aria-valuenow"),this.first=this.first?parseInt(this.first,10):this.min,this.now=this.first,this.goal=this.options.goal,this._frameId=null,this.initialized=!1,this._trigger("init"),this.init()};k.defaults={namespace:"asProgress",bootstrap:!1,min:0,max:100,goal:100,speed:20,easing:"ease",labelCallback:function(a){var b=this.getPercentage(a);return b+"%"}};var l=function(a,b,c,d){function e(a,b){return 1-3*b+3*a}function f(a,b){return 3*b-6*a}function g(a){return 3*a}function h(a,b,c){return((e(b,c)*a+f(b,c))*a+g(b))*a}function i(a,b,c){return 3*e(b,c)*a*a+2*f(b,c)*a+g(b)}function j(b){for(var d=b,e=0;4>e;++e){var f=i(d,a,c);if(0===f)return d;var g=h(d,a,c)-b;d-=g/f}return d}return a===b&&c===d?{css:"linear",fn:function(a){return a}}:{css:"cubic-bezier("+a+","+b+","+c+","+d+")",fn:function(a){return h(j(a),b,d)}}};a.extend(k.easing={},{ease:l(.25,.1,.25,1),linear:l(0,0,1,1),"ease-in":l(.42,0,1,1),"ease-out":l(0,0,.58,1),"ease-in-out":l(.42,0,.58,1)}),k.prototype={constructor:k,init:function(){this.$bar=this.$element.find("."+this.classes.bar),this.$label=this.$element.find("."+this.classes.label),this.reset(),this.initialized=!0,this._trigger("ready")},_trigger:function(a){var b=Array.prototype.slice.call(arguments,1),c=[this].concat(b);this.$element.trigger(j+"::"+a,c),a=a.replace(/\b\w+\b/g,function(a){return a.substring(0,1).toUpperCase()+a.substring(1)});var d="on"+a;"function"==typeof this.options[d]&&this.options[d].apply(this,b)},getPercentage:function(a){return Math.round(100*(a-this.min)/(this.max-this.min))},go:function(a){var b=this;this._clear(),d(a)&&(a=parseInt(a.replace("%",""),10),a=Math.round(this.min+a/100*(this.max-this.min))),"undefined"==typeof a&&(a=this.goal),a>this.max?a=this.max:a<this.min&&(a=this.min);var f=b.now,g=e(),h=function(d){var e=(d-g)/b.options.speed,i=Math.round(b.easing.fn(e/100)*(b.max-b.min));a>f?(i=f+i,i>a&&(i=a)):(i=f-i,a>i&&(i=a)),b._update(i),i===a?(c.cancelAnimationFrame(b._frameId),b._frameId=null,b.now===b.goal&&b._trigger("finish")):b._frameId=c.requestAnimationFrame(h)};b._frameId=c.requestAnimationFrame(h)},_update:function(a){this.now=a;var b=this.getPercentage(this.now);this.$bar.css("width",b+"%"),this.$target.attr("aria-valuenow",this.now),this.$label.length>0&&"function"==typeof this.options.labelCallback&&this.$label.html(this.options.labelCallback.call(this,[this.now])),this._trigger("update",a)},get:function(){return this.now},start:function(){this._clear(),this._trigger("start"),this.go(this.goal)},_clear:function(){this._frameId&&(c.cancelAnimationFrame(this._frameId),this._frameId=null)},reset:function(){this._clear(),this._update(this.first),this._trigger("reset")},stop:function(){this._clear(),this._trigger("stop")},finish:function(){this._clear(),this._update(this.goal),this._trigger("finish")},destory:function(){this.$element.data(j,null),this._trigger("destory")}},a.fn[j]=function(b){if("string"!=typeof b)return this.each(function(){a.data(this,j)||a.data(this,j,new k(this,b))});var c=b,d=Array.prototype.slice.call(arguments,1);if(/^\_/.test(c))return!1;if(!/^(get)$/.test(c))return this.each(function(){var b=a.data(this,j);b&&"function"==typeof b[c]&&b[c].apply(b,d)});var e=this.first().data(j);return e&&"function"==typeof e[c]?e[c].apply(e,d):void 0}}(jQuery,document,window);