bootstrap-progress-v0.9.0.js 6.0 KB

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