-
Notifications
You must be signed in to change notification settings - Fork 426
/
events.js
795 lines (646 loc) 路 36 KB
/
events.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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
import { decode, extend, getfirstTextNode, isChromeAndroidBrowser } from './helpers'
var deleteBackspaceTimeout;
export function triggerChangeEvent(){
if( this.settings.mixMode.integrated ) return;
var inputElm = this.DOM.originalInput,
changed = this.state.lastOriginalValueReported !== inputElm.value,
event = new CustomEvent("change", {bubbles: true}); // must use "CustomEvent" and not "Event" to support IE
if( !changed ) return;
// must apply this BEFORE triggering the simulated event
this.state.lastOriginalValueReported = inputElm.value
// React hack: https://github.com/facebook/react/issues/11488
event.simulated = true
if (inputElm._valueTracker)
inputElm._valueTracker.setValue(Math.random())
inputElm.dispatchEvent(event)
// also trigger a Tagify event
this.trigger("change", this.state.lastOriginalValueReported)
// React, for some reason, clears the input's value after "dispatchEvent" is fired
inputElm.value = this.state.lastOriginalValueReported
}
export default {
// bind custom events which were passed in the settings
customBinding(){
this.customEventsList.forEach(name => {
this.on(name, this.settings.callbacks[name])
})
},
binding( bindUnbind = true ){
var _CB = this.events.callbacks,
_CBR,
action = bindUnbind ? 'addEventListener' : 'removeEventListener';
// do not allow the main events to be bound more than once
if( this.state.mainEvents && bindUnbind )
return;
// set the binding state of the main events, so they will not be bound more than once
this.state.mainEvents = bindUnbind;
// everything inside gets executed only once-per instance
if( bindUnbind && !this.listeners.main ){
// this event should never be unbinded:
// IE cannot register "input" events on contenteditable elements, so the "keydown" should be used instead..
this.DOM.input.addEventListener(this.isIE ? "keydown" : "input", _CB[this.isIE ? "onInputIE" : "onInput"].bind(this));
window.addEventListener('keydown', _CB.onWindowKeyDown.bind(this));
if( this.settings.isJQueryPlugin )
jQuery(this.DOM.originalInput).on('tagify.removeAllTags', this.removeAllTags.bind(this))
}
// setup callback references so events could be removed later
_CBR = (this.listeners.main = this.listeners.main || {
focus : ['input', _CB.onFocusBlur.bind(this)],
blur : ['input', _CB.onFocusBlur.bind(this)],
keydown : ['input', _CB.onKeydown.bind(this)],
click : ['scope', _CB.onClickScope.bind(this)],
dblclick : ['scope', _CB.onDoubleClickScope.bind(this)],
paste : ['input', _CB.onPaste.bind(this)]
})
for( var eventName in _CBR ){
// make sure the focus/blur event is always regesitered (and never more than once)
if( eventName == 'blur' && !bindUnbind ) continue;
this.DOM[_CBR[eventName][0]][action](eventName, _CBR[eventName][1]);
}
},
/**
* DOM events callbacks
*/
callbacks : {
onFocusBlur(e){
var text = e.target ? this.trim(e.target.textContent) : '', // a string
_s = this.settings,
type = e.type,
ddEnabled = _s.dropdown.enabled >= 0,
eventData = {relatedTarget:e.relatedTarget},
isTargetSelectOption = this.state.actions.selectOption && (ddEnabled || !_s.dropdown.closeOnSelect),
isTargetAddNewBtn = this.state.actions.addNew && ddEnabled,
isRelatedTargetX = e.relatedTarget && e.relatedTarget.classList.contains(_s.classNames.tag) && this.DOM.scope.contains(e.relatedTarget),
shouldAddTags;
if( type == 'blur' ){
if( e.relatedTarget === this.DOM.scope ){
this.dropdown.hide()
this.DOM.input.focus()
return
}
this.postUpdate()
this.triggerChangeEvent()
}
if( isTargetSelectOption || isTargetAddNewBtn )
return;
this.state.hasFocus = type == "focus" ? +new Date() : false
this.toggleFocusClass(this.state.hasFocus)
if( _s.mode == 'mix' ){
if( type == "focus" ){
this.trigger("focus", eventData)
}
else if( e.type == "blur" ){
this.trigger("blur", eventData)
this.loading(false)
this.dropdown.hide()
// reset state which needs reseting
this.state.dropdown.visible = undefined
this.setStateSelection()
}
return
}
if( type == "focus" ){
this.trigger("focus", eventData)
// e.target.classList.remove('placeholder');
if( _s.dropdown.enabled === 0 ){ // && _s.mode != "select"
this.dropdown.show()
}
return
}
else if( type == "blur" ){
this.trigger("blur", eventData)
this.loading(false)
// when clicking the X button of a selected tag, it is unwanted it will be added back
// again in a few more lines of code (shouldAddTags && addTags)
if( this.settings.mode == 'select' && isRelatedTargetX )
text = '';
shouldAddTags = this.settings.mode == 'select' && text
? !this.value.length || this.value[0].value != text
: text && !this.state.actions.selectOption && _s.addTagOnBlur
// do not add a tag if "selectOption" action was just fired (this means a tag was just added from the dropdown)
shouldAddTags && this.addTags(text, true)
if( this.settings.mode == 'select' && !text )
this.removeTags()
}
this.DOM.input.removeAttribute('style')
this.dropdown.hide()
},
onWindowKeyDown(e){
var focusedElm = document.activeElement,
isTag = focusedElm.classList.contains(this.settings.classNames.tag),
isBelong = isTag && this.DOM.scope.contains(document.activeElement),
nextTag;
if( !isBelong ) return
nextTag = focusedElm.nextElementSibling
switch( e.key ){
// remove tag if has focus
case 'Backspace': {
this.removeTags(focusedElm);
(nextTag ? nextTag : this.DOM.input).focus()
break;
}
// edit tag if has focus
case 'Enter': {
setTimeout(this.editTag.bind(this), 0, focusedElm);
break;
}
}
},
onKeydown(e){
var s = this.trim(e.target.textContent);
this.trigger("keydown", {originalEvent:this.cloneEvent(e)})
/**
* ONLY FOR MIX-MODE:
*/
if( this.settings.mode == 'mix' ){
switch( e.key ){
case 'Left' :
case 'ArrowLeft' : {
// when left arrow was pressed, raise a flag so when the dropdown is shown, right-arrow will be ignored
// because it seems likely the user wishes to use the arrows to move the caret
this.state.actions.ArrowLeft = true
break
}
case 'Delete':
case 'Backspace' : {
if( this.state.editing ) return
var sel = document.getSelection(),
deleteKeyTagDetected = e.key == 'Delete' && sel.anchorOffset == (sel.anchorNode.length || 0),
isCaretAfterTag = sel.anchorNode.nodeType == 1 || !sel.anchorOffset && sel.anchorNode.previousElementSibling,
lastInputValue = decode(this.DOM.input.innerHTML),
lastTagElems = this.getTagElms(),
// isCaretInsideTag = sel.anchorNode.parentNode('.' + this.settings.classNames.tag),
tagBeforeCaret,
tagElmToBeDeleted,
firstTextNodeBeforeTag;
if( this.settings.backspace == 'edit' && isCaretAfterTag ){
tagBeforeCaret = sel.anchorNode.nodeType == 1 ? null : sel.anchorNode.previousElementSibling;
setTimeout(this.editTag.bind(this), 0, tagBeforeCaret); // timeout is needed to the last cahacrter in the edited tag won't get deleted
e.preventDefault() // needed so the tag elm won't get deleted
return;
}
if( isChromeAndroidBrowser() && isCaretAfterTag ){
firstTextNodeBeforeTag = getfirstTextNode(isCaretAfterTag)
if( !isCaretAfterTag.hasAttribute('readonly') )
isCaretAfterTag.remove() // since this is Chrome, can safetly use this "new" DOM API
// Android-Chrome wrongly hides the keyboard, and loses focus,
// so this hack below is needed to regain focus at the correct place:
this.DOM.input.focus()
setTimeout(() => {
this.placeCaretAfterNode(firstTextNodeBeforeTag)
this.DOM.input.click()
})
return
}
if( sel.anchorNode.nodeName == 'BR')
return
if( (deleteKeyTagDetected || isCaretAfterTag) && sel.anchorNode.nodeType == 1 )
if( sel.anchorOffset == 0 ) // caret is at the very begining, before a tag
tagElmToBeDeleted = deleteKeyTagDetected // delete key pressed
? lastTagElems[0]
: null;
else
tagElmToBeDeleted = lastTagElems[sel.anchorOffset - 1]
// find out if a tag *might* be a candidate for deletion, and if so, which
else if( deleteKeyTagDetected )
tagElmToBeDeleted = sel.anchorNode.nextElementSibling;
else if( isCaretAfterTag )
tagElmToBeDeleted = isCaretAfterTag;
// tagElm.hasAttribute('readonly')
if( sel.anchorNode.nodeType == 3 && // node at caret location is a Text node
!sel.anchorNode.nodeValue && // has some text
sel.anchorNode.previousElementSibling ) // text node has a Tag node before it
e.preventDefault()
// if backspace not allowed, do nothing
// TODO: a better way to detect if nodes were deleted is to simply check the "this.value" before & after
if( (isCaretAfterTag || deleteKeyTagDetected) && !this.settings.backspace ){
e.preventDefault()
return
}
if( sel.type != 'Range' && !sel.anchorOffset && sel.anchorNode == this.DOM.input && e.key != 'Delete' ){
e.preventDefault()
return
}
if( sel.type != 'Range' && tagElmToBeDeleted && tagElmToBeDeleted.hasAttribute('readonly') ){
// allows the continuation of deletion by placing the caret on the first previous textNode.
// since a few readonly-tags might be one after the other, iteration is needed:
this.placeCaretAfterNode( getfirstTextNode(tagElmToBeDeleted) )
return
}
// update regarding https://github.com/yairEO/tagify/issues/762#issuecomment-786464317:
// the bug described is more severe than the fixed below, therefore I disable the fix until a solution
// is found which work well for both cases.
// -------
// nodeType is "1" only when the caret is at the end after last tag (no text after), or before first first (no text before)
if( false && this.isFirefox && sel.anchorNode.nodeType == 1 && sel.anchorOffset != 0 ){
this.removeTags() // removes last tag by default if no parameter supplied
// place caret inside last textNode, if exist. it's an annoying bug only in FF,
// if the last tag is removed, and there is a textNode before it, the caret is not placed at its end
this.placeCaretAfterNode( this.setRangeAtStartEnd() )
}
clearTimeout(deleteBackspaceTimeout)
// a minimum delay is needed before the node actually gets detached from the document (don't know why),
// to know exactly which tag was deleted. This is the easiest way of knowing besides using MutationObserver
deleteBackspaceTimeout = setTimeout(() => {
var sel = document.getSelection(),
currentValue = decode(this.DOM.input.innerHTML),
prevElm = sel.anchorNode.previousElementSibling;
// fixes #384, where the first and only tag will not get removed with backspace
if( !isChromeAndroidBrowser() && currentValue.length >= lastInputValue.length && prevElm && !prevElm.hasAttribute('readonly') ){
this.removeTags(prevElm)
this.fixFirefoxLastTagNoCaret()
// the above "removeTag" methods removes the tag with a transition. Chrome adds a <br> element for some reason at this stage
if( this.DOM.input.children.length == 2 && this.DOM.input.children[1].tagName == "BR" ){
this.DOM.input.innerHTML = ""
this.value.length = 0
return true
}
}
// find out which tag(s) were deleted and trigger "remove" event
// iterate over the list of tags still in the document and then filter only those from the "this.value" collection
this.value = [].map.call(lastTagElems, (node, nodeIdx) => {
var tagData = this.tagData(node)
// since readonly cannot be removed (it's technically resurrected if removed somehow)
if( node.parentNode || tagData.readonly )
return tagData
else
this.trigger('remove', { tag:node, index:nodeIdx, data:tagData })
})
.filter(n=>n) // remove empty items in the mapped array
}, 20) // Firefox needs this higher duration for some reason or things get buggy when deleting text from the end
break;
}
// currently commented to allow new lines in mixed-mode
// case 'Enter' :
// e.preventDefault(); // solves Chrome bug - http://stackoverflow.com/a/20398191/104380
}
return true
}
switch( e.key ){
case 'Backspace' :
if( !this.state.dropdown.visible || this.settings.dropdown.position == 'manual' ){
if( s == "" || s.charCodeAt(0) == 8203 ){ // 8203: ZERO WIDTH SPACE unicode
if( this.settings.backspace === true )
this.removeTags();
else if( this.settings.backspace == 'edit' )
setTimeout(this.editTag.bind(this), 0) // timeout reason: when edited tag gets focused and the caret is placed at the end, the last character gets deletec (because of backspace)
}
}
break;
case 'Esc' :
case 'Escape' :
if( this.state.dropdown.visible ) return
e.target.blur()
break;
case 'Down' :
case 'ArrowDown' :
// if( this.settings.mode == 'select' ) // issue #333
if( !this.state.dropdown.visible )
this.dropdown.show()
break;
case 'ArrowRight' : {
let tagData = this.state.inputSuggestion || this.state.ddItemData
if( tagData && this.settings.autoComplete.rightKey ){
this.addTags([tagData], true)
return;
}
break
}
case 'Tab' : {
let selectMode = this.settings.mode == 'select'
if(s && !selectMode) e.preventDefault()
else return true;
}
case 'Enter' :
if( this.state.dropdown.visible || e.keyCode == 229 ) return
e.preventDefault(); // solves Chrome bug - http://stackoverflow.com/a/20398191/104380
// because the main "keydown" event is bound before the dropdown events, this will fire first and will not *yet*
// know if an option was just selected from the dropdown menu. If an option was selected,
// the dropdown events should handle adding the tag
setTimeout(()=>{
if( this.state.actions.selectOption )
return
this.addTags(s, true)
})
}
},
onInput(e){
if( this.settings.mode == 'mix' )
return this.events.callbacks.onMixTagsInput.call(this, e);
var value = this.input.normalize.call(this),
showSuggestions = value.length >= this.settings.dropdown.enabled,
eventData = {value, inputElm:this.DOM.input};
eventData.isValid = this.validateTag({value});
// for IE; since IE doesn't have an "input" event so "keyDown" is used instead to trigger the "onInput" callback,
// and so many keys do not change the input, and for those do not continue.
if( this.state.inputText == value ) return;
// save the value on the input's State object
this.input.set.call(this, value, false); // update the input with the normalized value and run validations
// this.setRangeAtStartEnd(); // fix caret position
if( value.search(this.settings.delimiters) != -1 ){
if( this.addTags( value ) ){
this.input.set.call(this); // clear the input field's value
}
}
else if( this.settings.dropdown.enabled >= 0 ){
this.dropdown[showSuggestions ? "show" : "hide"](value);
}
this.trigger('input', eventData) // "input" event must be triggered at this point, before the dropdown is shown
},
onMixTagsInput( e ){
var range, rangeText, match, matchedPatternCount, tag, showSuggestions, selection,
_s = this.settings,
lastTagsCount = this.value.length,
matchFlaggedTag,
matchDelimiters,
tagsElems = this.getTagElms(),
fragment = document.createDocumentFragment(),
range = window.getSelection().getRangeAt(0),
remainingTagsValues = [].map.call(tagsElems, node => this.tagData(node).value);
// Android Chrome "keydown" event argument does not report the correct "key".
// this workaround is needed to manually call "onKeydown" method with a synthesized event object
if( e.inputType == "deleteContentBackward" && isChromeAndroidBrowser() ){
this.events.callbacks.onKeydown.call(this, {
target: e.target,
key: "Backspace",
})
}
// re-add "readonly" tags which might have been removed
this.value.slice().forEach(item => {
if( item.readonly && !remainingTagsValues.includes(item.value) )
fragment.appendChild( this.createTagElem(item) )
})
if( fragment.childNodes.length ){
range.insertNode(fragment)
this.setRangeAtStartEnd(false, fragment.lastChild)
}
// check if tags were "magically" added/removed (browser redo/undo or CTRL-A -> delete)
if( tagsElems.length != lastTagsCount ){
this.value = [].map.call(this.getTagElms(), node => this.tagData(node))
this.update({ withoutChangeEvent:true })
return
}
if( this.hasMaxTags() )
return true
if( window.getSelection ){
selection = window.getSelection()
// only detect tags if selection is inside a textNode (not somehow on already-existing tag)
if( selection.rangeCount > 0 && selection.anchorNode.nodeType == 3 ){
range = selection.getRangeAt(0).cloneRange()
range.collapse(true)
range.setStart(selection.focusNode, 0)
rangeText = range.toString().slice(0, range.endOffset) // slice the range so everything AFTER the caret will be trimmed
// split = range.toString().split(_s.mixTagsAllowedAfter) // ["foo", "bar", "@baz"]
matchedPatternCount = rangeText.split(_s.pattern).length - 1;
match = rangeText.match( _s.pattern )
if( match )
// tag string, example: "@aaa ccc"
tag = rangeText.slice( rangeText.lastIndexOf(match[match.length-1]) )
if( tag ){
this.state.actions.ArrowLeft = false // start fresh, assuming the user did not (yet) used any arrow to move the caret
this.state.tag = {
prefix : tag.match(_s.pattern)[0],
value : tag.replace(_s.pattern, ''), // get rid of the prefix
}
this.state.tag.baseOffset = selection.baseOffset - this.state.tag.value.length
matchDelimiters = this.state.tag.value.match(_s.delimiters)
// if a delimeter exists, add the value as tag (exluding the delimiter)
if( matchDelimiters ){
this.state.tag.value = this.state.tag.value.replace(_s.delimiters, '')
this.state.tag.delimiters = matchDelimiters[0]
this.addTags(this.state.tag.value, _s.dropdown.clearOnSelect)
this.dropdown.hide()
return
}
showSuggestions = this.state.tag.value.length >= _s.dropdown.enabled
// When writeing something that might look like a tag (an email address) but isn't one - it is unwanted
// the suggestions dropdown be shown, so the user closes it (in any way), and while continue typing,
// dropdown should stay closed until another tag is typed.
// if( this.state.tag.value.length && this.state.dropdown.visible === false )
// showSuggestions = false
// test for similar flagged tags to the current tag
try{
matchFlaggedTag = this.state.flaggedTags[this.state.tag.baseOffset]
matchFlaggedTag = matchFlaggedTag.prefix == this.state.tag.prefix &&
matchFlaggedTag.value[0] == this.state.tag.value[0]
// reset
if( this.state.flaggedTags[this.state.tag.baseOffset] && !this.state.tag.value )
delete this.state.flaggedTags[this.state.tag.baseOffset];
}
catch(err){}
// scenario: (do not show suggestions of previous matched tag, if more than 1 detected)
// (2 tags exist) " a@a.com and @"
// (second tag is removed by backspace) " a@a.com and "
if( matchFlaggedTag || matchedPatternCount < this.state.mixMode.matchedPatternCount )
showSuggestions = false
}
// no (potential) tag found
else{
this.state.flaggedTags = {}
}
this.state.mixMode.matchedPatternCount = matchedPatternCount
}
}
// wait until the "this.value" has been updated (see "onKeydown" method for "mix-mode")
// the dropdown must be shown only after this event has been driggered, so an implementer could
// dynamically change the whitelist.
setTimeout(()=>{
this.update({withoutChangeEvent:true})
this.trigger("input", extend({}, this.state.tag, {textContent:this.DOM.input.textContent}))
if( this.state.tag )
this.dropdown[showSuggestions ? "show" : "hide"](this.state.tag.value);
}, 10)
},
onInputIE(e){
var _this = this;
// for the "e.target.textContent" to be changed, the browser requires a small delay
setTimeout(function(){
_this.events.callbacks.onInput.call(_this, e)
})
},
onClickScope(e){
var _s = this.settings,
tagElm = e.target.closest('.' + _s.classNames.tag),
timeDiffFocus = +new Date() - this.state.hasFocus;
if( e.target == this.DOM.scope ){
if( !this.state.hasFocus )
this.DOM.input.focus()
return
}
else if( e.target.classList.contains(_s.classNames.tagX) ){
this.removeTags( e.target.parentNode )
return
}
else if( tagElm ){
this.trigger("click", { tag:tagElm, index:this.getNodeIndex(tagElm), data:this.tagData(tagElm), originalEvent:this.cloneEvent(e) })
if( _s.editTags === 1 || _s.editTags.clicks === 1 )
this.events.callbacks.onDoubleClickScope.call(this, e)
return
}
// when clicking on the input itself
else if( e.target == this.DOM.input ){
if( _s.mode == 'mix' ){
// firefox won't show caret if last element is a tag (and not a textNode),
// so an empty textnode should be added
this.fixFirefoxLastTagNoCaret()
}
if( timeDiffFocus > 500 ){
if( this.state.dropdown.visible )
this.dropdown.hide()
else if( _s.dropdown.enabled === 0 && _s.mode != 'mix' )
this.dropdown.show()
return
}
}
if( _s.mode == 'select' )
!this.state.dropdown.visible && this.dropdown.show();
},
// special proccess is needed for pasted content in order to "clean" it
onPaste(e){
var clipboardData, pastedText;
e.preventDefault()
if( this.settings.readonly ) return
// Get pasted data via clipboard API
clipboardData = e.clipboardData || window.clipboardData
pastedText = clipboardData.getData('Text')
this.settings.hooks.beforePaste(e, {tagify:this, pastedText, clipboardData})
.then(result => {
if( result === undefined )
result = pastedText;
if( result ){
this.injectAtCaret(result, window.getSelection().getRangeAt(0))
if( this.settings.mode == 'mix' ){
this.events.callbacks.onMixTagsInput.call(this, e);
}
else if( this.settings.pasteAsTags )
this.addTags(result, true)
else
this.state.inputText = result
}
})
.catch(err => err)
},
onEditTagInput( editableElm, e ){
var tagElm = editableElm.closest('.' + this.settings.classNames.tag),
tagElmIdx = this.getNodeIndex(tagElm),
tagData = this.tagData(tagElm),
value = this.input.normalize.call(this, editableElm),
hasChanged = tagElm.innerHTML != tagElm.__tagifyTagData.__originalHTML,
isValid = this.validateTag({[this.settings.tagTextProp]:value}); // the value could have been invalid in the first-place so make sure to re-validate it (via "addEmptyTag" method)
// if the value is same as before-editing and the tag was valid before as well, ignore the current "isValid" result, which is false-positive
if( !hasChanged && editableElm.originalIsValid === true )
isValid = true
tagElm.classList.toggle(this.settings.classNames.tagInvalid, isValid !== true)
tagData.__isValid = isValid
tagElm.title = isValid === true
? tagData.title || tagData.value
: isValid // change the tag's title to indicate why is the tag invalid (if it's so)
// show dropdown if typed text is equal or more than the "enabled" dropdown setting
if( value.length >= this.settings.dropdown.enabled ){
// this check is needed apparently because doing browser "undo" will fire
// "onEditTagInput" but "this.state.editing" will be "false"
if( this.state.editing )
this.state.editing.value = value
this.dropdown.show(value)
}
this.trigger("edit:input", {
tag : tagElm,
index : tagElmIdx,
data : extend({}, this.value[tagElmIdx], {newValue:value}),
originalEvent: this.cloneEvent(e)
})
},
onEditTagFocus( tagElm ){
this.state.editing = {
scope: tagElm,
input: tagElm.querySelector("[contenteditable]")
}
},
onEditTagBlur( editableElm ){
if( !this.state.hasFocus )
this.toggleFocusClass()
// one scenario is when selecting a suggestion from the dropdown, when editing, and by selecting it
// the "onEditTagDone" is called directly, already replacing the tag, so the argument "editableElm"
// node isn't in the DOM anynmore because it has been replaced.
if( !this.DOM.scope.contains(editableElm) ) return;
var _s = this.settings,
tagElm = editableElm.closest('.' + _s.classNames.tag),
textValue = this.input.normalize.call(this, editableElm),
originalData = this.tagData(tagElm).__originalData, // pre-edit data
hasChanged = tagElm.innerHTML != tagElm.__tagifyTagData.__originalHTML,
isValid = this.validateTag({[_s.tagTextProp]:textValue}),
hasMaxTags,
newTagData;
// this.DOM.input.focus()
if( !textValue ){
this.onEditTagDone(tagElm)
return
}
// if nothing changed revert back to how it was before editing
if( !hasChanged ){
this.onEditTagDone(tagElm, originalData)
return
}
hasMaxTags = this.hasMaxTags()
newTagData = this.getWhitelistItem(textValue) || extend(
{},
originalData,
{
[_s.tagTextProp]:textValue,
value:textValue,
__isValid:isValid
}
)
_s.transformTag.call(this, newTagData, originalData)
// MUST re-validate after tag transformation
// only validate the "tagTextProp" because is the only thing that metters for validating an edited tag.
// -- Scenarios: --
// 1. max 3 tags allowd. there are 4 tags, one has invalid input and is edited to a valid one, and now should be marked as "not allowed" because limit of tags has reached
// 2. max 3 tags allowed. there are 3 tags, one is edited, and so max-tags vaildation should be OK
isValid = !hasMaxTags && this.validateTag({[_s.tagTextProp]:newTagData[_s.tagTextProp]})
if( isValid !== true ){
this.trigger("invalid", { data:newTagData, tag:tagElm, message:isValid })
// do nothing if invalid, stay in edit-mode until corrected or reverted by presssing esc
if( _s.editTags.keepInvalid ) return
if( _s.keepInvalidTags )
newTagData.__isValid = isValid
else
// revert back if not specified to keep
newTagData = originalData
}
else if( _s.keepInvalidTags ){
// cleaup any previous leftovers if the tag was
delete newTagData.title
delete newTagData["aria-invalid"]
delete newTagData.class
}
// tagElm.classList.toggle(_s.classNames.tagInvalid, true)
this.onEditTagDone(tagElm, newTagData)
},
onEditTagkeydown(e, tagElm){
this.trigger("edit:keydown", {originalEvent:this.cloneEvent(e)})
switch( e.key ){
case 'Esc' :
case 'Escape' :
tagElm.innerHTML = tagElm.__tagifyTagData.__originalHTML
case 'Enter' :
case 'Tab' :
e.preventDefault()
e.target.blur()
}
},
onDoubleClickScope(e){
var tagElm = e.target.closest('.' + this.settings.classNames.tag),
_s = this.settings,
isEditingTag,
isReadyOnlyTag;
if( !tagElm ) return
isEditingTag = tagElm.classList.contains(this.settings.classNames.tagEditing)
isReadyOnlyTag = tagElm.hasAttribute('readonly')
if( _s.mode != 'select' && !_s.readonly && !isEditingTag && !isReadyOnlyTag && this.settings.editTags )
this.editTag(tagElm)
this.toggleFocusClass(true)
this.trigger('dblclick', { tag:tagElm, index:this.getNodeIndex(tagElm), data:this.tagData(tagElm) })
}
}
}