forked from EvanOxfeld/angular-selectize.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathangular-selectize.js
More file actions
256 lines (222 loc) · 8.09 KB
/
angular-selectize.js
File metadata and controls
256 lines (222 loc) · 8.09 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
/**
* Directive to convert a select into a selectize.js hybrid textbox and <select>
* Supports an ngOptions expression. Tested with:
* `label for value in array`
* `select as label for value in array`
* In theory supports the same options as selectize.js
*
* Usage:
* <select
* multiple
* ng-model="selected"
* ng-options="option.id as option.name for option in options"
* selectize="{ plugins: ['remove_button'], create: 'true' }">
* </select>
*
* Attributes:
* multiple: Converts the select into text input of tags
*
* (c) 2014 Evan Oxfeld https://github.com/EvanOxfeld/angular-selectize.js
* License: MIT
**/
(function (angular) {
'use strict';
angular.module('selectize', [])
.directive('selectize', ['$parse', '$timeout', function($parse, $timeout) {
var NG_OPTIONS_REGEXP = /^\s*([\s\S]+?)(?:\s+as\s+([\s\S]+?))?(?:\s+group\s+by\s+([\s\S]+?))?\s+for\s+(?:([\$\w][\$\w]*)|(?:\(\s*([\$\w][\$\w]*)\s*,\s*([\$\w][\$\w]*)\s*\)))\s+in\s+([\s\S]+?)(?:\s+track\s+by\s+([\s\S]+?))?$/;
var OPTIONS_PROPERTY_REGEXP = /.*?(?=\s?\|)|.*\b/;
return {
scope: {
multiple: '@',
opts: '@selectize'
},
require: '?ngModel',
link: function(scope, element, attrs, ngModelCtrl) {
var opts = scope.$parent.$eval(scope.opts) || {};
var initializing = false;
var modelUpdate = false;
var optionsUpdate = false;
var selectize, newModelValue, newOptions, updateTimer;
watchModel();
if (attrs.ngDisabled) {
watchParentNgDisabled();
}
if (!attrs.ngOptions) {
return;
}
var match = attrs.ngOptions.match(NG_OPTIONS_REGEXP);
var valueName = match[4] || match[6];
var optionsExpression = match[7];
var optionsProperty = optionsExpression.match(OPTIONS_PROPERTY_REGEXP);
var displayFn = $parse(match[2] || match[1]);
var valueFn = $parse(match[2] ? match[1] : valueName);
watchParentOptions();
function watchModel() {
scope.$watchCollection(function() {
return ngModelCtrl.$modelValue;
}, function(modelValue) {
newModelValue = modelValue;
modelUpdate = true;
if (!updateTimer) {
scheduleUpdate();
}
});
}
function watchParentOptions() {
scope.$parent.$watchCollection(optionsExpression, function(options) {
newOptions = options || [];
optionsUpdate = true;
if (!updateTimer) {
scheduleUpdate();
}
});
}
function watchParentNgDisabled() {
scope.$parent.$watch(attrs.ngDisabled, function(isDisabled) {
if (selectize) {
isDisabled ? selectize.disable() : selectize.enable();
}
});
}
function scheduleUpdate() {
if (!selectize) {
if (!initializing) {
initSelectize();
}
modelUpdate = optionsUpdate = false;
return;
}
updateTimer = $timeout(function() {
var model = newModelValue;
var options = newOptions;
var selectizeOptions = Object.keys(selectize.options);
var optionsIsEmpty = selectizeOptions.length === 0 || selectize.options['?'] && selectizeOptions.length === 1;
if (optionsUpdate) {
if (!optionsIsEmpty) {
selectize.clearOptions();
}
selectize.load(function(cb) {
cb(options.map(function(option, index) {
return {
text: getOptionLabel(option),
value: index
};
}));
});
}
if (modelUpdate || optionsUpdate) {
var selectedItems = getSelectedItems(model);
if (scope.multiple || selectedItems.length === 0) {
selectize.clear();
//clear can set the model to null
ngModelCtrl.$setViewValue(model);
}
selectedItems.forEach(function(item) {
selectize.addItem(item);
});
//wait to remove ? to avoid a single select from briefly setting the model to null
selectize.removeOption('?');
var $option = selectize.getOption(0);
if ($option) selectize.setActiveOption($option);
}
modelUpdate = optionsUpdate = false;
updateTimer = null;
});
}
function initSelectize() {
initializing = true;
scope.$evalAsync(function() {
initializing = false;
element.selectize(opts);
selectize = element[0].selectize;
if (attrs.ngOptions) {
if (scope.multiple) {
selectize.on('item_add', onItemAddMultiSelect);
selectize.on('item_remove', onItemRemoveMultiSelect);
} else if (opts.create) {
selectize.on('item_add', onItemAddSingleSelect);
}
}
});
}
function onItemAddMultiSelect(value, $item) {
var model = ngModelCtrl.$viewValue;
var option = scope.$parent.$eval(optionsExpression)[value];
value = option ? getOptionValue(option) : value;
if (model.indexOf(value) === -1) {
model.push(value);
if (!option && opts.create && scope.$parent[optionsProperty].indexOf(value) === -1) {
scope.$parent[optionsProperty].push(value);
}
scope.$evalAsync(function() {
ngModelCtrl.$setViewValue(model);
});
}
}
function onItemAddSingleSelect(value, $item) {
var model = ngModelCtrl.$viewValue;
var option = scope.$parent.$eval(optionsExpression)[value];
value = option ? getOptionValue(option) : value;
if (model !== value) {
model = value;
if (!option && scope.$parent[optionsProperty].indexOf(value) === -1) {
scope.$parent[optionsProperty].push(value);
}
scope.$evalAsync(function() {
ngModelCtrl.$setViewValue(model);
});
}
}
function onItemRemoveMultiSelect(value) {
var model = ngModelCtrl.$viewValue;
var option = scope.$parent.$eval(optionsExpression)[value];
value = option ? getOptionValue(option) : value;
var index = model.indexOf(value);
if (index >= 0) {
model.splice(index, 1);
scope.$evalAsync(function() {
ngModelCtrl.$setViewValue(model);
});
}
}
function getSelectedItems(model) {
model = angular.isArray(model) ? model : [model] || [];
if (!attrs.ngOptions) {
return model.map(function(i) { return selectize.options[i] ? selectize.options[i].value : ''});
}
var options = scope.$parent.$eval(optionsExpression);
if (!options) {
return [];
}
var selections = options.reduce(function(selected, option, index) {
var optionValue = getOptionValue(option);
if (model.indexOf(optionValue) >= 0) {
selected[optionValue] = index;
}
return selected;
}, {});
return Object
.keys(selections)
.map(function(key) {
return selections[key];
});
}
function getOptionValue(option) {
var optionContext = {};
optionContext[valueName] = option;
return valueFn(optionContext);
}
function getOptionLabel(option) {
var optionContext = {};
optionContext[valueName] = option;
return displayFn(optionContext);
}
scope.$on('$destroy', function() {
if (updateTimer) {
$timeout.cancel(updateTimer);
}
});
}
};
}]);
})(angular);