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.

782 lines
25 KiB

9 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. /*!
  2. * # Semantic UI x.x - Progress
  3. * http://github.com/semantic-org/semantic-ui/
  4. *
  5. *
  6. * Copyright 2015 Contributors
  7. * Released under the MIT license
  8. * http://opensource.org/licenses/MIT
  9. *
  10. */
  11. ;(function ( $, window, document, undefined ) {
  12. "use strict";
  13. $.fn.progress = function(parameters) {
  14. var
  15. $allModules = $(this),
  16. moduleSelector = $allModules.selector || '',
  17. time = new Date().getTime(),
  18. performance = [],
  19. query = arguments[0],
  20. methodInvoked = (typeof query == 'string'),
  21. queryArguments = [].slice.call(arguments, 1),
  22. returnedValue
  23. ;
  24. $allModules
  25. .each(function() {
  26. var
  27. settings = ( $.isPlainObject(parameters) )
  28. ? $.extend(true, {}, $.fn.progress.settings, parameters)
  29. : $.extend({}, $.fn.progress.settings),
  30. className = settings.className,
  31. metadata = settings.metadata,
  32. namespace = settings.namespace,
  33. selector = settings.selector,
  34. error = settings.error,
  35. eventNamespace = '.' + namespace,
  36. moduleNamespace = 'module-' + namespace,
  37. $module = $(this),
  38. $bar = $(this).find(selector.bar),
  39. $progress = $(this).find(selector.progress),
  40. $label = $(this).find(selector.label),
  41. element = this,
  42. instance = $module.data(moduleNamespace),
  43. animating = false,
  44. transitionEnd,
  45. module
  46. ;
  47. module = {
  48. initialize: function() {
  49. module.debug('Initializing progress bar', settings);
  50. transitionEnd = module.get.transitionEnd();
  51. module.read.metadata();
  52. module.set.duration();
  53. module.set.initials();
  54. module.instantiate();
  55. },
  56. instantiate: function() {
  57. module.verbose('Storing instance of progress', module);
  58. instance = module;
  59. $module
  60. .data(moduleNamespace, module)
  61. ;
  62. },
  63. destroy: function() {
  64. module.verbose('Destroying previous progress for', $module);
  65. clearInterval(instance.interval);
  66. module.remove.state();
  67. $module.removeData(moduleNamespace);
  68. instance = undefined;
  69. },
  70. reset: function() {
  71. module.set.percent(0);
  72. },
  73. complete: function() {
  74. if(module.percent === undefined || module.percent < 100) {
  75. module.set.percent(100);
  76. }
  77. },
  78. read: {
  79. metadata: function() {
  80. if( $module.data(metadata.percent) ) {
  81. module.verbose('Current percent value set from metadata');
  82. module.percent = $module.data(metadata.percent);
  83. }
  84. if( $module.data(metadata.total) ) {
  85. module.verbose('Total value set from metadata');
  86. module.total = $module.data(metadata.total);
  87. }
  88. if( $module.data(metadata.value) ) {
  89. module.verbose('Current value set from metadata');
  90. module.value = $module.data(metadata.value);
  91. }
  92. },
  93. currentValue: function() {
  94. return (module.value !== undefined)
  95. ? module.value
  96. : false
  97. ;
  98. }
  99. },
  100. increment: function(incrementValue) {
  101. var
  102. total = module.total || false,
  103. edgeValue,
  104. startValue,
  105. newValue
  106. ;
  107. if(total) {
  108. startValue = module.value || 0;
  109. incrementValue = incrementValue || 1;
  110. newValue = startValue + incrementValue;
  111. edgeValue = module.total;
  112. module.debug('Incrementing value by', incrementValue, startValue, edgeValue);
  113. if(newValue > edgeValue ) {
  114. module.debug('Value cannot increment above total', edgeValue);
  115. newValue = edgeValue;
  116. }
  117. module.set.progress(newValue);
  118. }
  119. else {
  120. startValue = module.percent || 0;
  121. incrementValue = incrementValue || module.get.randomValue();
  122. newValue = startValue + incrementValue;
  123. edgeValue = 100;
  124. module.debug('Incrementing percentage by', incrementValue, startValue);
  125. if(newValue > edgeValue ) {
  126. module.debug('Value cannot increment above 100 percent');
  127. newValue = edgeValue;
  128. }
  129. module.set.progress(newValue);
  130. }
  131. },
  132. decrement: function(decrementValue) {
  133. var
  134. total = module.total || false,
  135. edgeValue = 0,
  136. startValue,
  137. newValue
  138. ;
  139. if(total) {
  140. startValue = module.value || 0;
  141. decrementValue = decrementValue || 1;
  142. newValue = startValue - decrementValue;
  143. module.debug('Decrementing value by', decrementValue, startValue);
  144. }
  145. else {
  146. startValue = module.percent || 0;
  147. decrementValue = decrementValue || module.get.randomValue();
  148. newValue = startValue - decrementValue;
  149. module.debug('Decrementing percentage by', decrementValue, startValue);
  150. }
  151. if(newValue < edgeValue) {
  152. module.debug('Value cannot decrement below 0');
  153. newValue = 0;
  154. }
  155. module.set.progress(newValue);
  156. },
  157. get: {
  158. text: function(templateText) {
  159. var
  160. value = module.value || 0,
  161. total = module.total || 0,
  162. percent = (animating)
  163. ? module.get.displayPercent()
  164. : module.percent || 0,
  165. left = (module.total > 0)
  166. ? (total - value)
  167. : (100 - percent)
  168. ;
  169. templateText = templateText || '';
  170. templateText = templateText
  171. .replace('{value}', value)
  172. .replace('{total}', total)
  173. .replace('{left}', left)
  174. .replace('{percent}', percent)
  175. ;
  176. module.debug('Adding variables to progress bar text', templateText);
  177. return templateText;
  178. },
  179. randomValue: function() {
  180. module.debug('Generating random increment percentage');
  181. return Math.floor((Math.random() * settings.random.max) + settings.random.min);
  182. },
  183. transitionEnd: function() {
  184. var
  185. element = document.createElement('element'),
  186. transitions = {
  187. 'transition' :'transitionend',
  188. 'OTransition' :'oTransitionEnd',
  189. 'MozTransition' :'transitionend',
  190. 'WebkitTransition' :'webkitTransitionEnd'
  191. },
  192. transition
  193. ;
  194. for(transition in transitions){
  195. if( element.style[transition] !== undefined ){
  196. return transitions[transition];
  197. }
  198. }
  199. },
  200. // gets current displayed percentage (if animating values this is the intermediary value)
  201. displayPercent: function() {
  202. var
  203. barWidth = $bar.width(),
  204. totalWidth = $module.width(),
  205. minDisplay = parseInt($bar.css('min-width'), 10),
  206. displayPercent = (barWidth > minDisplay)
  207. ? (barWidth / totalWidth * 100)
  208. : module.percent
  209. ;
  210. if(settings.precision === 0) {
  211. return Math.round(displayPercent);
  212. }
  213. return Math.round(displayPercent * (10 * settings.precision)) / (10 * settings.precision);
  214. },
  215. percent: function() {
  216. return module.percent || 0;
  217. },
  218. value: function() {
  219. return module.value || false;
  220. },
  221. total: function() {
  222. return module.total || false;
  223. }
  224. },
  225. is: {
  226. success: function() {
  227. return $module.hasClass(className.success);
  228. },
  229. warning: function() {
  230. return $module.hasClass(className.warning);
  231. },
  232. error: function() {
  233. return $module.hasClass(className.error);
  234. },
  235. active: function() {
  236. return $module.hasClass(className.active);
  237. },
  238. visible: function() {
  239. return $module.is(':visible');
  240. }
  241. },
  242. remove: {
  243. state: function() {
  244. module.verbose('Removing stored state');
  245. delete module.total;
  246. delete module.percent;
  247. delete module.value;
  248. },
  249. active: function() {
  250. module.verbose('Removing active state');
  251. $module.removeClass(className.active);
  252. },
  253. success: function() {
  254. module.verbose('Removing success state');
  255. $module.removeClass(className.success);
  256. },
  257. warning: function() {
  258. module.verbose('Removing warning state');
  259. $module.removeClass(className.warning);
  260. },
  261. error: function() {
  262. module.verbose('Removing error state');
  263. $module.removeClass(className.error);
  264. }
  265. },
  266. set: {
  267. barWidth: function(value) {
  268. if(value > 100) {
  269. module.error(error.tooHigh, value);
  270. }
  271. else if (value < 0) {
  272. module.error(error.tooLow, value);
  273. }
  274. else {
  275. $bar
  276. .css('width', value + '%')
  277. ;
  278. $module
  279. .attr('data-percent', parseInt(value, 10))
  280. ;
  281. }
  282. },
  283. duration: function(duration) {
  284. duration = duration || settings.duration;
  285. duration = (typeof duration == 'number')
  286. ? duration + 'ms'
  287. : duration
  288. ;
  289. module.verbose('Setting progress bar transition duration', duration);
  290. $bar
  291. .css({
  292. '-webkit-transition-duration': duration,
  293. '-moz-transition-duration': duration,
  294. '-ms-transition-duration': duration,
  295. '-o-transition-duration': duration,
  296. 'transition-duration': duration
  297. })
  298. ;
  299. },
  300. initials: function() {
  301. if(settings.total !== false) {
  302. module.verbose('Current total set in settings', settings.total);
  303. module.total = settings.total;
  304. }
  305. if(settings.value !== false) {
  306. module.verbose('Current value set in settings', settings.value);
  307. module.value = settings.value;
  308. }
  309. if(settings.percent !== false) {
  310. module.verbose('Current percent set in settings', settings.percent);
  311. module.percent = settings.percent;
  312. }
  313. if(module.percent !== undefined) {
  314. module.set.percent(module.percent);
  315. }
  316. else if(module.value !== undefined) {
  317. module.set.progress(module.value);
  318. }
  319. },
  320. percent: function(percent) {
  321. percent = (typeof percent == 'string')
  322. ? +(percent.replace('%', ''))
  323. : percent
  324. ;
  325. if(percent > 0 && percent < 1) {
  326. module.verbose('Module percentage passed as decimal, converting');
  327. percent = percent * 100;
  328. }
  329. // round percentage
  330. if(settings.precision === 0) {
  331. percent = Math.round(percent);
  332. }
  333. else {
  334. percent = Math.round(percent * (10 * settings.precision)) / (10 * settings.precision);
  335. }
  336. module.percent = percent;
  337. if(module.total) {
  338. module.value = Math.round( (percent / 100) * module.total * (10 * settings.precision)) / (10 * settings.precision);
  339. }
  340. else if(settings.limitValues) {
  341. module.value = (module.value > 100)
  342. ? 100
  343. : (module.value < 0)
  344. ? 0
  345. : module.value
  346. ;
  347. }
  348. module.set.barWidth(percent);
  349. module.set.labelInterval();
  350. module.set.labels();
  351. settings.onChange.call(element, percent, module.value, module.total);
  352. },
  353. labelInterval: function() {
  354. var
  355. animationCallback = function() {
  356. module.verbose('Bar finished animating, removing continuous label updates');
  357. clearInterval(module.interval);
  358. animating = false;
  359. module.set.labels();
  360. }
  361. ;
  362. clearInterval(module.interval);
  363. $bar.one(transitionEnd + eventNamespace, animationCallback);
  364. module.timer = setTimeout(animationCallback, settings.duration + 100);
  365. animating = true;
  366. module.interval = setInterval(module.set.labels, settings.framerate);
  367. },
  368. labels: function() {
  369. module.verbose('Setting both bar progress and outer label text');
  370. module.set.barLabel();
  371. module.set.state();
  372. },
  373. label: function(text) {
  374. text = text || '';
  375. if(text) {
  376. text = module.get.text(text);
  377. module.debug('Setting label to text', text);
  378. $label.text(text);
  379. }
  380. },
  381. state: function(percent) {
  382. percent = (percent !== undefined)
  383. ? percent
  384. : module.percent
  385. ;
  386. if(percent === 100) {
  387. if(settings.autoSuccess && !(module.is.warning() || module.is.error())) {
  388. module.set.success();
  389. module.debug('Automatically triggering success at 100%');
  390. }
  391. else {
  392. module.verbose('Reached 100% removing active state');
  393. module.remove.active();
  394. }
  395. }
  396. else if(percent > 0) {
  397. module.verbose('Adjusting active progress bar label', percent);
  398. module.set.active();
  399. }
  400. else {
  401. module.remove.active();
  402. module.set.label(settings.text.active);
  403. }
  404. },
  405. barLabel: function(text) {
  406. if(text !== undefined) {
  407. $progress.text( module.get.text(text) );
  408. }
  409. else if(settings.label == 'ratio' && module.total) {
  410. module.debug('Adding ratio to bar label');
  411. $progress.text( module.get.text(settings.text.ratio) );
  412. }
  413. else if(settings.label == 'percent') {
  414. module.debug('Adding percentage to bar label');
  415. $progress.text( module.get.text(settings.text.percent) );
  416. }
  417. },
  418. active: function(text) {
  419. text = text || settings.text.active;
  420. module.debug('Setting active state');
  421. if(settings.showActivity && !module.is.active() ) {
  422. $module.addClass(className.active);
  423. }
  424. module.remove.warning();
  425. module.remove.error();
  426. module.remove.success();
  427. if(text) {
  428. module.set.label(text);
  429. }
  430. settings.onActive.call(element, module.value, module.total);
  431. },
  432. success : function(text) {
  433. text = text || settings.text.success;
  434. module.debug('Setting success state');
  435. $module.addClass(className.success);
  436. module.remove.active();
  437. module.remove.warning();
  438. module.remove.error();
  439. module.complete();
  440. if(text) {
  441. module.set.label(text);
  442. }
  443. settings.onSuccess.call(element, module.total);
  444. },
  445. warning : function(text) {
  446. text = text || settings.text.warning;
  447. module.debug('Setting warning state');
  448. $module.addClass(className.warning);
  449. module.remove.active();
  450. module.remove.success();
  451. module.remove.error();
  452. module.complete();
  453. if(text) {
  454. module.set.label(text);
  455. }
  456. settings.onWarning.call(element, module.value, module.total);
  457. },
  458. error : function(text) {
  459. text = text || settings.text.error;
  460. module.debug('Setting error state');
  461. $module.addClass(className.error);
  462. module.remove.active();
  463. module.remove.success();
  464. module.remove.warning();
  465. module.complete();
  466. if(text) {
  467. module.set.label(text);
  468. }
  469. settings.onError.call(element, module.value, module.total);
  470. },
  471. total: function(totalValue) {
  472. module.total = totalValue;
  473. },
  474. progress: function(value) {
  475. var
  476. numericValue = (typeof value === 'string')
  477. ? (value.replace(/[^\d.]/g, '') !== '')
  478. ? +(value.replace(/[^\d.]/g, ''))
  479. : false
  480. : value,
  481. percentComplete
  482. ;
  483. if(numericValue === false) {
  484. module.error(error.nonNumeric, value);
  485. }
  486. if(module.total) {
  487. module.value = numericValue;
  488. percentComplete = (numericValue / module.total) * 100;
  489. module.debug('Calculating percent complete from total', percentComplete);
  490. module.set.percent( percentComplete );
  491. }
  492. else {
  493. percentComplete = numericValue;
  494. module.debug('Setting value to exact percentage value', percentComplete);
  495. module.set.percent( percentComplete );
  496. }
  497. }
  498. },
  499. setting: function(name, value) {
  500. module.debug('Changing setting', name, value);
  501. if( $.isPlainObject(name) ) {
  502. $.extend(true, settings, name);
  503. }
  504. else if(value !== undefined) {
  505. settings[name] = value;
  506. }
  507. else {
  508. return settings[name];
  509. }
  510. },
  511. internal: function(name, value) {
  512. if( $.isPlainObject(name) ) {
  513. $.extend(true, module, name);
  514. }
  515. else if(value !== undefined) {
  516. module[name] = value;
  517. }
  518. else {
  519. return module[name];
  520. }
  521. },
  522. debug: function() {
  523. if(settings.debug) {
  524. if(settings.performance) {
  525. module.performance.log(arguments);
  526. }
  527. else {
  528. module.debug = Function.prototype.bind.call(console.info, console, settings.name + ':');
  529. module.debug.apply(console, arguments);
  530. }
  531. }
  532. },
  533. verbose: function() {
  534. if(settings.verbose && settings.debug) {
  535. if(settings.performance) {
  536. module.performance.log(arguments);
  537. }
  538. else {
  539. module.verbose = Function.prototype.bind.call(console.info, console, settings.name + ':');
  540. module.verbose.apply(console, arguments);
  541. }
  542. }
  543. },
  544. error: function() {
  545. module.error = Function.prototype.bind.call(console.error, console, settings.name + ':');
  546. module.error.apply(console, arguments);
  547. },
  548. performance: {
  549. log: function(message) {
  550. var
  551. currentTime,
  552. executionTime,
  553. previousTime
  554. ;
  555. if(settings.performance) {
  556. currentTime = new Date().getTime();
  557. previousTime = time || currentTime;
  558. executionTime = currentTime - previousTime;
  559. time = currentTime;
  560. performance.push({
  561. 'Name' : message[0],
  562. 'Arguments' : [].slice.call(message, 1) || '',
  563. 'Element' : element,
  564. 'Execution Time' : executionTime
  565. });
  566. }
  567. clearTimeout(module.performance.timer);
  568. module.performance.timer = setTimeout(module.performance.display, 500);
  569. },
  570. display: function() {
  571. var
  572. title = settings.name + ':',
  573. totalTime = 0
  574. ;
  575. time = false;
  576. clearTimeout(module.performance.timer);
  577. $.each(performance, function(index, data) {
  578. totalTime += data['Execution Time'];
  579. });
  580. title += ' ' + totalTime + 'ms';
  581. if(moduleSelector) {
  582. title += ' \'' + moduleSelector + '\'';
  583. }
  584. if( (console.group !== undefined || console.table !== undefined) && performance.length > 0) {
  585. console.groupCollapsed(title);
  586. if(console.table) {
  587. console.table(performance);
  588. }
  589. else {
  590. $.each(performance, function(index, data) {
  591. console.log(data['Name'] + ': ' + data['Execution Time']+'ms');
  592. });
  593. }
  594. console.groupEnd();
  595. }
  596. performance = [];
  597. }
  598. },
  599. invoke: function(query, passedArguments, context) {
  600. var
  601. object = instance,
  602. maxDepth,
  603. found,
  604. response
  605. ;
  606. passedArguments = passedArguments || queryArguments;
  607. context = element || context;
  608. if(typeof query == 'string' && object !== undefined) {
  609. query = query.split(/[\. ]/);
  610. maxDepth = query.length - 1;
  611. $.each(query, function(depth, value) {
  612. var camelCaseValue = (depth != maxDepth)
  613. ? value + query[depth + 1].charAt(0).toUpperCase() + query[depth + 1].slice(1)
  614. : query
  615. ;
  616. if( $.isPlainObject( object[camelCaseValue] ) && (depth != maxDepth) ) {
  617. object = object[camelCaseValue];
  618. }
  619. else if( object[camelCaseValue] !== undefined ) {
  620. found = object[camelCaseValue];
  621. return false;
  622. }
  623. else if( $.isPlainObject( object[value] ) && (depth != maxDepth) ) {
  624. object = object[value];
  625. }
  626. else if( object[value] !== undefined ) {
  627. found = object[value];
  628. return false;
  629. }
  630. else {
  631. module.error(error.method, query);
  632. return false;
  633. }
  634. });
  635. }
  636. if ( $.isFunction( found ) ) {
  637. response = found.apply(context, passedArguments);
  638. }
  639. else if(found !== undefined) {
  640. response = found;
  641. }
  642. if($.isArray(returnedValue)) {
  643. returnedValue.push(response);
  644. }
  645. else if(returnedValue !== undefined) {
  646. returnedValue = [returnedValue, response];
  647. }
  648. else if(response !== undefined) {
  649. returnedValue = response;
  650. }
  651. return found;
  652. }
  653. };
  654. if(methodInvoked) {
  655. if(instance === undefined) {
  656. module.initialize();
  657. }
  658. module.invoke(query);
  659. }
  660. else {
  661. if(instance !== undefined) {
  662. instance.invoke('destroy');
  663. }
  664. module.initialize();
  665. }
  666. })
  667. ;
  668. return (returnedValue !== undefined)
  669. ? returnedValue
  670. : this
  671. ;
  672. };
  673. $.fn.progress.settings = {
  674. name : 'Progress',
  675. namespace : 'progress',
  676. debug : false,
  677. verbose : false,
  678. performance : true,
  679. random : {
  680. min : 2,
  681. max : 5
  682. },
  683. duration : 300,
  684. autoSuccess : true,
  685. showActivity : true,
  686. limitValues : true,
  687. label : 'percent',
  688. precision : 1,
  689. framerate : (1000 / 30), /// 30 fps
  690. percent : false,
  691. total : false,
  692. value : false,
  693. onChange : function(percent, value, total){},
  694. onSuccess : function(total){},
  695. onActive : function(value, total){},
  696. onError : function(value, total){},
  697. onWarning : function(value, total){},
  698. error : {
  699. method : 'The method you called is not defined.',
  700. nonNumeric : 'Progress value is non numeric',
  701. tooHigh : 'Value specified is above 100%',
  702. tooLow : 'Value specified is below 0%'
  703. },
  704. regExp: {
  705. variable: /\{\$*[A-z0-9]+\}/g
  706. },
  707. metadata: {
  708. percent : 'percent',
  709. total : 'total',
  710. value : 'value'
  711. },
  712. selector : {
  713. bar : '> .bar',
  714. label : '> .label',
  715. progress : '.bar > .progress'
  716. },
  717. text : {
  718. active : false,
  719. error : false,
  720. success : false,
  721. warning : false,
  722. percent : '{percent}%',
  723. ratio : '{value} of {total}'
  724. },
  725. className : {
  726. active : 'active',
  727. error : 'error',
  728. success : 'success',
  729. warning : 'warning'
  730. }
  731. };
  732. })( jQuery, window , document );