/*! choices.js v11.2.0 | © 2026 Josh Johnson | https://github.com/jshjohnson/Choices#readme */
/******************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
/* global Reflect, Promise, SuppressedError, Symbol */
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf || {
__proto__: []
} instanceof Array && function (d, b) {
d.__proto__ = b;
} || function (d, b) {
for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p];
};
return extendStatics(d, b);
};
function __extends(d, b) {
if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() {
this.constructor = d;
}
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
}
var __assign = function () {
__assign = Object.assign || function __assign(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
var e = new Error(message);
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
};
var ActionType = {
ADD_CHOICE: 'ADD_CHOICE',
REMOVE_CHOICE: 'REMOVE_CHOICE',
FILTER_CHOICES: 'FILTER_CHOICES',
ACTIVATE_CHOICES: 'ACTIVATE_CHOICES',
CLEAR_CHOICES: 'CLEAR_CHOICES',
ADD_GROUP: 'ADD_GROUP',
ADD_ITEM: 'ADD_ITEM',
REMOVE_ITEM: 'REMOVE_ITEM',
HIGHLIGHT_ITEM: 'HIGHLIGHT_ITEM',
};
var EventType = {
showDropdown: 'showDropdown',
hideDropdown: 'hideDropdown',
change: 'change',
choice: 'choice',
search: 'search',
addItem: 'addItem',
removeItem: 'removeItem',
highlightItem: 'highlightItem',
highlightChoice: 'highlightChoice',
unhighlightItem: 'unhighlightItem',
};
var KeyCodeMap = {
TAB_KEY: 9,
SHIFT_KEY: 16,
BACK_KEY: 46,
DELETE_KEY: 8,
ENTER_KEY: 13,
A_KEY: 65,
ESC_KEY: 27,
UP_KEY: 38,
DOWN_KEY: 40,
PAGE_UP_KEY: 33,
PAGE_DOWN_KEY: 34,
};
var ObjectsInConfig = ['fuseOptions', 'classNames'];
var PassedElementTypes = {
Text: 'text',
SelectOne: 'select-one',
SelectMultiple: 'select-multiple',
};
var addChoice = function (choice) { return ({
type: ActionType.ADD_CHOICE,
choice: choice,
}); };
var removeChoice = function (choice) { return ({
type: ActionType.REMOVE_CHOICE,
choice: choice,
}); };
var filterChoices = function (results) { return ({
type: ActionType.FILTER_CHOICES,
results: results,
}); };
var activateChoices = function (active) {
return ({
type: ActionType.ACTIVATE_CHOICES,
active: active,
});
};
var addGroup = function (group) { return ({
type: ActionType.ADD_GROUP,
group: group,
}); };
var addItem = function (item) { return ({
type: ActionType.ADD_ITEM,
item: item,
}); };
var removeItem$1 = function (item) { return ({
type: ActionType.REMOVE_ITEM,
item: item,
}); };
var highlightItem = function (item, highlighted) { return ({
type: ActionType.HIGHLIGHT_ITEM,
item: item,
highlighted: highlighted,
}); };
var getRandomNumber = function (min, max) { return Math.floor(Math.random() * (max - min) + min); };
var generateChars = function (length) {
return Array.from({ length: length }, function () { return getRandomNumber(0, 36).toString(36); }).join('');
};
var generateId = function (element, prefix) {
var id = element.id || (element.name && "".concat(element.name, "-").concat(generateChars(2))) || generateChars(4);
id = id.replace(/(:|\.|\[|\]|,)/g, '');
id = "".concat(prefix, "-").concat(id);
return id;
};
var getAdjacentEl = function (startEl, selector, direction) {
if (direction === void 0) { direction = 1; }
var prop = "".concat(direction > 0 ? 'next' : 'previous', "ElementSibling");
var sibling = startEl[prop];
while (sibling) {
if (sibling.matches(selector)) {
return sibling;
}
sibling = sibling[prop];
}
return null;
};
var isScrolledIntoView = function (element, parent, direction) {
if (direction === void 0) { direction = 1; }
var isVisible;
if (direction > 0) {
// In view from bottom
isVisible = parent.scrollTop + parent.offsetHeight >= element.offsetTop + element.offsetHeight;
}
else {
// In view from top
isVisible = element.offsetTop >= parent.scrollTop;
}
return isVisible;
};
var sanitise = function (value) {
if (typeof value !== 'string') {
if (value === null || value === undefined) {
return '';
}
if (typeof value === 'object') {
if ('raw' in value) {
return sanitise(value.raw);
}
if ('trusted' in value) {
return value.trusted;
}
}
return value;
}
return value
.replace(/&/g, '&')
.replace(/>/g, '>')
.replace(/= 0 &&
!window.matchMedia("(min-height: ".concat(dropdownPos + 1, "px)")).matches;
}
else if (this.position === 'top') {
shouldFlip = true;
}
return shouldFlip;
};
Container.prototype.setActiveDescendant = function (activeDescendantID) {
this.element.setAttribute('aria-activedescendant', activeDescendantID);
};
Container.prototype.removeActiveDescendant = function () {
this.element.removeAttribute('aria-activedescendant');
};
Container.prototype.open = function (dropdownPos, dropdownHeight) {
addClassesToElement(this.element, this.classNames.openState);
this.element.setAttribute('aria-expanded', 'true');
this.isOpen = true;
if (this.shouldFlip(dropdownPos, dropdownHeight)) {
addClassesToElement(this.element, this.classNames.flippedState);
this.isFlipped = true;
}
};
Container.prototype.close = function () {
removeClassesFromElement(this.element, this.classNames.openState);
this.element.setAttribute('aria-expanded', 'false');
this.removeActiveDescendant();
this.isOpen = false;
// A dropdown flips if it does not have space within the page
if (this.isFlipped) {
removeClassesFromElement(this.element, this.classNames.flippedState);
this.isFlipped = false;
}
};
Container.prototype.addFocusState = function () {
addClassesToElement(this.element, this.classNames.focusState);
};
Container.prototype.removeFocusState = function () {
removeClassesFromElement(this.element, this.classNames.focusState);
};
Container.prototype.addInvalidState = function () {
addClassesToElement(this.element, this.classNames.invalidState);
};
Container.prototype.removeInvalidState = function () {
removeClassesFromElement(this.element, this.classNames.invalidState);
};
Container.prototype.enable = function () {
removeClassesFromElement(this.element, this.classNames.disabledState);
this.element.removeAttribute('aria-disabled');
if (this.type === PassedElementTypes.SelectOne) {
this.element.setAttribute('tabindex', '0');
}
this.isDisabled = false;
};
Container.prototype.disable = function () {
addClassesToElement(this.element, this.classNames.disabledState);
this.element.setAttribute('aria-disabled', 'true');
if (this.type === PassedElementTypes.SelectOne) {
this.element.setAttribute('tabindex', '-1');
}
this.isDisabled = true;
};
Container.prototype.wrap = function (element) {
var el = this.element;
var parentNode = element.parentNode;
if (parentNode) {
if (element.nextSibling) {
parentNode.insertBefore(el, element.nextSibling);
}
else {
parentNode.appendChild(el);
}
}
el.appendChild(element);
};
Container.prototype.unwrap = function (element) {
var el = this.element;
var parentNode = el.parentNode;
if (parentNode) {
// Move passed element outside this element
parentNode.insertBefore(element, el);
// Remove this element
parentNode.removeChild(el);
}
};
Container.prototype.addLoadingState = function () {
addClassesToElement(this.element, this.classNames.loadingState);
this.element.setAttribute('aria-busy', 'true');
this.isLoading = true;
};
Container.prototype.removeLoadingState = function () {
removeClassesFromElement(this.element, this.classNames.loadingState);
this.element.removeAttribute('aria-busy');
this.isLoading = false;
};
return Container;
}());
var Input = /** @class */ (function () {
function Input(_a) {
var element = _a.element, type = _a.type, classNames = _a.classNames, preventPaste = _a.preventPaste;
this.element = element;
this.type = type;
this.classNames = classNames;
this.preventPaste = preventPaste;
this.isFocussed = this.element.isEqualNode(document.activeElement);
this.isDisabled = element.disabled;
this._onPaste = this._onPaste.bind(this);
this._onInput = this._onInput.bind(this);
this._onFocus = this._onFocus.bind(this);
this._onBlur = this._onBlur.bind(this);
}
Object.defineProperty(Input.prototype, "placeholder", {
set: function (placeholder) {
this.element.placeholder = placeholder;
},
enumerable: false,
configurable: true
});
Object.defineProperty(Input.prototype, "value", {
get: function () {
return this.element.value;
},
set: function (value) {
this.element.value = value;
},
enumerable: false,
configurable: true
});
Input.prototype.addEventListeners = function () {
var el = this.element;
el.addEventListener('paste', this._onPaste);
el.addEventListener('input', this._onInput, {
passive: true,
});
el.addEventListener('focus', this._onFocus, {
passive: true,
});
el.addEventListener('blur', this._onBlur, {
passive: true,
});
};
Input.prototype.removeEventListeners = function () {
var el = this.element;
el.removeEventListener('input', this._onInput);
el.removeEventListener('paste', this._onPaste);
el.removeEventListener('focus', this._onFocus);
el.removeEventListener('blur', this._onBlur);
};
Input.prototype.enable = function () {
var el = this.element;
el.removeAttribute('disabled');
this.isDisabled = false;
};
Input.prototype.disable = function () {
var el = this.element;
el.setAttribute('disabled', '');
this.isDisabled = true;
};
Input.prototype.focus = function () {
if (!this.isFocussed) {
this.element.focus();
}
};
Input.prototype.blur = function () {
if (this.isFocussed) {
this.element.blur();
}
};
Input.prototype.clear = function (setWidth) {
if (setWidth === void 0) { setWidth = true; }
this.element.value = '';
if (setWidth) {
this.setWidth();
}
return this;
};
/**
* Set the correct input width based on placeholder
* value or input value
*/
Input.prototype.setWidth = function () {
// Resize input to contents or placeholder
var element = this.element;
element.style.minWidth = "".concat(element.placeholder.length + 1, "ch");
element.style.width = "".concat(element.value.length + 1, "ch");
};
Input.prototype.setActiveDescendant = function (activeDescendantID) {
this.element.setAttribute('aria-activedescendant', activeDescendantID);
};
Input.prototype.removeActiveDescendant = function () {
this.element.removeAttribute('aria-activedescendant');
};
Input.prototype._onInput = function () {
if (this.type !== PassedElementTypes.SelectOne) {
this.setWidth();
}
};
Input.prototype._onPaste = function (event) {
if (this.preventPaste) {
event.preventDefault();
}
};
Input.prototype._onFocus = function () {
this.isFocussed = true;
};
Input.prototype._onBlur = function () {
this.isFocussed = false;
};
return Input;
}());
var SCROLLING_SPEED = 4;
var List = /** @class */ (function () {
function List(_a) {
var element = _a.element;
this.element = element;
this.scrollPos = this.element.scrollTop;
this.height = this.element.offsetHeight;
}
List.prototype.prepend = function (node) {
var child = this.element.firstElementChild;
if (child) {
this.element.insertBefore(node, child);
}
else {
this.element.append(node);
}
};
List.prototype.scrollToTop = function () {
this.element.scrollTop = 0;
};
List.prototype.scrollToChildElement = function (element, direction) {
var _this = this;
if (!element) {
return;
}
var listHeight = this.element.offsetHeight;
// Scroll position of dropdown
var listScrollPosition = this.element.scrollTop + listHeight;
var elementHeight = element.offsetHeight;
// Distance from bottom of element to top of parent
var elementPos = element.offsetTop + elementHeight;
// Difference between the element and scroll position
var destination = direction > 0 ? this.element.scrollTop + elementPos - listScrollPosition : element.offsetTop;
requestAnimationFrame(function () {
_this._animateScroll(destination, direction);
});
};
List.prototype._scrollDown = function (scrollPos, strength, destination) {
var easing = (destination - scrollPos) / strength;
var distance = easing > 1 ? easing : 1;
this.element.scrollTop = scrollPos + distance;
};
List.prototype._scrollUp = function (scrollPos, strength, destination) {
var easing = (scrollPos - destination) / strength;
var distance = easing > 1 ? easing : 1;
this.element.scrollTop = scrollPos - distance;
};
List.prototype._animateScroll = function (destination, direction) {
var _this = this;
var strength = SCROLLING_SPEED;
var choiceListScrollTop = this.element.scrollTop;
var continueAnimation = false;
if (direction > 0) {
this._scrollDown(choiceListScrollTop, strength, destination);
if (choiceListScrollTop < destination) {
continueAnimation = true;
}
}
else {
this._scrollUp(choiceListScrollTop, strength, destination);
if (choiceListScrollTop > destination) {
continueAnimation = true;
}
}
if (continueAnimation) {
requestAnimationFrame(function () {
_this._animateScroll(destination, direction);
});
}
};
return List;
}());
var WrappedElement = /** @class */ (function () {
function WrappedElement(_a) {
var element = _a.element, classNames = _a.classNames;
this.element = element;
this.classNames = classNames;
this.isDisabled = false;
}
Object.defineProperty(WrappedElement.prototype, "isActive", {
get: function () {
return this.element.dataset.choice === 'active';
},
enumerable: false,
configurable: true
});
Object.defineProperty(WrappedElement.prototype, "dir", {
get: function () {
return this.element.dir;
},
enumerable: false,
configurable: true
});
Object.defineProperty(WrappedElement.prototype, "value", {
get: function () {
return this.element.value;
},
set: function (value) {
this.element.setAttribute('value', value);
this.element.value = value;
},
enumerable: false,
configurable: true
});
WrappedElement.prototype.conceal = function () {
var el = this.element;
// Hide passed input
addClassesToElement(el, this.classNames.input);
el.hidden = true;
// Remove element from tab index
el.tabIndex = -1;
// Backup original styles if any
var origStyle = el.getAttribute('style');
if (origStyle) {
el.setAttribute('data-choice-orig-style', origStyle);
}
el.setAttribute('data-choice', 'active');
};
WrappedElement.prototype.reveal = function () {
var el = this.element;
// Reinstate passed element
removeClassesFromElement(el, this.classNames.input);
el.hidden = false;
el.removeAttribute('tabindex');
// Recover original styles if any
var origStyle = el.getAttribute('data-choice-orig-style');
if (origStyle) {
el.removeAttribute('data-choice-orig-style');
el.setAttribute('style', origStyle);
}
else {
el.removeAttribute('style');
}
el.removeAttribute('data-choice');
};
WrappedElement.prototype.enable = function () {
this.element.removeAttribute('disabled');
this.element.disabled = false;
this.isDisabled = false;
};
WrappedElement.prototype.disable = function () {
this.element.setAttribute('disabled', '');
this.element.disabled = true;
this.isDisabled = true;
};
WrappedElement.prototype.triggerEvent = function (eventType, data) {
dispatchEvent(this.element, eventType, data || {});
};
return WrappedElement;
}());
var WrappedInput = /** @class */ (function (_super) {
__extends(WrappedInput, _super);
function WrappedInput() {
return _super !== null && _super.apply(this, arguments) || this;
}
return WrappedInput;
}(WrappedElement));
var coerceBool = function (arg, defaultValue) {
if (defaultValue === void 0) { defaultValue = true; }
return typeof arg === 'undefined' ? defaultValue : !!arg;
};
var stringToHtmlClass = function (input) {
if (typeof input === 'string') {
// eslint-disable-next-line no-param-reassign
input = input.split(' ').filter(function (s) { return s.length; });
}
if (Array.isArray(input) && input.length) {
return input;
}
return undefined;
};
var mapInputToChoice = function (value, allowGroup, allowRawString) {
if (allowRawString === void 0) { allowRawString = true; }
if (typeof value === 'string') {
var sanitisedValue = sanitise(value);
var userValue = allowRawString || sanitisedValue === value ? value : { escaped: sanitisedValue, raw: value };
var result_1 = mapInputToChoice({
value: value,
label: userValue,
selected: true,
}, false);
return result_1;
}
var groupOrChoice = value;
if ('choices' in groupOrChoice) {
if (!allowGroup) {
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/optgroup
throw new TypeError("optGroup is not allowed");
}
var group = groupOrChoice;
var choices = group.choices.map(function (e) { return mapInputToChoice(e, false); });
var result_2 = {
id: 0, // actual ID will be assigned during _addGroup
label: unwrapStringForRaw(group.label) || group.value,
active: !!choices.length,
disabled: !!group.disabled,
choices: choices,
};
return result_2;
}
var choice = groupOrChoice;
var result = {
id: 0, // actual ID will be assigned during _addChoice
group: null, // actual group will be assigned during _addGroup but before _addChoice
score: 0, // used in search
rank: 0, // used in search, stable sort order
value: choice.value,
label: choice.label || choice.value,
active: coerceBool(choice.active),
selected: coerceBool(choice.selected, false),
disabled: coerceBool(choice.disabled, false),
placeholder: coerceBool(choice.placeholder, false),
highlighted: false,
labelClass: stringToHtmlClass(choice.labelClass),
labelDescription: choice.labelDescription,
customProperties: choice.customProperties,
};
return result;
};
var isHtmlInputElement = function (e) { return e.tagName === 'INPUT'; };
var isHtmlSelectElement = function (e) { return e.tagName === 'SELECT'; };
var isHtmlOption = function (e) { return e.tagName === 'OPTION'; };
var isHtmlOptgroup = function (e) { return e.tagName === 'OPTGROUP'; };
var WrappedSelect = /** @class */ (function (_super) {
__extends(WrappedSelect, _super);
function WrappedSelect(_a) {
var element = _a.element, classNames = _a.classNames, template = _a.template, extractPlaceholder = _a.extractPlaceholder;
var _this = _super.call(this, { element: element, classNames: classNames }) || this;
_this.template = template;
_this.extractPlaceholder = extractPlaceholder;
return _this;
}
Object.defineProperty(WrappedSelect.prototype, "placeholderOption", {
get: function () {
return (this.element.querySelector('option[value=""]') ||
// Backward compatibility layer for the non-standard placeholder attribute supported in older versions.
this.element.querySelector('option[placeholder]'));
},
enumerable: false,
configurable: true
});
WrappedSelect.prototype.addOptions = function (choices) {
var _this = this;
var fragment = document.createDocumentFragment();
choices.forEach(function (obj) {
var choice = obj;
if (choice.element) {
return;
}
var option = _this.template(choice);
fragment.appendChild(option);
choice.element = option;
});
this.element.appendChild(fragment);
};
WrappedSelect.prototype.optionsAsChoices = function () {
var _this = this;
var choices = [];
this.element.querySelectorAll(':scope > option, :scope > optgroup').forEach(function (e) {
if (isHtmlOption(e)) {
choices.push(_this._optionToChoice(e));
}
else if (isHtmlOptgroup(e)) {
choices.push(_this._optgroupToChoice(e));
}
// todo: hr as empty optgroup, requires displaying empty opt-groups to be useful
});
return choices;
};
// eslint-disable-next-line class-methods-use-this
WrappedSelect.prototype._optionToChoice = function (option) {
// option.value returns the label if there is no value attribute, which can break legacy placeholder attribute support
if (!option.hasAttribute('value') && option.hasAttribute('placeholder')) {
option.setAttribute('value', '');
option.value = '';
}
return {
id: 0,
group: null,
score: 0,
rank: 0,
value: option.value,
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/option
// This attribute is text for the label indicating the meaning of the option. If the `label` attribute isn't defined, its value is that of the element text content (ie `innerText`).
label: option.label,
element: option,
active: true,
// this returns true if nothing is selected on initial load, which will break placeholder support
selected: this.extractPlaceholder ? option.selected : option.hasAttribute('selected'),
disabled: option.disabled,
highlighted: false,
placeholder: this.extractPlaceholder && (!option.value || option.hasAttribute('placeholder')),
labelClass: typeof option.dataset.labelClass !== 'undefined' ? stringToHtmlClass(option.dataset.labelClass) : undefined,
labelDescription: typeof option.dataset.labelDescription !== 'undefined'
? { trusted: option.dataset.labelDescription }
: undefined,
customProperties: parseCustomProperties(option.dataset.customProperties),
};
};
WrappedSelect.prototype._optgroupToChoice = function (optgroup) {
var _this = this;
var options = optgroup.querySelectorAll('option');
var choices = Array.from(options).map(function (option) { return _this._optionToChoice(option); });
return {
id: 0,
label: optgroup.label || '',
element: optgroup,
active: !!choices.length,
disabled: optgroup.disabled,
choices: choices,
};
};
return WrappedSelect;
}(WrappedElement));
var DEFAULT_CLASSNAMES = {
containerOuter: ['choices'],
containerInner: ['choices__inner'],
input: ['choices__input'],
inputCloned: ['choices__input--cloned'],
list: ['choices__list'],
listItems: ['choices__list--multiple'],
listSingle: ['choices__list--single'],
listDropdown: ['choices__list--dropdown'],
item: ['choices__item'],
itemSelectable: ['choices__item--selectable'],
itemDisabled: ['choices__item--disabled'],
itemChoice: ['choices__item--choice'],
description: ['choices__description'],
placeholder: ['choices__placeholder'],
group: ['choices__group'],
groupHeading: ['choices__heading'],
button: ['choices__button'],
activeState: ['is-active'],
focusState: ['is-focused'],
openState: ['is-open'],
disabledState: ['is-disabled'],
highlightedState: ['is-highlighted'],
selectedState: ['is-selected'],
flippedState: ['is-flipped'],
loadingState: ['is-loading'],
invalidState: ['is-invalid'],
notice: ['choices__notice'],
addChoice: ['choices__item--selectable', 'add-choice'],
noResults: ['has-no-results'],
noChoices: ['has-no-choices'],
};
var DEFAULT_CONFIG = {
items: [],
choices: [],
silent: false,
renderChoiceLimit: -1,
maxItemCount: -1,
closeDropdownOnSelect: 'auto',
singleModeForMultiSelect: false,
addChoices: false,
addItems: true,
addItemFilter: function (value) { return !!value && value !== ''; },
removeItems: true,
removeItemButton: false,
removeItemButtonAlignLeft: false,
editItems: false,
allowHTML: false,
allowHtmlUserInput: false,
duplicateItemsAllowed: true,
delimiter: ',',
paste: true,
searchEnabled: true,
searchChoices: true,
searchDisabledChoices: false,
searchFloor: 1,
searchResultLimit: 4,
searchFields: ['label', 'value'],
position: 'auto',
resetScrollPosition: true,
shouldSort: true,
shouldSortItems: false,
sorter: sortByAlpha,
shadowRoot: null,
placeholder: true,
placeholderValue: null,
searchPlaceholderValue: null,
prependValue: null,
appendValue: null,
renderSelectedChoices: 'auto',
searchRenderSelectedChoices: true,
loadingText: 'Loading...',
noResultsText: 'No results found',
noChoicesText: 'No choices to choose from',
itemSelectText: 'Press to select',
uniqueItemText: 'Only unique values can be added',
customAddItemText: 'Only values matching specific conditions can be added',
addItemText: function (value) { return "Press Enter to add \"".concat(value, "\""); },
removeItemIconText: function () { return "Remove item"; },
removeItemLabelText: function (value, _valueRaw, i) {
return "Remove item: ".concat(i ? sanitise(i.label) : value);
},
maxItemText: function (maxItemCount) { return "Only ".concat(maxItemCount, " values can be added"); },
valueComparer: function (value1, value2) { return value1 === value2; },
fuseOptions: {
includeScore: true,
},
labelId: '',
callbackOnInit: null,
callbackOnCreateTemplates: null,
classNames: DEFAULT_CLASSNAMES,
appendGroupInSearch: false,
};
var removeItem = function (item) {
var itemEl = item.itemEl;
if (itemEl) {
itemEl.remove();
item.itemEl = undefined;
}
};
function items(s, action, context) {
var state = s;
var update = true;
switch (action.type) {
case ActionType.ADD_ITEM: {
action.item.selected = true;
var el = action.item.element;
if (el) {
el.selected = true;
el.setAttribute('selected', '');
}
state.push(action.item);
break;
}
case ActionType.REMOVE_ITEM: {
action.item.selected = false;
var el = action.item.element;
if (el) {
el.selected = false;
el.removeAttribute('selected');
// For a select-one, if all options are deselected, the first item is selected. To set a black value, select.value needs to be set
var select = el.parentElement;
if (select && isHtmlSelectElement(select) && select.type === PassedElementTypes.SelectOne) {
select.value = '';
}
}
// this is mixing concerns, but this is *so much faster*
removeItem(action.item);
state = state.filter(function (choice) { return choice.id !== action.item.id; });
break;
}
case ActionType.REMOVE_CHOICE: {
removeItem(action.choice);
state = state.filter(function (item) { return item.id !== action.choice.id; });
break;
}
case ActionType.HIGHLIGHT_ITEM: {
var highlighted = action.highlighted;
var item = state.find(function (obj) { return obj.id === action.item.id; });
if (item && item.highlighted !== highlighted) {
item.highlighted = highlighted;
if (context) {
updateClassList(item, highlighted ? context.classNames.highlightedState : context.classNames.selectedState, highlighted ? context.classNames.selectedState : context.classNames.highlightedState);
}
}
break;
}
default: {
update = false;
break;
}
}
return { state: state, update: update };
}
function groups(s, action) {
var state = s;
var update = true;
switch (action.type) {
case ActionType.ADD_GROUP: {
state.push(action.group);
break;
}
case ActionType.CLEAR_CHOICES: {
state = [];
break;
}
default: {
update = false;
break;
}
}
return { state: state, update: update };
}
/* eslint-disable */
function choices(s, action, context) {
var state = s;
var update = true;
switch (action.type) {
case ActionType.ADD_CHOICE: {
state.push(action.choice);
break;
}
case ActionType.REMOVE_CHOICE: {
action.choice.choiceEl = undefined;
if (action.choice.group) {
action.choice.group.choices = action.choice.group.choices.filter(function (obj) { return obj.id !== action.choice.id; });
}
state = state.filter(function (obj) { return obj.id !== action.choice.id; });
break;
}
case ActionType.ADD_ITEM:
case ActionType.REMOVE_ITEM: {
action.item.choiceEl = undefined;
break;
}
case ActionType.FILTER_CHOICES: {
// avoid O(n^2) algorithm complexity when searching/filtering choices
var scoreLookup_1 = [];
action.results.forEach(function (result) {
scoreLookup_1[result.item.id] = result;
});
state.forEach(function (choice) {
var result = scoreLookup_1[choice.id];
if (result !== undefined) {
choice.score = result.score;
choice.rank = result.rank;
choice.active = true;
}
else {
choice.score = 0;
choice.rank = 0;
choice.active = false;
}
if (context && context.appendGroupInSearch) {
choice.choiceEl = undefined;
}
});
break;
}
case ActionType.ACTIVATE_CHOICES: {
state.forEach(function (choice) {
choice.active = action.active;
if (context && context.appendGroupInSearch) {
choice.choiceEl = undefined;
}
});
break;
}
case ActionType.CLEAR_CHOICES: {
state = [];
break;
}
default: {
update = false;
break;
}
}
return { state: state, update: update };
}
var reducers = {
groups: groups,
items: items,
choices: choices,
};
var Store = /** @class */ (function () {
function Store(context) {
this._state = this.defaultState;
this._listeners = [];
this._txn = 0;
this._context = context;
}
Object.defineProperty(Store.prototype, "defaultState", {
// eslint-disable-next-line class-methods-use-this
get: function () {
return {
groups: [],
items: [],
choices: [],
};
},
enumerable: false,
configurable: true
});
// eslint-disable-next-line class-methods-use-this
Store.prototype.changeSet = function (init) {
return {
groups: init,
items: init,
choices: init,
};
};
Store.prototype.reset = function () {
this._state = this.defaultState;
var changes = this.changeSet(true);
if (this._txn) {
this._changeSet = changes;
}
else {
this._listeners.forEach(function (l) { return l(changes); });
}
};
Store.prototype.subscribe = function (onChange) {
this._listeners.push(onChange);
return this;
};
Store.prototype.dispatch = function (action) {
var _this = this;
var state = this._state;
var hasChanges = false;
var changes = this._changeSet || this.changeSet(false);
Object.keys(reducers).forEach(function (key) {
var stateUpdate = reducers[key](state[key], action, _this._context);
if (stateUpdate.update) {
hasChanges = true;
changes[key] = true;
state[key] = stateUpdate.state;
}
});
if (hasChanges) {
if (this._txn) {
this._changeSet = changes;
}
else {
this._listeners.forEach(function (l) { return l(changes); });
}
}
};
Store.prototype.withTxn = function (func) {
this._txn++;
try {
func();
}
finally {
this._txn = Math.max(0, this._txn - 1);
if (!this._txn) {
var changeSet_1 = this._changeSet;
if (changeSet_1) {
this._changeSet = undefined;
this._listeners.forEach(function (l) { return l(changeSet_1); });
}
}
}
};
Object.defineProperty(Store.prototype, "state", {
/**
* Get store object
*/
get: function () {
return this._state;
},
enumerable: false,
configurable: true
});
Object.defineProperty(Store.prototype, "items", {
/**
* Get items from store
*/
get: function () {
return this.state.items;
},
enumerable: false,
configurable: true
});
Object.defineProperty(Store.prototype, "highlightedActiveItems", {
/**
* Get highlighted items from store
*/
get: function () {
return this.items.filter(function (item) { return item.active && item.highlighted; });
},
enumerable: false,
configurable: true
});
Object.defineProperty(Store.prototype, "choices", {
/**
* Get choices from store
*/
get: function () {
return this.state.choices;
},
enumerable: false,
configurable: true
});
Object.defineProperty(Store.prototype, "activeChoices", {
/**
* Get active choices from store
*/
get: function () {
return this.choices.filter(function (choice) { return choice.active; });
},
enumerable: false,
configurable: true
});
Object.defineProperty(Store.prototype, "searchableChoices", {
/**
* Get choices that can be searched (excluding placeholders or disabled choices)
*/
get: function () {
var context = this._context;
return this.choices.filter(function (choice) { return !choice.placeholder && (context.searchDisabledChoices || !choice.disabled); });
},
enumerable: false,
configurable: true
});
Object.defineProperty(Store.prototype, "groups", {
/**
* Get groups from store
*/
get: function () {
return this.state.groups;
},
enumerable: false,
configurable: true
});
Object.defineProperty(Store.prototype, "activeGroups", {
/**
* Get active groups from store
*/
get: function () {
var _this = this;
return this.state.groups.filter(function (group) {
var isActive = group.active && !group.disabled;
var hasActiveOptions = _this.state.choices.some(function (choice) { return choice.active && !choice.disabled; });
return isActive && hasActiveOptions;
}, []);
},
enumerable: false,
configurable: true
});
Store.prototype.inTxn = function () {
return this._txn > 0;
};
/**
* Get single choice by it's ID
*/
Store.prototype.getChoiceById = function (id) {
return this.activeChoices.find(function (choice) { return choice.id === id; });
};
/**
* Get group by group id
*/
Store.prototype.getGroupById = function (id) {
return this.groups.find(function (group) { return group.id === id; });
};
return Store;
}());
var NoticeTypes = {
noChoices: 'no-choices',
noResults: 'no-results',
addChoice: 'add-choice',
generic: '',
};
var SearchByPrefixFilter = /** @class */ (function () {
function SearchByPrefixFilter(config) {
this._haystack = [];
this._fields = config.searchFields;
}
SearchByPrefixFilter.prototype.index = function (data) {
this._haystack = data;
};
SearchByPrefixFilter.prototype.reset = function () {
this._haystack = [];
};
SearchByPrefixFilter.prototype.isEmptyIndex = function () {
return !this._haystack.length;
};
SearchByPrefixFilter.prototype.search = function (_needle) {
var fields = this._fields;
if (!fields || !fields.length || !_needle) {
return [];
}
var needle = _needle.toLowerCase();
return this._haystack
.filter(function (obj) { return fields.some(function (field) { return field in obj && obj[field].toLowerCase().startsWith(needle); }); })
.map(function (value, index) {
return {
item: value,
score: index,
rank: index + 1,
};
});
};
return SearchByPrefixFilter;
}());
function getSearcher(config) {
return new SearchByPrefixFilter(config);
}
/**
* Helpers to create HTML elements used by Choices
* Can be overridden by providing `callbackOnCreateTemplates` option.
* `Choices.defaults.templates` allows access to the default template methods from `callbackOnCreateTemplates`
*/
var isEmptyObject = function (obj) {
// eslint-disable-next-line no-restricted-syntax
for (var prop in obj) {
if (Object.prototype.hasOwnProperty.call(obj, prop)) {
return false;
}
}
return true;
};
var assignCustomProperties = function (el, choice, withCustomProperties) {
var dataset = el.dataset;
var customProperties = choice.customProperties, labelClass = choice.labelClass, labelDescription = choice.labelDescription;
if (labelClass) {
dataset.labelClass = getClassNames(labelClass).join(' ');
}
if (labelDescription) {
dataset.labelDescription = unwrapStringForRaw(labelDescription);
}
if (withCustomProperties && customProperties) {
if (typeof customProperties === 'string') {
dataset.customProperties = customProperties;
}
else if (typeof customProperties === 'object' && !isEmptyObject(customProperties)) {
dataset.customProperties = JSON.stringify(customProperties);
}
}
};
var addAriaLabel = function (docRoot, id, element) {
var label = id && docRoot.querySelector("label[for='".concat(id, "']"));
var text = label && label.innerText;
if (text) {
element.setAttribute('aria-label', text);
}
};
var templates = {
containerOuter: function (_a, dir, isSelectElement, isSelectOneElement, searchEnabled, passedElementType, labelId) {
var containerOuter = _a.classNames.containerOuter;
var div = document.createElement('div');
addClassesToElement(div, containerOuter);
div.dataset.type = passedElementType;
if (dir) {
div.dir = dir;
}
if (isSelectOneElement) {
div.tabIndex = 0;
}
if (isSelectElement) {
div.setAttribute('role', searchEnabled ? 'combobox' : 'listbox');
if (searchEnabled) {
div.setAttribute('aria-autocomplete', 'list');
}
else if (!labelId) {
addAriaLabel(this._docRoot, this.passedElement.element.id, div);
}
div.setAttribute('aria-haspopup', 'true');
div.setAttribute('aria-expanded', 'false');
}
if (labelId) {
div.setAttribute('aria-labelledby', labelId);
}
return div;
},
containerInner: function (_a) {
var containerInner = _a.classNames.containerInner;
var div = document.createElement('div');
addClassesToElement(div, containerInner);
return div;
},
itemList: function (_a, isSelectOneElement) {
var searchEnabled = _a.searchEnabled, _b = _a.classNames, list = _b.list, listSingle = _b.listSingle, listItems = _b.listItems;
var div = document.createElement('div');
addClassesToElement(div, list);
addClassesToElement(div, isSelectOneElement ? listSingle : listItems);
if (this._isSelectElement && searchEnabled) {
div.setAttribute('role', 'listbox');
}
return div;
},
placeholder: function (_a, value) {
var allowHTML = _a.allowHTML, placeholder = _a.classNames.placeholder;
var div = document.createElement('div');
addClassesToElement(div, placeholder);
setElementHtml(div, allowHTML, value);
return div;
},
item: function (_a, choice, removeItemButton) {
var allowHTML = _a.allowHTML, removeItemButtonAlignLeft = _a.removeItemButtonAlignLeft, removeItemIconText = _a.removeItemIconText, removeItemLabelText = _a.removeItemLabelText, _b = _a.classNames, item = _b.item, button = _b.button, highlightedState = _b.highlightedState, itemSelectable = _b.itemSelectable, placeholder = _b.placeholder;
var rawValue = unwrapStringForRaw(choice.value);
var div = document.createElement('div');
addClassesToElement(div, item);
if (choice.labelClass) {
var spanLabel = document.createElement('span');
setElementHtml(spanLabel, allowHTML, choice.label);
addClassesToElement(spanLabel, choice.labelClass);
div.appendChild(spanLabel);
}
else {
setElementHtml(div, allowHTML, choice.label);
}
div.dataset.item = '';
div.dataset.id = choice.id;
div.dataset.value = rawValue;
assignCustomProperties(div, choice, true);
if (choice.disabled || this.containerOuter.isDisabled) {
div.setAttribute('aria-disabled', 'true');
}
if (this._isSelectElement) {
div.setAttribute('aria-selected', 'true');
div.setAttribute('role', 'option');
}
if (choice.placeholder) {
addClassesToElement(div, placeholder);
div.dataset.placeholder = '';
}
addClassesToElement(div, choice.highlighted ? highlightedState : itemSelectable);
if (removeItemButton) {
if (choice.disabled) {
removeClassesFromElement(div, itemSelectable);
}
div.dataset.deletable = '';
var removeButton = document.createElement('button');
removeButton.type = 'button';
addClassesToElement(removeButton, button);
var eventChoice = getChoiceForOutput(choice);
setElementHtml(removeButton, true, resolveNoticeFunction(removeItemIconText, choice.value, eventChoice));
var REMOVE_ITEM_LABEL = resolveNoticeFunction(removeItemLabelText, choice.value, eventChoice);
if (REMOVE_ITEM_LABEL) {
removeButton.setAttribute('aria-label', REMOVE_ITEM_LABEL);
}
removeButton.dataset.button = '';
if (removeItemButtonAlignLeft) {
div.insertAdjacentElement('afterbegin', removeButton);
}
else {
div.appendChild(removeButton);
}
}
return div;
},
choiceList: function (_a, isSelectOneElement) {
var list = _a.classNames.list;
var div = document.createElement('div');
addClassesToElement(div, list);
if (!isSelectOneElement) {
div.setAttribute('aria-multiselectable', 'true');
}
div.setAttribute('role', 'listbox');
return div;
},
choiceGroup: function (_a, _b) {
var allowHTML = _a.allowHTML, _c = _a.classNames, group = _c.group, groupHeading = _c.groupHeading, itemDisabled = _c.itemDisabled;
var id = _b.id, label = _b.label, disabled = _b.disabled;
var rawLabel = unwrapStringForRaw(label);
var div = document.createElement('div');
addClassesToElement(div, group);
if (disabled) {
addClassesToElement(div, itemDisabled);
}
div.setAttribute('role', 'group');
div.dataset.group = '';
div.dataset.id = id;
div.dataset.value = rawLabel;
if (disabled) {
div.setAttribute('aria-disabled', 'true');
}
var heading = document.createElement('div');
addClassesToElement(heading, groupHeading);
setElementHtml(heading, allowHTML, label || '');
div.appendChild(heading);
return div;
},
choice: function (_a, choice, selectText, groupName) {
var allowHTML = _a.allowHTML, _b = _a.classNames, item = _b.item, itemChoice = _b.itemChoice, itemSelectable = _b.itemSelectable, selectedState = _b.selectedState, itemDisabled = _b.itemDisabled, description = _b.description, placeholder = _b.placeholder;
// eslint-disable-next-line prefer-destructuring
var label = choice.label;
var rawValue = unwrapStringForRaw(choice.value);
var div = document.createElement('div');
div.id = choice.elementId;
addClassesToElement(div, item);
addClassesToElement(div, itemChoice);
if (groupName && typeof label === 'string') {
label = escapeForTemplate(allowHTML, label);
label += " (".concat(groupName, ")");
label = { trusted: label };
}
var describedBy = div;
if (choice.labelClass) {
var spanLabel = document.createElement('span');
setElementHtml(spanLabel, allowHTML, label);
addClassesToElement(spanLabel, choice.labelClass);
describedBy = spanLabel;
div.appendChild(spanLabel);
}
else {
setElementHtml(div, allowHTML, label);
}
if (choice.labelDescription) {
var descId = "".concat(choice.elementId, "-description");
describedBy.setAttribute('aria-describedby', descId);
var spanDesc = document.createElement('span');
setElementHtml(spanDesc, allowHTML, choice.labelDescription);
spanDesc.id = descId;
addClassesToElement(spanDesc, description);
div.appendChild(spanDesc);
}
if (choice.selected) {
addClassesToElement(div, selectedState);
}
if (choice.placeholder) {
addClassesToElement(div, placeholder);
}
div.setAttribute('role', choice.group ? 'treeitem' : 'option');
div.dataset.choice = '';
div.dataset.id = choice.id;
div.dataset.value = rawValue;
if (selectText) {
div.dataset.selectText = selectText;
}
if (choice.group) {
div.dataset.groupId = "".concat(choice.group.id);
}
assignCustomProperties(div, choice, false);
if (choice.disabled) {
addClassesToElement(div, itemDisabled);
div.dataset.choiceDisabled = '';
div.setAttribute('aria-disabled', 'true');
}
else {
addClassesToElement(div, itemSelectable);
div.dataset.choiceSelectable = '';
div.setAttribute('aria-selected', choice.selected ? 'true' : 'false');
}
return div;
},
input: function (_a, placeholderValue) {
var _b = _a.classNames, input = _b.input, inputCloned = _b.inputCloned, labelId = _a.labelId;
var inp = document.createElement('input');
inp.type = 'search';
addClassesToElement(inp, input);
addClassesToElement(inp, inputCloned);
inp.autocomplete = 'off';
inp.autocapitalize = 'off';
inp.spellcheck = false;
inp.setAttribute('aria-autocomplete', 'list');
if (placeholderValue) {
inp.setAttribute('aria-label', placeholderValue);
}
else if (!labelId) {
addAriaLabel(this._docRoot, this.passedElement.element.id, inp);
}
return inp;
},
dropdown: function (_a) {
var _b = _a.classNames, list = _b.list, listDropdown = _b.listDropdown;
var div = document.createElement('div');
addClassesToElement(div, list);
addClassesToElement(div, listDropdown);
div.setAttribute('aria-expanded', 'false');
return div;
},
notice: function (_a, innerHTML, type) {
var _b = _a.classNames, item = _b.item, itemChoice = _b.itemChoice, addChoice = _b.addChoice, noResults = _b.noResults, noChoices = _b.noChoices, noticeItem = _b.notice;
if (type === void 0) { type = NoticeTypes.generic; }
var notice = document.createElement('div');
setElementHtml(notice, true, innerHTML);
addClassesToElement(notice, item);
addClassesToElement(notice, itemChoice);
addClassesToElement(notice, noticeItem);
// eslint-disable-next-line default-case
switch (type) {
case NoticeTypes.addChoice:
addClassesToElement(notice, addChoice);
break;
case NoticeTypes.noResults:
addClassesToElement(notice, noResults);
break;
case NoticeTypes.noChoices:
addClassesToElement(notice, noChoices);
break;
}
if (type === NoticeTypes.addChoice) {
notice.dataset.choiceSelectable = '';
notice.dataset.choice = '';
}
return notice;
},
option: function (choice) {
// HtmlOptionElement's label value does not support HTML, so the avoid double escaping unwrap the untrusted string.
var labelValue = unwrapStringForRaw(choice.label);
var opt = new Option(labelValue, choice.value, false, choice.selected);
assignCustomProperties(opt, choice, true);
opt.disabled = choice.disabled;
if (choice.selected) {
opt.setAttribute('selected', '');
}
return opt;
},
};
/** @see {@link http://browserhacks.com/#hack-acea075d0ac6954f275a70023906050c} */
var IS_IE11 = '-ms-scroll-limit' in document.documentElement.style &&
'-ms-ime-align' in document.documentElement.style;
var USER_DEFAULTS = {};
var parseDataSetId = function (element) {
if (!element) {
return undefined;
}
return element.dataset.id ? parseInt(element.dataset.id, 10) : undefined;
};
var selectableChoiceIdentifier = '[data-choice-selectable]';
/**
* Choices
* @author Josh Johnson
*/
var Choices = /** @class */ (function () {
function Choices(element, userConfig) {
if (element === void 0) { element = '[data-choice]'; }
if (userConfig === void 0) { userConfig = {}; }
var _this = this;
this.initialisedOK = undefined;
this._hasNonChoicePlaceholder = false;
this._lastAddedChoiceId = 0;
this._lastAddedGroupId = 0;
var defaults = Choices.defaults;
this.config = __assign(__assign(__assign({}, defaults.allOptions), defaults.options), userConfig);
ObjectsInConfig.forEach(function (key) {
_this.config[key] = __assign(__assign(__assign({}, defaults.allOptions[key]), defaults.options[key]), userConfig[key]);
});
var config = this.config;
if (!config.silent) {
this._validateConfig();
}
var docRoot = config.shadowRoot || document.documentElement;
this._docRoot = docRoot;
var passedElement = typeof element === 'string' ? docRoot.querySelector(element) : element;
if (!passedElement ||
typeof passedElement !== 'object' ||
!(isHtmlInputElement(passedElement) || isHtmlSelectElement(passedElement))) {
if (!passedElement && typeof element === 'string') {
throw TypeError("Selector ".concat(element, " failed to find an element"));
}
throw TypeError("Expected one of the following types text|select-one|select-multiple");
}
var elementType = passedElement.type;
var isText = elementType === PassedElementTypes.Text;
if (isText || config.maxItemCount !== 1) {
config.singleModeForMultiSelect = false;
}
if (config.singleModeForMultiSelect) {
elementType = PassedElementTypes.SelectMultiple;
}
var isSelectOne = elementType === PassedElementTypes.SelectOne;
var isSelectMultiple = elementType === PassedElementTypes.SelectMultiple;
var isSelect = isSelectOne || isSelectMultiple;
this._elementType = elementType;
this._isTextElement = isText;
this._isSelectOneElement = isSelectOne;
this._isSelectMultipleElement = isSelectMultiple;
this._isSelectElement = isSelectOne || isSelectMultiple;
this._canAddUserChoices = (isText && config.addItems) || (isSelect && config.addChoices);
if (typeof config.renderSelectedChoices !== 'boolean') {
config.renderSelectedChoices = config.renderSelectedChoices === 'always' || isSelectOne;
}
if (config.closeDropdownOnSelect === 'auto') {
config.closeDropdownOnSelect = isText || isSelectOne || config.singleModeForMultiSelect;
}
else {
config.closeDropdownOnSelect = coerceBool(config.closeDropdownOnSelect);
}
if (config.placeholder) {
if (config.placeholderValue) {
this._hasNonChoicePlaceholder = true;
}
else if (passedElement.dataset.placeholder) {
this._hasNonChoicePlaceholder = true;
config.placeholderValue = passedElement.dataset.placeholder;
}
}
if (userConfig.addItemFilter && typeof userConfig.addItemFilter !== 'function') {
var re = userConfig.addItemFilter instanceof RegExp ? userConfig.addItemFilter : new RegExp(userConfig.addItemFilter);
config.addItemFilter = re.test.bind(re);
}
if (this._isTextElement) {
this.passedElement = new WrappedInput({
element: passedElement,
classNames: config.classNames,
});
}
else {
var selectEl = passedElement;
this.passedElement = new WrappedSelect({
element: selectEl,
classNames: config.classNames,
template: function (data) { return _this._templates.option(data); },
extractPlaceholder: config.placeholder && !this._hasNonChoicePlaceholder,
});
}
this.initialised = false;
this._store = new Store(config);
this._currentValue = '';
config.searchEnabled = !isText && config.searchEnabled;
this._canSearch = config.searchEnabled;
this._isScrollingOnIe = false;
this._highlightPosition = 0;
this._wasTap = true;
this._placeholderValue = this._generatePlaceholderValue();
this._baseId = generateId(passedElement, 'choices-');
/**
* setting direction in cases where it's explicitly set on passedElement
* or when calculated direction is different from the document
*/
this._direction = passedElement.dir;
if (!this._direction) {
var elementDirection = window.getComputedStyle(passedElement).direction;
var documentDirection = window.getComputedStyle(document.documentElement).direction;
if (elementDirection !== documentDirection) {
this._direction = elementDirection;
}
}
this._idNames = {
itemChoice: 'item-choice',
};
this._templates = defaults.templates;
this._render = this._render.bind(this);
this._onFocus = this._onFocus.bind(this);
this._onBlur = this._onBlur.bind(this);
this._onKeyUp = this._onKeyUp.bind(this);
this._onKeyDown = this._onKeyDown.bind(this);
this._onInput = this._onInput.bind(this);
this._onClick = this._onClick.bind(this);
this._onTouchMove = this._onTouchMove.bind(this);
this._onTouchEnd = this._onTouchEnd.bind(this);
this._onMouseDown = this._onMouseDown.bind(this);
this._onMouseOver = this._onMouseOver.bind(this);
this._onFormReset = this._onFormReset.bind(this);
this._onSelectKey = this._onSelectKey.bind(this);
this._onEnterKey = this._onEnterKey.bind(this);
this._onEscapeKey = this._onEscapeKey.bind(this);
this._onDirectionKey = this._onDirectionKey.bind(this);
this._onDeleteKey = this._onDeleteKey.bind(this);
this._onChange = this._onChange.bind(this);
this._onInvalid = this._onInvalid.bind(this);
// If element has already been initialised with Choices, fail silently
if (this.passedElement.isActive) {
if (!config.silent) {
console.warn('Trying to initialise Choices on element already initialised', { element: element });
}
this.initialised = true;
this.initialisedOK = false;
return;
}
// Let's go
this.init();
// preserve the selected item list after setup for form reset
this._initialItems = this._store.items.map(function (choice) { return choice.value; });
}
Object.defineProperty(Choices, "defaults", {
get: function () {
return Object.preventExtensions({
get options() {
return USER_DEFAULTS;
},
get allOptions() {
return DEFAULT_CONFIG;
},
get templates() {
return templates;
},
});
},
enumerable: false,
configurable: true
});
Choices.prototype.init = function () {
if (this.initialised || this.initialisedOK !== undefined) {
return;
}
this._searcher = getSearcher(this.config);
this._loadChoices();
this._createTemplates();
this._createElements();
this._createStructure();
if ((this._isTextElement && !this.config.addItems) ||
this.passedElement.element.hasAttribute('disabled') ||
!!this.passedElement.element.closest('fieldset:disabled')) {
this.disable();
}
else {
this.enable();
this._addEventListeners();
}
// should be triggered **after** disabled state to avoid additional re-draws
this._initStore();
this.initialised = true;
this.initialisedOK = true;
var callbackOnInit = this.config.callbackOnInit;
// Run callback if it is a function
if (typeof callbackOnInit === 'function') {
callbackOnInit.call(this);
}
};
Choices.prototype.destroy = function () {
if (!this.initialised) {
return;
}
this._removeEventListeners();
this.passedElement.reveal();
this.containerOuter.unwrap(this.passedElement.element);
this._store._listeners = []; // prevents select/input value being wiped
this.clearStore(false);
this._stopSearch();
this._templates = Choices.defaults.templates;
this.initialised = false;
this.initialisedOK = undefined;
};
Choices.prototype.enable = function () {
if (this.passedElement.isDisabled) {
this.passedElement.enable();
}
if (this.containerOuter.isDisabled) {
this._addEventListeners();
this.input.enable();
this.containerOuter.enable();
}
return this;
};
Choices.prototype.disable = function () {
if (!this.passedElement.isDisabled) {
this.passedElement.disable();
}
if (!this.containerOuter.isDisabled) {
this._removeEventListeners();
this.input.disable();
this.containerOuter.disable();
}
return this;
};
Choices.prototype.highlightItem = function (item, runEvent) {
if (runEvent === void 0) { runEvent = true; }
if (!item || !item.id) {
return this;
}
var choice = this._store.items.find(function (c) { return c.id === item.id; });
if (!choice || choice.highlighted) {
return this;
}
this._store.dispatch(highlightItem(choice, true));
if (runEvent) {
this.passedElement.triggerEvent(EventType.highlightItem, getChoiceForOutput(choice));
}
return this;
};
Choices.prototype.unhighlightItem = function (item, runEvent) {
if (runEvent === void 0) { runEvent = true; }
if (!item || !item.id) {
return this;
}
var choice = this._store.items.find(function (c) { return c.id === item.id; });
if (!choice || !choice.highlighted) {
return this;
}
this._store.dispatch(highlightItem(choice, false));
if (runEvent) {
this.passedElement.triggerEvent(EventType.unhighlightItem, getChoiceForOutput(choice));
}
return this;
};
Choices.prototype.highlightAll = function () {
var _this = this;
this._store.withTxn(function () {
_this._store.items.forEach(function (item) {
if (!item.highlighted) {
_this._store.dispatch(highlightItem(item, true));
_this.passedElement.triggerEvent(EventType.highlightItem, getChoiceForOutput(item));
}
});
});
return this;
};
Choices.prototype.unhighlightAll = function () {
var _this = this;
this._store.withTxn(function () {
_this._store.items.forEach(function (item) {
if (item.highlighted) {
_this._store.dispatch(highlightItem(item, false));
_this.passedElement.triggerEvent(EventType.highlightItem, getChoiceForOutput(item));
}
});
});
return this;
};
Choices.prototype.removeActiveItemsByValue = function (value) {
var _this = this;
this._store.withTxn(function () {
_this._store.items.filter(function (item) { return item.value === value; }).forEach(function (item) { return _this._removeItem(item); });
});
return this;
};
Choices.prototype.removeActiveItems = function (excludedId) {
var _this = this;
this._store.withTxn(function () {
_this._store.items.filter(function (_a) {
var id = _a.id;
return id !== excludedId;
}).forEach(function (item) { return _this._removeItem(item); });
});
return this;
};
Choices.prototype.removeHighlightedItems = function (runEvent) {
var _this = this;
if (runEvent === void 0) { runEvent = false; }
this._store.withTxn(function () {
_this._store.highlightedActiveItems.forEach(function (item) {
_this._removeItem(item);
// If this action was performed by the user
// trigger the event
if (runEvent) {
_this._triggerChange(item.value);
}
});
});
return this;
};
Choices.prototype.showDropdown = function (preventInputFocus) {
var _this = this;
if (this.dropdown.isActive) {
return this;
}
if (preventInputFocus === undefined) {
// eslint-disable-next-line no-param-reassign
preventInputFocus = !this._canSearch;
}
requestAnimationFrame(function () {
_this.dropdown.show();
var rect = _this.dropdown.element.getBoundingClientRect();
_this.containerOuter.open(rect.bottom, rect.height);
if (!preventInputFocus) {
_this.input.focus();
}
_this.passedElement.triggerEvent(EventType.showDropdown);
var activeElement = _this.choiceList.element.querySelector(getClassNamesSelector(_this.config.classNames.selectedState));
if (activeElement !== null && !isScrolledIntoView(activeElement, _this.choiceList.element)) {
// We use the native scrollIntoView function instead of choiceList.scrollToChildElement to avoid animated scroll.
activeElement.scrollIntoView();
}
});
return this;
};
Choices.prototype.hideDropdown = function (preventInputBlur) {
var _this = this;
if (!this.dropdown.isActive) {
return this;
}
this._removeHighlightedChoices();
requestAnimationFrame(function () {
_this.dropdown.hide();
_this.containerOuter.close();
if (!preventInputBlur && _this._canSearch) {
_this.input.removeActiveDescendant();
_this.input.blur();
}
_this.passedElement.triggerEvent(EventType.hideDropdown);
});
return this;
};
Choices.prototype.getValue = function (valueOnly) {
var values = this._store.items.map(function (item) {
return (valueOnly ? item.value : getChoiceForOutput(item));
});
return this._isSelectOneElement || this.config.singleModeForMultiSelect ? values[0] : values;
};
Choices.prototype.setValue = function (items) {
var _this = this;
if (!this.initialisedOK) {
this._warnChoicesInitFailed('setValue');
return this;
}
this._store.withTxn(function () {
items.forEach(function (value) {
if (value) {
_this._addChoice(mapInputToChoice(value, false));
}
});
});
// @todo integrate with Store
this._searcher.reset();
return this;
};
Choices.prototype.setChoiceByValue = function (value) {
var _this = this;
if (!this.initialisedOK) {
this._warnChoicesInitFailed('setChoiceByValue');
return this;
}
if (this._isTextElement) {
return this;
}
this._store.withTxn(function () {
// If only one value has been passed, convert to array
var choiceValue = Array.isArray(value) ? value : [value];
// Loop through each value and
choiceValue.forEach(function (val) { return _this._findAndSelectChoiceByValue(val); });
_this.unhighlightAll();
});
// @todo integrate with Store
this._searcher.reset();
return this;
};
/**
* Set choices of select input via an array of objects (or function that returns array of object or promise of it),
* a value field name and a label field name.
* This behaves the same as passing items via the choices option but can be called after initialising Choices.
* This can also be used to add groups of choices (see example 2); Optionally pass a true `replaceChoices` value to remove any existing choices.
* Optionally pass a `customProperties` object to add additional data to your choices (useful when searching/filtering etc).
*
* **Input types affected:** select-one, select-multiple
*
* @example
* ```js
* const example = new Choices(element);
*
* example.setChoices([
* {value: 'One', label: 'Label One', disabled: true},
* {value: 'Two', label: 'Label Two', selected: true},
* {value: 'Three', label: 'Label Three'},
* ], 'value', 'label', false);
* ```
*
* @example
* ```js
* const example = new Choices(element);
*
* example.setChoices(async () => {
* try {
* const items = await fetch('/items');
* return items.json()
* } catch(err) {
* console.error(err)
* }
* });
* ```
*
* @example
* ```js
* const example = new Choices(element);
*
* example.setChoices([{
* label: 'Group one',
* id: 1,
* disabled: false,
* choices: [
* {value: 'Child One', label: 'Child One', selected: true},
* {value: 'Child Two', label: 'Child Two', disabled: true},
* {value: 'Child Three', label: 'Child Three'},
* ]
* },
* {
* label: 'Group two',
* id: 2,
* disabled: false,
* choices: [
* {value: 'Child Four', label: 'Child Four', disabled: true},
* {value: 'Child Five', label: 'Child Five'},
* {value: 'Child Six', label: 'Child Six', customProperties: {
* description: 'Custom description about child six',
* random: 'Another random custom property'
* }},
* ]
* }], 'value', 'label', false);
* ```
*/
Choices.prototype.setChoices = function (choicesArrayOrFetcher, value, label, replaceChoices, clearSearchFlag, replaceItems) {
var _this = this;
if (choicesArrayOrFetcher === void 0) { choicesArrayOrFetcher = []; }
if (value === void 0) { value = 'value'; }
if (label === void 0) { label = 'label'; }
if (replaceChoices === void 0) { replaceChoices = false; }
if (clearSearchFlag === void 0) { clearSearchFlag = true; }
if (replaceItems === void 0) { replaceItems = false; }
if (!this.initialisedOK) {
this._warnChoicesInitFailed('setChoices');
return this;
}
if (!this._isSelectElement) {
throw new TypeError("setChoices can't be used with INPUT based Choices");
}
if (typeof value !== 'string' || !value) {
throw new TypeError("value parameter must be a name of 'value' field in passed objects");
}
if (typeof choicesArrayOrFetcher === 'function') {
// it's a choices fetcher function
var fetcher_1 = choicesArrayOrFetcher(this);
if (typeof Promise === 'function' && fetcher_1 instanceof Promise) {
// that's a promise
// eslint-disable-next-line no-promise-executor-return
return new Promise(function (resolve) { return requestAnimationFrame(resolve); })
.then(function () { return _this._handleLoadingState(true); })
.then(function () { return fetcher_1; })
.then(function (data) {
return _this.setChoices(data, value, label, replaceChoices, clearSearchFlag, replaceItems);
})
.catch(function (err) {
if (!_this.config.silent) {
console.error(err);
}
})
.then(function () { return _this._handleLoadingState(false); })
.then(function () { return _this; });
}
// function returned something else than promise, let's check if it's an array of choices
if (!Array.isArray(fetcher_1)) {
throw new TypeError(".setChoices first argument function must return either array of choices or Promise, got: ".concat(typeof fetcher_1));
}
// recursion with results, it's sync and choices were cleared already
return this.setChoices(fetcher_1, value, label, false);
}
if (!Array.isArray(choicesArrayOrFetcher)) {
throw new TypeError(".setChoices must be called either with array of choices with a function resulting into Promise of array of choices");
}
this.containerOuter.removeLoadingState();
this._store.withTxn(function () {
if (clearSearchFlag) {
_this._isSearching = false;
}
// Clear choices if needed
if (replaceChoices) {
_this.clearChoices(true, replaceItems);
}
var isDefaultValue = value === 'value';
var isDefaultLabel = label === 'label';
choicesArrayOrFetcher.forEach(function (groupOrChoice) {
if ('choices' in groupOrChoice) {
var group = groupOrChoice;
if (!isDefaultLabel) {
group = __assign(__assign({}, group), { label: group[label] });
}
_this._addGroup(mapInputToChoice(group, true));
}
else {
var choice = groupOrChoice;
if (!isDefaultLabel || !isDefaultValue) {
choice = __assign(__assign({}, choice), { value: choice[value], label: choice[label] });
}
var choiceFull = mapInputToChoice(choice, false);
_this._addChoice(choiceFull);
if (choiceFull.placeholder && !_this._hasNonChoicePlaceholder) {
_this._placeholderValue = unwrapStringForEscaped(choiceFull.label);
}
}
});
_this.unhighlightAll();
});
// @todo integrate with Store
this._searcher.reset();
return this;
};
Choices.prototype.refresh = function (withEvents, selectFirstOption, deselectAll) {
var _this = this;
if (withEvents === void 0) { withEvents = false; }
if (selectFirstOption === void 0) { selectFirstOption = false; }
if (deselectAll === void 0) { deselectAll = false; }
if (!this._isSelectElement) {
if (!this.config.silent) {
console.warn('refresh method can only be used on choices backed by a