31
31
* --------------------------------------------------------------------------------
32
32
*/
33
33
34
- angular . module ( 'multi-select' , [ 'ng' ] ) . directive ( 'multiSelect' , [ '$sce' , function ( $sce ) {
34
+ angular . module ( 'multi-select' , [ 'ng' ] ) . directive ( 'multiSelect' , [ '$sce' , '$filter' , function ( $sce , $filter ) {
35
35
return {
36
36
restrict :
37
37
'AE' ,
@@ -52,47 +52,93 @@ angular.module( 'multi-select', ['ng'] ).directive( 'multiSelect' , [ '$sce', fu
52
52
maxLabels : '@' ,
53
53
isDisabled : '=' ,
54
54
directiveId : '@' ,
55
- // JH DotComIt Added 5/8/2014
56
- onPopupopen : '&onPopupopen' ,
57
- onPopupclose : '&onPopupclose'
55
+ helperElements : '@' ,
56
+ onOpen : '&' ,
57
+ onClose : '&' ,
58
+ onBlur : '&' ,
59
+ onFocus : '&'
58
60
} ,
59
61
60
62
template :
61
- '<span class="multiSelect inlineBlock">' +
62
- '<button type="button" class="multiSelect button multiSelectButton" ng-click="toggleCheckboxes( $event ); refreshSelectedItems();" ng-bind-html="varButtonLabel">' +
63
+ '<span class="multiSelect inlineBlock" >' +
64
+ '<button type="button" class="multiSelect button multiSelectButton" ng-click="toggleCheckboxes( $event ); refreshSelectedItems();" ng-bind-html="varButtonLabel" ng-focus="onFocus()" ng-blur="onBlur()" >' +
63
65
'</button>' +
64
66
'<div class="multiSelect checkboxLayer hide">' +
65
67
'<div class="multiSelect line">' +
66
- '<span ng-if="!isDisabled">Select: </span>' +
67
- '<button type="button" ng-click="select( \'all\' )" class="multiSelect helperButton" ng-if="!isDisabled && selectionMode.toUpperCase() != \'SINGLE\' ">All</button> ' +
68
- '<button type="button" ng-click="select( \'none\' )" class="multiSelect helperButton" ng-if="!isDisabled && selectionMode.toUpperCase() != \'SINGLE\' ">None</button> ' +
69
- '<button type="button" ng-click="select( \'reset\' )" class="multiSelect helperButton" ng-if="!isDisabled">Reset</button>' +
68
+ '<span ng-if="!isDisabled && ( displayHelper( \'all\' ) || displayHelper( \'none\' ) || displayHelper( \'reset\' )) ">Select: </span>' +
69
+ '<button type="button" ng-click="select( \'all\' )" class="multiSelect helperButton" ng-if="!isDisabled && displayHelper( \'all\' ) ">All</button> ' +
70
+ '<button type="button" ng-click="select( \'none\' )" class="multiSelect helperButton" ng-if="!isDisabled && displayHelper( \'none\' ) ">None</button> ' +
71
+ '<button type="button" ng-click="select( \'reset\' )" class="multiSelect helperButton" ng-if="!isDisabled && displayHelper( \'reset\' ) ">Reset</button>' +
70
72
'</div>' +
71
- '<div class="multiSelect line">' +
73
+ '<div class="multiSelect line" ng-show="displayHelper( \'filter\' )" >' +
72
74
'Filter: <input class="multiSelect" type="text" ng-model="labelFilter" />' +
73
75
' <button type="button" class="multiSelect helperButton" ng-click="labelFilter=\'\'">Clear</button>' +
74
76
'</div>' +
75
- '<div ng-repeat="item in inputModel | filter:labelFilter" ng-class="orientation" class="multiSelect multiSelectItem">' +
77
+ '<div ng-repeat="item in (filteredModel = ( inputModel | filter:labelFilter )) " ng-class="orientation" class="multiSelect multiSelectItem">' +
76
78
'<div class="multiSelect acol">' +
77
79
'<div class="multiSelect" ng-show="item[ tickProperty ]">✔</div>' +
78
80
'</div>' +
79
81
'<div class="multiSelect acol">' +
80
82
'<label class="multiSelect" ng-class="{checkboxSelected:item[ tickProperty ]}">' +
81
- '<input class="multiSelect checkbox" type="checkbox" ng-disabled="itemIsDisabled( item )" ng-checked="item[ tickProperty ]" ng-click="syncItems( item, $event )" />' +
82
- '<span class="multiSelect" ng-class="{disabled:itemIsDisabled( item )}" ng-bind-html="writeLabel( item, \'itemLabel\' )"></span>' +
83
+ '<input class="multiSelect checkbox" type="checkbox" ng-disabled="itemIsDisabled( item )" ng-checked="item[ tickProperty ]" ng-click="syncItems( item, $event )"/>' +
84
+ '<span class="multiSelect" ng-class="{disabled:itemIsDisabled( item )}" ng-bind-html="writeLabel( item, \'itemLabel\' )"></span>' +
83
85
'</label> ' +
84
86
'</div>' +
85
87
'</div>' +
86
88
'</div>' +
87
89
'</span>' ,
88
90
89
- link : function ( $scope , element , attrs ) {
91
+ link : function ( $scope , element , attrs ) {
90
92
91
93
$scope . selectedItems = [ ] ;
92
94
$scope . backUp = [ ] ;
93
- $scope . varButtonLabel = '' ;
95
+ $scope . varButtonLabel = '' ;
96
+ $scope . tabIndex = 0 ;
97
+ $scope . tabables = null ;
98
+ $scope . currentButton = null ;
99
+
100
+ // Show or hide a helper element
101
+ $scope . displayHelper = function ( elementString ) {
102
+ if ( typeof attrs . helperElements === 'undefined' ) {
103
+ return true ;
104
+ }
105
+ switch ( elementString . toUpperCase ( ) ) {
106
+ case 'ALL' :
107
+ if ( attrs . selectionMode && $scope . selectionMode . toUpperCase ( ) === 'SINGLE' ) {
108
+ return false ;
109
+ }
110
+ else {
111
+ if ( attrs . helperElements && $scope . helperElements . toUpperCase ( ) . indexOf ( 'ALL' ) >= 0 ) {
112
+ return true ;
113
+ }
114
+ }
115
+ break ;
116
+ case 'NONE' :
117
+ if ( attrs . selectionMode && $scope . selectionMode . toUpperCase ( ) === 'SINGLE' ) {
118
+ return false ;
119
+ }
120
+ else {
121
+ if ( attrs . helperElements && $scope . helperElements . toUpperCase ( ) . indexOf ( 'NONE' ) >= 0 ) {
122
+ return true ;
123
+ }
124
+ }
125
+ break ;
126
+ case 'RESET' :
127
+ if ( attrs . helperElements && $scope . helperElements . toUpperCase ( ) . indexOf ( 'RESET' ) >= 0 ) {
128
+ return true ;
129
+ }
130
+ break ;
131
+ case 'FILTER' :
132
+ if ( attrs . helperElements && $scope . helperElements . toUpperCase ( ) . indexOf ( 'FILTER' ) >= 0 ) {
133
+ return true ;
134
+ }
135
+ break ;
136
+ default :
137
+ break ;
138
+ }
139
+ }
94
140
95
- // Checkbox is ticked
141
+ // Call this function when a checkbox is ticked...
96
142
$scope . syncItems = function ( item , e ) {
97
143
index = $scope . inputModel . indexOf ( item ) ;
98
144
$scope . inputModel [ index ] [ $scope . tickProperty ] = ! $scope . inputModel [ index ] [ $scope . tickProperty ] ;
@@ -107,8 +153,9 @@ angular.module( 'multi-select', ['ng'] ).directive( 'multiSelect' , [ '$sce', fu
107
153
}
108
154
$scope . toggleCheckboxes ( e ) ;
109
155
}
110
-
111
- $scope . refreshSelectedItems ( ) ;
156
+
157
+ $scope . refreshSelectedItems ( ) ;
158
+ e . target . focus ( ) ;
112
159
}
113
160
114
161
// Refresh the button to display the selected items and push into output model if specified
@@ -199,7 +246,7 @@ angular.module( 'multi-select', ['ng'] ).directive( 'multiSelect' , [ '$sce', fu
199
246
return $sce . trustAsHtml ( label ) ;
200
247
}
201
248
202
- // UI operations to show/hide checkboxes
249
+ // UI operations to show/hide checkboxes based on click event..
203
250
$scope . toggleCheckboxes = function ( e ) {
204
251
205
252
if ( e . target ) {
@@ -238,20 +285,19 @@ angular.module( 'multi-select', ['ng'] ).directive( 'multiSelect' , [ '$sce', fu
238
285
for ( i = 0 ; i < checkboxes . length ; i ++ ) {
239
286
if ( i != multiSelectIndex ) {
240
287
checkboxes [ i ] . className = 'multiSelect checkboxLayer hide' ;
241
- // JH DotComIt 5/8/2014 Added method handler for closing the popup
242
- $scope . onPopupclose ( ) ;
243
288
}
244
289
}
245
290
246
291
if ( checkboxes [ multiSelectIndex ] . className == 'multiSelect checkboxLayer hide' ) {
292
+ $scope . currentButton = multiSelectButtons [ multiSelectIndex ] ;
247
293
checkboxes [ multiSelectIndex ] . className = 'multiSelect checkboxLayer show' ;
248
- // JH DotComIt 5/8/2014 Added method handler for opening the popup
249
- $scope . onPopupopen ( ) ;
294
+ // https://github.com/isteven/angular-multi-select/pull/5 - On open callback
295
+ $scope . onOpen ( ) ;
250
296
}
251
297
else if ( checkboxes [ multiSelectIndex ] . className == 'multiSelect checkboxLayer show' ) {
252
298
checkboxes [ multiSelectIndex ] . className = 'multiSelect checkboxLayer hide' ;
253
- // JH DotComIt 5/8/2014 Added method handler for closing the popup
254
- $scope . onPopupclose ( ) ;
299
+ // https://github.com/isteven/angular-multi-select/pull/5 - On close callback
300
+ $scope . onClose ( ) ;
255
301
}
256
302
}
257
303
}
@@ -289,8 +335,8 @@ angular.module( 'multi-select', ['ng'] ).directive( 'multiSelect' , [ '$sce', fu
289
335
}
290
336
} ) ;
291
337
break ;
292
- case 'RESET' :
293
- $scope . inputModel = angular . copy ( $scope . backUp ) ;
338
+ case 'RESET' :
339
+ $scope . inputModel = angular . copy ( $scope . backUp ) ;
294
340
break ;
295
341
default :
296
342
}
@@ -299,6 +345,7 @@ angular.module( 'multi-select', ['ng'] ).directive( 'multiSelect' , [ '$sce', fu
299
345
300
346
301
347
// Generic validation for required attributes
348
+ // Might give false positives so just ignore if everything's alright.
302
349
validate = function ( ) {
303
350
if ( ! ( 'inputModel' in attrs ) ) {
304
351
console . log ( 'Multi-select error: input-model is not defined! (ID: ' + $scope . directiveId + ')' ) ;
@@ -337,17 +384,16 @@ angular.module( 'multi-select', ['ng'] ).directive( 'multiSelect' , [ '$sce', fu
337
384
} ) ;
338
385
if ( notThere === true ) {
339
386
console . log ( 'Multi-select error: property "' + missingLabel + '" is not available in the input model. (Name: ' + $scope . directiveId + ')' ) ;
340
- }
341
-
342
- }
387
+ }
388
+ }
343
389
344
390
///////////////////////
345
391
// Logic starts here
346
392
///////////////////////
347
393
348
394
validate ( ) ;
349
- $scope . refreshSelectedItems ( ) ;
350
-
395
+ $scope . refreshSelectedItems ( ) ;
396
+
351
397
// Watch for changes in input model (allow dynamic input)
352
398
$scope . $watch ( 'inputModel' , function ( oldVal , newVal ) {
353
399
if ( $scope . inputModel !== 'undefined' ) {
@@ -368,14 +414,12 @@ angular.module( 'multi-select', ['ng'] ).directive( 'multiSelect' , [ '$sce', fu
368
414
var checkboxes = document . querySelectorAll ( '.checkboxLayer' ) ;
369
415
if ( e . target . className . indexOf ( 'multiSelect' ) === - 1 ) {
370
416
for ( i = 0 ; i < checkboxes . length ; i ++ ) {
371
- checkboxes [ i ] . className = 'multiSelect checkboxLayer hide' ;
372
- // JH DotComIt 5/8/2014 Added method handler for closing the popup
373
- $scope . onPopupclose ( ) ;
417
+ checkboxes [ i ] . className = 'multiSelect checkboxLayer hide' ;
374
418
}
375
419
e . stopPropagation ( ) ;
376
420
}
377
- } ) ;
378
-
421
+ } ) ;
422
+
379
423
// For IE8, perhaps. Not sure if this is really executed.
380
424
if ( ! Array . prototype . indexOf ) {
381
425
Array . prototype . indexOf = function ( what , i ) {
0 commit comments