import {D, W, getAttribute, getChildFirst, getChildren, getNext, getParent, getParentForm, getPrev, getText, hasClass, hasParent, letClass, letClasses, letElement, setClass, setChildLast, setClasses, setElement, setNext, setPrev, setText} from '@taufik-nurrohman/document'; import {delay} from '@taufik-nurrohman/tick'; import {fromStates} from '@taufik-nurrohman/from'; import {hasValue} from '@taufik-nurrohman/has'; import {hook} from '@taufik-nurrohman/hook'; import {isArray, isInstance, isNumber, isSet, isString} from '@taufik-nurrohman/is'; import {offEvent, offEvents, offEventDefault, offEventPropagation, onEvent, onEvents} from '@taufik-nurrohman/event'; import {toArrayKey, toCaseLower, toCount, toObjectCount, toObjectKeys} from '@taufik-nurrohman/to'; import {toPattern} from '@taufik-nurrohman/pattern'; let name = 'TP'; const KEY_A = 'a'; const KEY_ARROW_LEFT = 'ArrowLeft'; const KEY_ARROW_RIGHT = 'ArrowRight'; const KEY_BEGIN = 'Home'; const KEY_DELETE_LEFT = 'Backspace'; const KEY_DELETE_RIGHT = 'Delete'; const KEY_END = 'End'; const KEY_ENTER = 'Enter'; const KEY_TAB = 'Tab'; function TP(source, state = {}) { const $ = this; if (!source) { return $; } // Already instantiated, skip! if (source[name]) { return source[name]; } // Return new instance if `TP` was called without the `new` operator if (!isInstance($, TP)) { return new TP(source, state); } let sourceIsDisabled = () => source.disabled, sourceIsReadOnly = () => source.readOnly, thePlaceholder = getAttribute(source, 'placeholder'), theTabIndex = getAttribute(source, 'tabindex'); let {hooks, fire} = hook($); $.state = state = fromStates({}, TP.state, isString(state) ? { join: state } : (state || {})); $.source = source; // Store current instance to `TP.instances` TP.instances[source.id || source.name || toObjectCount(TP.instances)] = $; // Mark current DOM as active tag picker to prevent duplicate instance source[name] = $; let classNameB = state['class'], classNameE = classNameB + '__', classNameM = classNameB + '--', form = getParentForm(source), // Capture the closest `