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.

783 lines
25 KiB

9 years ago
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
9 years ago
10 years ago
10 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
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
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. /*!
  2. * # Semantic UI 2.0.0 - 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. return (settings.precision > 0)
  211. ? Math.round(displayPercent * (10 * settings.precision)) / (10 * settings.precision)
  212. : Math.round(displayPercent)
  213. ;
  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. percent = (settings.precision > 0)
  331. ? Math.round(percent * (10 * settings.precision)) / (10 * settings.precision)
  332. : Math.round(percent)
  333. ;
  334. module.percent = percent;
  335. if(module.total) {
  336. module.value = (settings.precision > 0)
  337. ? Math.round( (percent / 100) * module.total * (10 * settings.precision)) / (10 * settings.precision)
  338. : Math.round( (percent / 100) * module.total * 10) / 10
  339. ;
  340. }
  341. else if(settings.limitValues) {
  342. module.value = (module.value > 100)
  343. ? 100
  344. : (module.value < 0)
  345. ? 0
  346. : module.value
  347. ;
  348. }
  349. module.set.barWidth(percent);
  350. module.set.labelInterval();
  351. module.set.labels();
  352. settings.onChange.call(element, percent, module.value, module.total);
  353. },
  354. labelInterval: function() {
  355. var
  356. animationCallback = function() {
  357. module.verbose('Bar finished animating, removing continuous label updates');
  358. clearInterval(module.interval);
  359. animating = false;
  360. module.set.labels();
  361. }
  362. ;
  363. clearInterval(module.interval);
  364. $bar.one(transitionEnd + eventNamespace, animationCallback);
  365. module.timer = setTimeout(animationCallback, settings.duration + 100);
  366. animating = true;
  367. module.interval = setInterval(module.set.labels, settings.framerate);
  368. },
  369. labels: function() {
  370. module.verbose('Setting both bar progress and outer label text');
  371. module.set.barLabel();
  372. module.set.state();
  373. },
  374. label: function(text) {
  375. text = text || '';
  376. if(text) {
  377. text = module.get.text(text);
  378. module.debug('Setting label to text', text);
  379. $label.text(text);
  380. }
  381. },
  382. state: function(percent) {
  383. percent = (percent !== undefined)
  384. ? percent
  385. : module.percent
  386. ;
  387. if(percent === 100) {
  388. if(settings.autoSuccess && !(module.is.warning() || module.is.error())) {
  389. module.set.success();
  390. module.debug('Automatically triggering success at 100%');
  391. }
  392. else {
  393. module.verbose('Reached 100% removing active state');
  394. module.remove.active();
  395. }
  396. }
  397. else if(percent > 0) {
  398. module.verbose('Adjusting active progress bar label', percent);
  399. module.set.active();
  400. }
  401. else {
  402. module.remove.active();
  403. module.set.label(settings.text.active);
  404. }
  405. },
  406. barLabel: function(text) {
  407. if(text !== undefined) {
  408. $progress.text( module.get.text(text) );
  409. }
  410. else if(settings.label == 'ratio' && module.total) {
  411. module.debug('Adding ratio to bar label');
  412. $progress.text( module.get.text(settings.text.ratio) );
  413. }
  414. else if(settings.label == 'percent') {
  415. module.debug('Adding percentage to bar label');
  416. $progress.text( module.get.text(settings.text.percent) );
  417. }
  418. },
  419. active: function(text) {
  420. text = text || settings.text.active;
  421. module.debug('Setting active state');
  422. if(settings.showActivity && !module.is.active() ) {
  423. $module.addClass(className.active);
  424. }
  425. module.remove.warning();
  426. module.remove.error();
  427. module.remove.success();
  428. if(text) {
  429. module.set.label(text);
  430. }
  431. settings.onActive.call(element, module.value, module.total);
  432. },
  433. success : function(text) {
  434. text = text || settings.text.success;
  435. module.debug('Setting success state');
  436. $module.addClass(className.success);
  437. module.remove.active();
  438. module.remove.warning();
  439. module.remove.error();
  440. module.complete();
  441. if(text) {
  442. module.set.label(text);
  443. }
  444. settings.onSuccess.call(element, module.total);
  445. },
  446. warning : function(text) {
  447. text = text || settings.text.warning;
  448. module.debug('Setting warning state');
  449. $module.addClass(className.warning);
  450. module.remove.active();
  451. module.remove.success();
  452. module.remove.error();
  453. module.complete();
  454. if(text) {
  455. module.set.label(text);
  456. }
  457. settings.onWarning.call(element, module.value, module.total);
  458. },
  459. error : function(text) {
  460. text = text || settings.text.error;
  461. module.debug('Setting error state');
  462. $module.addClass(className.error);
  463. module.remove.active();
  464. module.remove.success();
  465. module.remove.warning();
  466. module.complete();
  467. if(text) {
  468. module.set.label(text);
  469. }
  470. settings.onError.call(element, module.value, module.total);
  471. },
  472. total: function(totalValue) {
  473. module.total = totalValue;
  474. },
  475. progress: function(value) {
  476. var
  477. numericValue = (typeof value === 'string')
  478. ? (value.replace(/[^\d.]/g, '') !== '')
  479. ? +(value.replace(/[^\d.]/g, ''))
  480. : false
  481. : value,
  482. percentComplete
  483. ;
  484. if(numericValue === false) {
  485. module.error(error.nonNumeric, value);
  486. }
  487. if(module.total) {
  488. module.value = numericValue;
  489. percentComplete = (numericValue / module.total) * 100;
  490. module.debug('Calculating percent complete from total', percentComplete);
  491. module.set.percent( percentComplete );
  492. }
  493. else {
  494. percentComplete = numericValue;
  495. module.debug('Setting value to exact percentage value', percentComplete);
  496. module.set.percent( percentComplete );
  497. }
  498. }
  499. },
  500. setting: function(name, value) {
  501. module.debug('Changing setting', name, value);
  502. if( $.isPlainObject(name) ) {
  503. $.extend(true, settings, name);
  504. }
  505. else if(value !== undefined) {
  506. settings[name] = value;
  507. }
  508. else {
  509. return settings[name];
  510. }
  511. },
  512. internal: function(name, value) {
  513. if( $.isPlainObject(name) ) {
  514. $.extend(true, module, name);
  515. }
  516. else if(value !== undefined) {
  517. module[name] = value;
  518. }
  519. else {
  520. return module[name];
  521. }
  522. },
  523. debug: function() {
  524. if(settings.debug) {
  525. if(settings.performance) {
  526. module.performance.log(arguments);
  527. }
  528. else {
  529. module.debug = Function.prototype.bind.call(console.info, console, settings.name + ':');
  530. module.debug.apply(console, arguments);
  531. }
  532. }
  533. },
  534. verbose: function() {
  535. if(settings.verbose && settings.debug) {
  536. if(settings.performance) {
  537. module.performance.log(arguments);
  538. }
  539. else {
  540. module.verbose = Function.prototype.bind.call(console.info, console, settings.name + ':');
  541. module.verbose.apply(console, arguments);
  542. }
  543. }
  544. },
  545. error: function() {
  546. module.error = Function.prototype.bind.call(console.error, console, settings.name + ':');
  547. module.error.apply(console, arguments);
  548. },
  549. performance: {
  550. log: function(message) {
  551. var
  552. currentTime,
  553. executionTime,
  554. previousTime
  555. ;
  556. if(settings.performance) {
  557. currentTime = new Date().getTime();
  558. previousTime = time || currentTime;
  559. executionTime = currentTime - previousTime;
  560. time = currentTime;
  561. performance.push({
  562. 'Name' : message[0],
  563. 'Arguments' : [].slice.call(message, 1) || '',
  564. 'Element' : element,
  565. 'Execution Time' : executionTime
  566. });
  567. }
  568. clearTimeout(module.performance.timer);
  569. module.performance.timer = setTimeout(module.performance.display, 500);
  570. },
  571. display: function() {
  572. var
  573. title = settings.name + ':',
  574. totalTime = 0
  575. ;
  576. time = false;
  577. clearTimeout(module.performance.timer);
  578. $.each(performance, function(index, data) {
  579. totalTime += data['Execution Time'];
  580. });
  581. title += ' ' + totalTime + 'ms';
  582. if(moduleSelector) {
  583. title += ' \'' + moduleSelector + '\'';
  584. }
  585. if( (console.group !== undefined || console.table !== undefined) && performance.length > 0) {
  586. console.groupCollapsed(title);
  587. if(console.table) {
  588. console.table(performance);
  589. }
  590. else {
  591. $.each(performance, function(index, data) {
  592. console.log(data['Name'] + ': ' + data['Execution Time']+'ms');
  593. });
  594. }
  595. console.groupEnd();
  596. }
  597. performance = [];
  598. }
  599. },
  600. invoke: function(query, passedArguments, context) {
  601. var
  602. object = instance,
  603. maxDepth,
  604. found,
  605. response
  606. ;
  607. passedArguments = passedArguments || queryArguments;
  608. context = element || context;
  609. if(typeof query == 'string' && object !== undefined) {
  610. query = query.split(/[\. ]/);
  611. maxDepth = query.length - 1;
  612. $.each(query, function(depth, value) {
  613. var camelCaseValue = (depth != maxDepth)
  614. ? value + query[depth + 1].charAt(0).toUpperCase() + query[depth + 1].slice(1)
  615. : query
  616. ;
  617. if( $.isPlainObject( object[camelCaseValue] ) && (depth != maxDepth) ) {
  618. object = object[camelCaseValue];
  619. }
  620. else if( object[camelCaseValue] !== undefined ) {
  621. found = object[camelCaseValue];
  622. return false;
  623. }
  624. else if( $.isPlainObject( object[value] ) && (depth != maxDepth) ) {
  625. object = object[value];
  626. }
  627. else if( object[value] !== undefined ) {
  628. found = object[value];
  629. return false;
  630. }
  631. else {
  632. module.error(error.method, query);
  633. return false;
  634. }
  635. });
  636. }
  637. if ( $.isFunction( found ) ) {
  638. response = found.apply(context, passedArguments);
  639. }
  640. else if(found !== undefined) {
  641. response = found;
  642. }
  643. if($.isArray(returnedValue)) {
  644. returnedValue.push(response);
  645. }
  646. else if(returnedValue !== undefined) {
  647. returnedValue = [returnedValue, response];
  648. }
  649. else if(response !== undefined) {
  650. returnedValue = response;
  651. }
  652. return found;
  653. }
  654. };
  655. if(methodInvoked) {
  656. if(instance === undefined) {
  657. module.initialize();
  658. }
  659. module.invoke(query);
  660. }
  661. else {
  662. if(instance !== undefined) {
  663. instance.invoke('destroy');
  664. }
  665. module.initialize();
  666. }
  667. })
  668. ;
  669. return (returnedValue !== undefined)
  670. ? returnedValue
  671. : this
  672. ;
  673. };
  674. $.fn.progress.settings = {
  675. name : 'Progress',
  676. namespace : 'progress',
  677. debug : false,
  678. verbose : false,
  679. performance : true,
  680. random : {
  681. min : 2,
  682. max : 5
  683. },
  684. duration : 300,
  685. autoSuccess : true,
  686. showActivity : true,
  687. limitValues : true,
  688. label : 'percent',
  689. precision : 0,
  690. framerate : (1000 / 30), /// 30 fps
  691. percent : false,
  692. total : false,
  693. value : false,
  694. onChange : function(percent, value, total){},
  695. onSuccess : function(total){},
  696. onActive : function(value, total){},
  697. onError : function(value, total){},
  698. onWarning : function(value, total){},
  699. error : {
  700. method : 'The method you called is not defined.',
  701. nonNumeric : 'Progress value is non numeric',
  702. tooHigh : 'Value specified is above 100%',
  703. tooLow : 'Value specified is below 0%'
  704. },
  705. regExp: {
  706. variable: /\{\$*[A-z0-9]+\}/g
  707. },
  708. metadata: {
  709. percent : 'percent',
  710. total : 'total',
  711. value : 'value'
  712. },
  713. selector : {
  714. bar : '> .bar',
  715. label : '> .label',
  716. progress : '.bar > .progress'
  717. },
  718. text : {
  719. active : false,
  720. error : false,
  721. success : false,
  722. warning : false,
  723. percent : '{percent}%',
  724. ratio : '{value} of {total}'
  725. },
  726. className : {
  727. active : 'active',
  728. error : 'error',
  729. success : 'success',
  730. warning : 'warning'
  731. }
  732. };
  733. })( jQuery, window , document );