This repository was archived by the owner on Feb 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathmultiselect.js
More file actions
362 lines (321 loc) · 13.5 KB
/
multiselect.js
File metadata and controls
362 lines (321 loc) · 13.5 KB
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
// Source: https://github.com/amitava82/angular-multiselect
angular.module('am.multiselect', [])
// from bootstrap-ui typeahead parser
.factory('optionParser', ['$parse', function ($parse) {
// 00000111000000000000022200000000000000003333333333333330000000000044000
var TYPEAHEAD_REGEXP = /^\s*([\s\S]+?)(?:\s+as\s+([\s\S]+?))?\s+for\s+(?:([\$\w][\$\w\d]*))\s+in\s+([\s\S]+?)$/;
return {
parse:function (input) {
var match = input.match(TYPEAHEAD_REGEXP);
if (!match) {
throw new Error(
'Expected typeahead specification in form of "_modelValue_ (as _label_)? for _item_ in _collection_"' +
' but got "' + input + '".');
}
return {
itemName:match[3],
source:$parse(match[4]),
viewMapper:$parse(match[2] || match[1]),
modelMapper:$parse(match[1])
};
}
};
}])
.directive('amMultiselect', ['$parse', '$document', '$compile', '$interpolate', '$filter', 'optionParser',
function ($parse, $document, $compile, $interpolate, $filter, optionParser) {
return {
restrict: 'E',
require: 'ngModel',
link: function (originalScope, element, attrs, modelCtrl) {
// Redefine isEmpty - this allows this to work on at least Angular 1.2.x
var isEmpty = modelCtrl.$isEmpty;
modelCtrl.$isEmpty = function(value) {
return isEmpty(value) || (angular.isArray(value) && value.length == 0);
};
var exp = attrs.options,
parsedResult = optionParser.parse(exp),
isMultiple = attrs.multiple ? true : false,
isHover = attrs.hover ? true : false,
required = false,
scope = originalScope.$new(),
changeHandler = attrs.change || angular.noop;
scope.items = [];
scope.header = 'Select';
scope.multiple = isMultiple;
scope.disabled = false;
scope.searchDisable = false;
scope.onBlur = attrs.ngBlur || angular.noop;
scope.hoverText = isHover ? scope.header : '';
scope.onFocus = attrs.ngFocus;
scope.clazz = attrs.clazz;
originalScope.$on('$destroy', function () {
scope.$destroy();
});
var popUpEl = angular.element('<am-multiselect-popup' +
(attrs.templateUrl ? (' template-url="' + attrs.templateUrl + '"'): '' ) +
'></am-multiselect-popup>');
// required validator
if (attrs.required || attrs.ngRequired) {
required = true;
}
attrs.$observe('required', function(newVal) {
required = newVal;
});
// watch disabled state
scope.$watch(function () {
return $parse(attrs.ngDisabled)(originalScope);
}, function (newVal) {
scope.disabled = newVal;
});
// watch disabled state for search text box
scope.$watch(function () {
return $parse(attrs.searchDisable)(originalScope);
}, function (newVal) {
scope.searchDisable = newVal;
});
// watch single/multiple state for dynamically change single to multiple
scope.$watch(function () {
return $parse(attrs.multiple)(originalScope);
}, function (newVal) {
isMultiple = newVal || false;
});
// watch option changes for options that are populated dynamically
scope.$watch(function () {
return parsedResult.source(originalScope);
}, function (newVal) {
if (angular.isDefined(newVal))
parseModel();
}, true);
// watch model change
scope.$watch(function () {
return modelCtrl.$modelValue;
}, function (newVal) {
// When the model is assigned a "" or undefined value from controller, need to uncheck all items and clear searchText.label
if(angular.isUndefined(newVal) || newVal==="" || newVal===null) {
scope.uncheckAll();
if(angular.isDefined(scope.searchText))
scope.searchText.label="";
}
// when directive initialize, newVal usually undefined. Also, if model value already set in the controller
// for preselected list then we need to mark checked in our scope item. But we don't want to do this every time
// model changes. We need to do this only if it is done outside directive scope, from controller, for example.
else if (angular.isDefined(newVal)) {
markChecked(newVal);
scope.$eval(changeHandler);
}
getHeaderText();
scope.hoverText = isHover ? scope.header : '';
modelCtrl.$setValidity('required', scope.valid());
}, true);
function parseModel() {
scope.items.length = 0;
var model = parsedResult.source(originalScope);
if(angular.isUndefined(model)) return;
for (var i = 0; i < model.length; i++) {
var local = {};
local[parsedResult.itemName] = model[i];
scope.items.push({
label: parsedResult.viewMapper(local),
model: parsedResult.modelMapper(local),
checked: false
});
}
}
parseModel();
element.append($compile(popUpEl)(scope));
function getItemLabel(items,model) {
for(var i = 0; i < items.length; i++) {
if(items[i].model==model) {
return items[i].label;
}
}
}
function getHeaderText() {
if (is_empty(modelCtrl.$modelValue)) return scope.header = (angular.isDefined(attrs.msHeader) ? attrs.msHeader : 'Select');
if (isMultiple) {
if (attrs.msSelected) {
scope.header = $interpolate(attrs.msSelected)(scope);
} else {
if (modelCtrl.$modelValue.length == 1) {
for(var i = 0; i < scope.items.length; i++) {
if(scope.items[i].model === modelCtrl.$modelValue[0]) {
scope.header = scope.items[i].label;
}
}
} else {
scope.header = modelCtrl.$modelValue.length + ' ' + 'selected';
}
}
} else {
if(angular.isString(modelCtrl.$modelValue)){
scope.header = getItemLabel(scope.items,modelCtrl.$modelValue);
} else {
var local = {};
local[parsedResult.itemName] = modelCtrl.$modelValue;
scope.header = parsedResult.viewMapper(local) || getItemLabel(scope.items,modelCtrl.$modelValue);
}
}
}
function is_empty(obj) {
if (angular.isNumber(obj)) return false;
if (obj && obj.length && obj.length > 0) return false;
for (var prop in obj) if (obj[prop]) return false;
return true;
}
scope.valid = function validModel() {
if(!required) return true;
var value = modelCtrl.$modelValue;
return (angular.isArray(value) && value.length > 0) || (!angular.isArray(value) && value != null);
};
function selectSingle(item) {
if (item.checked) {
scope.uncheckAll();
} else {
scope.uncheckAll();
item.checked = !item.checked;
}
setModelValue(false);
}
function selectMultiple(item) {
item.checked = !item.checked;
setModelValue(true);
}
function setModelValue(isMultiple) {
var value = undefined;
if (isMultiple) {
value = [];
angular.forEach(scope.items, function (item) {
if (item.checked) value.push(item.model);
})
if(value.length==0) value=undefined;
} else {
angular.forEach(scope.items, function (item) {
if (item.checked) {
value = item.model;
return false;
}
})
}
modelCtrl.$setViewValue(value);
}
function markChecked(newVal) {
if (!angular.isArray(newVal)) {
angular.forEach(scope.items, function (item) {
if (angular.equals(item.model, newVal)) {
scope.uncheckAll();
item.checked = true;
setModelValue(false);
return false;
}
});
} else {
angular.forEach(scope.items, function (item) {
item.checked = false;
angular.forEach(newVal, function (i) {
if (angular.equals(item.model, i)) {
item.checked = true;
}
});
});
}
}
scope.checkAll = function () {
if (!isMultiple) return;
var items = (scope.searchText && scope.searchText.label.length > 0) ? $filter('filter')(scope.items, scope.searchText) : scope.items;
angular.forEach(items, function (item) {
item.checked = true;
});
setModelValue(true);
};
scope.uncheckAll = function () {
// need to uncheck from the entire list of items. If user filers with ine text and selects item A. Next time user fileters and selects item B (item A now not in the filtered set). The item A will not get unchecked
// var items = (scope.searchText && scope.searchText.label.length > 0) ? $filter('filter')(scope.items, scope.searchText) : scope.items;
angular.forEach(scope.items, function (item) {
item.checked = false;
});
// sending scope.multiple instead of true to setModelValue. Since different values geeting set when single and multiple.
setModelValue(scope.multiple);
};
scope.select = function (item) {
if (isMultiple === false) {
selectSingle(item);
scope.toggleSelect();
} else {
selectMultiple(item);
}
}
}
};
}])
.directive('amMultiselectPopup', ['$document', '$filter', function ($document, $filter) {
return {
restrict: 'E',
scope: false,
replace: true,
templateUrl: 'html/multiselect.tmpl.html',
link: function (scope, element) {
scope.selectedIndex = null;
scope.isVisible = false;
scope.filteredItems = null;
scope.ngClazz = {
'error': !scope.valid()
};
scope.ngClazz[scope.$eval(scope.clazz)] = true;
scope.toggleSelect = function () {
if (element.hasClass('open')) {
element.removeClass('open');
$document.unbind('click', clickHandler);
scope.$parent.$eval(scope.onBlur);
} else {
element.addClass('open');
$document.bind('click', clickHandler);
scope.focus();
}
};
scope.evalFocus = function() {
scope.$parent.$eval(scope.onFocus);
}
function clickHandler(event) {
if (elementMatchesAnyInArray(event.target, element.find(event.target.tagName))) {
scope.$parent.$eval(scope.onBlur);
} else {
element.removeClass('open');
$document.unbind('click', clickHandler);
scope.$apply();
}
}
scope.focus = function focus(){
var searchBox = element.find('input')[0];
if (searchBox) {
searchBox.focus();
}
}
scope.keydown = function (event) {
var list = $filter('filter')(scope.items, scope.searchText);
var keyCode = (event.keyCode || event.which);
if(keyCode === 13){ // On enter
if(list[scope.selectedIndex]){
scope.select(list[scope.selectedIndex]); // (un)select item
}
}else if(keyCode === 38){ // On arrow up
scope.selectedIndex = scope.selectedIndex===null ? list.length-1 : scope.selectedIndex-1;
}else if(keyCode === 40){ // On arrow down
scope.selectedIndex = scope.selectedIndex===null ? 0 : scope.selectedIndex+1;
}else{ // On any other key
scope.selectedIndex = null;
}
if(scope.selectedIndex < 0){ // Select last in list
scope.selectedIndex = list.length-1;
}else if(scope.selectedIndex > list.length-1){ // Set selection to first item in list
scope.selectedIndex = 0;
}
};
var elementMatchesAnyInArray = function (element, elementArray) {
for (var i = 0; i < elementArray.length; i++)
if (element == elementArray[i])
return true;
return false;
}
}
}
}]);