/*! * Tokenize2 v1.3.3 (https://github.com/dragonofmercy/Tokenize2) * Copyright 2016-2017 DragonOfMercy. * Licensed under the new BSD license */ (function (factory) { if (typeof define === 'function' && define.amd) { // AMD. Register as an anonymous module. define(['jquery'], factory); } else if (typeof module === 'object' && module.exports) { // Node/CommonJS module.exports = function (root, jQuery) { if (jQuery === undefined) { // require('jQuery') returns a factory that requires window to // build a jQuery instance, we normalize how we use modules // that require this pattern but the window provided is a noop // if it's defined (how jquery works) if (typeof window !== 'undefined') { jQuery = require('jquery'); } else { jQuery = require('jquery')(root); } } factory(jQuery); return jQuery; }; } else { // Browser globals factory(jQuery); } }(function ($) { /** * Tokenize2 constructor. * * @param {object} element * @param {object} options * @constructor */ var Tokenize2 = function (element, options) { this.control = false; this.element = $(element); this.options = $.extend({}, Tokenize2.DEFAULTS, options); this.options.tabIndex = this.options.tabIndex === -1 ? 0 : this.options.tabIndex; this.options.sortable = this.options.tokensMaxItems === 1 ? false : this.options.sortable; this.bind(); this.trigger('tokenize:load'); }; /** * Keycodes constants * * @type {object} */ var KEYS = { BACKSPACE: 8, TAB: 9, ENTER: 13, ESCAPE: 27, ARROW_UP: 38, ARROW_DOWN: 40, CTRL: 17, MAJ: 16 }; Tokenize2.VERSION = '1.3.3'; Tokenize2.DEBOUNCE = null; Tokenize2.DEFAULTS = { tokensMaxItems: 0, tokensAllowCustom: false, dropdownMaxItems: 10, dropdownSelectFirstItem: true, searchMinLength: 0, searchMaxLength: 0, searchFromStart: true, searchHighlight: true, displayNoResultsMessage: false, noResultsMessageText: 'No results mached "%s"', delimiter: ',', dataSource: 'select', debounce: 0, placeholder: false, sortable: false, allowEmptyValues: false, zIndexMargin: 500, tabIndex: 0 }; /** * Trigger an event * * @see $.trigger */ Tokenize2.prototype.trigger = function (event, data, elem, onlyHandlers) { this.element.trigger(event, data, elem, onlyHandlers); }; /** * Bind events */ Tokenize2.prototype.bind = function () { this.element.on('tokenize:load', {}, $.proxy(function () { this.init() }, this)) .on('tokenize:clear', {}, $.proxy(function () { this.clear() }, this)) .on('tokenize:remap', {}, $.proxy(function () { this.remap() }, this)) .on('tokenize:select', {}, $.proxy(function (e, c) { this.focus(c) }, this)) .on('tokenize:deselect', {}, $.proxy(function () { this.blur() }, this)) .on('tokenize:search', {}, $.proxy(function (e, v) { this.find(v) }, this)) .on('tokenize:paste', {}, $.proxy(function () { this.paste() }, this)) .on('tokenize:dropdown:up', {}, $.proxy(function () { this.dropdownSelectionMove(-1) }, this)) .on('tokenize:dropdown:down', {}, $.proxy(function () { this.dropdownSelectionMove(1) }, this)) .on('tokenize:dropdown:clear', {}, $.proxy(function () { this.dropdownClear() }, this)) .on('tokenize:dropdown:show', {}, $.proxy(function () { this.dropdownShow() }, this)) .on('tokenize:dropdown:hide', {}, $.proxy(function () { this.dropdownHide() }, this)) .on('tokenize:dropdown:fill', {}, $.proxy(function (e, i) { this.dropdownFill(i) }, this)) .on('tokenize:dropdown:itemAdd', {}, $.proxy(function (e, i) { this.dropdownAddItem(i) }, this)) .on('tokenize:keypress', {}, $.proxy(function (e, routedEvent) { this.keypress(routedEvent) }, this)) .on('tokenize:keydown', {}, $.proxy(function (e, routedEvent) { this.keydown(routedEvent) }, this)) .on('tokenize:keyup', {}, $.proxy(function (e, routedEvent) { this.keyup(routedEvent) }, this)) .on('tokenize:tokens:reorder', {}, $.proxy(function () { this.reorder() }, this)) .on('tokenize:tokens:add', {}, $.proxy(function (e, v, t, c) { this.tokenAdd(v, t, c) }, this)) .on('tokenize:tokens:remove', {}, $.proxy(function (e, v) { this.tokenRemove(v) }, this)); }; /** * Init function */ Tokenize2.prototype.init = function () { this.id = this.guid(); this.element.hide(); if (!this.element.attr('multiple')) { console.error('Attribute multiple is missing, tokenize2 can be buggy !') } this.dropdown = undefined; this.searchContainer = $('