forked from shentao/vue-multiselect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiselectMixin.js
440 lines (369 loc) · 12.4 KB
/
multiselectMixin.js
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
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var _utils = require('./utils');
var _utils2 = _interopRequireDefault(_utils);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function isEmpty(opt) {
if (opt === 0) return false;
if (Array.isArray(opt) && opt.length === 0) return true;
return !opt;
}
function includes(str, query) {
if (str === undefined) str = 'undefined';
if (str === null) str = 'null';
if (str === false) str = 'false';
var text = str.toString().toLowerCase();
return text.indexOf(query.trim()) !== -1;
}
function filterOptions(options, search, label, customLabel) {
return options.filter(function (option) {
return includes(customLabel(option, label), search);
});
}
function stripGroups(options) {
return options.filter(function (option) {
return !option.$isLabel;
});
}
function flattenOptions(values, label) {
return function (options) {
return options.reduce(function (prev, curr) {
if (curr[values] && curr[values].length) {
prev.push({
$groupLabel: curr[label],
$isLabel: true
});
return prev.concat(curr[values]);
}
return prev;
}, []);
};
}
function filterGroups(search, label, values, groupLabel, customLabel) {
return function (groups) {
return groups.map(function (group) {
var _ref;
if (!group[values]) {
console.warn('Options passed to vue-multiselect do not contain groups, despite the config.');
return [];
}
var groupOptions = filterOptions(group[values], search, label, customLabel);
return groupOptions.length ? (_ref = {}, _defineProperty(_ref, groupLabel, group[groupLabel]), _defineProperty(_ref, values, groupOptions), _ref) : [];
});
};
}
var flow = function flow() {
for (var _len = arguments.length, fns = Array(_len), _key = 0; _key < _len; _key++) {
fns[_key] = arguments[_key];
}
return function (x) {
return fns.reduce(function (v, f) {
return f(v);
}, x);
};
};
exports.default = {
data: function data() {
return {
search: '',
isOpen: false,
prefferedOpenDirection: 'below',
optimizedHeight: this.maxHeight,
internalValue: this.value || this.value === 0 ? (0, _utils2.default)(Array.isArray(this.value) ? this.value : [this.value]) : []
};
},
props: {
internalSearch: {
type: Boolean,
default: true
},
options: {
type: Array,
required: true
},
multiple: {
type: Boolean,
default: false
},
value: {
type: null,
default: function _default() {
return [];
}
},
trackBy: {
type: String
},
label: {
type: String
},
searchable: {
type: Boolean,
default: true
},
clearOnSelect: {
type: Boolean,
default: true
},
hideSelected: {
type: Boolean,
default: false
},
placeholder: {
type: String,
default: 'Select option'
},
allowEmpty: {
type: Boolean,
default: true
},
resetAfter: {
type: Boolean,
default: false
},
closeOnSelect: {
type: Boolean,
default: true
},
customLabel: {
type: Function,
default: function _default(option, label) {
if (isEmpty(option)) return '';
return label ? option[label] : option;
}
},
taggable: {
type: Boolean,
default: false
},
tagPlaceholder: {
type: String,
default: 'Press enter to create a tag'
},
tagPosition: {
type: String,
default: 'top'
},
max: {
type: [Number, Boolean],
default: false
},
id: {
default: null
},
optionsLimit: {
type: Number,
default: 1000
},
groupValues: {
type: String
},
groupLabel: {
type: String
},
blockKeys: {
type: Array,
default: function _default() {
return [];
}
},
preserveSearch: {
type: Boolean,
default: false
}
},
mounted: function mounted() {
if (!this.multiple && !this.clearOnSelect) {
console.warn('[Vue-Multiselect warn]: ClearOnSelect and Multiple props can’t be both set to false.');
}
if (!this.multiple && this.max) {
console.warn('[Vue-Multiselect warn]: Max prop should not be used when prop Multiple equals false.');
}
},
computed: {
filteredOptions: function filteredOptions() {
var search = this.search || '';
var normalizedSearch = search.toLowerCase().trim();
var options = this.options.concat();
if (this.internalSearch) {
options = this.groupValues ? this.filterAndFlat(options, normalizedSearch, this.label) : filterOptions(options, normalizedSearch, this.label, this.customLabel);
} else {
options = this.groupValues ? flattenOptions(this.groupValues, this.groupLabel)(options) : options;
}
options = this.hideSelected ? options.filter(this.isNotSelected) : options;
if (this.taggable && normalizedSearch.length && !this.isExistingOption(normalizedSearch)) {
if (this.tagPosition === 'bottom') {
options.push({ isTag: true, label: normalizedSearch });
} else {
options.unshift({ isTag: true, label: normalizedSearch });
}
}
return options.slice(0, this.optionsLimit);
},
valueKeys: function valueKeys() {
var _this = this;
if (this.trackBy) {
return this.internalValue.map(function (element) {
return element[_this.trackBy];
});
} else {
return this.internalValue;
}
},
optionKeys: function optionKeys() {
var _this2 = this;
var options = this.groupValues ? this.flatAndStrip(this.options) : this.options;
return options.map(function (element) {
return _this2.customLabel(element, _this2.label).toString().toLowerCase();
});
},
currentOptionLabel: function currentOptionLabel() {
return this.multiple ? this.searchable ? '' : this.placeholder : this.internalValue.length ? this.getOptionLabel(this.internalValue[0]) : this.searchable ? '' : this.placeholder;
}
},
watch: {
internalValue: function internalValue(newVal, oldVal) {
if (this.resetAfter && this.internalValue.length) {
this.search = '';
this.internalValue = [];
}
},
search: function search() {
this.$emit('search-change', this.search, this.id);
},
value: function value(_value) {
this.internalValue = this.getInternalValue(_value);
}
},
methods: {
getValue: function getValue() {
return this.multiple ? (0, _utils2.default)(this.internalValue) : this.internalValue.length === 0 ? null : (0, _utils2.default)(this.internalValue[0]);
},
getInternalValue: function getInternalValue(value) {
return value === null || value === undefined ? [] : this.multiple ? (0, _utils2.default)(value) : (0, _utils2.default)([value]);
},
filterAndFlat: function filterAndFlat(options, search, label) {
return flow(filterGroups(search, label, this.groupValues, this.groupLabel, this.customLabel), flattenOptions(this.groupValues, this.groupLabel))(options);
},
flatAndStrip: function flatAndStrip(options) {
return flow(flattenOptions(this.groupValues, this.groupLabel), stripGroups)(options);
},
updateSearch: function updateSearch(query) {
this.search = query;
},
isExistingOption: function isExistingOption(query) {
return !this.options ? false : this.optionKeys.indexOf(query) > -1;
},
isSelected: function isSelected(option) {
var opt = this.trackBy ? option[this.trackBy] : option;
return this.valueKeys.indexOf(opt) > -1;
},
isNotSelected: function isNotSelected(option) {
return !this.isSelected(option);
},
getOptionLabel: function getOptionLabel(option) {
if (isEmpty(option)) return '';
if (option.isTag) return option.label;
if (option.$isLabel) return option.$groupLabel;
var label = this.customLabel(option, this.label);
if (isEmpty(label)) return '';
return label;
},
select: function select(option, key) {
if (this.blockKeys.indexOf(key) !== -1 || this.disabled || option.$isLabel || option.$isDisabled) return;
if (this.max && this.multiple && this.internalValue.length === this.max) return;
if (key === 'Tab' && !this.pointerDirty) return;
if (option.isTag) {
this.$emit('tag', option.label, this.id);
this.search = '';
if (this.closeOnSelect && !this.multiple) this.deactivate();
} else {
var isSelected = this.isSelected(option);
if (isSelected) {
if (key !== 'Tab') this.removeElement(option);
return;
} else if (this.multiple) {
this.internalValue.push(option);
} else {
this.internalValue = [option];
}
this.$emit('select', (0, _utils2.default)(option), this.id);
this.$emit('input', this.getValue(), this.id);
if (this.clearOnSelect) this.search = '';
}
if (this.closeOnSelect) this.deactivate();
},
removeElement: function removeElement(option) {
var shouldClose = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
if (this.disabled) return;
if (!this.allowEmpty && this.internalValue.length <= 1) {
this.deactivate();
return;
}
var index = (typeof option === 'undefined' ? 'undefined' : _typeof(option)) === 'object' ? this.valueKeys.indexOf(option[this.trackBy]) : this.valueKeys.indexOf(option);
this.internalValue.splice(index, 1);
this.$emit('input', this.getValue(), this.id);
this.$emit('remove', (0, _utils2.default)(option), this.id);
if (this.closeOnSelect && shouldClose) this.deactivate();
},
removeLastElement: function removeLastElement() {
if (this.blockKeys.indexOf('Delete') !== -1) return;
if (this.search.length === 0 && Array.isArray(this.internalValue)) {
this.removeElement(this.internalValue[this.internalValue.length - 1], false);
}
},
activate: function activate() {
var _this3 = this;
if (this.isOpen || this.disabled) return;
this.adjustPosition();
if (this.groupValues && this.pointer === 0 && this.filteredOptions.length) {
this.pointer = 1;
}
this.isOpen = true;
if (this.searchable) {
if (!this.preserveSearch) this.search = '';
this.$nextTick(function () {
return _this3.$refs.search.focus();
});
} else {
this.$el.focus();
}
this.$emit('open', this.id);
},
deactivate: function deactivate() {
if (!this.isOpen) return;
this.isOpen = false;
if (this.searchable) {
this.$refs.search.blur();
} else {
this.$el.blur();
}
if (!this.preserveSearch) this.search = '';
this.$emit('close', this.getValue(), this.id);
},
toggle: function toggle() {
this.isOpen ? this.deactivate() : this.activate();
},
adjustPosition: function adjustPosition() {
if (typeof window === 'undefined') return;
var spaceAbove = this.$el.getBoundingClientRect().top;
var spaceBelow = window.innerHeight - this.$el.getBoundingClientRect().bottom;
var hasEnoughSpaceBelow = spaceBelow > this.maxHeight;
if (hasEnoughSpaceBelow || spaceBelow > spaceAbove || this.openDirection === 'below' || this.openDirection === 'bottom') {
this.prefferedOpenDirection = 'below';
this.optimizedHeight = Math.min(spaceBelow - 40, this.maxHeight);
} else {
this.prefferedOpenDirection = 'above';
this.optimizedHeight = Math.min(spaceAbove - 40, this.maxHeight);
}
},
handleTouchStartStop: function handleTouchStartStop(index, option) {
this.pointerSet(index);
this.select(option);
}
}
};