bootstrap-progressbar.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*!
  2. * bootstrap-progressbar v0.9.0 by @minddust
  3. * Copyright (c) 2012-2015 Stephan Groß
  4. *
  5. * http://www.minddust.com/project/bootstrap-progressbar/
  6. *
  7. * Licensed under the MIT license:
  8. * http://www.opensource.org/licenses/MIT
  9. */
  10. (function($) {
  11. 'use strict';
  12. // PROGRESSBAR CLASS DEFINITION
  13. // ============================
  14. var Progressbar = function(element, options) {
  15. this.$element = $(element);
  16. this.options = $.extend({}, Progressbar.defaults, options);
  17. };
  18. Progressbar.defaults = {
  19. transition_delay: 300,
  20. refresh_speed: 50,
  21. display_text: 'none',
  22. use_percentage: true,
  23. percent_format: function(percent) { return percent + '%'; },
  24. amount_format: function(amount_part, amount_max, amount_min) { return amount_part + ' / ' + amount_max; },
  25. update: $.noop,
  26. done: $.noop,
  27. fail: $.noop
  28. };
  29. Progressbar.prototype.transition = function() {
  30. var $this = this.$element;
  31. var $parent = $this.parent();
  32. var $back_text = this.$back_text;
  33. var $front_text = this.$front_text;
  34. var options = this.options;
  35. var data_transitiongoal = parseInt($this.attr('data-transitiongoal'));
  36. var aria_valuemin = parseInt($this.attr('aria-valuemin')) || 0;
  37. var aria_valuemax = parseInt($this.attr('aria-valuemax')) || 100;
  38. var is_vertical = $parent.hasClass('vertical');
  39. var update = options.update && typeof options.update === 'function' ? options.update : Progressbar.defaults.update;
  40. var done = options.done && typeof options.done === 'function' ? options.done : Progressbar.defaults.done;
  41. var fail = options.fail && typeof options.fail === 'function' ? options.fail : Progressbar.defaults.fail;
  42. if (isNaN(data_transitiongoal)) {
  43. fail('data-transitiongoal not set');
  44. return;
  45. }
  46. var percentage = Math.round(100 * (data_transitiongoal - aria_valuemin) / (aria_valuemax - aria_valuemin));
  47. if (options.display_text === 'center' && !$back_text && !$front_text) {
  48. this.$back_text = $back_text = $('<span>').addClass('progressbar-back-text').prependTo($parent);
  49. this.$front_text = $front_text = $('<span>').addClass('progressbar-front-text').prependTo($this);
  50. var parent_size;
  51. if (is_vertical) {
  52. parent_size = $parent.css('height');
  53. $back_text.css({height: parent_size, 'line-height': parent_size});
  54. $front_text.css({height: parent_size, 'line-height': parent_size});
  55. $(window).resize(function() {
  56. parent_size = $parent.css('height');
  57. $back_text.css({height: parent_size, 'line-height': parent_size});
  58. $front_text.css({height: parent_size, 'line-height': parent_size});
  59. }); // normal resizing would brick the structure because width is in px
  60. }
  61. else {
  62. parent_size = $parent.css('width');
  63. $front_text.css({width: parent_size});
  64. $(window).resize(function() {
  65. parent_size = $parent.css('width');
  66. $front_text.css({width: parent_size});
  67. }); // normal resizing would brick the structure because width is in px
  68. }
  69. }
  70. setTimeout(function() {
  71. var current_percentage;
  72. var current_value;
  73. var this_size;
  74. var parent_size;
  75. var text;
  76. if (is_vertical) {
  77. $this.css('height', percentage + '%');
  78. }
  79. else {
  80. $this.css('width', percentage + '%');
  81. }
  82. var progress = setInterval(function() {
  83. if (is_vertical) {
  84. this_size = $this.height();
  85. parent_size = $parent.height();
  86. }
  87. else {
  88. this_size = $this.width();
  89. parent_size = $parent.width();
  90. }
  91. current_percentage = Math.round(100 * this_size / parent_size);
  92. current_value = Math.round(aria_valuemin + this_size / parent_size * (aria_valuemax - aria_valuemin));
  93. if (current_percentage >= percentage) {
  94. current_percentage = percentage;
  95. current_value = data_transitiongoal;
  96. done($this);
  97. clearInterval(progress);
  98. }
  99. if (options.display_text !== 'none') {
  100. text = options.use_percentage ? options.percent_format(current_percentage) : options.amount_format(current_value, aria_valuemax, aria_valuemin);
  101. if (options.display_text === 'fill') {
  102. $this.text(text);
  103. }
  104. else if (options.display_text === 'center') {
  105. $back_text.text(text);
  106. $front_text.text(text);
  107. }
  108. }
  109. $this.attr('aria-valuenow', current_value);
  110. update(current_percentage, $this);
  111. }, options.refresh_speed);
  112. }, options.transition_delay);
  113. };
  114. // PROGRESSBAR PLUGIN DEFINITION
  115. // =============================
  116. var old = $.fn.progressbar;
  117. $.fn.progressbar = function(option) {
  118. return this.each(function () {
  119. var $this = $(this);
  120. var data = $this.data('bs.progressbar');
  121. var options = typeof option === 'object' && option;
  122. if (data && options) {
  123. $.extend(data.options, options);
  124. }
  125. if (!data) {
  126. $this.data('bs.progressbar', (data = new Progressbar(this, options)));
  127. }
  128. data.transition();
  129. });
  130. };
  131. $.fn.progressbar.Constructor = Progressbar;
  132. // PROGRESSBAR NO CONFLICT
  133. // =======================
  134. $.fn.progressbar.noConflict = function () {
  135. $.fn.progressbar = old;
  136. return this;
  137. };
  138. })(window.jQuery);