|
import { sameStr, removeCollectionProp, omit, isObject, parseHTML, removeTextChildNodes, escapeHTML, extend, concatWithoutDups, getUID, isNodeTag } from './parts/helpers' |
|
import DEFAULTS from './parts/defaults' |
|
import _dropdown, { initDropdown } from './parts/dropdown' |
|
import { getPersistedData, setPersistedData, clearPersistedData } from './parts/persist' |
|
import TEXTS from './parts/texts' |
|
import templates from './parts/templates' |
|
import EventDispatcher from './parts/EventDispatcher' |
|
import events, { triggerChangeEvent } from './parts/events' |
|
|
|
|
|
|
|
|
|
|
|
|
|
function Tagify( input, settings ){ |
|
if( !input ){ |
|
console.warn('Tagify:', 'input element not found', input) |
|
|
|
|
|
const mockInstance = new Proxy(this, { get(){ return () => mockInstance } }) |
|
return mockInstance |
|
} |
|
|
|
|
|
if( input.previousElementSibling && input.previousElementSibling.classList.contains('tagify') ){ |
|
console.warn('Tagify: ', 'input element is already Tagified', input) |
|
return this |
|
} |
|
|
|
extend(this, EventDispatcher(this)) |
|
this.isFirefox = typeof InstallTrigger !== 'undefined' |
|
this.isIE = window.document.documentMode; |
|
|
|
settings = settings || {}; |
|
this.getPersistedData = getPersistedData(settings.id) |
|
this.setPersistedData = setPersistedData(settings.id) |
|
this.clearPersistedData = clearPersistedData(settings.id) |
|
this.applySettings(input, settings) |
|
|
|
this.state = { |
|
inputText: '', |
|
editing : false, |
|
actions : {}, |
|
mixMode : {}, |
|
dropdown: {}, |
|
flaggedTags: {} |
|
} |
|
|
|
this.value = [] |
|
|
|
|
|
this.listeners = {} |
|
|
|
this.DOM = {} |
|
|
|
this.build(input) |
|
initDropdown.call(this) |
|
|
|
this.getCSSVars() |
|
this.loadOriginalValues() |
|
|
|
this.events.customBinding.call(this); |
|
this.events.binding.call(this) |
|
input.autofocus && this.DOM.input.focus() |
|
} |
|
|
|
Tagify.prototype = { |
|
_dropdown, |
|
|
|
customEventsList : ['change', 'add', 'remove', 'invalid', 'input', 'click', 'keydown', 'focus', 'blur', 'edit:input', 'edit:beforeUpdate', 'edit:updated', 'edit:start', 'edit:keydown', 'dropdown:show', 'dropdown:hide', 'dropdown:select', 'dropdown:updated', 'dropdown:noMatch', 'dropdown:scroll'], |
|
dataProps: ['__isValid', '__removed', '__originalData', '__originalHTML', '__tagId'], |
|
|
|
trim(text){ |
|
return this.settings.trim && text && typeof text == "string" ? text.trim() : text |
|
}, |
|
|
|
|
|
parseHTML, |
|
|
|
templates, |
|
|
|
parseTemplate(template, data){ |
|
template = this.settings.templates[template] || template; |
|
return this.parseHTML( template.apply(this, data) ) |
|
}, |
|
|
|
set whitelist( arr ){ |
|
const isArray = arr && Array.isArray(arr) |
|
this.settings.whitelist = isArray ? arr : [] |
|
this.setPersistedData(isArray ? arr : [], 'whitelist') |
|
}, |
|
|
|
get whitelist(){ |
|
return this.settings.whitelist |
|
}, |
|
|
|
applySettings( input, settings ){ |
|
DEFAULTS.templates = this.templates |
|
|
|
var _s = this.settings = extend({}, DEFAULTS, settings) |
|
|
|
_s.disabled = input.hasAttribute('disabled') |
|
_s.readonly = _s.readonly || input.hasAttribute('readonly') |
|
|
|
|
|
|
|
_s.placeholder = escapeHTML(input.getAttribute('placeholder') || _s.placeholder || "") |
|
_s.required = input.hasAttribute('required') |
|
|
|
for( let name in _s.classNames ) |
|
Object.defineProperty(_s.classNames, name + "Selector" , { |
|
get(){ return "."+this[name].split(" ")[0] } |
|
}) |
|
|
|
if( this.isIE ) |
|
_s.autoComplete = false; |
|
|
|
["whitelist", "blacklist"].forEach(name => { |
|
var attrVal = input.getAttribute('data-' + name) |
|
if( attrVal ){ |
|
attrVal = attrVal.split(_s.delimiters) |
|
if( attrVal instanceof Array ) |
|
_s[name] = attrVal |
|
} |
|
}) |
|
|
|
|
|
if( "autoComplete" in settings && !isObject(settings.autoComplete) ){ |
|
_s.autoComplete = DEFAULTS.autoComplete |
|
_s.autoComplete.enabled = settings.autoComplete |
|
} |
|
|
|
if( _s.mode == 'mix' ){ |
|
_s.autoComplete.rightKey = true |
|
_s.delimiters = settings.delimiters || null |
|
|
|
|
|
|
|
|
|
if( _s.tagTextProp && !_s.dropdown.searchKeys.includes(_s.tagTextProp) ) |
|
_s.dropdown.searchKeys.push(_s.tagTextProp) |
|
} |
|
|
|
if( input.pattern ) |
|
try { _s.pattern = new RegExp(input.pattern) } |
|
catch(e){} |
|
|
|
|
|
if( this.settings.delimiters ){ |
|
try { _s.delimiters = new RegExp(this.settings.delimiters, "g") } |
|
catch(e){} |
|
} |
|
|
|
if( _s.disabled ) |
|
_s.userInput = false; |
|
|
|
this.TEXTS = {...TEXTS, ...(_s.texts || {})} |
|
|
|
|
|
if( _s.mode == 'select' || !_s.userInput ) |
|
_s.dropdown.enabled = 0 |
|
|
|
_s.dropdown.appendTarget = settings.dropdown && settings.dropdown.appendTarget |
|
? settings.dropdown.appendTarget |
|
: document.body |
|
|
|
|
|
|
|
let persistedWhitelist = this.getPersistedData('whitelist'); |
|
|
|
if( Array.isArray(persistedWhitelist)) |
|
this.whitelist = Array.isArray(_s.whitelist) |
|
? concatWithoutDups(_s.whitelist, persistedWhitelist) |
|
: persistedWhitelist; |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
getAttributes( data ){ |
|
var attrs = this.getCustomAttributes(data), s = '', k; |
|
|
|
for( k in attrs ) |
|
s += " " + k + (data[k] !== undefined ? `="${attrs[k]}"` : ""); |
|
|
|
return s; |
|
}, |
|
|
|
|
|
|
|
|
|
getCustomAttributes( data ){ |
|
|
|
if( !isObject(data) ) |
|
return ''; |
|
|
|
var output = {}, propName; |
|
|
|
for( propName in data ){ |
|
if( propName.slice(0,2) != '__' && propName != 'class' && data.hasOwnProperty(propName) && data[propName] !== undefined ) |
|
output[propName] = escapeHTML(data[propName]) |
|
} |
|
return output |
|
}, |
|
|
|
setStateSelection(){ |
|
var selection = window.getSelection() |
|
|
|
|
|
var sel = { |
|
anchorOffset: selection.anchorOffset, |
|
anchorNode : selection.anchorNode, |
|
range : selection.getRangeAt && selection.rangeCount && selection.getRangeAt(0) |
|
} |
|
|
|
this.state.selection = sel |
|
return sel |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
getCaretGlobalPosition(){ |
|
const sel = document.getSelection() |
|
|
|
if( sel.rangeCount ){ |
|
const r = sel.getRangeAt(0) |
|
const node = r.startContainer |
|
const offset = r.startOffset |
|
let rect, r2; |
|
|
|
if (offset > 0) { |
|
r2 = document.createRange() |
|
r2.setStart(node, offset - 1) |
|
r2.setEnd(node, offset) |
|
rect = r2.getBoundingClientRect() |
|
return {left:rect.right, top:rect.top, bottom:rect.bottom} |
|
} |
|
|
|
if( node.getBoundingClientRect ) |
|
return node.getBoundingClientRect() |
|
} |
|
|
|
return {left:-9999, top:-9999} |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
getCSSVars(){ |
|
var compStyle = getComputedStyle(this.DOM.scope, null) |
|
|
|
const getProp = name => compStyle.getPropertyValue('--'+name) |
|
|
|
function seprateUnitFromValue(a){ |
|
if( !a ) return {} |
|
a = a.trim().split(' ')[0] |
|
var unit = a.split(/\d+/g).filter(n=>n).pop().trim(), |
|
value = +a.split(unit).filter(n=>n)[0].trim() |
|
return {value, unit} |
|
} |
|
|
|
this.CSSVars = { |
|
tagHideTransition: (({value, unit}) => unit=='s' ? value * 1000 : value)(seprateUnitFromValue(getProp('tag-hide-transition'))) |
|
} |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
build( input ){ |
|
var DOM = this.DOM; |
|
|
|
if( this.settings.mixMode.integrated ){ |
|
DOM.originalInput = null; |
|
DOM.scope = input; |
|
DOM.input = input; |
|
} |
|
|
|
else { |
|
DOM.originalInput = input |
|
DOM.scope = this.parseTemplate('wrapper', [input, this.settings]) |
|
DOM.input = DOM.scope.querySelector(this.settings.classNames.inputSelector) |
|
input.parentNode.insertBefore(DOM.scope, input) |
|
} |
|
}, |
|
|
|
|
|
|
|
|
|
destroy(){ |
|
this.events.unbindGlobal.call(this) |
|
this.DOM.scope.parentNode.removeChild(this.DOM.scope) |
|
this.dropdown.hide(true) |
|
clearTimeout(this.dropdownHide__bindEventsTimeout) |
|
}, |
|
|
|
|
|
|
|
|
|
loadOriginalValues( value ){ |
|
var lastChild, |
|
_s = this.settings |
|
|
|
|
|
|
|
this.state.blockChangeEvent = true |
|
|
|
if( value === undefined ){ |
|
const persistedOriginalValue = this.getPersistedData('value') |
|
|
|
|
|
|
|
if( persistedOriginalValue && !this.DOM.originalInput.value ) |
|
value = persistedOriginalValue |
|
else |
|
value = _s.mixMode.integrated ? this.DOM.input.textContent : this.DOM.originalInput.value |
|
} |
|
|
|
this.removeAllTags() |
|
|
|
if( value ){ |
|
if( _s.mode == 'mix' ){ |
|
this.parseMixTags(this.trim(value)) |
|
|
|
lastChild = this.DOM.input.lastChild; |
|
|
|
if( !lastChild || lastChild.tagName != 'BR' ) |
|
this.DOM.input.insertAdjacentHTML('beforeend', '<br>') |
|
} |
|
|
|
else{ |
|
try{ |
|
if( JSON.parse(value) instanceof Array ) |
|
value = JSON.parse(value) |
|
} |
|
catch(err){} |
|
this.addTags(value).forEach(tag => tag && tag.classList.add(_s.classNames.tagNoAnimation)) |
|
} |
|
} |
|
|
|
else |
|
this.postUpdate() |
|
|
|
this.state.lastOriginalValueReported = _s.mixMode.integrated ? '' : this.DOM.originalInput.value |
|
this.state.blockChangeEvent = false |
|
}, |
|
|
|
cloneEvent(e){ |
|
var clonedEvent = {} |
|
for( var v in e ) |
|
clonedEvent[v] = e[v] |
|
return clonedEvent |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
loading( isLoading ){ |
|
this.state.isLoading = isLoading |
|
|
|
this.DOM.scope.classList[isLoading?"add":"remove"](this.settings.classNames.scopeLoading) |
|
return this |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
tagLoading( tagElm, isLoading ){ |
|
if( tagElm ) |
|
|
|
tagElm.classList[isLoading?"add":"remove"](this.settings.classNames.tagLoading) |
|
return this |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
toggleClass( className, force ){ |
|
if( typeof className == 'string' ) |
|
this.DOM.scope.classList.toggle(className, force) |
|
}, |
|
|
|
toggleFocusClass( force ){ |
|
this.toggleClass(this.settings.classNames.focus, !!force) |
|
}, |
|
|
|
triggerChangeEvent, |
|
|
|
events, |
|
|
|
fixFirefoxLastTagNoCaret(){ |
|
return |
|
var inputElm = this.DOM.input |
|
|
|
if( this.isFirefox && inputElm.childNodes.length && inputElm.lastChild.nodeType == 1 ){ |
|
inputElm.appendChild(document.createTextNode("\u200b")) |
|
this.setRangeAtStartEnd(true) |
|
return true |
|
} |
|
}, |
|
|
|
placeCaretAfterNode( node ){ |
|
if( !node || !node.parentNode ) return |
|
|
|
var nextSibling = node.nextSibling, |
|
sel = window.getSelection(), |
|
range = sel.getRangeAt(0); |
|
|
|
if (sel.rangeCount) { |
|
range.setStartAfter(nextSibling || node); |
|
range.collapse(true) |
|
|
|
sel.removeAllRanges(); |
|
sel.addRange(range); |
|
} |
|
}, |
|
|
|
insertAfterTag( tagElm, newNode ){ |
|
newNode = newNode || this.settings.mixMode.insertAfterTag; |
|
|
|
if( !tagElm || !tagElm.parentNode || !newNode ) return |
|
|
|
newNode = typeof newNode == 'string' |
|
? document.createTextNode(newNode) |
|
: newNode |
|
|
|
tagElm.parentNode.insertBefore(newNode, tagElm.nextSibling) |
|
return newNode |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
editTag( tagElm, opts ){ |
|
tagElm = tagElm || this.getLastTag() |
|
opts = opts || {} |
|
|
|
this.dropdown.hide() |
|
|
|
var _s = this.settings; |
|
|
|
function getEditableElm(){ |
|
return tagElm.querySelector(_s.classNames.tagTextSelector) |
|
} |
|
|
|
var editableElm = getEditableElm(), |
|
tagIdx = this.getNodeIndex(tagElm), |
|
tagData = this.tagData(tagElm), |
|
_CB = this.events.callbacks, |
|
that = this, |
|
isValid = true, |
|
delayed_onEditTagBlur = function(){ |
|
setTimeout(() => _CB.onEditTagBlur.call(that, getEditableElm())) |
|
} |
|
|
|
if( !editableElm ){ |
|
console.warn('Cannot find element in Tag template: .', _s.classNames.tagTextSelector); |
|
return; |
|
} |
|
|
|
if( tagData instanceof Object && "editable" in tagData && !tagData.editable ) |
|
return |
|
|
|
editableElm.setAttribute('contenteditable', true) |
|
tagElm.classList.add( _s.classNames.tagEditing ) |
|
|
|
|
|
this.tagData(tagElm, { |
|
__originalData: extend({}, tagData), |
|
__originalHTML: tagElm.innerHTML |
|
}) |
|
|
|
editableElm.addEventListener('focus', _CB.onEditTagFocus.bind(this, tagElm)) |
|
editableElm.addEventListener('blur', delayed_onEditTagBlur) |
|
editableElm.addEventListener('input', _CB.onEditTagInput.bind(this, editableElm)) |
|
editableElm.addEventListener('keydown', e => _CB.onEditTagkeydown.call(this, e, tagElm)) |
|
|
|
editableElm.focus() |
|
this.setRangeAtStartEnd(false, editableElm) |
|
|
|
if( !opts.skipValidation ) |
|
isValid = this.editTagToggleValidity(tagElm) |
|
|
|
editableElm.originalIsValid = isValid |
|
|
|
this.trigger("edit:start", { tag:tagElm, index:tagIdx, data:tagData, isValid }) |
|
|
|
return this |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
editTagToggleValidity( tagElm, tagData ){ |
|
var tagData = tagData || this.tagData(tagElm), |
|
isValid; |
|
|
|
if( !tagData ){ |
|
console.warn("tag has no data: ", tagElm, tagData) |
|
return; |
|
} |
|
|
|
isValid = !("__isValid" in tagData) || tagData.__isValid === true |
|
|
|
if( !isValid ){ |
|
this.removeTagsFromValue(tagElm) |
|
} |
|
|
|
this.update() |
|
|
|
|
|
|
|
tagElm.classList.toggle(this.settings.classNames.tagNotAllowed, !isValid) |
|
return tagData.__isValid |
|
}, |
|
|
|
onEditTagDone(tagElm, tagData){ |
|
tagElm = tagElm || this.state.editing.scope |
|
tagData = tagData || {} |
|
|
|
var eventData = { |
|
tag : tagElm, |
|
index : this.getNodeIndex(tagElm), |
|
previousData: this.tagData(tagElm), |
|
data : tagData |
|
} |
|
|
|
this.trigger("edit:beforeUpdate", eventData, {cloneData:false}) |
|
|
|
this.state.editing = false; |
|
|
|
delete tagData.__originalData |
|
delete tagData.__originalHTML |
|
|
|
if( tagElm && tagData[this.settings.tagTextProp] ){ |
|
tagElm = this.replaceTag(tagElm, tagData) |
|
this.editTagToggleValidity(tagElm, tagData) |
|
|
|
if( this.settings.a11y.focusableTags ) |
|
tagElm.focus() |
|
else |
|
|
|
this.placeCaretAfterNode(tagElm.previousSibling) |
|
} |
|
|
|
else if(tagElm) |
|
this.removeTags(tagElm) |
|
|
|
this.trigger("edit:updated", eventData) |
|
this.dropdown.hide() |
|
|
|
|
|
if( this.settings.keepInvalidTags ) |
|
this.reCheckInvalidTags() |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
replaceTag(tagElm, tagData){ |
|
if( !tagData || !tagData.value ) |
|
tagData = tagElm.__tagifyTagData |
|
|
|
|
|
if( tagData.__isValid && tagData.__isValid != true ) |
|
extend( tagData, this.getInvalidTagAttrs(tagData, tagData.__isValid) ) |
|
|
|
var newTagElm = this.createTagElem(tagData) |
|
|
|
|
|
tagElm.parentNode.replaceChild(newTagElm, tagElm) |
|
this.updateValueByDOMTags() |
|
return newTagElm |
|
}, |
|
|
|
|
|
|
|
|
|
updateValueByDOMTags(){ |
|
this.value.length = 0; |
|
|
|
[].forEach.call(this.getTagElms(), node => { |
|
if( node.classList.contains(this.settings.classNames.tagNotAllowed.split(' ')[0]) ) return |
|
this.value.push( this.tagData(node) ) |
|
}) |
|
|
|
this.update() |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
setRangeAtStartEnd( start, node ){ |
|
start = typeof start == 'number' ? start : !!start |
|
node = node || this.DOM.input; |
|
node = node.lastChild || node; |
|
var sel = document.getSelection() |
|
|
|
try{ |
|
if( sel.rangeCount >= 1 ){ |
|
['Start', 'End'].forEach(pos => |
|
sel.getRangeAt(0)["set" + pos](node, start ? start : node.length) |
|
) |
|
} |
|
} catch(err){ |
|
|
|
} |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
injectAtCaret( injectedNode, range ){ |
|
range = range || this.state.selection.range |
|
|
|
if( !range ) return; |
|
|
|
if( typeof injectedNode == 'string' ) |
|
injectedNode = document.createTextNode(injectedNode); |
|
|
|
range.deleteContents() |
|
|
|
range.insertNode(injectedNode) |
|
|
|
this.setRangeAtStartEnd(false, injectedNode) |
|
|
|
this.updateValueByDOMTags() |
|
this.update() |
|
|
|
return this |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
input : { |
|
set( s = '', updateDOM = true ){ |
|
var hideDropdown = this.settings.dropdown.closeOnSelect |
|
this.state.inputText = s |
|
|
|
if( updateDOM ) |
|
this.DOM.input.innerHTML = escapeHTML(""+s); |
|
|
|
if( !s && hideDropdown ) |
|
this.dropdown.hide.bind(this) |
|
|
|
this.input.autocomplete.suggest.call(this); |
|
this.input.validate.call(this); |
|
}, |
|
|
|
raw(){ |
|
return this.DOM.input.textContent |
|
}, |
|
|
|
|
|
|
|
|
|
validate(){ |
|
var isValid = !this.state.inputText || this.validateTag({value:this.state.inputText}) === true; |
|
|
|
this.DOM.input.classList.toggle(this.settings.classNames.inputInvalid, !isValid) |
|
|
|
return isValid |
|
}, |
|
|
|
|
|
normalize( node ){ |
|
var clone = node || this.DOM.input, |
|
v = []; |
|
|
|
|
|
|
|
clone.childNodes.forEach(n => n.nodeType==3 && v.push(n.nodeValue)) |
|
v = v.join("\n") |
|
|
|
try{ |
|
|
|
v = v.replace(/(?:\r\n|\r|\n)/g, this.settings.delimiters.source.charAt(0)) |
|
} |
|
catch(err){} |
|
|
|
v = v.replace(/\s/g, ' ') |
|
|
|
if( this.settings.trim ) |
|
v = v.replace(/^\s+/, '') |
|
|
|
return v |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
autocomplete : { |
|
suggest( data ){ |
|
if( !this.settings.autoComplete.enabled ) return; |
|
|
|
data = data || {} |
|
|
|
if( typeof data == 'string' ) |
|
data = {value:data} |
|
|
|
var suggestedText = data.value ? ''+data.value : '', |
|
suggestionStart = suggestedText.substr(0, this.state.inputText.length).toLowerCase(), |
|
suggestionTrimmed = suggestedText.substring(this.state.inputText.length); |
|
|
|
if( !suggestedText || !this.state.inputText || suggestionStart != this.state.inputText.toLowerCase() ){ |
|
this.DOM.input.removeAttribute("data-suggest"); |
|
delete this.state.inputSuggestion |
|
} |
|
else{ |
|
this.DOM.input.setAttribute("data-suggest", suggestionTrimmed); |
|
this.state.inputSuggestion = data |
|
} |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
set( s ){ |
|
var dataSuggest = this.DOM.input.getAttribute('data-suggest'), |
|
suggestion = s || (dataSuggest ? this.state.inputText + dataSuggest : null); |
|
|
|
if( suggestion ){ |
|
if( this.settings.mode == 'mix' ){ |
|
this.replaceTextWithNode( document.createTextNode(this.state.tag.prefix + suggestion) ) |
|
} |
|
else{ |
|
this.input.set.call(this, suggestion); |
|
this.setRangeAtStartEnd() |
|
} |
|
|
|
this.input.autocomplete.suggest.call(this); |
|
this.dropdown.hide(); |
|
|
|
return true; |
|
} |
|
|
|
return false; |
|
} |
|
} |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
getTagIdx( tagData ){ |
|
return this.value.findIndex(item => item.__tagId == (tagData||{}).__tagId ) |
|
}, |
|
|
|
getNodeIndex( node ){ |
|
var index = 0; |
|
|
|
if( node ) |
|
while( (node = node.previousElementSibling) ) |
|
index++; |
|
|
|
return index; |
|
}, |
|
|
|
getTagElms( ...classess ){ |
|
var classname = '.' + [...this.settings.classNames.tag.split(' '), ...classess].join('.') |
|
return [].slice.call(this.DOM.scope.querySelectorAll(classname)) |
|
}, |
|
|
|
|
|
|
|
|
|
getLastTag(){ |
|
var lastTag = this.DOM.scope.querySelectorAll(`${this.settings.classNames.tagSelector}:not(.${this.settings.classNames.tagHide}):not([readonly])`); |
|
return lastTag[lastTag.length - 1]; |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
tagData(tagElm, data, override){ |
|
if( !tagElm ){ |
|
console.warn("tag element doesn't exist",tagElm, data) |
|
return data |
|
} |
|
|
|
if( data ) |
|
tagElm.__tagifyTagData = override |
|
? data |
|
: extend({}, tagElm.__tagifyTagData || {}, data) |
|
|
|
return tagElm.__tagifyTagData |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
isTagDuplicate( value, caseSensitive ){ |
|
var dupsCount, |
|
_s = this.settings; |
|
|
|
|
|
if( _s.mode == 'select' ) |
|
return false |
|
|
|
dupsCount = this.value.reduce((acc, item) => ( |
|
sameStr( this.trim(""+value), item.value, caseSensitive || _s.dropdown.caseSensitive ) |
|
? acc+1 |
|
: acc |
|
), 0) |
|
|
|
return dupsCount |
|
}, |
|
|
|
getTagIndexByValue( value ){ |
|
var indices = []; |
|
|
|
this.getTagElms().forEach((tagElm, i) => { |
|
if( sameStr( this.trim(tagElm.textContent), value, this.settings.dropdown.caseSensitive ) ) |
|
indices.push(i) |
|
}) |
|
|
|
return indices; |
|
}, |
|
|
|
getTagElmByValue( value ){ |
|
var tagIdx = this.getTagIndexByValue(value)[0] |
|
return this.getTagElms()[tagIdx] |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
flashTag( tagElm ){ |
|
if( tagElm ){ |
|
tagElm.classList.add(this.settings.classNames.tagFlash) |
|
setTimeout(() => { tagElm.classList.remove(this.settings.classNames.tagFlash) }, 100) |
|
} |
|
}, |
|
|
|
|
|
|
|
|
|
isTagBlacklisted( v ){ |
|
v = this.trim(v.toLowerCase()); |
|
return this.settings.blacklist.filter(x => (""+x).toLowerCase() == v).length; |
|
}, |
|
|
|
|
|
|
|
|
|
isTagWhitelisted( v ){ |
|
return !!this.getWhitelistItem(v) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
getWhitelistItem( value, prop, whitelist ){ |
|
var result, |
|
prop = prop || 'value', |
|
_s = this.settings, |
|
whitelist = whitelist || _s.whitelist; |
|
|
|
whitelist.some(_wi => { |
|
var _wiv = typeof _wi == 'string' ? _wi : (_wi[prop] || _wi.value), |
|
isSameStr = sameStr(_wiv, value, _s.dropdown.caseSensitive, _s.trim) |
|
|
|
if( isSameStr ){ |
|
result = typeof _wi == 'string' ? {value:_wi} : _wi |
|
return true |
|
} |
|
}) |
|
|
|
|
|
|
|
if( !result && prop == 'value' && _s.tagTextProp != 'value' ){ |
|
|
|
result = this.getWhitelistItem(value, _s.tagTextProp, whitelist) |
|
} |
|
|
|
return result |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
validateTag( tagData ){ |
|
var _s = this.settings, |
|
|
|
prop = "value" in tagData ? "value" : _s.tagTextProp, |
|
v = this.trim(tagData[prop] + ""); |
|
|
|
|
|
if( !(tagData[prop]+"").trim() ) |
|
return this.TEXTS.empty; |
|
|
|
|
|
if( _s.pattern && _s.pattern instanceof RegExp && !(_s.pattern.test(v)) ) |
|
return this.TEXTS.pattern; |
|
|
|
|
|
if( !_s.duplicates && this.isTagDuplicate(v, this.state.editing) ) |
|
return this.TEXTS.duplicate; |
|
|
|
if( this.isTagBlacklisted(v) || (_s.enforceWhitelist && !this.isTagWhitelisted(v)) ) |
|
return this.TEXTS.notAllowed; |
|
|
|
if( _s.validate ) |
|
return _s.validate(tagData) |
|
|
|
return true |
|
}, |
|
|
|
getInvalidTagAttrs(tagData, validation){ |
|
return { |
|
"aria-invalid" : true, |
|
"class": `${tagData.class || ''} ${this.settings.classNames.tagNotAllowed}`.trim(), |
|
"title": validation |
|
} |
|
}, |
|
|
|
hasMaxTags(){ |
|
return this.value.length >= this.settings.maxTags |
|
? this.TEXTS.exceed |
|
: false |
|
}, |
|
|
|
setReadonly( toggle, attrribute ){ |
|
var _s = this.settings |
|
|
|
document.activeElement.blur() |
|
_s[attrribute || 'readonly'] = toggle |
|
this.DOM.scope[(toggle ? 'set' : 'remove') + 'Attribute'](attrribute || 'readonly', true) |
|
|
|
if( _s.mode == 'mix' ){ |
|
this.setContentEditable(!toggle) |
|
} |
|
}, |
|
|
|
setContentEditable(state){ |
|
if( !this.settings.readonly && this.settings.userInput ) |
|
this.DOM.input.contentEditable = state |
|
}, |
|
|
|
setDisabled( isDisabled ){ |
|
this.setReadonly(isDisabled, 'disabled') |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
normalizeTags( tagsItems ){ |
|
var {whitelist, delimiters, mode, tagTextProp, enforceWhitelist} = this.settings, |
|
whitelistMatches = [], |
|
whitelistWithProps = whitelist ? whitelist[0] instanceof Object : false, |
|
|
|
isArray = tagsItems instanceof Array, |
|
mapStringToCollection = s => (s+"").split(delimiters).filter(n => n).map(v => ({ [tagTextProp]:this.trim(v), value:this.trim(v) })) |
|
|
|
if( typeof tagsItems == 'number' ) |
|
tagsItems = tagsItems.toString() |
|
|
|
|
|
if( typeof tagsItems == 'string' ){ |
|
if( !tagsItems.trim() ) return []; |
|
|
|
|
|
tagsItems = mapStringToCollection(tagsItems) |
|
} |
|
|
|
|
|
else if( isArray ){ |
|
|
|
tagsItems = [].concat(...tagsItems.map(item => item.value |
|
? item |
|
: mapStringToCollection(item) |
|
)) |
|
} |
|
|
|
|
|
|
|
if( whitelistWithProps ){ |
|
tagsItems.forEach(item => { |
|
var whitelistMatchesValues = whitelistMatches.map(a=>a.value) |
|
|
|
|
|
|
|
var filteredList = this.dropdown.filterListItems.call(this, item[tagTextProp], { exact:true }) |
|
|
|
|
|
if( !this.settings.duplicates ) |
|
|
|
filteredList = filteredList.filter(filteredItem => !whitelistMatchesValues.includes(filteredItem.value)) |
|
|
|
|
|
|
|
var matchObj = filteredList.length > 1 |
|
? this.getWhitelistItem(item[tagTextProp], tagTextProp, filteredList) |
|
: filteredList[0] |
|
|
|
if( matchObj && matchObj instanceof Object ){ |
|
whitelistMatches.push( matchObj ) |
|
} |
|
else if( mode != 'mix' ){ |
|
if( item.value == undefined ) |
|
item.value = item[tagTextProp] |
|
whitelistMatches.push(item) |
|
} |
|
}) |
|
|
|
if( whitelistMatches.length ) |
|
tagsItems = whitelistMatches |
|
} |
|
|
|
return tagsItems; |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
parseMixTags( s ){ |
|
var {mixTagsInterpolator, duplicates, transformTag, enforceWhitelist, maxTags, tagTextProp} = this.settings, |
|
tagsDataSet = []; |
|
|
|
s = s.split(mixTagsInterpolator[0]).map((s1, i) => { |
|
var s2 = s1.split(mixTagsInterpolator[1]), |
|
preInterpolated = s2[0], |
|
maxTagsReached = tagsDataSet.length == maxTags, |
|
textProp, |
|
tagData, |
|
tagElm; |
|
|
|
try{ |
|
|
|
if( preInterpolated == +preInterpolated ) |
|
throw Error |
|
tagData = JSON.parse(preInterpolated) |
|
} catch(err){ |
|
tagData = this.normalizeTags(preInterpolated)[0] || {value:preInterpolated} |
|
} |
|
|
|
|
|
transformTag.call(this, tagData) |
|
|
|
if( !maxTagsReached && |
|
s2.length > 1 && |
|
(!enforceWhitelist || this.isTagWhitelisted(tagData.value)) && |
|
!(!duplicates && this.isTagDuplicate(tagData.value)) ){ |
|
|
|
|
|
textProp = tagData[tagTextProp] ? tagTextProp : 'value' |
|
tagData[textProp] = this.trim(tagData[textProp]) |
|
|
|
tagElm = this.createTagElem(tagData) |
|
tagsDataSet.push( tagData ) |
|
tagElm.classList.add(this.settings.classNames.tagNoAnimation) |
|
|
|
s2[0] = tagElm.outerHTML |
|
this.value.push(tagData) |
|
} |
|
else if(s1) |
|
return i ? mixTagsInterpolator[0] + s1 : s1 |
|
|
|
return s2.join('') |
|
}).join('') |
|
|
|
this.DOM.input.innerHTML = s |
|
this.DOM.input.appendChild(document.createTextNode('')) |
|
this.DOM.input.normalize() |
|
this.getTagElms().forEach((elm, idx) => this.tagData(elm, tagsDataSet[idx])) |
|
this.update({withoutChangeEvent:true}) |
|
return s |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
replaceTextWithNode( newWrapperNode, strToReplace ){ |
|
if( !this.state.tag && !strToReplace ) return; |
|
|
|
strToReplace = strToReplace || this.state.tag.prefix + this.state.tag.value; |
|
var idx, nodeToReplace, |
|
selection = window.getSelection(), |
|
nodeAtCaret = selection.anchorNode, |
|
firstSplitOffset = this.state.tag.delimiters ? this.state.tag.delimiters.length : 0; |
|
|
|
|
|
|
|
|
|
|
|
nodeAtCaret.splitText(selection.anchorOffset - firstSplitOffset) |
|
|
|
|
|
|
|
|
|
|
|
idx = nodeAtCaret.nodeValue.lastIndexOf(strToReplace) |
|
|
|
if( idx == -1 ) return true; |
|
|
|
nodeToReplace = nodeAtCaret.splitText(idx) |
|
|
|
|
|
|
|
|
|
newWrapperNode && nodeAtCaret.parentNode.replaceChild(newWrapperNode, nodeToReplace) |
|
|
|
|
|
|
|
|
|
|
|
return true; |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
selectTag( tagElm, tagData ){ |
|
var _s = this.settings |
|
|
|
if( _s.enforceWhitelist && !this.isTagWhitelisted(tagData.value) ) |
|
return |
|
|
|
this.input.set.call(this, tagData[_s.tagTextProp] || tagData.value, true) |
|
|
|
|
|
if( this.state.actions.selectOption ) |
|
setTimeout(this.setRangeAtStartEnd.bind(this)) |
|
|
|
var lastTagElm = this.getLastTag() |
|
|
|
if( lastTagElm ) |
|
this.replaceTag(lastTagElm, tagData) |
|
else |
|
this.appendTag(tagElm) |
|
|
|
if( _s.enforceWhitelist ) |
|
this.setContentEditable(false); |
|
|
|
this.value[0] = tagData |
|
this.update() |
|
this.trigger('add', { tag:tagElm, data:tagData }) |
|
|
|
return [tagElm] |
|
}, |
|
|
|
|
|
|
|
|
|
addEmptyTag( initialData ){ |
|
var tagData = extend({ value:"" }, initialData || {}), |
|
tagElm = this.createTagElem(tagData) |
|
|
|
this.tagData(tagElm, tagData) |
|
|
|
|
|
this.appendTag(tagElm) |
|
this.editTag(tagElm, {skipValidation:true}) |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
addTags( tagsItems, clearInput, skipInvalid ){ |
|
var tagElems = [], |
|
_s = this.settings, |
|
frag = document.createDocumentFragment() |
|
|
|
skipInvalid = skipInvalid || _s.skipInvalid; |
|
|
|
if( !tagsItems || tagsItems.length == 0 ){ |
|
if( _s.mode == 'select' ) |
|
this.removeAllTags() |
|
return tagElems |
|
} |
|
|
|
|
|
tagsItems = this.normalizeTags(tagsItems) |
|
|
|
if( _s.mode == 'mix' ){ |
|
return this.addMixTags(tagsItems) |
|
} |
|
|
|
if( _s.mode == 'select' ) |
|
clearInput = false |
|
|
|
this.DOM.input.removeAttribute('style') |
|
|
|
tagsItems.forEach(tagData => { |
|
var tagElm, |
|
tagElmParams = {}, |
|
originalData = Object.assign({}, tagData, {value:tagData.value+""}); |
|
|
|
|
|
tagData = Object.assign({}, originalData) |
|
_s.transformTag.call(this, tagData) |
|
tagData.__isValid = this.hasMaxTags() || this.validateTag(tagData) |
|
|
|
if( tagData.__isValid !== true ){ |
|
if( skipInvalid ) |
|
return |
|
|
|
|
|
|
|
extend(tagElmParams, this.getInvalidTagAttrs(tagData, tagData.__isValid), {__preInvalidData:originalData}) |
|
|
|
if( tagData.__isValid == this.TEXTS.duplicate ) |
|
|
|
this.flashTag( this.getTagElmByValue(tagData.value) ) |
|
} |
|
|
|
|
|
if( 'readonly' in tagData ){ |
|
if( tagData.readonly ) |
|
tagElmParams["aria-readonly"] = true |
|
|
|
else |
|
delete tagData.readonly |
|
} |
|
|
|
|
|
tagElm = this.createTagElem(tagData, tagElmParams) |
|
tagElems.push(tagElm) |
|
|
|
|
|
if( _s.mode == 'select' ){ |
|
return this.selectTag(tagElm, tagData) |
|
} |
|
|
|
|
|
|
|
frag.appendChild(tagElm) |
|
|
|
if( tagData.__isValid && tagData.__isValid === true ){ |
|
|
|
this.value.push(tagData) |
|
this.trigger('add', {tag:tagElm, index:this.value.length - 1, data:tagData}) |
|
} |
|
else{ |
|
this.trigger("invalid", {data:tagData, index:this.value.length, tag:tagElm, message:tagData.__isValid}) |
|
if( !_s.keepInvalidTags ) |
|
|
|
setTimeout(() => this.removeTags(tagElm, true), 1000) |
|
} |
|
|
|
this.dropdown.position() |
|
}) |
|
|
|
this.appendTag(frag) |
|
this.update() |
|
|
|
if( tagsItems.length && clearInput ){ |
|
this.input.set.call(this) |
|
} |
|
|
|
this.dropdown.refilter() |
|
return tagElems |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
addMixTags( tagsData ){ |
|
tagsData = this.normalizeTags(tagsData); |
|
|
|
if( tagsData[0].prefix || this.state.tag ){ |
|
return this.prefixedTextToTag(tagsData[0]) |
|
} |
|
|
|
if( typeof tagsData == 'string' ) |
|
tagsData = [{ value:tagsData }] |
|
|
|
var selection = !!this.state.selection, |
|
frag = document.createDocumentFragment() |
|
|
|
tagsData.forEach(tagData => { |
|
var tagElm = this.createTagElem(tagData) |
|
frag.appendChild(tagElm) |
|
this.insertAfterTag(tagElm) |
|
}) |
|
|
|
|
|
|
|
if( selection ){ |
|
this.injectAtCaret(frag) |
|
} |
|
|
|
else{ |
|
this.DOM.input.focus() |
|
selection = this.setStateSelection() |
|
selection.range.setStart(this.DOM.input, selection.range.endOffset) |
|
selection.range.setEnd(this.DOM.input, selection.range.endOffset) |
|
this.DOM.input.appendChild(frag) |
|
|
|
this.updateValueByDOMTags() |
|
this.update() |
|
} |
|
|
|
return frag |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
prefixedTextToTag( tagItem ){ |
|
var _s = this.settings, |
|
tagElm, |
|
createdFromDelimiters = this.state.tag.delimiters; |
|
|
|
_s.transformTag.call(this, tagItem) |
|
|
|
tagItem.prefix = tagItem.prefix || this.state.tag ? this.state.tag.prefix : (_s.pattern.source||_s.pattern)[0]; |
|
|
|
|
|
tagElm = this.createTagElem(tagItem) |
|
|
|
|
|
|
|
if( !this.replaceTextWithNode(tagElm) ){ |
|
this.DOM.input.appendChild(tagElm) |
|
} |
|
|
|
setTimeout(()=> tagElm.classList.add(this.settings.classNames.tagNoAnimation), 300) |
|
|
|
this.value.push(tagItem) |
|
this.update() |
|
|
|
if( !createdFromDelimiters ) { |
|
var elm = this.insertAfterTag(tagElm) || tagElm; |
|
this.placeCaretAfterNode(elm) |
|
} |
|
|
|
this.state.tag = null |
|
this.trigger('add', extend({}, {tag:tagElm}, {data:tagItem})) |
|
|
|
return tagElm |
|
}, |
|
|
|
|
|
|
|
|
|
appendTag(tagElm){ |
|
var DOM = this.DOM, |
|
insertBeforeNode = DOM.scope.lastElementChild; |
|
|
|
if( insertBeforeNode === DOM.input ) |
|
DOM.scope.insertBefore(tagElm, insertBeforeNode) |
|
else |
|
DOM.scope.appendChild(tagElm) |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
createTagElem( tagData, extraData ){ |
|
tagData.__tagId = getUID() |
|
|
|
var tagElm, |
|
templateData = extend({}, tagData, { value:escapeHTML(tagData.value+""), ...extraData }); |
|
|
|
|
|
|
|
|
|
tagElm = this.parseTemplate('tag', [templateData]) |
|
|
|
|
|
|
|
removeTextChildNodes(tagElm) |
|
|
|
|
|
|
|
this.tagData(tagElm, tagData) |
|
return tagElm |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
reCheckInvalidTags(){ |
|
var _s = this.settings |
|
|
|
this.getTagElms(_s.classNames.tagNotAllowed).forEach((tagElm, i) => { |
|
var tagData = this.tagData(tagElm), |
|
hasMaxTags = this.hasMaxTags(), |
|
tagValidation = this.validateTag(tagData); |
|
|
|
|
|
if( tagValidation === true && !hasMaxTags ){ |
|
tagData = tagData.__preInvalidData |
|
? tagData.__preInvalidData |
|
: { value:tagData.value } |
|
|
|
return this.replaceTag(tagElm, tagData) |
|
} |
|
|
|
|
|
tagElm.title = hasMaxTags || tagValidation |
|
}) |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
removeTags( tagElms, silent, tranDuration ){ |
|
var tagsToRemove; |
|
|
|
tagElms = tagElms && tagElms instanceof HTMLElement |
|
? [tagElms] |
|
: tagElms instanceof Array |
|
? tagElms |
|
: tagElms |
|
? [tagElms] |
|
: [this.getLastTag()] |
|
|
|
|
|
|
|
|
|
|
|
|
|
tagsToRemove = tagElms.reduce((elms, tagElm) => { |
|
if( tagElm && typeof tagElm == 'string') |
|
tagElm = this.getTagElmByValue(tagElm) |
|
|
|
var tagData = this.tagData(tagElm); |
|
|
|
if( tagElm && tagData && !tagData.readonly ) |
|
|
|
|
|
elms.push({ |
|
node: tagElm, |
|
idx: this.getTagIdx(tagData), |
|
data: this.tagData(tagElm, {'__removed':true}) |
|
}) |
|
|
|
return elms |
|
}, []) |
|
|
|
tranDuration = typeof tranDuration == "number" ? tranDuration : this.CSSVars.tagHideTransition |
|
|
|
if( this.settings.mode == 'select' ){ |
|
tranDuration = 0; |
|
this.input.set.call(this) |
|
} |
|
|
|
|
|
if( tagsToRemove.length == 1 ){ |
|
if( tagsToRemove[0].node.classList.contains(this.settings.classNames.tagNotAllowed) ) |
|
silent = true |
|
} |
|
|
|
if( !tagsToRemove.length ) |
|
return; |
|
|
|
return this.settings.hooks.beforeRemoveTag(tagsToRemove, {tagify:this}) |
|
.then(() => { |
|
function removeNode( tag ){ |
|
if( !tag.node.parentNode ) return |
|
|
|
tag.node.parentNode.removeChild(tag.node) |
|
|
|
if( !silent ){ |
|
|
|
this.trigger('remove', { tag:tag.node, index:tag.idx, data:tag.data }) |
|
this.dropdown.refilter() |
|
this.dropdown.position() |
|
this.DOM.input.normalize() |
|
|
|
|
|
if( this.settings.keepInvalidTags ) |
|
this.reCheckInvalidTags() |
|
} |
|
else if( this.settings.keepInvalidTags ) |
|
this.trigger('remove', { tag:tag.node, index:tag.idx }) |
|
} |
|
|
|
function animation( tag ){ |
|
tag.node.style.width = parseFloat(window.getComputedStyle(tag.node).width) + 'px' |
|
document.body.clientTop |
|
tag.node.classList.add(this.settings.classNames.tagHide) |
|
|
|
|
|
setTimeout(removeNode.bind(this), tranDuration, tag) |
|
} |
|
|
|
if( tranDuration && tranDuration > 10 && tagsToRemove.length == 1 ) |
|
animation.call(this, tagsToRemove[0]) |
|
else |
|
tagsToRemove.forEach(removeNode.bind(this)) |
|
|
|
|
|
if( !silent ){ |
|
this.removeTagsFromValue(tagsToRemove.map(tag => tag.node)) |
|
this.update() |
|
|
|
if( this.settings.mode == 'select' ) |
|
this.setContentEditable(true); |
|
} |
|
}) |
|
.catch(reason => {}) |
|
}, |
|
|
|
removeTagsFromDOM(){ |
|
[].slice.call(this.getTagElms()).forEach(elm => elm.parentNode.removeChild(elm)) |
|
}, |
|
|
|
|
|
|
|
|
|
removeTagsFromValue( tags ){ |
|
tags = Array.isArray(tags) ? tags : [tags]; |
|
|
|
tags.forEach(tag => { |
|
var tagData = this.tagData(tag), |
|
tagIdx = this.getTagIdx(tagData) |
|
|
|
|
|
|
|
if( tagIdx > -1 ) |
|
this.value.splice(tagIdx, 1) |
|
}) |
|
}, |
|
|
|
removeAllTags( opts ){ |
|
opts = opts || {} |
|
this.value = [] |
|
|
|
if( this.settings.mode == 'mix' ) |
|
this.DOM.input.innerHTML = '' |
|
else |
|
this.removeTagsFromDOM() |
|
|
|
this.dropdown.position() |
|
|
|
if( this.settings.mode == 'select' ){ |
|
this.input.set.call(this) |
|
this.setContentEditable(true) |
|
} |
|
|
|
|
|
|
|
this.update(opts) |
|
}, |
|
|
|
postUpdate(){ |
|
var classNames = this.settings.classNames, |
|
hasValue = this.settings.mode == 'mix' |
|
? this.settings.mixMode.integrated |
|
? this.DOM.input.textContent |
|
: this.DOM.originalInput.value.trim() |
|
: this.value.length + this.input.raw.call(this).length; |
|
|
|
this.toggleClass(classNames.hasMaxTags, this.value.length >= this.settings.maxTags) |
|
this.toggleClass(classNames.hasNoTags, !this.value.length) |
|
this.toggleClass(classNames.empty, !hasValue) |
|
}, |
|
|
|
setOriginalInputValue( v ){ |
|
var inputElm = this.DOM.originalInput; |
|
|
|
if( !this.settings.mixMode.integrated ){ |
|
inputElm.value = v |
|
inputElm.tagifyValue = inputElm.value |
|
this.setPersistedData(v, 'value') |
|
} |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
update( args ){ |
|
var inputValue = this.getInputValue(); |
|
|
|
this.setOriginalInputValue(inputValue) |
|
this.postUpdate() |
|
|
|
if( !(args||{}).withoutChangeEvent && !this.state.blockChangeEvent ) |
|
this.triggerChangeEvent() |
|
}, |
|
|
|
getInputValue(){ |
|
var value = this.getCleanValue(); |
|
|
|
return this.settings.mode == 'mix' |
|
? this.getMixedTagsAsString(value) |
|
: value.length |
|
? this.settings.originalInputValueFormat |
|
? this.settings.originalInputValueFormat(value) |
|
: JSON.stringify(value) |
|
: "" |
|
}, |
|
|
|
|
|
|
|
|
|
getCleanValue(v){ |
|
return removeCollectionProp(v || this.value, this.dataProps); |
|
}, |
|
|
|
getMixedTagsAsString(){ |
|
var result = "", |
|
that = this, |
|
_interpolator = this.settings.mixTagsInterpolator; |
|
|
|
function iterateChildren(rootNode){ |
|
rootNode.childNodes.forEach((node) => { |
|
if( node.nodeType == 1 ){ |
|
const tagData = that.tagData(node); |
|
|
|
if( node.tagName == 'BR' ){ |
|
result += "\r\n"; |
|
} |
|
|
|
if( node.tagName == 'DIV' || node.tagName == 'P' ){ |
|
result += "\r\n"; |
|
|
|
|
|
iterateChildren(node) |
|
} |
|
|
|
else if( isNodeTag.call(that, node) && tagData ){ |
|
if( tagData.__removed ) |
|
return; |
|
else |
|
result += _interpolator[0] + JSON.stringify( omit(tagData, that.dataProps) ) + _interpolator[1] |
|
} |
|
} |
|
else |
|
result += node.textContent; |
|
}) |
|
} |
|
|
|
iterateChildren(this.DOM.input) |
|
|
|
return result; |
|
} |
|
} |
|
|
|
|
|
Tagify.prototype.removeTag = Tagify.prototype.removeTags |
|
|
|
export default Tagify |
|
|