1+ angular . module ( 'ui.bootstrap.progressbar' , [ 'ui.bootstrap.transition' ] )
2+
3+ . constant ( 'progressConfig' , {
4+ animate : true ,
5+ autoType : false ,
6+ stackedTypes : [ 'success' , 'info' , 'warning' , 'danger' ]
7+ } )
8+
9+ . controller ( 'ProgressBarController' , [ '$scope' , '$attrs' , 'progressConfig' , function ( $scope , $attrs , progressConfig ) {
10+
11+ // Whether bar transitions should be animated
12+ var animate = angular . isDefined ( $attrs . animate ) ? $scope . $eval ( $attrs . animate ) : progressConfig . animate ;
13+ var autoType = angular . isDefined ( $attrs . autoType ) ? $scope . $eval ( $attrs . autoType ) : progressConfig . autoType ;
14+ var stackedTypes = angular . isDefined ( $attrs . stackedTypes ) ? $scope . $eval ( '[' + $attrs . stackedTypes + ']' ) : progressConfig . stackedTypes ;
15+
16+ // Create bar object
17+ this . makeBar = function ( newBar , oldBar , index ) {
18+ var newValue = ( angular . isObject ( newBar ) ) ? newBar . value : ( newBar || 0 ) ;
19+ var oldValue = ( angular . isObject ( oldBar ) ) ? oldBar . value : ( oldBar || 0 ) ;
20+ var type = ( angular . isObject ( newBar ) && angular . isDefined ( newBar . type ) ) ? newBar . type : ( autoType ) ? getStackedType ( index || 0 ) : null ;
21+
22+ return {
23+ from : oldValue ,
24+ to : newValue ,
25+ type : type ,
26+ animate : animate
27+ } ;
28+ } ;
29+
30+ function getStackedType ( index ) {
31+ return stackedTypes [ index ] ;
32+ }
33+
34+ this . addBar = function ( bar ) {
35+ $scope . bars . push ( bar ) ;
36+ $scope . totalPercent += bar . to ;
37+ } ;
38+
39+ this . clearBars = function ( ) {
40+ $scope . bars = [ ] ;
41+ $scope . totalPercent = 0 ;
42+ } ;
43+ this . clearBars ( ) ;
44+ } ] )
45+
46+ . directive ( 'progress' , function ( ) {
47+ return {
48+ restrict : 'EA' ,
49+ replace : true ,
50+ controller : 'ProgressBarController' ,
51+ scope : {
52+ value : '=' ,
53+ onFull : '&' ,
54+ onEmpty : '&'
55+ } ,
56+ templateUrl : 'template/progressbar/progress.html' ,
57+ link : function ( scope , element , attrs , controller ) {
58+ scope . $watch ( 'value' , function ( newValue , oldValue ) {
59+ controller . clearBars ( ) ;
60+
61+ if ( angular . isArray ( newValue ) ) {
62+ // Stacked progress bar
63+ for ( var i = 0 , n = newValue . length ; i < n ; i ++ ) {
64+ controller . addBar ( controller . makeBar ( newValue [ i ] , oldValue [ i ] , i ) ) ;
65+ }
66+ } else {
67+ // Simple bar
68+ controller . addBar ( controller . makeBar ( newValue , oldValue ) ) ;
69+ }
70+ } , true ) ;
71+
72+ // Total percent listeners
73+ scope . $watch ( 'totalPercent' , function ( value ) {
74+ if ( value >= 100 ) {
75+ scope . onFull ( ) ;
76+ } else if ( value <= 0 ) {
77+ scope . onEmpty ( ) ;
78+ }
79+ } , true ) ;
80+ }
81+ } ;
82+ } )
83+
84+ . directive ( 'progressbar' , [ '$transition' , function ( $transition ) {
85+ return {
86+ restrict : 'EA' ,
87+ replace : true ,
88+ scope : {
89+ width : '=' ,
90+ old : '=' ,
91+ type : '=' ,
92+ animate : '='
93+ } ,
94+ templateUrl : 'template/progressbar/bar.html' ,
95+ link : function ( scope , element ) {
96+ scope . $watch ( 'width' , function ( value ) {
97+ if ( scope . animate ) {
98+ element . css ( 'width' , scope . old + '%' ) ;
99+ $transition ( element , { width : value + '%' } ) ;
100+ } else {
101+ element . css ( 'width' , value + '%' ) ;
102+ }
103+ } ) ;
104+ }
105+ } ;
106+ } ] ) ;
0 commit comments