-
Notifications
You must be signed in to change notification settings - Fork 26
/
insignia.es5.js
506 lines (435 loc) · 13.1 KB
/
insignia.es5.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
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
'use strict';
var _sell = require('sell');
var _sell2 = _interopRequireDefault(_sell);
var _crossvent = require('crossvent');
var _crossvent2 = _interopRequireDefault(_crossvent);
var _emitter = require('contra/emitter');
var _emitter2 = _interopRequireDefault(_emitter);
var _dom = require('./dom');
var _dom2 = _interopRequireDefault(_dom);
var _text = require('./text');
var _text2 = _interopRequireDefault(_text);
var _autosize = require('./autosize');
var _autosize2 = _interopRequireDefault(_autosize);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
var inputTag = /^input$/i;
var ELEMENT = 1;
var BACKSPACE = 8;
var END = 35;
var HOME = 36;
var LEFT = 37;
var RIGHT = 39;
var sinkableKeys = [END, HOME];
var tagClass = /\bnsg-tag\b/;
var tagRemovalClass = /\bnsg-tag-remove\b/;
var editorClass = /\bnsg-editor\b/g;
var inputClass = /\bnsg-input\b/g;
var end = { start: 'end', end: 'end' };
var defaultDelimiter = ' ';
function insignia(el) {
var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
var currentValues = [];
var o = options;
var delimiter = o.delimiter || defaultDelimiter;
if (delimiter.length !== 1) {
throw new Error('insignia expected a single-character delimiter string');
}
var any = hasSiblings(el);
if (any || !inputTag.test(el.tagName)) {
throw new Error('insignia expected an input element without any siblings');
}
var free = o.free !== false;
var validate = o.validate || defaultValidate;
var render = o.render || defaultRenderer;
var convertOnBlur = o.convertOnBlur !== false;
var toItemData = defaultToItemData;
var userGetText = o.getText;
var userGetValue = o.getValue;
var getText = typeof userGetText === 'string' ? function (d) {
return d[userGetText];
} : typeof userGetText === 'function' ? userGetText : function (d) {
return d.toString();
};
var getValue = typeof userGetValue === 'string' ? function (d) {
return d[userGetValue];
} : typeof userGetValue === 'function' ? userGetValue : function (d) {
return d;
};
var before = (0, _dom2.default)('span', 'nsg-tags nsg-tags-before');
var after = (0, _dom2.default)('span', 'nsg-tags nsg-tags-after');
var parent = el.parentElement;
var blurblock = tick();
el.className += ' nsg-input';
parent.className += ' nsg-editor';
parent.insertBefore(before, el);
parent.insertBefore(after, el.nextSibling);
var shrinker = (0, _autosize2.default)(el);
var api = (0, _emitter2.default)({
addItem: addItem,
findItem: function findItem(data) {
return _findItem(data);
},
findItemIndex: function findItemIndex(data) {
return _findItemIndex(data);
},
findItemByElement: function findItemByElement(el) {
return _findItem(el, 'el');
},
removeItem: removeItemByData,
removeItemByElement: removeItemByElement,
value: readValue,
allValues: readValueAll,
refresh: convert,
destroy: destroy
});
var placeholder = el.getAttribute('placeholder');
var placeheld = true;
bind();
(document.activeElement === el ? evaluateSelect : evaluateNoSelect)([delimiter], true);
return api;
function _findItem(value) {
var prop = arguments.length <= 1 || arguments[1] === undefined ? 'data' : arguments[1];
var comp = prop === 'data' ? function (item) {
return getValue(item[prop]) === getValue(value);
} : function (item) {
return item[prop] === value;
};
for (var i = 0; i < currentValues.length; i++) {
if (comp(currentValues[i])) {
return currentValues[i];
}
}
return null;
}
function _findItemIndex(value) {
var prop = arguments.length <= 1 || arguments[1] === undefined ? 'data' : arguments[1];
var comp = prop === 'data' ? function (item) {
return getValue(item[prop]) === getValue(value);
} : function (item) {
return item[prop] === value;
};
for (var i = 0; i < currentValues.length; i++) {
if (comp(currentValues[i])) {
return i;
}
}
return null;
}
function addItem(data) {
var valid = validate(data);
var item = { data: data, valid: valid };
if (o.preventInvalid && !valid) {
return api;
}
var el = renderItem(item);
if (!el) {
return api;
}
item.el = el;
currentValues.push(item);
api.emit('add', data, el);
invalidate();
return api;
}
function removeItem(item) {
if (!item) {
return api;
}
removeItemElement(item.el);
currentValues.splice(currentValues.indexOf(item), 1);
api.emit('remove', item.data);
invalidate();
return api;
}
function invalidate() {
currentValues.slice().forEach(function (v, i) {
currentValues.splice(i, 1);
var valid = validate(v.data, i);
if (valid) {
v.el.classList.add('nsg-valid');
v.el.classList.remove('nsg-invalid');
} else {
v.el.classList.add('nsg-invalid');
v.el.classList.remove('nsg-valid');
api.emit('invalid', v.data, v.el);
}
v.valid = valid;
currentValues.splice(i, 0, v);
});
}
function removeItemByData(data) {
return removeItem(_findItem(data));
}
function removeItemByElement(el) {
return removeItem(_findItem(el, 'el'));
}
function renderItem(item) {
return createTag(before, item);
}
function removeItemElement(el) {
if (el.parentElement) {
el.parentElement.removeChild(el);
}
}
function createTag(buffer, item) {
var data = item.data;
var empty = typeof data === 'string' && data.trim().length === 0;
if (empty) {
return null;
}
var el = (0, _dom2.default)('span', 'nsg-tag');
render(el, item);
if (o.deletion) {
el.appendChild((0, _dom2.default)('span', 'nsg-tag-remove'));
}
buffer.appendChild(el);
return el;
}
function defaultToItemData(s) {
return s;
}
function readValue() {
return currentValues.filter(function (v) {
return v.valid;
}).map(function (v) {
return v.data;
});
}
function readValueAll() {
return currentValues.map(function (v) {
return v.data;
});
}
function updatePlaceholder(e) {
var any = parent.querySelector('.nsg-tag');
if (!any && !placeheld) {
el.setAttribute('placeholder', placeholder);
placeheld = true;
} else if (any && placeheld) {
el.removeAttribute('placeholder');
placeheld = false;
}
}
function bind(remove) {
var op = remove ? 'remove' : 'add';
var ev = remove ? 'off' : 'on';
_crossvent2.default[op](el, 'keydown', keydown);
_crossvent2.default[op](el, 'keypress', keypress);
_crossvent2.default[op](el, 'paste', paste);
_crossvent2.default[op](parent, 'click', click);
if (convertOnBlur) {
_crossvent2.default[op](document.documentElement, 'blur', documentblur, true);
_crossvent2.default[op](document.documentElement, 'mousedown', documentmousedown);
}
if (placeholder) {
api[ev]('add', updatePlaceholder);
api[ev]('remove', updatePlaceholder);
_crossvent2.default[op](el, 'keydown', updatePlaceholder);
_crossvent2.default[op](el, 'keypress', updatePlaceholder);
_crossvent2.default[op](el, 'paste', updatePlaceholder);
_crossvent2.default[op](parent, 'click', updatePlaceholder);
updatePlaceholder();
}
}
function destroy() {
bind(true);
el.value = '';
el.className = el.className.replace(inputClass, '');
parent.className = parent.className.replace(editorClass, '');
if (before.parentElement) {
before.parentElement.removeChild(before);
}
if (after.parentElement) {
after.parentElement.removeChild(after);
}
shrinker.destroy();
api.destroyed = true;
api.destroy = api.addItem = api.removeItem = function () {
return api;
};
api.tags = api.value = function () {
return null;
};
return api;
}
function tick() {
return new Date().valueOf();
}
function documentblur(e) {
if (blurblock > tick()) {
return;
}
convert(true);
}
function documentmousedown(e) {
var el = e.target;
while (el) {
if (el === parent) {
blurblock = tick() + 100;
}
el = el.parentElement;
}
}
function click(e) {
var target = e.target;
if (tagRemovalClass.test(target.className)) {
removeItemByElement(target.parentElement);
el.focus();
return;
}
var parent = target;
var tagged = tagClass.test(parent.className);
while (tagged === false && parent.parentElement) {
parent = parent.parentElement;
tagged = tagClass.test(parent.className);
}
if (tagged && free) {
focusTag(parent, end);
} else if (target !== el) {
shift();
el.focus();
}
}
function shift() {
focusTag(after.lastChild, end);
evaluateSelect([delimiter], true);
}
function convert(all) {
(all ? evaluateNoSelect : evaluateSelect)([delimiter], all);
if (all) {
each(after, moveLeft);
}
return api;
}
function moveLeft(value, tag) {
before.appendChild(tag);
}
function keydown(e) {
var sel = (0, _sell2.default)(el);
var key = e.which || e.keyCode || e.charCode;
var canMoveLeft = sel.start === 0 && sel.end === 0 && before.lastChild;
var canMoveRight = sel.start === el.value.length && sel.end === el.value.length && after.firstChild;
if (free) {
if (key === HOME) {
if (before.firstChild) {
focusTag(before.firstChild, {});
} else {
(0, _sell2.default)(el, { start: 0, end: 0 });
}
} else if (key === END) {
if (after.lastChild) {
focusTag(after.lastChild, end);
} else {
(0, _sell2.default)(el, end);
}
} else if (key === BACKSPACE && canMoveLeft) {
removeItemByElement(before.lastChild);
} else if (key === RIGHT && canMoveRight) {
focusTag(after.firstChild, {});
} else if (key === LEFT && canMoveLeft) {
focusTag(before.lastChild, end);
} else {
return;
}
} else {
if (key === BACKSPACE && canMoveLeft) {
removeItemByElement(before.lastChild);
} else if (key === RIGHT && canMoveRight) {
before.appendChild(after.firstChild);
} else if (key === LEFT && canMoveLeft) {
after.insertBefore(before.lastChild, after.firstChild);
} else if (sinkableKeys.indexOf(key) === -1) {
// prevent default otherwise
return;
}
}
e.preventDefault();
return false;
}
function keypress(e) {
var key = e.which || e.keyCode || e.charCode;
if (String.fromCharCode(key) === delimiter) {
convert();
e.preventDefault();
return false;
}
}
function paste() {
setTimeout(function () {
return evaluateSelect();
}, 0);
}
function evaluateNoSelect(extras, entirely) {
evaluateInternal(extras, entirely); // necessary for blur events, initialization, unfocused evaluation
}
function evaluateSelect(extras, entirely) {
evaluateInternal(extras, entirely, (0, _sell2.default)(el)); // only if we know the input has/should have focus
}
function evaluateInternal(extras, entirely, p) {
var len = entirely || !p ? Infinity : p.start;
var tags = el.value.slice(0, len).concat(extras || []).split(delimiter);
if (tags.length < 1 || !free) {
return;
}
var rest = tags.pop() + el.value.slice(len);
var removal = tags.join(delimiter).length;
tags.forEach(function (tag) {
return addItem(toItemData(tag));
});
el.value = rest;
reselect();
shrinker.refresh();
function reselect() {
if (p) {
p.start -= removal;
p.end -= removal;
(0, _sell2.default)(el, p);
}
}
}
function defaultRenderer(container, item) {
(0, _text2.default)(container, getText(item.data));
}
function readTag(tag) {
return (0, _text2.default)(tag);
}
function focusTag(tag, p) {
if (!tag) {
return;
}
evaluateSelect([delimiter], true);
var parent = tag.parentElement;
if (parent === before) {
while (parent.lastChild !== tag) {
after.insertBefore(parent.lastChild, after.firstChild);
}
} else {
while (parent.firstChild !== tag) {
before.appendChild(parent.firstChild);
}
}
var value = p.remove ? '' : readTag(tag);
removeItemByElement(tag);
el.value = value;
el.focus();
(0, _sell2.default)(el, p);
shrinker.refresh();
}
function hasSiblings() {
var children = el.parentElement.children;
return [].concat(_toConsumableArray(children)).some(function (s) {
return s !== el && s.nodeType === ELEMENT;
});
}
function each(container, fn) {
[].concat(_toConsumableArray(container.children)).forEach(function (tag, i) {
return fn(readTag(tag), tag, i);
});
}
function defaultValidate(value, i) {
var x = _findItemIndex(value);
return x === i || x === null;
}
}
module.exports = insignia;