1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
|
/*
* Angular JS Multi Select
* Creates a dropdown-like button with checkboxes.
*
* Created: Tue, 14 Jan 2014 - 5:18:02 PM
*
* Released under the MIT License
*
* --------------------------------------------------------------------------------
* The MIT License (MIT)
*
* Copyright (c) 2014 Ignatius Steven (https://github.com/isteven)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* --------------------------------------------------------------------------------
*/
angular.module( 'multi-select', ['ng'] ).directive( 'multiSelect' , [ '$sce', '$filter', function ( $sce, $filter ) {
return {
restrict:
'AE',
replace:
true,
scope:
{
inputModel : '=',
outputModel : '=',
buttonLabel : '@',
selectionMode : '@',
itemLabel : '@',
tickProperty : '@',
disableProperty : '@',
orientation : '@',
maxLabels : '@',
isDisabled : '=',
directiveId : '@',
helperElements : '@',
onOpen : '&',
onClose : '&',
onBlur : '&',
onFocus : '&',
},
template:
'<span class="multiSelect inlineBlock" >' +
'<button type="button" class="multiSelect button multiSelectButton" ng-click="toggleCheckboxes( $event ); refreshSelectedItems();" ng-bind-html="varButtonLabel" ng-focus="onFocus()" ng-blur="onBlur()">' +
'</button>' +
'<div class="multiSelect checkboxLayer hide">' +
'<div class="multiSelect line" ng-show="displayHelper( \'all\' ) || displayHelper( \'none\' ) || displayHelper( \'reset\' )">' +
'<span ng-if="!isDisabled && ( displayHelper( \'all\' ) || displayHelper( \'none\' ) || displayHelper( \'reset\' ))">Select: </span>' +
'<button type="button" ng-click="select( \'all\' )" class="multiSelect helperButton" ng-if="!isDisabled && displayHelper( \'all\' )">All</button> ' +
'<button type="button" ng-click="select( \'none\' )" class="multiSelect helperButton" ng-if="!isDisabled && displayHelper( \'none\' )">None</button> ' +
'<button type="button" ng-click="select( \'reset\' )" class="multiSelect helperButton" ng-if="!isDisabled && displayHelper( \'reset\' )">Reset</button>' +
'</div>' +
'<div class="multiSelect line" ng-show="displayHelper( \'filter\' )">' +
'Filter: <input class="multiSelect" type="text" ng-model="labelFilter" />' +
' <button type="button" class="multiSelect helperButton" ng-click="labelFilter=\'\'">Clear</button>' +
'</div>' +
'<div ng-repeat="item in (filteredModel = (inputModel | filter:labelFilter ))" ng-class="orientation" class="multiSelect multiSelectItem">' +
'<div class="multiSelect acol">' +
'<div class="multiSelect" ng-show="item[ tickProperty ]">✔</div>' +
'</div>' +
'<div class="multiSelect acol">' +
'<label class="multiSelect" ng-class="{checkboxSelected:item[ tickProperty ]}">' +
'<input class="multiSelect checkbox" type="checkbox" ng-disabled="itemIsDisabled( item )" ng-checked="item[ tickProperty ]" ng-click="syncItems( item, $event )"/>' +
'<span class="multiSelect" ng-class="{disabled:itemIsDisabled( item )}" ng-bind-html="writeLabel( item, \'itemLabel\' )"></span>' +
'</label> ' +
'</div>' +
'</div>' +
'</div>' +
'</span>',
link: function ( $scope, element, attrs ) {
$scope.selectedItems = [];
$scope.backUp = [];
$scope.varButtonLabel = '';
$scope.currentButton = null;
$scope.scrolled = false;
// Show or hide a helper element
$scope.displayHelper = function( elementString ) {
if ( typeof attrs.helperElements === 'undefined' ) {
return true;
}
switch( elementString.toUpperCase() ) {
case 'ALL':
if ( attrs.selectionMode && $scope.selectionMode.toUpperCase() === 'SINGLE' ) {
return false;
}
else {
if ( attrs.helperElements && $scope.helperElements.toUpperCase().indexOf( 'ALL' ) >= 0 ) {
return true;
}
}
break;
case 'NONE':
if ( attrs.selectionMode && $scope.selectionMode.toUpperCase() === 'SINGLE' ) {
return false;
}
else {
if ( attrs.helperElements && $scope.helperElements.toUpperCase().indexOf( 'NONE' ) >= 0 ) {
return true;
}
}
break;
case 'RESET':
if ( attrs.helperElements && $scope.helperElements.toUpperCase().indexOf( 'RESET' ) >= 0 ) {
return true;
}
break;
case 'FILTER':
if ( attrs.helperElements && $scope.helperElements.toUpperCase().indexOf( 'FILTER' ) >= 0 ) {
return true;
}
break;
default:
break;
}$scope
}
// Call this function when a checkbox is ticked...
$scope.syncItems = function( item, e ) {
index = $scope.inputModel.indexOf( item );
$scope.inputModel[ index ][ $scope.tickProperty ] = !$scope.inputModel[ index ][ $scope.tickProperty ];
// If it's single selection mode
if ( attrs.selectionMode && $scope.selectionMode.toUpperCase() === 'SINGLE' ) {
$scope.inputModel[ index ][ $scope.tickProperty ] = true;
for( i=0; i<$scope.inputModel.length;i++) {
if ( i !== index ) {
$scope.inputModel[ i ][ $scope.tickProperty ] = false;
}
}
$scope.toggleCheckboxes( e );
}
$scope.refreshSelectedItems();
e.target.focus();
}
// Refresh the button to display the selected items and push into output model if specified
$scope.refreshSelectedItems = function() {
$scope.varButtonLabel = '';
$scope.selectedItems = [];
ctr = 0;
angular.forEach( $scope.inputModel, function( value, key ) {
if ( typeof value !== 'undefined' ) {
if ( value[ $scope.tickProperty ] === true || value[ $scope.tickProperty ] === 'true' ) {
$scope.selectedItems.push( value );
}
}
});
// Push into output model
if ( typeof attrs.outputModel !== 'undefined' ) {
$scope.outputModel = angular.copy( $scope.selectedItems );
}
// Write label...
if ( $scope.selectedItems.length === 0 ) {
$scope.varButtonLabel = 'None selected';
}
else {
var tempMaxLabels = $scope.selectedItems.length;
if ( typeof $scope.maxLabels !== 'undefined' && $scope.maxLabels !== '' && $scope.maxLabels !== "0" ) {
tempMaxLabels = $scope.maxLabels;
}
// If max amount of labels displayed..
if ( $scope.selectedItems.length > tempMaxLabels ) {
$scope.more = true;
}
else {
$scope.more = false;
}
angular.forEach( $scope.selectedItems, function( value, key ) {
if ( typeof value !== 'undefined' ) {
if ( ctr < tempMaxLabels ) {
$scope.varButtonLabel += ( $scope.varButtonLabel.length > 0 ? ', ' : '') + $scope.writeLabel( value, 'buttonLabel' );
}
ctr++;
}
});
if ( $scope.more === true ) {
$scope.varButtonLabel += ', ... (Total: ' + $scope.selectedItems.length + ')';
}$scope
}
$scope.varButtonLabel = $sce.trustAsHtml( $scope.varButtonLabel + '<span class="multiSelect caret"></span>' );
}
// Check if a checkbox is disabled or enabled. It will check the granular control (disableProperty) and global control (isDisabled)
// Take note that the granular control has higher priority.
$scope.itemIsDisabled = function( item ) {
if ( item[ $scope.disableProperty ] === true ) {
return true;
}
else { $scope
if ( $scope.isDisabled === true ) {
return true;
}
else {
return false;
}
}
}
// A simple function to parse the item label settings
$scope.writeLabel = function( item, type ) {
var label = '';
var temp = $scope[ type ].split( ' ' );
angular.forEach( temp, function( value2, key2 ) {
if ( typeof value2 !== 'undefined' ) {
angular.forEach( item, function( value1, key1 ) {
if ( key1 == value2 ) {
label += ' ' + value1;
}
});
}
});
return $sce.trustAsHtml( label );
}
// UI operations to show/hide checkboxes based on click event..
$scope.toggleCheckboxes = function( e ) {
// Determine what element is clicked (has to be button).
if ( e.target ) {
if ( e.target.tagName.toUpperCase() !== 'BUTTON' && e.target.className.indexOf( 'multiSelectButton' ) < 0 ) {
if ( attrs.selectionMode && $scope.selectionMode.toUpperCase() === 'SINGLE' ) {
if ( e.target.tagName.toUpperCase() === 'INPUT' )
{
e = $scope.findUpTag( e.target, 'div', 'checkboxLayer' );
e = e.previousSibling;
}
}
else {
e = $scope.findUpTag( e.target, 'button', 'multiSelectButton' );
}
}
else {
e = e.target;
}
}
$scope.labelFilter = '';
// Search all the multi-select instances based on the class names
var multiSelectIndex = -1;
var checkboxes = document.querySelectorAll( '.checkboxLayer' );
var multiSelectButtons = document.querySelectorAll( '.multiSelectButton' );
// Mark which instance is clicked
for( i=0; i < multiSelectButtons.length; i++ ) {
if ( e === multiSelectButtons[ i ] ) {
multiSelectIndex = i;
break;
}
}
// Apply the hide css to all multi-select instances except the clicked one
if ( multiSelectIndex > -1 ) {
for( i=0; i < checkboxes.length; i++ ) {
if ( i != multiSelectIndex ) {
checkboxes[i].className = 'multiSelect checkboxLayer hide';
}
}
// If it's already hidden, show it
if ( checkboxes[ multiSelectIndex ].className == 'multiSelect checkboxLayer hide' ) {
$scope.currentButton = multiSelectButtons[ multiSelectIndex ];
checkboxes[ multiSelectIndex ].className = 'multiSelect checkboxLayer show';
// https://github.com/isteven/angular-multi-select/pull/5 - On open callback
$scope.onOpen();
}
// If it's already displayed, hide it
else if ( checkboxes[ multiSelectIndex ].className == 'multiSelect checkboxLayer show' ) {
checkboxes[ multiSelectIndex ].className = 'multiSelect checkboxLayer hide';
// https://github.com/isteven/angular-multi-select/pull/5 - On close callback
$scope.onClose();
}
}
}
// Traverse up to find the button tag
// http://stackoverflow.com/questions/7332179/how-to-recursively-search-all-parentnodes
$scope.findUpTag = function ( el, tag, className ) {
while ( el.parentNode ) {
el = el.parentNode;
if ( typeof el.tagName !== 'undefined' ) {
if ( el.tagName.toUpperCase() === tag.toUpperCase() && el.className.indexOf( className ) > -1 ) {
return el;
}
}
}
return null;
}
// Select All / None / Reset
$scope.select = function( type ) {
var temp = [];
switch( type.toUpperCase() ) {
case 'ALL':
angular.forEach( $scope.inputModel, function( value, key ) {
if ( typeof value !== 'undefined' && value[ $scope.disableProperty ] !== true ) {
value[ $scope.tickProperty ] = true;
}
});
break;
case 'NONE':
angular.forEach( $scope.inputModel, function( value, key ) {
if ( typeof value !== 'undefined' && value[ $scope.disableProperty ] !== true ) {
value[ $scope.tickProperty ] = false;
}
});
break;
case 'RESET':
$scope.inputModel = angular.copy( $scope.backUp );
break;
default:
}
$scope.refreshSelectedItems();
}
// Generic validation for required attributes
// Might give false positives so just ignore if everything's alright.
validate = function() {
if ( !( 'inputModel' in attrs )) {
console.log( 'Multi-select error: input-model is not defined! (ID: ' + $scope.directiveId + ')' );
}
if ( !( 'buttonLabel' in attrs )) {
console.log( 'Multi-select error: button-label is not defined! (ID: ' + $scope.directiveId + ')' );
}
if ( !( 'itemLabel' in attrs )) {
console.log( 'Multi-select error: item-label is not defined! (ID: ' + $scope.directiveId + ')' );
}
if ( !( 'tickProperty' in attrs )) {
console.log( 'Multi-select error: tick-property is not defined! (ID: ' + $scope.directiveId + ')' );
}
}
// Validate whether the properties specified in the directive attributes are present in the input model
validateProperties = function( arrProperties, arrObject ) {
var notThere = false;
var missingProperty = '';
angular.forEach( arrProperties, function( value1, key1 ) {
if ( typeof value1 !== 'undefined' ) {
var keepGoing = true;
angular.forEach( arrObject, function( value2, key2 ) {
if ( typeof value2 !== 'undefined' && keepGoing ) {
if (!( value1 in value2 )) {
notThere = true;
keepGoing = false;
missingLabel = value1;
}
}
});
}
});
if ( notThere === true ) {
console.log( 'Multi-select error: property "' + missingLabel + '" is not available in the input model. (Name: ' + $scope.directiveId + ')' );
}
}
///////////////////////
// Logic starts here
///////////////////////
validate();
$scope.refreshSelectedItems();
// Watch for changes in input model
// Updates multi-select when user select/deselect a single checkbox programatically
// https://github.com/isteven/angular-multi-select/issues/8
$scope.$watch( 'inputModel' , function( oldVal, newVal ) {
if ( $scope.newVal !== 'undefined' ) {
validateProperties( $scope.itemLabel.split( ' ' ), $scope.inputModel );
validateProperties( new Array( $scope.tickProperty ), $scope.inputModel );
}
$scope.refreshSelectedItems();
}, true);
// Watch for changes in input model
// This on updates the multi-select when a user load a whole new input-model. We also update the $scope.backUp variable
$scope.$watch( 'inputModel' , function( oldVal, newVal ) {
if ( $scope.newVal !== 'undefined' ) {
validateProperties( $scope.itemLabel.split( ' ' ), $scope.inputModel );
validateProperties( new Array( $scope.tickProperty ), $scope.inputModel );
}
$scope.backUp = angular.copy( $scope.inputModel );
$scope.refreshSelectedItems();
});
// Watch for changes in directive state (disabled or enabled)
$scope.$watch( 'isDisabled' , function( newVal ) {
$scope.isDisabled = newVal;
});
// This is for touch enabled devices. We don't want to hide checkboxes on scroll.
angular.element( document ).bind( 'touchstart', function( e ) {
$scope.$apply( function() {
$scope.scrolled = false;
});
});
angular.element( document ).bind( 'touchmove', function( e ) {
$scope.$apply( function() {
$scope.scrolled = true;
});
});
// Monitor for click or touches outside the button element to hide the checkboxes
angular.element( document ).bind( 'click touchend' , function( e ) {
if ( e.type === 'click' || e.type === 'touchend' && $scope.scrolled === false ) {
var checkboxes = document.querySelectorAll( '.checkboxLayer' );
if ( e.target.className.indexOf === undefined || e.target.className.indexOf( 'multiSelect' ) {
for( i=0; i < checkboxes.length; i++ ) {
checkboxes[i].className = 'multiSelect checkboxLayer hide';
}
e.stopPropagation();
}
}
});
// For IE8, perhaps. Not sure if this is really executed.
if ( !Array.prototype.indexOf ) {
Array.prototype.indexOf = function(what, i) {
i = i || 0;
var L = this.length;
while (i < L) {
if(this[i] === what) return i;
++i;
}
return -1;
};
}
}
}
}]);
|