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.

778 lines
25 KiB

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