@materializecss/materialize 1.1.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Gruntfile.js +38 -24
- package/LICENSE +21 -21
- package/README.md +91 -97
- package/dist/css/materialize.css +8608 -8631
- package/dist/css/materialize.min.css +12 -12
- package/dist/js/materialize.js +12816 -12669
- package/dist/js/materialize.min.js +6 -6
- package/extras/noUiSlider/nouislider.css +404 -406
- package/extras/noUiSlider/nouislider.js +2147 -2147
- package/extras/noUiSlider/nouislider.min.js +0 -0
- package/js/anime.min.js +34 -34
- package/js/autocomplete.js +479 -479
- package/js/buttons.js +354 -354
- package/js/cards.js +40 -40
- package/js/carousel.js +732 -732
- package/js/cash.js +960 -960
- package/js/characterCounter.js +136 -136
- package/js/chips.js +486 -486
- package/js/collapsible.js +275 -275
- package/js/component.js +44 -44
- package/js/datepicker.js +983 -983
- package/js/dropdown.js +669 -669
- package/js/forms.js +285 -275
- package/js/global.js +428 -424
- package/js/materialbox.js +453 -453
- package/js/modal.js +382 -382
- package/js/parallax.js +138 -138
- package/js/pushpin.js +148 -148
- package/js/range.js +263 -263
- package/js/scrollspy.js +295 -295
- package/js/select.js +391 -310
- package/js/sidenav.js +583 -583
- package/js/slider.js +359 -359
- package/js/tabs.js +402 -402
- package/js/tapTarget.js +315 -315
- package/js/timepicker.js +712 -648
- package/js/toasts.js +325 -322
- package/js/tooltip.js +320 -320
- package/js/waves.js +614 -614
- package/package.json +87 -82
- package/sass/_style.scss +929 -929
- package/sass/components/_badges.scss +55 -55
- package/sass/components/_buttons.scss +322 -322
- package/sass/components/_cards.scss +195 -195
- package/sass/components/_carousel.scss +90 -90
- package/sass/components/_chips.scss +96 -96
- package/sass/components/_collapsible.scss +91 -91
- package/sass/components/_collection.scss +106 -106
- package/sass/components/_color-classes.scss +32 -32
- package/sass/components/_color-variables.scss +370 -370
- package/sass/components/_datepicker.scss +191 -191
- package/sass/components/_dropdown.scss +84 -84
- package/sass/components/_global.scss +646 -642
- package/sass/components/_grid.scss +158 -158
- package/sass/components/_icons-material-design.scss +5 -5
- package/sass/components/_materialbox.scss +42 -42
- package/sass/components/_modal.scss +97 -97
- package/sass/components/_navbar.scss +208 -208
- package/sass/components/_normalize.scss +447 -447
- package/sass/components/_preloader.scss +334 -334
- package/sass/components/_pulse.scss +34 -34
- package/sass/components/_sidenav.scss +214 -214
- package/sass/components/_slider.scss +91 -91
- package/sass/components/_table_of_contents.scss +33 -33
- package/sass/components/_tabs.scss +99 -99
- package/sass/components/_tapTarget.scss +103 -103
- package/sass/components/_timepicker.scss +199 -183
- package/sass/components/_toast.scss +58 -58
- package/sass/components/_tooltip.scss +32 -32
- package/sass/components/_transitions.scss +12 -12
- package/sass/components/_typography.scss +62 -62
- package/sass/components/_variables.scss +352 -352
- package/sass/components/_waves.scss +187 -187
- package/sass/components/forms/_checkboxes.scss +200 -200
- package/sass/components/forms/_file-input.scss +44 -44
- package/sass/components/forms/_forms.scss +22 -22
- package/sass/components/forms/_input-fields.scss +388 -379
- package/sass/components/forms/_radio-buttons.scss +115 -115
- package/sass/components/forms/_range.scss +161 -161
- package/sass/components/forms/_select.scss +199 -199
- package/sass/components/forms/_switches.scss +91 -91
- package/sass/ghpages-materialize.scss +7 -7
- package/sass/materialize.scss +42 -42
package/js/chips.js
CHANGED
|
@@ -1,486 +1,486 @@
|
|
|
1
|
-
(function($) {
|
|
2
|
-
'use strict';
|
|
3
|
-
|
|
4
|
-
let _defaults = {
|
|
5
|
-
data: [],
|
|
6
|
-
placeholder: '',
|
|
7
|
-
secondaryPlaceholder: '',
|
|
8
|
-
autocompleteOptions: {},
|
|
9
|
-
autocompleteOnly: false,
|
|
10
|
-
limit: Infinity,
|
|
11
|
-
onChipAdd: null,
|
|
12
|
-
onChipSelect: null,
|
|
13
|
-
onChipDelete: null
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* @typedef {Object} chip
|
|
18
|
-
* @property {String} tag chip tag string
|
|
19
|
-
* @property {String} [image] chip avatar image string
|
|
20
|
-
*/
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* @class
|
|
24
|
-
*
|
|
25
|
-
*/
|
|
26
|
-
class Chips extends Component {
|
|
27
|
-
/**
|
|
28
|
-
* Construct Chips instance and set up overlay
|
|
29
|
-
* @constructor
|
|
30
|
-
* @param {Element} el
|
|
31
|
-
* @param {Object} options
|
|
32
|
-
*/
|
|
33
|
-
constructor(el, options) {
|
|
34
|
-
super(Chips, el, options);
|
|
35
|
-
|
|
36
|
-
this.el.M_Chips = this;
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Options for the modal
|
|
40
|
-
* @member Chips#options
|
|
41
|
-
* @prop {Array} data
|
|
42
|
-
* @prop {String} placeholder
|
|
43
|
-
* @prop {String} secondaryPlaceholder
|
|
44
|
-
* @prop {Object} autocompleteOptions
|
|
45
|
-
*/
|
|
46
|
-
this.options = $.extend({}, Chips.defaults, options);
|
|
47
|
-
|
|
48
|
-
this.$el.addClass('chips input-field');
|
|
49
|
-
this.chipsData = [];
|
|
50
|
-
this.$chips = $();
|
|
51
|
-
this._setupInput();
|
|
52
|
-
this.hasAutocomplete = Object.keys(this.options.autocompleteOptions).length > 0;
|
|
53
|
-
|
|
54
|
-
// Set input id
|
|
55
|
-
if (!this.$input.attr('id')) {
|
|
56
|
-
this.$input.attr('id', M.guid());
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
// Render initial chips
|
|
60
|
-
if (this.options.data.length) {
|
|
61
|
-
this.chipsData = this.options.data;
|
|
62
|
-
this._renderChips(this.chipsData);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
// Setup autocomplete if needed
|
|
66
|
-
if (this.hasAutocomplete) {
|
|
67
|
-
this._setupAutocomplete();
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
this._setPlaceholder();
|
|
71
|
-
this._setupLabel();
|
|
72
|
-
this._setupEventHandlers();
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
static get defaults() {
|
|
76
|
-
return _defaults;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
static init(els, options) {
|
|
80
|
-
return super.init(this, els, options);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Get Instance
|
|
85
|
-
*/
|
|
86
|
-
static getInstance(el) {
|
|
87
|
-
let domElem = !!el.jquery ? el[0] : el;
|
|
88
|
-
return domElem.M_Chips;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Get Chips Data
|
|
93
|
-
*/
|
|
94
|
-
getData() {
|
|
95
|
-
return this.chipsData;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Teardown component
|
|
100
|
-
*/
|
|
101
|
-
destroy() {
|
|
102
|
-
this._removeEventHandlers();
|
|
103
|
-
this.$chips.remove();
|
|
104
|
-
this.el.M_Chips = undefined;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Setup Event Handlers
|
|
109
|
-
*/
|
|
110
|
-
_setupEventHandlers() {
|
|
111
|
-
this._handleChipClickBound = this._handleChipClick.bind(this);
|
|
112
|
-
this._handleInputKeydownBound = this._handleInputKeydown.bind(this);
|
|
113
|
-
this._handleInputFocusBound = this._handleInputFocus.bind(this);
|
|
114
|
-
this._handleInputBlurBound = this._handleInputBlur.bind(this);
|
|
115
|
-
|
|
116
|
-
this.el.addEventListener('click', this._handleChipClickBound);
|
|
117
|
-
document.addEventListener('keydown', Chips._handleChipsKeydown);
|
|
118
|
-
document.addEventListener('keyup', Chips._handleChipsKeyup);
|
|
119
|
-
this.el.addEventListener('blur', Chips._handleChipsBlur, true);
|
|
120
|
-
this.$input[0].addEventListener('focus', this._handleInputFocusBound);
|
|
121
|
-
this.$input[0].addEventListener('blur', this._handleInputBlurBound);
|
|
122
|
-
this.$input[0].addEventListener('keydown', this._handleInputKeydownBound);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* Remove Event Handlers
|
|
127
|
-
*/
|
|
128
|
-
_removeEventHandlers() {
|
|
129
|
-
this.el.removeEventListener('click', this._handleChipClickBound);
|
|
130
|
-
document.removeEventListener('keydown', Chips._handleChipsKeydown);
|
|
131
|
-
document.removeEventListener('keyup', Chips._handleChipsKeyup);
|
|
132
|
-
this.el.removeEventListener('blur', Chips._handleChipsBlur, true);
|
|
133
|
-
this.$input[0].removeEventListener('focus', this._handleInputFocusBound);
|
|
134
|
-
this.$input[0].removeEventListener('blur', this._handleInputBlurBound);
|
|
135
|
-
this.$input[0].removeEventListener('keydown', this._handleInputKeydownBound);
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* Handle Chip Click
|
|
140
|
-
* @param {Event} e
|
|
141
|
-
*/
|
|
142
|
-
_handleChipClick(e) {
|
|
143
|
-
let $chip = $(e.target).closest('.chip');
|
|
144
|
-
let clickedClose = $(e.target).is('.close');
|
|
145
|
-
if ($chip.length) {
|
|
146
|
-
let index = $chip.index();
|
|
147
|
-
if (clickedClose) {
|
|
148
|
-
// delete chip
|
|
149
|
-
this.deleteChip(index);
|
|
150
|
-
this.$input[0].focus();
|
|
151
|
-
} else {
|
|
152
|
-
// select chip
|
|
153
|
-
this.selectChip(index);
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
// Default handle click to focus on input
|
|
157
|
-
} else {
|
|
158
|
-
this.$input[0].focus();
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* Handle Chips Keydown
|
|
164
|
-
* @param {Event} e
|
|
165
|
-
*/
|
|
166
|
-
static _handleChipsKeydown(e) {
|
|
167
|
-
Chips._keydown = true;
|
|
168
|
-
|
|
169
|
-
let $chips = $(e.target).closest('.chips');
|
|
170
|
-
let chipsKeydown = e.target && $chips.length;
|
|
171
|
-
|
|
172
|
-
// Don't handle keydown inputs on input and textarea
|
|
173
|
-
if ($(e.target).is('input, textarea') || !chipsKeydown) {
|
|
174
|
-
return;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
let currChips = $chips[0].M_Chips;
|
|
178
|
-
|
|
179
|
-
// backspace and delete
|
|
180
|
-
if (e.keyCode === 8 || e.keyCode === 46) {
|
|
181
|
-
e.preventDefault();
|
|
182
|
-
|
|
183
|
-
let selectIndex = currChips.chipsData.length;
|
|
184
|
-
if (currChips._selectedChip) {
|
|
185
|
-
let index = currChips._selectedChip.index();
|
|
186
|
-
currChips.deleteChip(index);
|
|
187
|
-
currChips._selectedChip = null;
|
|
188
|
-
|
|
189
|
-
// Make sure selectIndex doesn't go negative
|
|
190
|
-
selectIndex = Math.max(index - 1, 0);
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
if (currChips.chipsData.length) {
|
|
194
|
-
currChips.selectChip(selectIndex);
|
|
195
|
-
} else {
|
|
196
|
-
currChips.$input[0].focus();
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
// left arrow key
|
|
200
|
-
} else if (e.keyCode === 37) {
|
|
201
|
-
if (currChips._selectedChip) {
|
|
202
|
-
let selectIndex = currChips._selectedChip.index() - 1;
|
|
203
|
-
if (selectIndex < 0) {
|
|
204
|
-
return;
|
|
205
|
-
}
|
|
206
|
-
currChips.selectChip(selectIndex);
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
// right arrow key
|
|
210
|
-
} else if (e.keyCode === 39) {
|
|
211
|
-
if (currChips._selectedChip) {
|
|
212
|
-
let selectIndex = currChips._selectedChip.index() + 1;
|
|
213
|
-
|
|
214
|
-
if (selectIndex >= currChips.chipsData.length) {
|
|
215
|
-
currChips.$input[0].focus();
|
|
216
|
-
} else {
|
|
217
|
-
currChips.selectChip(selectIndex);
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
/**
|
|
224
|
-
* Handle Chips Keyup
|
|
225
|
-
* @param {Event} e
|
|
226
|
-
*/
|
|
227
|
-
static _handleChipsKeyup(e) {
|
|
228
|
-
Chips._keydown = false;
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
/**
|
|
232
|
-
* Handle Chips Blur
|
|
233
|
-
* @param {Event} e
|
|
234
|
-
*/
|
|
235
|
-
static _handleChipsBlur(e) {
|
|
236
|
-
if (!Chips._keydown && document.hidden) {
|
|
237
|
-
let $chips = $(e.target).closest('.chips');
|
|
238
|
-
let currChips = $chips[0].M_Chips;
|
|
239
|
-
|
|
240
|
-
currChips._selectedChip = null;
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
/**
|
|
245
|
-
* Handle Input Focus
|
|
246
|
-
*/
|
|
247
|
-
_handleInputFocus() {
|
|
248
|
-
this.$el.addClass('focus');
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
/**
|
|
252
|
-
* Handle Input Blur
|
|
253
|
-
*/
|
|
254
|
-
_handleInputBlur() {
|
|
255
|
-
this.$el.removeClass('focus');
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
/**
|
|
259
|
-
* Handle Input Keydown
|
|
260
|
-
* @param {Event} e
|
|
261
|
-
*/
|
|
262
|
-
_handleInputKeydown(e) {
|
|
263
|
-
Chips._keydown = true;
|
|
264
|
-
|
|
265
|
-
// enter
|
|
266
|
-
if (e.keyCode === 13) {
|
|
267
|
-
// Override enter if autocompleting.
|
|
268
|
-
if (this.hasAutocomplete && this.autocomplete && this.autocomplete.isOpen) {
|
|
269
|
-
return;
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
e.preventDefault();
|
|
273
|
-
if (!this.hasAutocomplete || (this.hasAutocomplete && !this.options.autocompleteOnly)) {
|
|
274
|
-
this.addChip({
|
|
275
|
-
tag: this.$input[0].value
|
|
276
|
-
});
|
|
277
|
-
}
|
|
278
|
-
this.$input[0].value = '';
|
|
279
|
-
|
|
280
|
-
// delete or left
|
|
281
|
-
} else if (
|
|
282
|
-
(e.keyCode === 8 || e.keyCode === 37) &&
|
|
283
|
-
this.$input[0].value === '' &&
|
|
284
|
-
this.chipsData.length
|
|
285
|
-
) {
|
|
286
|
-
e.preventDefault();
|
|
287
|
-
this.selectChip(this.chipsData.length - 1);
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
/**
|
|
292
|
-
* Render Chip
|
|
293
|
-
* @param {chip} chip
|
|
294
|
-
* @return {Element}
|
|
295
|
-
*/
|
|
296
|
-
_renderChip(chip) {
|
|
297
|
-
if (!chip.tag) {
|
|
298
|
-
return;
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
let renderedChip = document.createElement('div');
|
|
302
|
-
let closeIcon = document.createElement('i');
|
|
303
|
-
renderedChip.classList.add('chip');
|
|
304
|
-
renderedChip.textContent = chip.tag;
|
|
305
|
-
renderedChip.setAttribute('tabindex', 0);
|
|
306
|
-
$(closeIcon).addClass('material-icons close');
|
|
307
|
-
closeIcon.textContent = 'close';
|
|
308
|
-
|
|
309
|
-
// attach image if needed
|
|
310
|
-
if (chip.image) {
|
|
311
|
-
let img = document.createElement('img');
|
|
312
|
-
img.setAttribute('src', chip.image);
|
|
313
|
-
renderedChip.insertBefore(img, renderedChip.firstChild);
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
renderedChip.appendChild(closeIcon);
|
|
317
|
-
return renderedChip;
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
/**
|
|
321
|
-
* Render Chips
|
|
322
|
-
*/
|
|
323
|
-
_renderChips() {
|
|
324
|
-
this.$chips.remove();
|
|
325
|
-
for (let i = 0; i < this.chipsData.length; i++) {
|
|
326
|
-
let chipEl = this._renderChip(this.chipsData[i]);
|
|
327
|
-
this.$el.append(chipEl);
|
|
328
|
-
this.$chips.add(chipEl);
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
// move input to end
|
|
332
|
-
this.$el.append(this.$input[0]);
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
/**
|
|
336
|
-
* Setup Autocomplete
|
|
337
|
-
*/
|
|
338
|
-
_setupAutocomplete() {
|
|
339
|
-
this.options.autocompleteOptions.onAutocomplete = (val) => {
|
|
340
|
-
this.addChip({
|
|
341
|
-
tag: val
|
|
342
|
-
});
|
|
343
|
-
this.$input[0].value = '';
|
|
344
|
-
this.$input[0].focus();
|
|
345
|
-
};
|
|
346
|
-
|
|
347
|
-
this.autocomplete = M.Autocomplete.init(this.$input[0], this.options.autocompleteOptions);
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
/**
|
|
351
|
-
* Setup Input
|
|
352
|
-
*/
|
|
353
|
-
_setupInput() {
|
|
354
|
-
this.$input = this.$el.find('input');
|
|
355
|
-
if (!this.$input.length) {
|
|
356
|
-
this.$input = $('<input></input>');
|
|
357
|
-
this.$el.append(this.$input);
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
this.$input.addClass('input');
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
/**
|
|
364
|
-
* Setup Label
|
|
365
|
-
*/
|
|
366
|
-
_setupLabel() {
|
|
367
|
-
this.$label = this.$el.find('label');
|
|
368
|
-
if (this.$label.length) {
|
|
369
|
-
this.$label[0].setAttribute('for', this.$input.attr('id'));
|
|
370
|
-
}
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
/**
|
|
374
|
-
* Set placeholder
|
|
375
|
-
*/
|
|
376
|
-
_setPlaceholder() {
|
|
377
|
-
if (this.chipsData !== undefined && !this.chipsData.length && this.options.placeholder) {
|
|
378
|
-
$(this.$input).prop('placeholder', this.options.placeholder);
|
|
379
|
-
} else if (
|
|
380
|
-
(this.chipsData === undefined || !!this.chipsData.length) &&
|
|
381
|
-
this.options.secondaryPlaceholder
|
|
382
|
-
) {
|
|
383
|
-
$(this.$input).prop('placeholder', this.options.secondaryPlaceholder);
|
|
384
|
-
}
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
/**
|
|
388
|
-
* Check if chip is valid
|
|
389
|
-
* @param {chip} chip
|
|
390
|
-
*/
|
|
391
|
-
_isValid(chip) {
|
|
392
|
-
if (chip.hasOwnProperty('tag') && chip.tag !== '') {
|
|
393
|
-
let exists = false;
|
|
394
|
-
for (let i = 0; i < this.chipsData.length; i++) {
|
|
395
|
-
if (this.chipsData[i].tag === chip.tag) {
|
|
396
|
-
exists = true;
|
|
397
|
-
break;
|
|
398
|
-
}
|
|
399
|
-
}
|
|
400
|
-
return !exists;
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
return false;
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
/**
|
|
407
|
-
* Add chip
|
|
408
|
-
* @param {chip} chip
|
|
409
|
-
*/
|
|
410
|
-
addChip(chip) {
|
|
411
|
-
if (!this._isValid(chip) || this.chipsData.length >= this.options.limit) {
|
|
412
|
-
return;
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
let renderedChip = this._renderChip(chip);
|
|
416
|
-
this.$chips.add(renderedChip);
|
|
417
|
-
this.chipsData.push(chip);
|
|
418
|
-
$(this.$input).before(renderedChip);
|
|
419
|
-
this._setPlaceholder();
|
|
420
|
-
|
|
421
|
-
// fire chipAdd callback
|
|
422
|
-
if (typeof this.options.onChipAdd === 'function') {
|
|
423
|
-
this.options.onChipAdd.call(this, this.$el, renderedChip);
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
/**
|
|
428
|
-
* Delete chip
|
|
429
|
-
* @param {Number} chip
|
|
430
|
-
*/
|
|
431
|
-
deleteChip(chipIndex) {
|
|
432
|
-
let $chip = this.$chips.eq(chipIndex);
|
|
433
|
-
this.$chips.eq(chipIndex).remove();
|
|
434
|
-
this.$chips = this.$chips.filter(function(el) {
|
|
435
|
-
return $(el).index() >= 0;
|
|
436
|
-
});
|
|
437
|
-
this.chipsData.splice(chipIndex, 1);
|
|
438
|
-
this._setPlaceholder();
|
|
439
|
-
|
|
440
|
-
// fire chipDelete callback
|
|
441
|
-
if (typeof this.options.onChipDelete === 'function') {
|
|
442
|
-
this.options.onChipDelete.call(this, this.$el, $chip[0]);
|
|
443
|
-
}
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
/**
|
|
447
|
-
* Select chip
|
|
448
|
-
* @param {Number} chip
|
|
449
|
-
*/
|
|
450
|
-
selectChip(chipIndex) {
|
|
451
|
-
let $chip = this.$chips.eq(chipIndex);
|
|
452
|
-
this._selectedChip = $chip;
|
|
453
|
-
$chip[0].focus();
|
|
454
|
-
|
|
455
|
-
// fire chipSelect callback
|
|
456
|
-
if (typeof this.options.onChipSelect === 'function') {
|
|
457
|
-
this.options.onChipSelect.call(this, this.$el, $chip[0]);
|
|
458
|
-
}
|
|
459
|
-
}
|
|
460
|
-
}
|
|
461
|
-
|
|
462
|
-
/**
|
|
463
|
-
* @static
|
|
464
|
-
* @memberof Chips
|
|
465
|
-
*/
|
|
466
|
-
Chips._keydown = false;
|
|
467
|
-
|
|
468
|
-
M.Chips = Chips;
|
|
469
|
-
|
|
470
|
-
if (M.jQueryLoaded) {
|
|
471
|
-
M.initializeJqueryWrapper(Chips, 'chips', 'M_Chips');
|
|
472
|
-
}
|
|
473
|
-
|
|
474
|
-
$(document).ready(function() {
|
|
475
|
-
// Handle removal of static chips.
|
|
476
|
-
$(document.body).on('click', '.chip .close', function() {
|
|
477
|
-
let $chips = $(this).closest('.chips');
|
|
478
|
-
if ($chips.length && $chips[0].M_Chips) {
|
|
479
|
-
return;
|
|
480
|
-
}
|
|
481
|
-
$(this)
|
|
482
|
-
.closest('.chip')
|
|
483
|
-
.remove();
|
|
484
|
-
});
|
|
485
|
-
});
|
|
486
|
-
})(cash);
|
|
1
|
+
(function($) {
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
let _defaults = {
|
|
5
|
+
data: [],
|
|
6
|
+
placeholder: '',
|
|
7
|
+
secondaryPlaceholder: '',
|
|
8
|
+
autocompleteOptions: {},
|
|
9
|
+
autocompleteOnly: false,
|
|
10
|
+
limit: Infinity,
|
|
11
|
+
onChipAdd: null,
|
|
12
|
+
onChipSelect: null,
|
|
13
|
+
onChipDelete: null
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @typedef {Object} chip
|
|
18
|
+
* @property {String} tag chip tag string
|
|
19
|
+
* @property {String} [image] chip avatar image string
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @class
|
|
24
|
+
*
|
|
25
|
+
*/
|
|
26
|
+
class Chips extends Component {
|
|
27
|
+
/**
|
|
28
|
+
* Construct Chips instance and set up overlay
|
|
29
|
+
* @constructor
|
|
30
|
+
* @param {Element} el
|
|
31
|
+
* @param {Object} options
|
|
32
|
+
*/
|
|
33
|
+
constructor(el, options) {
|
|
34
|
+
super(Chips, el, options);
|
|
35
|
+
|
|
36
|
+
this.el.M_Chips = this;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Options for the modal
|
|
40
|
+
* @member Chips#options
|
|
41
|
+
* @prop {Array} data
|
|
42
|
+
* @prop {String} placeholder
|
|
43
|
+
* @prop {String} secondaryPlaceholder
|
|
44
|
+
* @prop {Object} autocompleteOptions
|
|
45
|
+
*/
|
|
46
|
+
this.options = $.extend({}, Chips.defaults, options);
|
|
47
|
+
|
|
48
|
+
this.$el.addClass('chips input-field');
|
|
49
|
+
this.chipsData = [];
|
|
50
|
+
this.$chips = $();
|
|
51
|
+
this._setupInput();
|
|
52
|
+
this.hasAutocomplete = Object.keys(this.options.autocompleteOptions).length > 0;
|
|
53
|
+
|
|
54
|
+
// Set input id
|
|
55
|
+
if (!this.$input.attr('id')) {
|
|
56
|
+
this.$input.attr('id', M.guid());
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Render initial chips
|
|
60
|
+
if (this.options.data.length) {
|
|
61
|
+
this.chipsData = this.options.data;
|
|
62
|
+
this._renderChips(this.chipsData);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Setup autocomplete if needed
|
|
66
|
+
if (this.hasAutocomplete) {
|
|
67
|
+
this._setupAutocomplete();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
this._setPlaceholder();
|
|
71
|
+
this._setupLabel();
|
|
72
|
+
this._setupEventHandlers();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
static get defaults() {
|
|
76
|
+
return _defaults;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
static init(els, options) {
|
|
80
|
+
return super.init(this, els, options);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Get Instance
|
|
85
|
+
*/
|
|
86
|
+
static getInstance(el) {
|
|
87
|
+
let domElem = !!el.jquery ? el[0] : el;
|
|
88
|
+
return domElem.M_Chips;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Get Chips Data
|
|
93
|
+
*/
|
|
94
|
+
getData() {
|
|
95
|
+
return this.chipsData;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Teardown component
|
|
100
|
+
*/
|
|
101
|
+
destroy() {
|
|
102
|
+
this._removeEventHandlers();
|
|
103
|
+
this.$chips.remove();
|
|
104
|
+
this.el.M_Chips = undefined;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Setup Event Handlers
|
|
109
|
+
*/
|
|
110
|
+
_setupEventHandlers() {
|
|
111
|
+
this._handleChipClickBound = this._handleChipClick.bind(this);
|
|
112
|
+
this._handleInputKeydownBound = this._handleInputKeydown.bind(this);
|
|
113
|
+
this._handleInputFocusBound = this._handleInputFocus.bind(this);
|
|
114
|
+
this._handleInputBlurBound = this._handleInputBlur.bind(this);
|
|
115
|
+
|
|
116
|
+
this.el.addEventListener('click', this._handleChipClickBound);
|
|
117
|
+
document.addEventListener('keydown', Chips._handleChipsKeydown);
|
|
118
|
+
document.addEventListener('keyup', Chips._handleChipsKeyup);
|
|
119
|
+
this.el.addEventListener('blur', Chips._handleChipsBlur, true);
|
|
120
|
+
this.$input[0].addEventListener('focus', this._handleInputFocusBound);
|
|
121
|
+
this.$input[0].addEventListener('blur', this._handleInputBlurBound);
|
|
122
|
+
this.$input[0].addEventListener('keydown', this._handleInputKeydownBound);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Remove Event Handlers
|
|
127
|
+
*/
|
|
128
|
+
_removeEventHandlers() {
|
|
129
|
+
this.el.removeEventListener('click', this._handleChipClickBound);
|
|
130
|
+
document.removeEventListener('keydown', Chips._handleChipsKeydown);
|
|
131
|
+
document.removeEventListener('keyup', Chips._handleChipsKeyup);
|
|
132
|
+
this.el.removeEventListener('blur', Chips._handleChipsBlur, true);
|
|
133
|
+
this.$input[0].removeEventListener('focus', this._handleInputFocusBound);
|
|
134
|
+
this.$input[0].removeEventListener('blur', this._handleInputBlurBound);
|
|
135
|
+
this.$input[0].removeEventListener('keydown', this._handleInputKeydownBound);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Handle Chip Click
|
|
140
|
+
* @param {Event} e
|
|
141
|
+
*/
|
|
142
|
+
_handleChipClick(e) {
|
|
143
|
+
let $chip = $(e.target).closest('.chip');
|
|
144
|
+
let clickedClose = $(e.target).is('.close');
|
|
145
|
+
if ($chip.length) {
|
|
146
|
+
let index = $chip.index();
|
|
147
|
+
if (clickedClose) {
|
|
148
|
+
// delete chip
|
|
149
|
+
this.deleteChip(index);
|
|
150
|
+
this.$input[0].focus();
|
|
151
|
+
} else {
|
|
152
|
+
// select chip
|
|
153
|
+
this.selectChip(index);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// Default handle click to focus on input
|
|
157
|
+
} else {
|
|
158
|
+
this.$input[0].focus();
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Handle Chips Keydown
|
|
164
|
+
* @param {Event} e
|
|
165
|
+
*/
|
|
166
|
+
static _handleChipsKeydown(e) {
|
|
167
|
+
Chips._keydown = true;
|
|
168
|
+
|
|
169
|
+
let $chips = $(e.target).closest('.chips');
|
|
170
|
+
let chipsKeydown = e.target && $chips.length;
|
|
171
|
+
|
|
172
|
+
// Don't handle keydown inputs on input and textarea
|
|
173
|
+
if ($(e.target).is('input, textarea') || !chipsKeydown) {
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
let currChips = $chips[0].M_Chips;
|
|
178
|
+
|
|
179
|
+
// backspace and delete
|
|
180
|
+
if (e.keyCode === 8 || e.keyCode === 46) {
|
|
181
|
+
e.preventDefault();
|
|
182
|
+
|
|
183
|
+
let selectIndex = currChips.chipsData.length;
|
|
184
|
+
if (currChips._selectedChip) {
|
|
185
|
+
let index = currChips._selectedChip.index();
|
|
186
|
+
currChips.deleteChip(index);
|
|
187
|
+
currChips._selectedChip = null;
|
|
188
|
+
|
|
189
|
+
// Make sure selectIndex doesn't go negative
|
|
190
|
+
selectIndex = Math.max(index - 1, 0);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (currChips.chipsData.length) {
|
|
194
|
+
currChips.selectChip(selectIndex);
|
|
195
|
+
} else {
|
|
196
|
+
currChips.$input[0].focus();
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// left arrow key
|
|
200
|
+
} else if (e.keyCode === 37) {
|
|
201
|
+
if (currChips._selectedChip) {
|
|
202
|
+
let selectIndex = currChips._selectedChip.index() - 1;
|
|
203
|
+
if (selectIndex < 0) {
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
currChips.selectChip(selectIndex);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// right arrow key
|
|
210
|
+
} else if (e.keyCode === 39) {
|
|
211
|
+
if (currChips._selectedChip) {
|
|
212
|
+
let selectIndex = currChips._selectedChip.index() + 1;
|
|
213
|
+
|
|
214
|
+
if (selectIndex >= currChips.chipsData.length) {
|
|
215
|
+
currChips.$input[0].focus();
|
|
216
|
+
} else {
|
|
217
|
+
currChips.selectChip(selectIndex);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Handle Chips Keyup
|
|
225
|
+
* @param {Event} e
|
|
226
|
+
*/
|
|
227
|
+
static _handleChipsKeyup(e) {
|
|
228
|
+
Chips._keydown = false;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Handle Chips Blur
|
|
233
|
+
* @param {Event} e
|
|
234
|
+
*/
|
|
235
|
+
static _handleChipsBlur(e) {
|
|
236
|
+
if (!Chips._keydown && document.hidden) {
|
|
237
|
+
let $chips = $(e.target).closest('.chips');
|
|
238
|
+
let currChips = $chips[0].M_Chips;
|
|
239
|
+
|
|
240
|
+
currChips._selectedChip = null;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Handle Input Focus
|
|
246
|
+
*/
|
|
247
|
+
_handleInputFocus() {
|
|
248
|
+
this.$el.addClass('focus');
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Handle Input Blur
|
|
253
|
+
*/
|
|
254
|
+
_handleInputBlur() {
|
|
255
|
+
this.$el.removeClass('focus');
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Handle Input Keydown
|
|
260
|
+
* @param {Event} e
|
|
261
|
+
*/
|
|
262
|
+
_handleInputKeydown(e) {
|
|
263
|
+
Chips._keydown = true;
|
|
264
|
+
|
|
265
|
+
// enter
|
|
266
|
+
if (e.keyCode === 13) {
|
|
267
|
+
// Override enter if autocompleting.
|
|
268
|
+
if (this.hasAutocomplete && this.autocomplete && this.autocomplete.isOpen) {
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
e.preventDefault();
|
|
273
|
+
if (!this.hasAutocomplete || (this.hasAutocomplete && !this.options.autocompleteOnly)) {
|
|
274
|
+
this.addChip({
|
|
275
|
+
tag: this.$input[0].value
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
this.$input[0].value = '';
|
|
279
|
+
|
|
280
|
+
// delete or left
|
|
281
|
+
} else if (
|
|
282
|
+
(e.keyCode === 8 || e.keyCode === 37) &&
|
|
283
|
+
this.$input[0].value === '' &&
|
|
284
|
+
this.chipsData.length
|
|
285
|
+
) {
|
|
286
|
+
e.preventDefault();
|
|
287
|
+
this.selectChip(this.chipsData.length - 1);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* Render Chip
|
|
293
|
+
* @param {chip} chip
|
|
294
|
+
* @return {Element}
|
|
295
|
+
*/
|
|
296
|
+
_renderChip(chip) {
|
|
297
|
+
if (!chip.tag) {
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
let renderedChip = document.createElement('div');
|
|
302
|
+
let closeIcon = document.createElement('i');
|
|
303
|
+
renderedChip.classList.add('chip');
|
|
304
|
+
renderedChip.textContent = chip.tag;
|
|
305
|
+
renderedChip.setAttribute('tabindex', 0);
|
|
306
|
+
$(closeIcon).addClass('material-icons close');
|
|
307
|
+
closeIcon.textContent = 'close';
|
|
308
|
+
|
|
309
|
+
// attach image if needed
|
|
310
|
+
if (chip.image) {
|
|
311
|
+
let img = document.createElement('img');
|
|
312
|
+
img.setAttribute('src', chip.image);
|
|
313
|
+
renderedChip.insertBefore(img, renderedChip.firstChild);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
renderedChip.appendChild(closeIcon);
|
|
317
|
+
return renderedChip;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Render Chips
|
|
322
|
+
*/
|
|
323
|
+
_renderChips() {
|
|
324
|
+
this.$chips.remove();
|
|
325
|
+
for (let i = 0; i < this.chipsData.length; i++) {
|
|
326
|
+
let chipEl = this._renderChip(this.chipsData[i]);
|
|
327
|
+
this.$el.append(chipEl);
|
|
328
|
+
this.$chips.add(chipEl);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// move input to end
|
|
332
|
+
this.$el.append(this.$input[0]);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* Setup Autocomplete
|
|
337
|
+
*/
|
|
338
|
+
_setupAutocomplete() {
|
|
339
|
+
this.options.autocompleteOptions.onAutocomplete = (val) => {
|
|
340
|
+
this.addChip({
|
|
341
|
+
tag: val
|
|
342
|
+
});
|
|
343
|
+
this.$input[0].value = '';
|
|
344
|
+
this.$input[0].focus();
|
|
345
|
+
};
|
|
346
|
+
|
|
347
|
+
this.autocomplete = M.Autocomplete.init(this.$input[0], this.options.autocompleteOptions);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* Setup Input
|
|
352
|
+
*/
|
|
353
|
+
_setupInput() {
|
|
354
|
+
this.$input = this.$el.find('input');
|
|
355
|
+
if (!this.$input.length) {
|
|
356
|
+
this.$input = $('<input></input>');
|
|
357
|
+
this.$el.append(this.$input);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
this.$input.addClass('input');
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Setup Label
|
|
365
|
+
*/
|
|
366
|
+
_setupLabel() {
|
|
367
|
+
this.$label = this.$el.find('label');
|
|
368
|
+
if (this.$label.length) {
|
|
369
|
+
this.$label[0].setAttribute('for', this.$input.attr('id'));
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* Set placeholder
|
|
375
|
+
*/
|
|
376
|
+
_setPlaceholder() {
|
|
377
|
+
if (this.chipsData !== undefined && !this.chipsData.length && this.options.placeholder) {
|
|
378
|
+
$(this.$input).prop('placeholder', this.options.placeholder);
|
|
379
|
+
} else if (
|
|
380
|
+
(this.chipsData === undefined || !!this.chipsData.length) &&
|
|
381
|
+
this.options.secondaryPlaceholder
|
|
382
|
+
) {
|
|
383
|
+
$(this.$input).prop('placeholder', this.options.secondaryPlaceholder);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* Check if chip is valid
|
|
389
|
+
* @param {chip} chip
|
|
390
|
+
*/
|
|
391
|
+
_isValid(chip) {
|
|
392
|
+
if (chip.hasOwnProperty('tag') && chip.tag !== '') {
|
|
393
|
+
let exists = false;
|
|
394
|
+
for (let i = 0; i < this.chipsData.length; i++) {
|
|
395
|
+
if (this.chipsData[i].tag === chip.tag) {
|
|
396
|
+
exists = true;
|
|
397
|
+
break;
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
return !exists;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
return false;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/**
|
|
407
|
+
* Add chip
|
|
408
|
+
* @param {chip} chip
|
|
409
|
+
*/
|
|
410
|
+
addChip(chip) {
|
|
411
|
+
if (!this._isValid(chip) || this.chipsData.length >= this.options.limit) {
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
let renderedChip = this._renderChip(chip);
|
|
416
|
+
this.$chips.add(renderedChip);
|
|
417
|
+
this.chipsData.push(chip);
|
|
418
|
+
$(this.$input).before(renderedChip);
|
|
419
|
+
this._setPlaceholder();
|
|
420
|
+
|
|
421
|
+
// fire chipAdd callback
|
|
422
|
+
if (typeof this.options.onChipAdd === 'function') {
|
|
423
|
+
this.options.onChipAdd.call(this, this.$el, renderedChip);
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* Delete chip
|
|
429
|
+
* @param {Number} chip
|
|
430
|
+
*/
|
|
431
|
+
deleteChip(chipIndex) {
|
|
432
|
+
let $chip = this.$chips.eq(chipIndex);
|
|
433
|
+
this.$chips.eq(chipIndex).remove();
|
|
434
|
+
this.$chips = this.$chips.filter(function(el) {
|
|
435
|
+
return $(el).index() >= 0;
|
|
436
|
+
});
|
|
437
|
+
this.chipsData.splice(chipIndex, 1);
|
|
438
|
+
this._setPlaceholder();
|
|
439
|
+
|
|
440
|
+
// fire chipDelete callback
|
|
441
|
+
if (typeof this.options.onChipDelete === 'function') {
|
|
442
|
+
this.options.onChipDelete.call(this, this.$el, $chip[0]);
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
/**
|
|
447
|
+
* Select chip
|
|
448
|
+
* @param {Number} chip
|
|
449
|
+
*/
|
|
450
|
+
selectChip(chipIndex) {
|
|
451
|
+
let $chip = this.$chips.eq(chipIndex);
|
|
452
|
+
this._selectedChip = $chip;
|
|
453
|
+
$chip[0].focus();
|
|
454
|
+
|
|
455
|
+
// fire chipSelect callback
|
|
456
|
+
if (typeof this.options.onChipSelect === 'function') {
|
|
457
|
+
this.options.onChipSelect.call(this, this.$el, $chip[0]);
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
/**
|
|
463
|
+
* @static
|
|
464
|
+
* @memberof Chips
|
|
465
|
+
*/
|
|
466
|
+
Chips._keydown = false;
|
|
467
|
+
|
|
468
|
+
M.Chips = Chips;
|
|
469
|
+
|
|
470
|
+
if (M.jQueryLoaded) {
|
|
471
|
+
M.initializeJqueryWrapper(Chips, 'chips', 'M_Chips');
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
$(document).ready(function() {
|
|
475
|
+
// Handle removal of static chips.
|
|
476
|
+
$(document.body).on('click', '.chip .close', function() {
|
|
477
|
+
let $chips = $(this).closest('.chips');
|
|
478
|
+
if ($chips.length && $chips[0].M_Chips) {
|
|
479
|
+
return;
|
|
480
|
+
}
|
|
481
|
+
$(this)
|
|
482
|
+
.closest('.chip')
|
|
483
|
+
.remove();
|
|
484
|
+
});
|
|
485
|
+
});
|
|
486
|
+
})(cash);
|