progress.min.js 14 KB

1234567891011
  1. /*
  2. * # Fomantic UI - 2.8.4
  3. * https://github.com/fomantic/Fomantic-UI
  4. * http://fomantic-ui.com/
  5. *
  6. * Copyright 2014 Contributors
  7. * Released under the MIT license
  8. * http://opensource.org/licenses/MIT
  9. *
  10. */
  11. !function(E,e,T,P){"use strict";E.isFunction=E.isFunction||function(e){return"function"==typeof e&&"number"!=typeof e.nodeType},e=void 0!==e&&e.Math==Math?e:"undefined"!=typeof self&&self.Math==Math?self:Function("return this")(),E.fn.progress=function(m){var b,e=E(this),h=e.selector||"",y=(new Date).getTime(),x=[],w=m,A="string"==typeof w,V=[].slice.call(arguments,1);return e.each(function(){var u,s=E.isPlainObject(m)?E.extend(!0,{},E.fn.progress.settings,m):E.extend({},E.fn.progress.settings),n=s.className,t=s.metadata,e=s.namespace,r=s.selector,l=s.error,a="."+e,o="module-"+e,c=E(this),p=E(this).find(r.bar),i=E(this).find(r.progress),d=E(this).find(r.label),g=this,v=c.data(o),f=!1;u={helper:{sum:function(e){return Array.isArray(e)?e.reduce(function(e,t){return e+Number(t)},0):0},derivePrecision:function(e,t){for(var n=0,r=1,a=e/t;n<10&&!(1<(a*=r));)r=Math.pow(10,n++);return r},forceArray:function(e){return Array.isArray(e)?e:isNaN(e)?"string"==typeof e?e.split(","):[]:[e]}},initialize:function(){u.set.duration(),u.set.transitionEvent(),u.debug(g),u.read.metadata(),u.read.settings(),u.instantiate()},instantiate:function(){u.verbose("Storing instance of progress",u),v=u,c.data(o,u)},destroy:function(){u.verbose("Destroying previous progress for",c),clearInterval(v.interval),u.remove.state(),c.removeData(o),v=P},reset:function(){u.remove.nextValue(),u.update.progress(0)},complete:function(e){(u.percent===P||u.percent<100)&&(u.remove.progressPoll(),!0!==e&&u.set.percent(100))},read:{metadata:function(){var e={percent:u.helper.forceArray(c.data(t.percent)),total:c.data(t.total),value:u.helper.forceArray(c.data(t.value))};e.total&&(u.debug("Total value set from metadata",e.total),u.set.total(e.total)),0<e.value.length&&(u.debug("Current value set from metadata",e.value),u.set.value(e.value),u.set.progress(e.value)),0<e.percent.length&&(u.debug("Current percent value set from metadata",e.percent),u.set.percent(e.percent))},settings:function(){!1!==s.total&&(u.debug("Current total set in settings",s.total),u.set.total(s.total)),!1!==s.value&&(u.debug("Current value set in settings",s.value),u.set.value(s.value),u.set.progress(u.value)),!1!==s.percent&&(u.debug("Current percent set in settings",s.percent),u.set.percent(s.percent))}},bind:{transitionEnd:function(t){var e=u.get.transitionEnd();p.one(e+a,function(e){clearTimeout(u.failSafeTimer),t.call(this,e)}),u.failSafeTimer=setTimeout(function(){p.triggerHandler(e)},s.duration+s.failSafeDelay),u.verbose("Adding fail safe timer",u.timer)}},increment:function(e){var t,n;e=u.has.total()?(t=u.get.value(),e||1):(t=u.get.percent(),e||u.get.randomValue()),n=t+e,u.debug("Incrementing percentage by",t,n,e),n=u.get.normalizedValue(n),u.set.progress(n)},decrement:function(e){var t,n;u.get.total()?(n=(t=u.get.value())-(e=e||1),u.debug("Decrementing value by",e,t)):(n=(t=u.get.percent())-(e=e||u.get.randomValue()),u.debug("Decrementing percentage by",e,t)),n=u.get.normalizedValue(n),u.set.progress(n)},has:{progressPoll:function(){return u.progressPoll},total:function(){return!1!==u.get.total()}},get:{text:function(e,t){var n=t||0,r=u.get.value(n),a=u.total||0,o=f?u.get.displayPercent(n):u.get.percent(n),i=0<u.total?a-r:100-o;return e=(e=e||"").replace("{value}",r).replace("{total}",a).replace("{left}",i).replace("{percent}",o).replace("{bar}",s.text.bars[n]||""),u.verbose("Adding variables to progress bar text",e),e},normalizedValue:function(e){if(e<0)return u.debug("Value cannot decrement below 0"),0;if(u.has.total()){if(e>u.total)return u.debug("Value cannot increment above total",u.total),u.total}else if(100<e)return u.debug("Value cannot increment above 100 percent"),100;return e},updateInterval:function(){return"auto"==s.updateInterval?s.duration:s.updateInterval},randomValue:function(){return u.debug("Generating random increment percentage"),Math.floor(Math.random()*s.random.max+s.random.min)},numericValue:function(e){return"string"==typeof e?""!==e.replace(/[^\d.]/g,"")&&+e.replace(/[^\d.]/g,""):e},transitionEnd:function(){var e,t=T.createElement("element"),n={transition:"transitionend",OTransition:"oTransitionEnd",MozTransition:"transitionend",WebkitTransition:"webkitTransitionEnd"};for(e in n)if(t.style[e]!==P)return n[e]},displayPercent:function(e){var t=E(p[e]),n=t.width(),r=c.width(),a=parseInt(t.css("min-width"),10)<n?n/r*100:u.percent;return 0<s.precision?Math.round(a*(10*s.precision))/(10*s.precision):Math.round(a)},percent:function(e){return u.percent&&u.percent[e||0]||0},value:function(e){return u.nextValue||u.value&&u.value[e||0]||0},total:function(){return u.total||!1}},create:{progressPoll:function(){u.progressPoll=setTimeout(function(){u.update.toNextValue(),u.remove.progressPoll()},u.get.updateInterval())}},is:{complete:function(){return u.is.success()||u.is.warning()||u.is.error()},success:function(){return c.hasClass(n.success)},warning:function(){return c.hasClass(n.warning)},error:function(){return c.hasClass(n.error)},active:function(){return c.hasClass(n.active)},visible:function(){return c.is(":visible")}},remove:{progressPoll:function(){u.verbose("Removing progress poll timer"),u.progressPoll&&(clearTimeout(u.progressPoll),delete u.progressPoll)},nextValue:function(){u.verbose("Removing progress value stored for next update"),delete u.nextValue},state:function(){u.verbose("Removing stored state"),delete u.total,delete u.percent,delete u.value},active:function(){u.verbose("Removing active state"),c.removeClass(n.active)},success:function(){u.verbose("Removing success state"),c.removeClass(n.success)},warning:function(){u.verbose("Removing warning state"),c.removeClass(n.warning)},error:function(){u.verbose("Removing error state"),c.removeClass(n.error)}},set:{barWidth:function(e){u.debug("set bar width with ",e),e=u.helper.forceArray(e);var a=-1,o=-1,i=u.helper.sum(e),s=p.length,l=1<s,t=e.map(function(e,t){var n=t===s-1&&0===i,r=E(p[t]);return 0===e&&l&&!n?r.css("display","none"):(l&&n&&r.css("background","transparent"),-1==a&&(a=t),o=t,r.css({display:"block",width:e+"%"})),parseFloat(e)});e.forEach(function(e,t){E(p[t]).css({borderTopLeftRadius:t==a?"":0,borderBottomLeftRadius:t==a?"":0,borderTopRightRadius:t==o?"":0,borderBottomRightRadius:t==o?"":0})}),c.attr("data-percent",t)},duration:function(e){e="number"==typeof(e=e||s.duration)?e+"ms":e,u.verbose("Setting progress bar transition duration",e),p.css({"transition-duration":e})},percent:function(e){e=u.helper.forceArray(e).map(function(e){return"string"==typeof e?+e.replace("%",""):e});var t=u.has.total(),n=u.helper.sum(e),r=1<e.length&&t,a=u.helper.sum(u.helper.forceArray(u.value));if(r&&a>u.total)u.error(l.sumExceedsTotal,a,u.total);else if(!r&&100<n)u.error(l.tooHigh,n);else if(n<0)u.error(l.tooLow,n);else{var o=0<s.precision?s.precision:r?u.helper.derivePrecision(Math.min.apply(null,u.value),u.total):P,i=e.map(function(e){return 0<o?Math.round(e*(10*o))/(10*o):Math.round(e)});u.percent=i,t||(u.value=i.map(function(e){return 0<o?Math.round(e/100*u.total*(10*o))/(10*o):Math.round(e/100*u.total*10)/10}),s.limitValues&&(u.value=u.value.map(function(e){return 100<e?100:u.value<0?0:u.value}))),u.set.barWidth(e),u.set.labelInterval(),u.set.labels()}s.onChange.call(g,e,u.value,u.total)},labelInterval:function(){clearInterval(u.interval),u.bind.transitionEnd(function(){u.verbose("Bar finished animating, removing continuous label updates"),clearInterval(u.interval),f=!1,u.set.labels()}),f=!0,u.interval=setInterval(function(){E.contains(T.documentElement,g)||(clearInterval(u.interval),f=!1),u.set.labels()},s.framerate)},labels:function(){u.verbose("Setting both bar progress and outer label text"),u.set.barLabel(),u.set.state()},label:function(e){(e=e||"")&&(e=u.get.text(e),u.verbose("Setting label to text",e),d.text(e))},state:function(e){100===(e=e!==P?e:u.helper.sum(u.percent))?s.autoSuccess&&1===p.length&&!(u.is.warning()||u.is.error()||u.is.success())?(u.set.success(),u.debug("Automatically triggering success at 100%")):(u.verbose("Reached 100% removing active state"),u.remove.active(),u.remove.progressPoll()):0<e?(u.verbose("Adjusting active progress bar label",e),u.set.active()):(u.remove.active(),u.set.label(s.text.active))},barLabel:function(r){i.map(function(e,t){var n=E(t);r!==P?n.text(u.get.text(r,e)):"ratio"==s.label&&u.total?(u.verbose("Adding ratio to bar label"),n.text(u.get.text(s.text.ratio,e))):"percent"==s.label&&(u.verbose("Adding percentage to bar label"),n.text(u.get.text(s.text.percent,e)))})},active:function(e){e=e||s.text.active,u.debug("Setting active state"),s.showActivity&&!u.is.active()&&c.addClass(n.active),u.remove.warning(),u.remove.error(),u.remove.success(),(e=s.onLabelUpdate("active",e,u.value,u.total))&&u.set.label(e),u.bind.transitionEnd(function(){s.onActive.call(g,u.value,u.total)})},success:function(e,t){e=e||s.text.success||s.text.active,u.debug("Setting success state"),c.addClass(n.success),u.remove.active(),u.remove.warning(),u.remove.error(),u.complete(t),e=s.text.success?s.onLabelUpdate("success",e,u.value,u.total):s.onLabelUpdate("active",e,u.value,u.total),u.set.label(e),u.bind.transitionEnd(function(){s.onSuccess.call(g,u.total)})},warning:function(e,t){e=e||s.text.warning,u.debug("Setting warning state"),c.addClass(n.warning),u.remove.active(),u.remove.success(),u.remove.error(),u.complete(t),(e=s.onLabelUpdate("warning",e,u.value,u.total))&&u.set.label(e),u.bind.transitionEnd(function(){s.onWarning.call(g,u.value,u.total)})},error:function(e,t){e=e||s.text.error,u.debug("Setting error state"),c.addClass(n.error),u.remove.active(),u.remove.success(),u.remove.warning(),u.complete(t),(e=s.onLabelUpdate("error",e,u.value,u.total))&&u.set.label(e),u.bind.transitionEnd(function(){s.onError.call(g,u.value,u.total)})},transitionEvent:function(){u.get.transitionEnd()},total:function(e){u.total=e},value:function(e){u.value=u.helper.forceArray(e)},progress:function(e){u.has.progressPoll()?(u.debug("Updated within interval, setting next update to use new value",e),u.set.nextValue(e)):(u.debug("First update in progress update interval, immediately updating",e),u.update.progress(e),u.create.progressPoll())},nextValue:function(e){u.nextValue=e}},update:{toNextValue:function(){var e=u.nextValue;e&&(u.debug("Update interval complete using last updated value",e),u.update.progress(e),u.remove.nextValue())},progress:function(e){var n=u.has.total();n&&u.set.value(e);var t=u.helper.forceArray(e).map(function(e){var t;return!1===(e=u.get.numericValue(e))&&u.error(l.nonNumeric,e),e=u.get.normalizedValue(e),n?(t=e/u.total*100,u.debug("Calculating percent complete from total",t)):(t=e,u.debug("Setting value to exact percentage value",t)),t});u.set.percent(t)}},setting:function(e,t){if(u.debug("Changing setting",e,t),E.isPlainObject(e))E.extend(!0,s,e);else{if(t===P)return s[e];E.isPlainObject(s[e])?E.extend(!0,s[e],t):s[e]=t}},internal:function(e,t){if(E.isPlainObject(e))E.extend(!0,u,e);else{if(t===P)return u[e];u[e]=t}},debug:function(){!s.silent&&s.debug&&(s.performance?u.performance.log(arguments):(u.debug=Function.prototype.bind.call(console.info,console,s.name+":"),u.debug.apply(console,arguments)))},verbose:function(){!s.silent&&s.verbose&&s.debug&&(s.performance?u.performance.log(arguments):(u.verbose=Function.prototype.bind.call(console.info,console,s.name+":"),u.verbose.apply(console,arguments)))},error:function(){s.silent||(u.error=Function.prototype.bind.call(console.error,console,s.name+":"),u.error.apply(console,arguments))},performance:{log:function(e){var t,n;s.performance&&(n=(t=(new Date).getTime())-(y||t),y=t,x.push({Name:e[0],Arguments:[].slice.call(e,1)||"",Element:g,"Execution Time":n})),clearTimeout(u.performance.timer),u.performance.timer=setTimeout(u.performance.display,500)},display:function(){var e=s.name+":",n=0;y=!1,clearTimeout(u.performance.timer),E.each(x,function(e,t){n+=t["Execution Time"]}),e+=" "+n+"ms",h&&(e+=" '"+h+"'"),(console.group!==P||console.table!==P)&&0<x.length&&(console.groupCollapsed(e),console.table?console.table(x):E.each(x,function(e,t){console.log(t.Name+": "+t["Execution Time"]+"ms")}),console.groupEnd()),x=[]}},invoke:function(r,e,t){var a,o,n,i=v;return e=e||V,t=g||t,"string"==typeof r&&i!==P&&(r=r.split(/[\. ]/),a=r.length-1,E.each(r,function(e,t){var n=e!=a?t+r[e+1].charAt(0).toUpperCase()+r[e+1].slice(1):r;if(E.isPlainObject(i[n])&&e!=a)i=i[n];else{if(i[n]!==P)return o=i[n],!1;if(!E.isPlainObject(i[t])||e==a)return i[t]!==P?o=i[t]:u.error(l.method,r),!1;i=i[t]}})),E.isFunction(o)?n=o.apply(t,e):o!==P&&(n=o),Array.isArray(b)?b.push(n):b!==P?b=[b,n]:n!==P&&(b=n),o}},A?(v===P&&u.initialize(),u.invoke(w)):(v!==P&&v.invoke("destroy"),u.initialize())}),b!==P?b:this},E.fn.progress.settings={name:"Progress",namespace:"progress",silent:!1,debug:!1,verbose:!1,performance:!0,random:{min:2,max:5},duration:300,updateInterval:"auto",autoSuccess:!0,showActivity:!0,limitValues:!0,label:"percent",precision:0,framerate:1e3/30,percent:!1,total:!1,value:!1,failSafeDelay:100,onLabelUpdate:function(e,t,n,r){return t},onChange:function(e,t,n){},onSuccess:function(e){},onActive:function(e,t){},onError:function(e,t){},onWarning:function(e,t){},error:{method:"The method you called is not defined.",nonNumeric:"Progress value is non numeric",tooHigh:"Value specified is above 100%",tooLow:"Value specified is below 0%",sumExceedsTotal:"Sum of multple values exceed total"},regExp:{variable:/\{\$*[A-z0-9]+\}/g},metadata:{percent:"percent",total:"total",value:"value"},selector:{bar:"> .bar",label:"> .label",progress:".bar > .progress"},text:{active:!1,error:!1,success:!1,warning:!1,percent:"{percent}%",ratio:"{value} of {total}",bars:[""]},className:{active:"active",error:"error",success:"success",warning:"warning"}}}(jQuery,window,document);