progressbar.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. angular.module('ui.bootstrap.progressbar', [])
  2. .constant('uibProgressConfig', {
  3. animate: true,
  4. max: 100
  5. })
  6. .controller('UibProgressController', ['$scope', '$attrs', 'uibProgressConfig', function($scope, $attrs, progressConfig) {
  7. var self = this,
  8. animate = angular.isDefined($attrs.animate) ? $scope.$parent.$eval($attrs.animate) : progressConfig.animate;
  9. this.bars = [];
  10. $scope.max = angular.isDefined($scope.max) ? $scope.max : progressConfig.max;
  11. this.addBar = function(bar, element, attrs) {
  12. if (!animate) {
  13. element.css({'transition': 'none'});
  14. }
  15. this.bars.push(bar);
  16. bar.max = $scope.max;
  17. bar.title = attrs && angular.isDefined(attrs.title) ? attrs.title : 'progressbar';
  18. bar.$watch('value', function(value) {
  19. bar.recalculatePercentage();
  20. });
  21. bar.recalculatePercentage = function() {
  22. var totalPercentage = self.bars.reduce(function(total, bar) {
  23. bar.percent = +(100 * bar.value / bar.max).toFixed(2);
  24. return total + bar.percent;
  25. }, 0);
  26. if (totalPercentage > 100) {
  27. bar.percent -= totalPercentage - 100;
  28. }
  29. };
  30. bar.$on('$destroy', function() {
  31. element = null;
  32. self.removeBar(bar);
  33. });
  34. };
  35. this.removeBar = function(bar) {
  36. this.bars.splice(this.bars.indexOf(bar), 1);
  37. this.bars.forEach(function (bar) {
  38. bar.recalculatePercentage();
  39. });
  40. };
  41. $scope.$watch('max', function(max) {
  42. self.bars.forEach(function(bar) {
  43. bar.max = $scope.max;
  44. bar.recalculatePercentage();
  45. });
  46. });
  47. }])
  48. .directive('uibProgress', function() {
  49. return {
  50. replace: true,
  51. transclude: true,
  52. controller: 'UibProgressController',
  53. require: 'uibProgress',
  54. scope: {
  55. max: '=?'
  56. },
  57. templateUrl: 'template/progressbar/progress.html'
  58. };
  59. })
  60. .directive('uibBar', function() {
  61. return {
  62. replace: true,
  63. transclude: true,
  64. require: '^uibProgress',
  65. scope: {
  66. value: '=',
  67. type: '@'
  68. },
  69. templateUrl: 'template/progressbar/bar.html',
  70. link: function(scope, element, attrs, progressCtrl) {
  71. progressCtrl.addBar(scope, element, attrs);
  72. }
  73. };
  74. })
  75. .directive('uibProgressbar', function() {
  76. return {
  77. replace: true,
  78. transclude: true,
  79. controller: 'UibProgressController',
  80. scope: {
  81. value: '=',
  82. max: '=?',
  83. type: '@'
  84. },
  85. templateUrl: 'template/progressbar/progressbar.html',
  86. link: function(scope, element, attrs, progressCtrl) {
  87. progressCtrl.addBar(scope, angular.element(element.children()[0]), {title: attrs.title});
  88. }
  89. };
  90. });
  91. /* Deprecated progressbar below */
  92. angular.module('ui.bootstrap.progressbar')
  93. .value('$progressSuppressWarning', false)
  94. .controller('ProgressController', ['$scope', '$attrs', 'uibProgressConfig', '$log', '$progressSuppressWarning', function($scope, $attrs, progressConfig, $log, $progressSuppressWarning) {
  95. if (!$progressSuppressWarning) {
  96. $log.warn('ProgressController is now deprecated. Use UibProgressController instead.');
  97. }
  98. var self = this,
  99. animate = angular.isDefined($attrs.animate) ? $scope.$parent.$eval($attrs.animate) : progressConfig.animate;
  100. this.bars = [];
  101. $scope.max = angular.isDefined($scope.max) ? $scope.max : progressConfig.max;
  102. this.addBar = function(bar, element, attrs) {
  103. if (!animate) {
  104. element.css({'transition': 'none'});
  105. }
  106. this.bars.push(bar);
  107. bar.max = $scope.max;
  108. bar.title = attrs && angular.isDefined(attrs.title) ? attrs.title : 'progressbar';
  109. bar.$watch('value', function(value) {
  110. bar.recalculatePercentage();
  111. });
  112. bar.recalculatePercentage = function() {
  113. bar.percent = +(100 * bar.value / bar.max).toFixed(2);
  114. var totalPercentage = self.bars.reduce(function(total, bar) {
  115. return total + bar.percent;
  116. }, 0);
  117. if (totalPercentage > 100) {
  118. bar.percent -= totalPercentage - 100;
  119. }
  120. };
  121. bar.$on('$destroy', function() {
  122. element = null;
  123. self.removeBar(bar);
  124. });
  125. };
  126. this.removeBar = function(bar) {
  127. this.bars.splice(this.bars.indexOf(bar), 1);
  128. };
  129. $scope.$watch('max', function(max) {
  130. self.bars.forEach(function(bar) {
  131. bar.max = $scope.max;
  132. bar.recalculatePercentage();
  133. });
  134. });
  135. }])
  136. .directive('progress', ['$log', '$progressSuppressWarning', function($log, $progressSuppressWarning) {
  137. return {
  138. replace: true,
  139. transclude: true,
  140. controller: 'ProgressController',
  141. require: 'progress',
  142. scope: {
  143. max: '=?',
  144. title: '@?'
  145. },
  146. templateUrl: 'template/progressbar/progress.html',
  147. link: function() {
  148. if (!$progressSuppressWarning) {
  149. $log.warn('progress is now deprecated. Use uib-progress instead.');
  150. }
  151. }
  152. };
  153. }])
  154. .directive('bar', ['$log', '$progressSuppressWarning', function($log, $progressSuppressWarning) {
  155. return {
  156. replace: true,
  157. transclude: true,
  158. require: '^progress',
  159. scope: {
  160. value: '=',
  161. type: '@'
  162. },
  163. templateUrl: 'template/progressbar/bar.html',
  164. link: function(scope, element, attrs, progressCtrl) {
  165. if (!$progressSuppressWarning) {
  166. $log.warn('bar is now deprecated. Use uib-bar instead.');
  167. }
  168. progressCtrl.addBar(scope, element);
  169. }
  170. };
  171. }])
  172. .directive('progressbar', ['$log', '$progressSuppressWarning', function($log, $progressSuppressWarning) {
  173. return {
  174. replace: true,
  175. transclude: true,
  176. controller: 'ProgressController',
  177. scope: {
  178. value: '=',
  179. max: '=?',
  180. type: '@'
  181. },
  182. templateUrl: 'template/progressbar/progressbar.html',
  183. link: function(scope, element, attrs, progressCtrl) {
  184. if (!$progressSuppressWarning) {
  185. $log.warn('progressbar is now deprecated. Use uib-progressbar instead.');
  186. }
  187. progressCtrl.addBar(scope, angular.element(element.children()[0]), {title: attrs.title});
  188. }
  189. };
  190. }]);