1
0

progress.js 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  1. /*!
  2. * # Fomantic-UI - Progress
  3. * http://github.com/fomantic/Fomantic-UI/
  4. *
  5. *
  6. * Released under the MIT license
  7. * http://opensource.org/licenses/MIT
  8. *
  9. */
  10. ;(function ($, window, document, undefined) {
  11. 'use strict';
  12. $.isFunction = $.isFunction || function(obj) {
  13. return typeof obj === "function" && typeof obj.nodeType !== "number";
  14. };
  15. window = (typeof window != 'undefined' && window.Math == Math)
  16. ? window
  17. : (typeof self != 'undefined' && self.Math == Math)
  18. ? self
  19. : Function('return this')()
  20. ;
  21. $.fn.progress = function(parameters) {
  22. var
  23. $allModules = $(this),
  24. moduleSelector = $allModules.selector || '',
  25. time = new Date().getTime(),
  26. performance = [],
  27. query = arguments[0],
  28. methodInvoked = (typeof query == 'string'),
  29. queryArguments = [].slice.call(arguments, 1),
  30. returnedValue
  31. ;
  32. $allModules
  33. .each(function() {
  34. var
  35. settings = ( $.isPlainObject(parameters) )
  36. ? $.extend(true, {}, $.fn.progress.settings, parameters)
  37. : $.extend({}, $.fn.progress.settings),
  38. className = settings.className,
  39. metadata = settings.metadata,
  40. namespace = settings.namespace,
  41. selector = settings.selector,
  42. error = settings.error,
  43. eventNamespace = '.' + namespace,
  44. moduleNamespace = 'module-' + namespace,
  45. $module = $(this),
  46. $bars = $(this).find(selector.bar),
  47. $progresses = $(this).find(selector.progress),
  48. $label = $(this).find(selector.label),
  49. element = this,
  50. instance = $module.data(moduleNamespace),
  51. animating = false,
  52. transitionEnd,
  53. module
  54. ;
  55. module = {
  56. helper: {
  57. sum: function (nums) {
  58. return Array.isArray(nums) ? nums.reduce(function (left, right) {
  59. return left + Number(right);
  60. }, 0) : 0;
  61. },
  62. /**
  63. * Derive precision for multiple progress with total and values.
  64. *
  65. * This helper dervices a precision that is sufficiently large to show minimum value of multiple progress.
  66. *
  67. * Example1
  68. * - total: 1122
  69. * - values: [325, 111, 74, 612]
  70. * - min ratio: 74/1122 = 0.0659...
  71. * - required precision: 100
  72. *
  73. * Example2
  74. * - total: 10541
  75. * - values: [3235, 1111, 74, 6121]
  76. * - min ratio: 74/10541 = 0.0070...
  77. * - required precision: 1000
  78. *
  79. * @param min A minimum value within multiple values
  80. * @param total A total amount of multiple values
  81. * @returns {number} A precison. Could be 1, 10, 100, ... 1e+10.
  82. */
  83. derivePrecision: function(min, total) {
  84. var precisionPower = 0
  85. var precision = 1;
  86. var ratio = min / total;
  87. while (precisionPower < 10) {
  88. ratio = ratio * precision;
  89. if (ratio > 1) {
  90. break;
  91. }
  92. precision = Math.pow(10, precisionPower++);
  93. }
  94. return precision;
  95. },
  96. forceArray: function (element) {
  97. return Array.isArray(element)
  98. ? element
  99. : !isNaN(element)
  100. ? [element]
  101. : typeof element == 'string'
  102. ? element.split(',')
  103. : []
  104. ;
  105. }
  106. },
  107. initialize: function() {
  108. module.set.duration();
  109. module.set.transitionEvent();
  110. module.debug(element);
  111. module.read.metadata();
  112. module.read.settings();
  113. module.instantiate();
  114. },
  115. instantiate: function() {
  116. module.verbose('Storing instance of progress', module);
  117. instance = module;
  118. $module
  119. .data(moduleNamespace, module)
  120. ;
  121. },
  122. destroy: function() {
  123. module.verbose('Destroying previous progress for', $module);
  124. clearInterval(instance.interval);
  125. module.remove.state();
  126. $module.removeData(moduleNamespace);
  127. instance = undefined;
  128. },
  129. reset: function() {
  130. module.remove.nextValue();
  131. module.update.progress(0);
  132. },
  133. complete: function() {
  134. if(module.percent === undefined || module.percent < 100) {
  135. module.remove.progressPoll();
  136. module.set.percent(100);
  137. }
  138. },
  139. read: {
  140. metadata: function() {
  141. var
  142. data = {
  143. percent : module.helper.forceArray($module.data(metadata.percent)),
  144. total : $module.data(metadata.total),
  145. value : module.helper.forceArray($module.data(metadata.value))
  146. }
  147. ;
  148. if(data.total) {
  149. module.debug('Total value set from metadata', data.total);
  150. module.set.total(data.total);
  151. }
  152. if(data.value.length > 0) {
  153. module.debug('Current value set from metadata', data.value);
  154. module.set.value(data.value);
  155. module.set.progress(data.value);
  156. }
  157. if(data.percent.length > 0) {
  158. module.debug('Current percent value set from metadata', data.percent);
  159. module.set.percent(data.percent);
  160. }
  161. },
  162. settings: function() {
  163. if(settings.total !== false) {
  164. module.debug('Current total set in settings', settings.total);
  165. module.set.total(settings.total);
  166. }
  167. if(settings.value !== false) {
  168. module.debug('Current value set in settings', settings.value);
  169. module.set.value(settings.value);
  170. module.set.progress(module.value);
  171. }
  172. if(settings.percent !== false) {
  173. module.debug('Current percent set in settings', settings.percent);
  174. module.set.percent(settings.percent);
  175. }
  176. }
  177. },
  178. bind: {
  179. transitionEnd: function(callback) {
  180. var
  181. transitionEnd = module.get.transitionEnd()
  182. ;
  183. $bars
  184. .one(transitionEnd + eventNamespace, function(event) {
  185. clearTimeout(module.failSafeTimer);
  186. callback.call(this, event);
  187. })
  188. ;
  189. module.failSafeTimer = setTimeout(function() {
  190. $bars.triggerHandler(transitionEnd);
  191. }, settings.duration + settings.failSafeDelay);
  192. module.verbose('Adding fail safe timer', module.timer);
  193. }
  194. },
  195. increment: function(incrementValue) {
  196. var
  197. startValue,
  198. newValue
  199. ;
  200. if( module.has.total() ) {
  201. startValue = module.get.value();
  202. incrementValue = incrementValue || 1;
  203. }
  204. else {
  205. startValue = module.get.percent();
  206. incrementValue = incrementValue || module.get.randomValue();
  207. }
  208. newValue = startValue + incrementValue;
  209. module.debug('Incrementing percentage by', startValue, newValue, incrementValue);
  210. newValue = module.get.normalizedValue(newValue);
  211. module.set.progress(newValue);
  212. },
  213. decrement: function(decrementValue) {
  214. var
  215. total = module.get.total(),
  216. startValue,
  217. newValue
  218. ;
  219. if(total) {
  220. startValue = module.get.value();
  221. decrementValue = decrementValue || 1;
  222. newValue = startValue - decrementValue;
  223. module.debug('Decrementing value by', decrementValue, startValue);
  224. }
  225. else {
  226. startValue = module.get.percent();
  227. decrementValue = decrementValue || module.get.randomValue();
  228. newValue = startValue - decrementValue;
  229. module.debug('Decrementing percentage by', decrementValue, startValue);
  230. }
  231. newValue = module.get.normalizedValue(newValue);
  232. module.set.progress(newValue);
  233. },
  234. has: {
  235. progressPoll: function() {
  236. return module.progressPoll;
  237. },
  238. total: function() {
  239. return (module.get.total() !== false);
  240. }
  241. },
  242. get: {
  243. text: function(templateText, index) {
  244. var
  245. index_ = index || 0,
  246. value = module.get.value(index_),
  247. total = module.total || 0,
  248. percent = (animating)
  249. ? module.get.displayPercent(index_)
  250. : module.get.percent(index_),
  251. left = (module.total > 0)
  252. ? (total - value)
  253. : (100 - percent)
  254. ;
  255. templateText = templateText || '';
  256. templateText = templateText
  257. .replace('{value}', value)
  258. .replace('{total}', total)
  259. .replace('{left}', left)
  260. .replace('{percent}', percent)
  261. .replace('{bar}', settings.text.bars[index_] || '')
  262. ;
  263. module.verbose('Adding variables to progress bar text', templateText);
  264. return templateText;
  265. },
  266. normalizedValue: function(value) {
  267. if(value < 0) {
  268. module.debug('Value cannot decrement below 0');
  269. return 0;
  270. }
  271. if(module.has.total()) {
  272. if(value > module.total) {
  273. module.debug('Value cannot increment above total', module.total);
  274. return module.total;
  275. }
  276. }
  277. else if(value > 100 ) {
  278. module.debug('Value cannot increment above 100 percent');
  279. return 100;
  280. }
  281. return value;
  282. },
  283. updateInterval: function() {
  284. if(settings.updateInterval == 'auto') {
  285. return settings.duration;
  286. }
  287. return settings.updateInterval;
  288. },
  289. randomValue: function() {
  290. module.debug('Generating random increment percentage');
  291. return Math.floor((Math.random() * settings.random.max) + settings.random.min);
  292. },
  293. numericValue: function(value) {
  294. return (typeof value === 'string')
  295. ? (value.replace(/[^\d.]/g, '') !== '')
  296. ? +(value.replace(/[^\d.]/g, ''))
  297. : false
  298. : value
  299. ;
  300. },
  301. transitionEnd: function() {
  302. var
  303. element = document.createElement('element'),
  304. transitions = {
  305. 'transition' :'transitionend',
  306. 'OTransition' :'oTransitionEnd',
  307. 'MozTransition' :'transitionend',
  308. 'WebkitTransition' :'webkitTransitionEnd'
  309. },
  310. transition
  311. ;
  312. for(transition in transitions){
  313. if( element.style[transition] !== undefined ){
  314. return transitions[transition];
  315. }
  316. }
  317. },
  318. // gets current displayed percentage (if animating values this is the intermediary value)
  319. displayPercent: function(index) {
  320. var
  321. $bar = $($bars[index]),
  322. barWidth = $bar.width(),
  323. totalWidth = $module.width(),
  324. minDisplay = parseInt($bar.css('min-width'), 10),
  325. displayPercent = (barWidth > minDisplay)
  326. ? (barWidth / totalWidth * 100)
  327. : module.percent
  328. ;
  329. return (settings.precision > 0)
  330. ? Math.round(displayPercent * (10 * settings.precision)) / (10 * settings.precision)
  331. : Math.round(displayPercent)
  332. ;
  333. },
  334. percent: function(index) {
  335. return module.percent && module.percent[index || 0] || 0;
  336. },
  337. value: function(index) {
  338. return module.nextValue || module.value && module.value[index || 0] || 0;
  339. },
  340. total: function() {
  341. return module.total || false;
  342. }
  343. },
  344. create: {
  345. progressPoll: function() {
  346. module.progressPoll = setTimeout(function() {
  347. module.update.toNextValue();
  348. module.remove.progressPoll();
  349. }, module.get.updateInterval());
  350. },
  351. },
  352. is: {
  353. complete: function() {
  354. return module.is.success() || module.is.warning() || module.is.error();
  355. },
  356. success: function() {
  357. return $module.hasClass(className.success);
  358. },
  359. warning: function() {
  360. return $module.hasClass(className.warning);
  361. },
  362. error: function() {
  363. return $module.hasClass(className.error);
  364. },
  365. active: function() {
  366. return $module.hasClass(className.active);
  367. },
  368. visible: function() {
  369. return $module.is(':visible');
  370. }
  371. },
  372. remove: {
  373. progressPoll: function() {
  374. module.verbose('Removing progress poll timer');
  375. if(module.progressPoll) {
  376. clearTimeout(module.progressPoll);
  377. delete module.progressPoll;
  378. }
  379. },
  380. nextValue: function() {
  381. module.verbose('Removing progress value stored for next update');
  382. delete module.nextValue;
  383. },
  384. state: function() {
  385. module.verbose('Removing stored state');
  386. delete module.total;
  387. delete module.percent;
  388. delete module.value;
  389. },
  390. active: function() {
  391. module.verbose('Removing active state');
  392. $module.removeClass(className.active);
  393. },
  394. success: function() {
  395. module.verbose('Removing success state');
  396. $module.removeClass(className.success);
  397. },
  398. warning: function() {
  399. module.verbose('Removing warning state');
  400. $module.removeClass(className.warning);
  401. },
  402. error: function() {
  403. module.verbose('Removing error state');
  404. $module.removeClass(className.error);
  405. }
  406. },
  407. set: {
  408. barWidth: function(values) {
  409. module.debug("set bar width with ", values);
  410. values = module.helper.forceArray(values);
  411. var firstNonZeroIndex = -1;
  412. var lastNonZeroIndex = -1;
  413. var valuesSum = module.helper.sum(values);
  414. var barCounts = $bars.length;
  415. var isMultiple = barCounts > 1;
  416. var percents = values.map(function(value, index) {
  417. var allZero = (index === barCounts - 1 && valuesSum === 0);
  418. var $bar = $($bars[index]);
  419. if (value === 0 && isMultiple && !allZero) {
  420. $bar.css('display', 'none');
  421. } else {
  422. if (isMultiple && allZero) {
  423. $bar.css('background', 'transparent');
  424. }
  425. if (firstNonZeroIndex == -1) {
  426. firstNonZeroIndex = index;
  427. }
  428. lastNonZeroIndex = index;
  429. $bar.css({
  430. display: 'block',
  431. width: value + '%'
  432. });
  433. }
  434. return parseFloat(value);
  435. });
  436. values.forEach(function(_, index) {
  437. var $bar = $($bars[index]);
  438. $bar.css({
  439. borderTopLeftRadius: index == firstNonZeroIndex ? '' : 0,
  440. borderBottomLeftRadius: index == firstNonZeroIndex ? '' : 0,
  441. borderTopRightRadius: index == lastNonZeroIndex ? '' : 0,
  442. borderBottomRightRadius: index == lastNonZeroIndex ? '' : 0
  443. });
  444. });
  445. $module
  446. .attr('data-percent', percents)
  447. ;
  448. },
  449. duration: function(duration) {
  450. duration = duration || settings.duration;
  451. duration = (typeof duration == 'number')
  452. ? duration + 'ms'
  453. : duration
  454. ;
  455. module.verbose('Setting progress bar transition duration', duration);
  456. $bars
  457. .css({
  458. 'transition-duration': duration
  459. })
  460. ;
  461. },
  462. percent: function(percents) {
  463. percents = module.helper.forceArray(percents).map(function(percent) {
  464. return (typeof percent == 'string')
  465. ? +(percent.replace('%', ''))
  466. : percent
  467. ;
  468. });
  469. var hasTotal = module.has.total();
  470. var totalPecent = module.helper.sum(percents);
  471. var isMultpleValues = percents.length > 1 && hasTotal;
  472. var sumTotal = module.helper.sum(module.helper.forceArray(module.value));
  473. if (isMultpleValues && sumTotal > module.total) {
  474. // Sum values instead of pecents to avoid precision issues when summing floats
  475. module.error(error.sumExceedsTotal, sumTotal, module.total);
  476. } else if (!isMultpleValues && totalPecent > 100) {
  477. // Sum before rouding since sum of rounded may have error though sum of actual is fine
  478. module.error(error.tooHigh, totalPecent);
  479. } else if (totalPecent < 0) {
  480. module.error(error.tooLow, totalPecent);
  481. } else {
  482. var autoPrecision = settings.precision > 0
  483. ? settings.precision
  484. : isMultpleValues
  485. ? module.helper.derivePrecision(Math.min.apply(null, module.value), module.total)
  486. : undefined;
  487. // round display percentage
  488. var roundedPercents = percents.map(function (percent) {
  489. return (autoPrecision > 0)
  490. ? Math.round(percent * (10 * autoPrecision)) / (10 * autoPrecision)
  491. : Math.round(percent)
  492. ;
  493. });
  494. module.percent = roundedPercents;
  495. if (!hasTotal) {
  496. module.value = roundedPercents.map(function (percent) {
  497. return (autoPrecision > 0)
  498. ? Math.round((percent / 100) * module.total * (10 * autoPrecision)) / (10 * autoPrecision)
  499. : Math.round((percent / 100) * module.total * 10) / 10
  500. ;
  501. });
  502. if (settings.limitValues) {
  503. module.value = module.value.map(function (value) {
  504. return (value > 100)
  505. ? 100
  506. : (module.value < 0)
  507. ? 0
  508. : module.value;
  509. });
  510. }
  511. }
  512. module.set.barWidth(percents);
  513. module.set.labelInterval();
  514. module.set.labels();
  515. }
  516. settings.onChange.call(element, percents, module.value, module.total);
  517. },
  518. labelInterval: function() {
  519. var
  520. animationCallback = function() {
  521. module.verbose('Bar finished animating, removing continuous label updates');
  522. clearInterval(module.interval);
  523. animating = false;
  524. module.set.labels();
  525. }
  526. ;
  527. clearInterval(module.interval);
  528. module.bind.transitionEnd(animationCallback);
  529. animating = true;
  530. module.interval = setInterval(function() {
  531. var
  532. isInDOM = $.contains(document.documentElement, element)
  533. ;
  534. if(!isInDOM) {
  535. clearInterval(module.interval);
  536. animating = false;
  537. }
  538. module.set.labels();
  539. }, settings.framerate);
  540. },
  541. labels: function() {
  542. module.verbose('Setting both bar progress and outer label text');
  543. module.set.barLabel();
  544. module.set.state();
  545. },
  546. label: function(text) {
  547. text = text || '';
  548. if(text) {
  549. text = module.get.text(text);
  550. module.verbose('Setting label to text', text);
  551. $label.text(text);
  552. }
  553. },
  554. state: function(percent) {
  555. percent = (percent !== undefined)
  556. ? percent
  557. : module.helper.sum(module.percent)
  558. ;
  559. if(percent === 100) {
  560. if(settings.autoSuccess && $bars.length === 1 && !(module.is.warning() || module.is.error() || module.is.success())) {
  561. module.set.success();
  562. module.debug('Automatically triggering success at 100%');
  563. }
  564. else {
  565. module.verbose('Reached 100% removing active state');
  566. module.remove.active();
  567. module.remove.progressPoll();
  568. }
  569. }
  570. else if(percent > 0) {
  571. module.verbose('Adjusting active progress bar label', percent);
  572. module.set.active();
  573. }
  574. else {
  575. module.remove.active();
  576. module.set.label(settings.text.active);
  577. }
  578. },
  579. barLabel: function(text) {
  580. $progresses.map(function(index, element){
  581. var $progress = $(element);
  582. if (text !== undefined) {
  583. $progress.text( module.get.text(text, index) );
  584. }
  585. else if (settings.label == 'ratio' && module.total) {
  586. module.verbose('Adding ratio to bar label');
  587. $progress.text( module.get.text(settings.text.ratio, index) );
  588. }
  589. else if (settings.label == 'percent') {
  590. module.verbose('Adding percentage to bar label');
  591. $progress.text( module.get.text(settings.text.percent, index) );
  592. }
  593. });
  594. },
  595. active: function(text) {
  596. text = text || settings.text.active;
  597. module.debug('Setting active state');
  598. if(settings.showActivity && !module.is.active() ) {
  599. $module.addClass(className.active);
  600. }
  601. module.remove.warning();
  602. module.remove.error();
  603. module.remove.success();
  604. text = settings.onLabelUpdate('active', text, module.value, module.total);
  605. if(text) {
  606. module.set.label(text);
  607. }
  608. module.bind.transitionEnd(function() {
  609. settings.onActive.call(element, module.value, module.total);
  610. });
  611. },
  612. success : function(text) {
  613. text = text || settings.text.success || settings.text.active;
  614. module.debug('Setting success state');
  615. $module.addClass(className.success);
  616. module.remove.active();
  617. module.remove.warning();
  618. module.remove.error();
  619. module.complete();
  620. if(settings.text.success) {
  621. text = settings.onLabelUpdate('success', text, module.value, module.total);
  622. module.set.label(text);
  623. }
  624. else {
  625. text = settings.onLabelUpdate('active', text, module.value, module.total);
  626. module.set.label(text);
  627. }
  628. module.bind.transitionEnd(function() {
  629. settings.onSuccess.call(element, module.total);
  630. });
  631. },
  632. warning : function(text) {
  633. text = text || settings.text.warning;
  634. module.debug('Setting warning state');
  635. $module.addClass(className.warning);
  636. module.remove.active();
  637. module.remove.success();
  638. module.remove.error();
  639. module.complete();
  640. text = settings.onLabelUpdate('warning', text, module.value, module.total);
  641. if(text) {
  642. module.set.label(text);
  643. }
  644. module.bind.transitionEnd(function() {
  645. settings.onWarning.call(element, module.value, module.total);
  646. });
  647. },
  648. error : function(text) {
  649. text = text || settings.text.error;
  650. module.debug('Setting error state');
  651. $module.addClass(className.error);
  652. module.remove.active();
  653. module.remove.success();
  654. module.remove.warning();
  655. module.complete();
  656. text = settings.onLabelUpdate('error', text, module.value, module.total);
  657. if(text) {
  658. module.set.label(text);
  659. }
  660. module.bind.transitionEnd(function() {
  661. settings.onError.call(element, module.value, module.total);
  662. });
  663. },
  664. transitionEvent: function() {
  665. transitionEnd = module.get.transitionEnd();
  666. },
  667. total: function(totalValue) {
  668. module.total = totalValue;
  669. },
  670. value: function(value) {
  671. module.value = module.helper.forceArray(value);
  672. },
  673. progress: function(value) {
  674. if(!module.has.progressPoll()) {
  675. module.debug('First update in progress update interval, immediately updating', value);
  676. module.update.progress(value);
  677. module.create.progressPoll();
  678. }
  679. else {
  680. module.debug('Updated within interval, setting next update to use new value', value);
  681. module.set.nextValue(value);
  682. }
  683. },
  684. nextValue: function(value) {
  685. module.nextValue = value;
  686. }
  687. },
  688. update: {
  689. toNextValue: function() {
  690. var
  691. nextValue = module.nextValue
  692. ;
  693. if(nextValue) {
  694. module.debug('Update interval complete using last updated value', nextValue);
  695. module.update.progress(nextValue);
  696. module.remove.nextValue();
  697. }
  698. },
  699. progress: function(values) {
  700. var hasTotal = module.has.total();
  701. if (hasTotal) {
  702. module.set.value(values);
  703. }
  704. var percentCompletes = module.helper.forceArray(values).map(function(value) {
  705. var
  706. percentComplete
  707. ;
  708. value = module.get.numericValue(value);
  709. if (value === false) {
  710. module.error(error.nonNumeric, value);
  711. }
  712. value = module.get.normalizedValue(value);
  713. if (hasTotal) {
  714. percentComplete = (value / module.total) * 100;
  715. module.debug('Calculating percent complete from total', percentComplete);
  716. }
  717. else {
  718. percentComplete = value;
  719. module.debug('Setting value to exact percentage value', percentComplete);
  720. }
  721. return percentComplete;
  722. });
  723. module.set.percent( percentCompletes );
  724. }
  725. },
  726. setting: function(name, value) {
  727. module.debug('Changing setting', name, value);
  728. if( $.isPlainObject(name) ) {
  729. $.extend(true, settings, name);
  730. }
  731. else if(value !== undefined) {
  732. if($.isPlainObject(settings[name])) {
  733. $.extend(true, settings[name], value);
  734. }
  735. else {
  736. settings[name] = value;
  737. }
  738. }
  739. else {
  740. return settings[name];
  741. }
  742. },
  743. internal: function(name, value) {
  744. if( $.isPlainObject(name) ) {
  745. $.extend(true, module, name);
  746. }
  747. else if(value !== undefined) {
  748. module[name] = value;
  749. }
  750. else {
  751. return module[name];
  752. }
  753. },
  754. debug: function() {
  755. if(!settings.silent && settings.debug) {
  756. if(settings.performance) {
  757. module.performance.log(arguments);
  758. }
  759. else {
  760. module.debug = Function.prototype.bind.call(console.info, console, settings.name + ':');
  761. module.debug.apply(console, arguments);
  762. }
  763. }
  764. },
  765. verbose: function() {
  766. if(!settings.silent && settings.verbose && settings.debug) {
  767. if(settings.performance) {
  768. module.performance.log(arguments);
  769. }
  770. else {
  771. module.verbose = Function.prototype.bind.call(console.info, console, settings.name + ':');
  772. module.verbose.apply(console, arguments);
  773. }
  774. }
  775. },
  776. error: function() {
  777. if(!settings.silent) {
  778. module.error = Function.prototype.bind.call(console.error, console, settings.name + ':');
  779. module.error.apply(console, arguments);
  780. }
  781. },
  782. performance: {
  783. log: function(message) {
  784. var
  785. currentTime,
  786. executionTime,
  787. previousTime
  788. ;
  789. if(settings.performance) {
  790. currentTime = new Date().getTime();
  791. previousTime = time || currentTime;
  792. executionTime = currentTime - previousTime;
  793. time = currentTime;
  794. performance.push({
  795. 'Name' : message[0],
  796. 'Arguments' : [].slice.call(message, 1) || '',
  797. 'Element' : element,
  798. 'Execution Time' : executionTime
  799. });
  800. }
  801. clearTimeout(module.performance.timer);
  802. module.performance.timer = setTimeout(module.performance.display, 500);
  803. },
  804. display: function() {
  805. var
  806. title = settings.name + ':',
  807. totalTime = 0
  808. ;
  809. time = false;
  810. clearTimeout(module.performance.timer);
  811. $.each(performance, function(index, data) {
  812. totalTime += data['Execution Time'];
  813. });
  814. title += ' ' + totalTime + 'ms';
  815. if(moduleSelector) {
  816. title += ' \'' + moduleSelector + '\'';
  817. }
  818. if( (console.group !== undefined || console.table !== undefined) && performance.length > 0) {
  819. console.groupCollapsed(title);
  820. if(console.table) {
  821. console.table(performance);
  822. }
  823. else {
  824. $.each(performance, function(index, data) {
  825. console.log(data['Name'] + ': ' + data['Execution Time']+'ms');
  826. });
  827. }
  828. console.groupEnd();
  829. }
  830. performance = [];
  831. }
  832. },
  833. invoke: function(query, passedArguments, context) {
  834. var
  835. object = instance,
  836. maxDepth,
  837. found,
  838. response
  839. ;
  840. passedArguments = passedArguments || queryArguments;
  841. context = element || context;
  842. if(typeof query == 'string' && object !== undefined) {
  843. query = query.split(/[\. ]/);
  844. maxDepth = query.length - 1;
  845. $.each(query, function(depth, value) {
  846. var camelCaseValue = (depth != maxDepth)
  847. ? value + query[depth + 1].charAt(0).toUpperCase() + query[depth + 1].slice(1)
  848. : query
  849. ;
  850. if( $.isPlainObject( object[camelCaseValue] ) && (depth != maxDepth) ) {
  851. object = object[camelCaseValue];
  852. }
  853. else if( object[camelCaseValue] !== undefined ) {
  854. found = object[camelCaseValue];
  855. return false;
  856. }
  857. else if( $.isPlainObject( object[value] ) && (depth != maxDepth) ) {
  858. object = object[value];
  859. }
  860. else if( object[value] !== undefined ) {
  861. found = object[value];
  862. return false;
  863. }
  864. else {
  865. module.error(error.method, query);
  866. return false;
  867. }
  868. });
  869. }
  870. if ( $.isFunction( found ) ) {
  871. response = found.apply(context, passedArguments);
  872. }
  873. else if(found !== undefined) {
  874. response = found;
  875. }
  876. if(Array.isArray(returnedValue)) {
  877. returnedValue.push(response);
  878. }
  879. else if(returnedValue !== undefined) {
  880. returnedValue = [returnedValue, response];
  881. }
  882. else if(response !== undefined) {
  883. returnedValue = response;
  884. }
  885. return found;
  886. }
  887. };
  888. if(methodInvoked) {
  889. if(instance === undefined) {
  890. module.initialize();
  891. }
  892. module.invoke(query);
  893. }
  894. else {
  895. if(instance !== undefined) {
  896. instance.invoke('destroy');
  897. }
  898. module.initialize();
  899. }
  900. })
  901. ;
  902. return (returnedValue !== undefined)
  903. ? returnedValue
  904. : this
  905. ;
  906. };
  907. $.fn.progress.settings = {
  908. name : 'Progress',
  909. namespace : 'progress',
  910. silent : false,
  911. debug : false,
  912. verbose : false,
  913. performance : true,
  914. random : {
  915. min : 2,
  916. max : 5
  917. },
  918. duration : 300,
  919. updateInterval : 'auto',
  920. autoSuccess : true,
  921. showActivity : true,
  922. limitValues : true,
  923. label : 'percent',
  924. precision : 0,
  925. framerate : (1000 / 30), /// 30 fps
  926. percent : false,
  927. total : false,
  928. value : false,
  929. // delay in ms for fail safe animation callback
  930. failSafeDelay : 100,
  931. onLabelUpdate : function(state, text, value, total){
  932. return text;
  933. },
  934. onChange : function(percent, value, total){},
  935. onSuccess : function(total){},
  936. onActive : function(value, total){},
  937. onError : function(value, total){},
  938. onWarning : function(value, total){},
  939. error : {
  940. method : 'The method you called is not defined.',
  941. nonNumeric : 'Progress value is non numeric',
  942. tooHigh : 'Value specified is above 100%',
  943. tooLow : 'Value specified is below 0%',
  944. sumExceedsTotal : 'Sum of multple values exceed total',
  945. },
  946. regExp: {
  947. variable: /\{\$*[A-z0-9]+\}/g
  948. },
  949. metadata: {
  950. percent : 'percent',
  951. total : 'total',
  952. value : 'value'
  953. },
  954. selector : {
  955. bar : '> .bar',
  956. label : '> .label',
  957. progress : '.bar > .progress'
  958. },
  959. text : {
  960. active : false,
  961. error : false,
  962. success : false,
  963. warning : false,
  964. percent : '{percent}%',
  965. ratio : '{value} of {total}',
  966. bars : ['']
  967. },
  968. className : {
  969. active : 'active',
  970. error : 'error',
  971. success : 'success',
  972. warning : 'warning'
  973. }
  974. };
  975. })( jQuery, window, document );