2
2
3
3
angular . module ( 'mgcrea.ngStrap.collapse' , [ ] )
4
4
5
- . provider ( '$collapse' , function ( ) {
5
+ . provider ( '$collapse' , function ( ) {
6
6
7
7
var defaults = this . defaults = {
8
8
animation : 'am-collapse' ,
@@ -12,19 +12,19 @@ angular.module('mgcrea.ngStrap.collapse', [])
12
12
allowMultiple : false
13
13
} ;
14
14
15
- var controller = this . controller = function ( $scope , $element , $attrs ) {
15
+ var controller = this . controller = function ( $scope , $element , $attrs ) {
16
16
var self = this ;
17
17
18
18
// Attributes options
19
19
self . $options = angular . copy ( defaults ) ;
20
20
angular . forEach ( [ 'animation' , 'disallowToggle' , 'activeClass' , 'startCollapsed' , 'allowMultiple' ] , function ( key ) {
21
- if ( angular . isDefined ( $attrs [ key ] ) ) self . $options [ key ] = $attrs [ key ] ;
21
+ if ( angular . isDefined ( $attrs [ key ] ) ) self . $options [ key ] = $attrs [ key ] ;
22
22
} ) ;
23
23
24
24
// use string regex match boolean attr falsy values, leave truthy values be
25
25
var falseValueRegExp = / ^ ( f a l s e | 0 | ) $ / i;
26
- angular . forEach ( [ 'disallowToggle' , 'startCollapsed' , 'allowMultiple' ] , function ( key ) {
27
- if ( angular . isDefined ( $attrs [ key ] ) && falseValueRegExp . test ( $attrs [ key ] ) ) {
26
+ angular . forEach ( [ 'disallowToggle' , 'startCollapsed' , 'allowMultiple' ] , function ( key ) {
27
+ if ( angular . isDefined ( $attrs [ key ] ) && falseValueRegExp . test ( $attrs [ key ] ) ) {
28
28
self . $options [ key ] = false ;
29
29
}
30
30
} ) ;
@@ -34,19 +34,19 @@ angular.module('mgcrea.ngStrap.collapse', [])
34
34
35
35
self . $viewChangeListeners = [ ] ;
36
36
37
- self . $registerToggle = function ( element ) {
37
+ self . $registerToggle = function ( element ) {
38
38
self . $toggles . push ( element ) ;
39
39
} ;
40
- self . $registerTarget = function ( element ) {
40
+ self . $registerTarget = function ( element ) {
41
41
self . $targets . push ( element ) ;
42
42
} ;
43
43
44
- self . $unregisterToggle = function ( element ) {
44
+ self . $unregisterToggle = function ( element ) {
45
45
var index = self . $toggles . indexOf ( element ) ;
46
46
// remove toggle from $toggles array
47
47
self . $toggles . splice ( index , 1 ) ;
48
48
} ;
49
- self . $unregisterTarget = function ( element ) {
49
+ self . $unregisterTarget = function ( element ) {
50
50
var index = self . $targets . indexOf ( element ) ;
51
51
52
52
// remove element from $targets array
@@ -60,39 +60,39 @@ angular.module('mgcrea.ngStrap.collapse', [])
60
60
// fix active item indexes
61
61
fixActiveItemIndexes ( index ) ;
62
62
63
- self . $viewChangeListeners . forEach ( function ( fn ) {
63
+ self . $viewChangeListeners . forEach ( function ( fn ) {
64
64
fn ( ) ;
65
65
} ) ;
66
66
} ;
67
67
68
68
// use array to store all the currently open panels
69
69
self . $targets . $active = ! self . $options . startCollapsed ? [ 0 ] : [ ] ;
70
- self . $setActive = $scope . $setActive = function ( value ) {
71
- if ( angular . isArray ( value ) ) {
70
+ self . $setActive = $scope . $setActive = function ( value ) {
71
+ if ( angular . isArray ( value ) ) {
72
72
self . $targets . $active = value ;
73
73
} else if ( ! self . $options . disallowToggle && isActive ( value ) ) {
74
74
deactivateItem ( value ) ;
75
75
} else {
76
76
activateItem ( value ) ;
77
77
}
78
78
79
- self . $viewChangeListeners . forEach ( function ( fn ) {
79
+ self . $viewChangeListeners . forEach ( function ( fn ) {
80
80
fn ( ) ;
81
81
} ) ;
82
82
} ;
83
83
84
- self . $activeIndexes = function ( ) {
84
+ self . $activeIndexes = function ( ) {
85
85
if ( self . $options . allowMultiple ) {
86
86
return self . $targets . $active ;
87
87
}
88
88
return self . $targets . $active . length === 1 ? self . $targets . $active [ 0 ] : - 1 ;
89
89
} ;
90
90
91
- function fixActiveItemIndexes ( index ) {
91
+ function fixActiveItemIndexes ( index ) {
92
92
// item with index was removed, so we
93
93
// need to adjust other items index values
94
94
var activeIndexes = self . $targets . $active ;
95
- for ( var i = 0 ; i < activeIndexes . length ; i ++ ) {
95
+ for ( var i = 0 ; i < activeIndexes . length ; i ++ ) {
96
96
if ( index < activeIndexes [ i ] ) {
97
97
activeIndexes [ i ] = activeIndexes [ i ] - 1 ;
98
98
}
@@ -105,19 +105,19 @@ angular.module('mgcrea.ngStrap.collapse', [])
105
105
}
106
106
}
107
107
108
- function isActive ( value ) {
108
+ function isActive ( value ) {
109
109
var activeItems = self . $targets . $active ;
110
110
return activeItems . indexOf ( value ) !== - 1 ;
111
111
}
112
112
113
- function deactivateItem ( value ) {
113
+ function deactivateItem ( value ) {
114
114
var index = self . $targets . $active . indexOf ( value ) ;
115
115
if ( index !== - 1 ) {
116
116
self . $targets . $active . splice ( index , 1 ) ;
117
117
}
118
118
}
119
119
120
- function activateItem ( value ) {
120
+ function activateItem ( value ) {
121
121
if ( ! self . $options . allowMultiple ) {
122
122
// remove current selected item
123
123
self . $targets . $active . splice ( 0 , 1 ) ;
@@ -130,7 +130,7 @@ angular.module('mgcrea.ngStrap.collapse', [])
130
130
131
131
} ;
132
132
133
- this . $get = function ( ) {
133
+ this . $get = function ( ) {
134
134
var $collapse = { } ;
135
135
$collapse . defaults = defaults ;
136
136
$collapse . controller = controller ;
@@ -139,25 +139,25 @@ angular.module('mgcrea.ngStrap.collapse', [])
139
139
140
140
} )
141
141
142
- . directive ( 'bsCollapse' , function ( $window , $animate , $collapse ) {
142
+ . directive ( 'bsCollapse' , function ( $window , $animate , $collapse ) {
143
143
144
144
return {
145
145
require : [ '?ngModel' , 'bsCollapse' ] ,
146
146
controller : [ '$scope' , '$element' , '$attrs' , $collapse . controller ] ,
147
- link : function postLink ( scope , element , attrs , controllers ) {
147
+ link : function postLink ( scope , element , attrs , controllers ) {
148
148
149
149
var ngModelCtrl = controllers [ 0 ] ;
150
150
var bsCollapseCtrl = controllers [ 1 ] ;
151
151
152
- if ( ngModelCtrl ) {
152
+ if ( ngModelCtrl ) {
153
153
154
154
// Update the modelValue following
155
- bsCollapseCtrl . $viewChangeListeners . push ( function ( ) {
155
+ bsCollapseCtrl . $viewChangeListeners . push ( function ( ) {
156
156
ngModelCtrl . $setViewValue ( bsCollapseCtrl . $activeIndexes ( ) ) ;
157
157
} ) ;
158
158
159
159
// modelValue -> $formatters -> viewValue
160
- ngModelCtrl . $formatters . push ( function ( modelValue ) {
160
+ ngModelCtrl . $formatters . push ( function ( modelValue ) {
161
161
// console.warn('$formatter("%s"): modelValue=%o (%o)', element.attr('ng-model'), modelValue, typeof modelValue);
162
162
if ( angular . isArray ( modelValue ) ) {
163
163
// model value is an array, so just replace
@@ -186,11 +186,11 @@ angular.module('mgcrea.ngStrap.collapse', [])
186
186
187
187
} )
188
188
189
- . directive ( 'bsCollapseToggle' , function ( ) {
189
+ . directive ( 'bsCollapseToggle' , function ( ) {
190
190
191
191
return {
192
192
require : [ '^?ngModel' , '^bsCollapse' ] ,
193
- link : function postLink ( scope , element , attrs , controllers ) {
193
+ link : function postLink ( scope , element , attrs , controllers ) {
194
194
195
195
// var ngModelCtrl = controllers[0];
196
196
var bsCollapseCtrl = controllers [ 1 ] ;
@@ -202,29 +202,36 @@ angular.module('mgcrea.ngStrap.collapse', [])
202
202
bsCollapseCtrl . $registerToggle ( element ) ;
203
203
204
204
// remove toggle from collapse controller when toggle is destroyed
205
- scope . $on ( '$destroy' , function ( ) {
205
+ scope . $on ( '$destroy' , function ( ) {
206
206
bsCollapseCtrl . $unregisterToggle ( element ) ;
207
207
} ) ;
208
208
209
- element . on ( 'click' , function ( ) {
209
+ var actionEventHandler = function ( ) {
210
210
if ( ! attrs . disabled ) {
211
- var index = attrs . bsCollapseToggle && attrs . bsCollapseToggle !== 'bs-collapse-toggle' ? attrs . bsCollapseToggle : bsCollapseCtrl . $toggles . indexOf ( element ) ;
212
- bsCollapseCtrl . $setActive ( index * 1 ) ;
213
- scope . $apply ( ) ;
211
+ var index = attrs . bsCollapseToggle && attrs . bsCollapseToggle !== 'bs-collapse-toggle' ? attrs . bsCollapseToggle : bsCollapseCtrl . $toggles . indexOf ( element ) ;
212
+ bsCollapseCtrl . $setActive ( index * 1 ) ;
213
+ scope . $apply ( ) ;
214
214
}
215
- } ) ;
215
+ }
216
216
217
+ element . on ( 'click' , actionEventHandler ) ;
218
+ element . bind ( 'keydown keypress' , function ( e ) {
219
+ if ( e . which === 13 ) {
220
+ actionEventHandler ( ) ;
221
+ }
222
+ e . preventDefault ( ) ;
223
+ } ) ;
217
224
}
218
225
} ;
219
226
220
227
} )
221
228
222
- . directive ( 'bsCollapseTarget' , function ( $animate ) {
229
+ . directive ( 'bsCollapseTarget' , function ( $animate ) {
223
230
224
231
return {
225
232
require : [ '^?ngModel' , '^bsCollapse' ] ,
226
233
// scope: true,
227
- link : function postLink ( scope , element , attrs , controllers ) {
234
+ link : function postLink ( scope , element , attrs , controllers ) {
228
235
229
236
// var ngModelCtrl = controllers[0];
230
237
var bsCollapseCtrl = controllers [ 1 ] ;
@@ -233,19 +240,19 @@ angular.module('mgcrea.ngStrap.collapse', [])
233
240
element . addClass ( 'collapse' ) ;
234
241
235
242
// Add animation class
236
- if ( bsCollapseCtrl . $options . animation ) {
243
+ if ( bsCollapseCtrl . $options . animation ) {
237
244
element . addClass ( bsCollapseCtrl . $options . animation ) ;
238
245
}
239
246
240
247
// Push pane to parent bsCollapse controller
241
248
bsCollapseCtrl . $registerTarget ( element ) ;
242
249
243
250
// remove pane target from collapse controller when target is destroyed
244
- scope . $on ( '$destroy' , function ( ) {
251
+ scope . $on ( '$destroy' , function ( ) {
245
252
bsCollapseCtrl . $unregisterTarget ( element ) ;
246
253
} ) ;
247
254
248
- function render ( ) {
255
+ function render ( ) {
249
256
var index = bsCollapseCtrl . $targets . indexOf ( element ) ;
250
257
var active = bsCollapseCtrl . $activeIndexes ( ) ;
251
258
var action = 'removeClass' ;
@@ -260,7 +267,7 @@ angular.module('mgcrea.ngStrap.collapse', [])
260
267
$animate [ action ] ( element , bsCollapseCtrl . $options . activeClass ) ;
261
268
}
262
269
263
- bsCollapseCtrl . $viewChangeListeners . push ( function ( ) {
270
+ bsCollapseCtrl . $viewChangeListeners . push ( function ( ) {
264
271
render ( ) ;
265
272
} ) ;
266
273
render ( ) ;
0 commit comments