/** * Tom Select v2.4.2 * Licensed under the Apache License, Version 2.0 (the "License"); */ (function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.caret_position = factory()); })(this, (function () { 'use strict'; /** * Converts a scalar to its best string representation * for hash keys and HTML attribute values. * * Transformations: * 'str' -> 'str' * null -> '' * undefined -> '' * true -> '1' * false -> '0' * 0 -> '0' * 1 -> '1' * */ /** * Iterates over arrays and hashes. * * ``` * iterate(this.items, function(item, id) { * // invoked for each item * }); * ``` * */ const iterate = (object, callback) => { if (Array.isArray(object)) { object.forEach(callback); } else { for (var key in object) { if (object.hasOwnProperty(key)) { callback(object[key], key); } } } }; /** * Remove css classes * */ const removeClasses = (elmts, ...classes) => { var norm_classes = classesArray(classes); elmts = castAsArray(elmts); elmts.map(el => { norm_classes.map(cls => { el.classList.remove(cls); }); }); }; /** * Return arguments * */ const classesArray = args => { var classes = []; iterate(args, _classes => { if (typeof _classes === 'string') { _classes = _classes.trim().split(/[\t\n\f\r\s]/); } if (Array.isArray(_classes)) { classes = classes.concat(_classes); } }); return classes.filter(Boolean); }; /** * Create an array from arg if it's not already an array * */ const castAsArray = arg => { if (!Array.isArray(arg)) { arg = [arg]; } return arg; }; /** * Get the index of an element amongst sibling nodes of the same type * */ const nodeIndex = (el, amongst) => { if (!el) return -1; amongst = amongst || el.nodeName; var i = 0; while (el = el.previousElementSibling) { if (el.matches(amongst)) { i++; } } return i; }; /** * Plugin: "dropdown_input" (Tom Select) * Copyright (c) contributors * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this * file except in compliance with the License. You may obtain a copy of the License at: * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF * ANY KIND, either express or implied. See the License for the specific language * governing permissions and limitations under the License. * */ function plugin () { var self = this; /** * Moves the caret to the specified index. * * The input must be moved by leaving it in place and moving the * siblings, due to the fact that focus cannot be restored once lost * on mobile webkit devices * */ self.hook('instead', 'setCaret', new_pos => { if (self.settings.mode === 'single' || !self.control.contains(self.control_input)) { new_pos = self.items.length; } else { new_pos = Math.max(0, Math.min(self.items.length, new_pos)); if (new_pos != self.caretPos && !self.isPending) { self.controlChildren().forEach((child, j) => { if (j < new_pos) { self.control_input.insertAdjacentElement('beforebegin', child); } else { self.control.appendChild(child); } }); } } self.caretPos = new_pos; }); self.hook('instead', 'moveCaret', direction => { if (!self.isFocused) return; // move caret before or after selected items const last_active = self.getLastActive(direction); if (last_active) { const idx = nodeIndex(last_active); self.setCaret(direction > 0 ? idx + 1 : idx); self.setActiveItem(); removeClasses(last_active, 'last-active'); // move caret left or right of current position } else { self.setCaret(self.caretPos + direction); } }); } return plugin; })); //# sourceMappingURL=caret_position.js.map