2016-05-03 15:55:38 +02:00
|
|
|
import Fuse from 'fuse.js';
|
2016-05-07 13:36:50 +02:00
|
|
|
import Store from './store/index.js';
|
2016-08-14 23:14:37 +02:00
|
|
|
import {
|
2016-09-05 23:04:15 +02:00
|
|
|
addItem,
|
|
|
|
removeItem,
|
|
|
|
highlightItem,
|
|
|
|
addChoice,
|
|
|
|
filterChoices,
|
|
|
|
activateChoices,
|
|
|
|
addGroup,
|
|
|
|
clearAll,
|
2016-09-26 18:11:32 +02:00
|
|
|
clearChoices,
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
|
|
|
from './actions/index';
|
2016-08-14 23:14:37 +02:00
|
|
|
import {
|
2016-09-05 23:04:15 +02:00
|
|
|
isScrolledIntoView,
|
|
|
|
getAdjacentEl,
|
|
|
|
wrap,
|
2017-02-17 10:23:52 +01:00
|
|
|
getType,
|
2016-09-05 23:04:15 +02:00
|
|
|
isType,
|
|
|
|
isElement,
|
|
|
|
strToEl,
|
|
|
|
extend,
|
|
|
|
getWidthOfInput,
|
|
|
|
sortByAlpha,
|
|
|
|
sortByScore,
|
2017-01-01 16:32:09 +01:00
|
|
|
triggerEvent,
|
2017-03-12 14:21:00 +01:00
|
|
|
findAncestorByAttrName
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
|
|
|
from './lib/utils.js';
|
2016-08-14 23:14:37 +02:00
|
|
|
import './lib/polyfills.js';
|
|
|
|
|
2016-04-13 15:40:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Choices
|
|
|
|
*/
|
2016-10-18 15:15:00 +02:00
|
|
|
class Choices {
|
2016-09-05 23:04:15 +02:00
|
|
|
constructor(element = '[data-choice]', userConfig = {}) {
|
|
|
|
// If there are multiple elements, create a new instance
|
|
|
|
// for each element besides the first one (as that already has an instance)
|
|
|
|
if (isType('String', element)) {
|
|
|
|
const elements = document.querySelectorAll(element);
|
|
|
|
if (elements.length > 1) {
|
|
|
|
for (let i = 1; i < elements.length; i++) {
|
|
|
|
const el = elements[i];
|
|
|
|
new Choices(el, userConfig);
|
2016-04-10 22:23:42 +02:00
|
|
|
}
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
|
|
|
}
|
2016-04-10 22:23:42 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
const defaultConfig = {
|
|
|
|
items: [],
|
|
|
|
choices: [],
|
|
|
|
maxItemCount: -1,
|
|
|
|
addItems: true,
|
|
|
|
removeItems: true,
|
|
|
|
removeItemButton: false,
|
|
|
|
editItems: false,
|
|
|
|
duplicateItems: true,
|
|
|
|
delimiter: ',',
|
|
|
|
paste: true,
|
|
|
|
search: true,
|
2017-04-06 15:42:11 +02:00
|
|
|
searchChoices: true,
|
2016-09-27 21:07:32 +02:00
|
|
|
searchFloor: 1,
|
2017-04-07 09:49:15 +02:00
|
|
|
searchFields: ['label', 'value'],
|
2017-02-21 21:47:12 +01:00
|
|
|
position: 'auto',
|
2017-01-12 15:13:21 +01:00
|
|
|
resetScrollPosition: true,
|
2016-09-05 23:04:15 +02:00
|
|
|
regexFilter: null,
|
|
|
|
shouldSort: true,
|
|
|
|
sortFilter: sortByAlpha,
|
|
|
|
placeholder: true,
|
|
|
|
placeholderValue: null,
|
|
|
|
prependValue: null,
|
|
|
|
appendValue: null,
|
|
|
|
loadingText: 'Loading...',
|
2016-10-21 06:43:32 +02:00
|
|
|
noResultsText: 'No results found',
|
2016-09-05 23:04:15 +02:00
|
|
|
noChoicesText: 'No choices to choose from',
|
2016-09-26 11:36:04 +02:00
|
|
|
itemSelectText: 'Press to select',
|
2016-11-07 11:27:04 +01:00
|
|
|
addItemText: (value) => {
|
|
|
|
return `Press Enter to add <b>"${value}"</b>`;
|
|
|
|
},
|
|
|
|
maxItemText: (maxItemCount) => {
|
|
|
|
return `Only ${maxItemCount} values can be added.`;
|
|
|
|
},
|
|
|
|
uniqueItemText: 'Only unique values can be added.',
|
2016-09-05 23:04:15 +02:00
|
|
|
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',
|
|
|
|
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',
|
|
|
|
hiddenState: 'is-hidden',
|
|
|
|
flippedState: 'is-flipped',
|
|
|
|
loadingState: 'is-loading',
|
|
|
|
},
|
2017-01-01 16:59:43 +01:00
|
|
|
fuseOptions: {
|
|
|
|
include: 'score',
|
|
|
|
},
|
2016-09-27 21:07:32 +02:00
|
|
|
callbackOnInit: null,
|
2016-09-30 09:40:06 +02:00
|
|
|
callbackOnCreateTemplates: null,
|
2016-09-05 23:04:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Retrieve triggering element (i.e. element with 'data-choice' trigger)
|
2016-10-22 21:15:28 +02:00
|
|
|
this.element = element;
|
2016-09-05 23:04:15 +02:00
|
|
|
this.passedElement = isType('String', element) ? document.querySelector(element) : element;
|
2016-10-26 16:43:15 +02:00
|
|
|
this.isSelectElement = this.passedElement.type === 'select-one' || this.passedElement.type === 'select-multiple';
|
|
|
|
this.isTextElement = this.passedElement.type === 'text';
|
2016-09-05 23:04:15 +02:00
|
|
|
|
|
|
|
if (!this.passedElement) {
|
|
|
|
console.error('Passed element not found');
|
|
|
|
return;
|
|
|
|
}
|
2016-03-15 23:42:10 +01:00
|
|
|
|
2016-11-18 16:01:17 +01:00
|
|
|
// It only makes sense for addItems to be true for
|
|
|
|
// text inputs by default
|
|
|
|
if (this.isSelectElement) {
|
|
|
|
defaultConfig.addItems = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge options with user options
|
|
|
|
this.config = extend(defaultConfig, userConfig);
|
2016-04-04 22:44:32 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
// Assing preset choices from passed object
|
|
|
|
this.presetChoices = this.config.choices;
|
2016-05-04 15:31:29 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
// Assign preset items from passed object first
|
|
|
|
this.presetItems = this.config.items;
|
2016-08-31 20:18:46 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
// Then add any values passed from attribute
|
|
|
|
if (this.passedElement.value) {
|
|
|
|
this.presetItems = this.presetItems.concat(this.passedElement.value.split(this.config.delimiter));
|
|
|
|
}
|
2016-09-04 14:44:31 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
// Bind methods
|
|
|
|
this.init = this.init.bind(this);
|
|
|
|
this.render = this.render.bind(this);
|
|
|
|
this.destroy = this.destroy.bind(this);
|
|
|
|
this.disable = this.disable.bind(this);
|
|
|
|
|
|
|
|
// Bind event handlers
|
|
|
|
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._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._onPaste = this._onPaste.bind(this);
|
|
|
|
this._onInput = this._onInput.bind(this);
|
|
|
|
|
2016-11-18 16:01:17 +01:00
|
|
|
// Create data store
|
|
|
|
this.store = new Store(this.render);
|
|
|
|
|
|
|
|
// State tracking
|
|
|
|
this.initialised = false;
|
|
|
|
this.currentState = {};
|
|
|
|
this.prevState = {};
|
|
|
|
this.currentValue = '';
|
|
|
|
this.highlightPosition = 0;
|
|
|
|
this.canSearch = this.config.search;
|
2016-09-05 23:04:15 +02:00
|
|
|
this.wasTap = true;
|
|
|
|
|
|
|
|
// Cutting the mustard
|
2016-10-26 16:43:15 +02:00
|
|
|
const cuttingTheMustard = 'classList' in document.documentElement;
|
2016-09-05 23:04:15 +02:00
|
|
|
if (!cuttingTheMustard) console.error('Choices: Your browser doesn\'t support Choices');
|
|
|
|
|
|
|
|
// Input type check
|
2016-09-15 22:34:10 +02:00
|
|
|
const isValidType = ['select-one', 'select-multiple', 'text'].some(type => type === this.passedElement.type);
|
|
|
|
const canInit = isElement(this.passedElement) && isValidType;
|
2016-09-05 23:04:15 +02:00
|
|
|
|
|
|
|
if (canInit) {
|
|
|
|
// If element has already been initalised with Choices
|
|
|
|
if (this.passedElement.getAttribute('data-choice') === 'active') return;
|
|
|
|
|
|
|
|
// Let's go
|
|
|
|
this.init();
|
|
|
|
} else {
|
|
|
|
console.error('Incompatible input passed');
|
|
|
|
}
|
|
|
|
}
|
2016-09-04 23:23:20 +02:00
|
|
|
|
2016-09-27 14:44:35 +02:00
|
|
|
/*========================================
|
|
|
|
= Public functions =
|
|
|
|
========================================*/
|
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
/**
|
|
|
|
* Initialise Choices
|
|
|
|
* @return
|
|
|
|
* @public
|
|
|
|
*/
|
2016-10-19 08:33:38 +02:00
|
|
|
init() {
|
2016-09-05 23:31:20 +02:00
|
|
|
if (this.initialised === true) return;
|
|
|
|
|
2016-10-19 08:33:38 +02:00
|
|
|
const callback = this.config.callbackOnInit;
|
|
|
|
|
2016-09-05 23:31:20 +02:00
|
|
|
// Set initialise flag
|
|
|
|
this.initialised = true;
|
|
|
|
// Create required elements
|
|
|
|
this._createTemplates();
|
|
|
|
// Generate input markup
|
|
|
|
this._createInput();
|
|
|
|
// Subscribe store to render method
|
|
|
|
this.store.subscribe(this.render);
|
|
|
|
// Render any items
|
|
|
|
this.render();
|
|
|
|
// Trigger event listeners
|
|
|
|
this._addEventListeners();
|
2016-05-02 22:39:33 +02:00
|
|
|
|
2016-09-05 23:31:20 +02:00
|
|
|
// Run callback if it is a function
|
|
|
|
if (callback) {
|
|
|
|
if (isType('Function', callback)) {
|
2016-11-07 11:37:47 +01:00
|
|
|
callback.call(this);
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
2016-03-17 16:00:22 +01:00
|
|
|
}
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroy Choices and nullify values
|
|
|
|
* @return
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
destroy() {
|
2016-09-05 23:31:20 +02:00
|
|
|
if (this.initialised === false) return;
|
2016-09-05 23:04:15 +02:00
|
|
|
|
2016-09-05 23:31:20 +02:00
|
|
|
// Remove all event listeners
|
|
|
|
this._removeEventListeners();
|
2016-09-05 23:04:15 +02:00
|
|
|
|
2016-09-05 23:31:20 +02:00
|
|
|
// Reinstate passed element
|
|
|
|
this.passedElement.classList.remove(this.config.classNames.input, this.config.classNames.hiddenState);
|
2016-10-22 21:15:28 +02:00
|
|
|
this.passedElement.removeAttribute('tabindex');
|
2016-09-05 23:31:20 +02:00
|
|
|
this.passedElement.removeAttribute('style', 'display:none;');
|
|
|
|
this.passedElement.removeAttribute('aria-hidden');
|
2016-10-22 21:15:28 +02:00
|
|
|
this.passedElement.removeAttribute('data-choice', 'active');
|
2016-09-05 23:04:15 +02:00
|
|
|
|
2016-10-22 21:15:28 +02:00
|
|
|
// Re-assign values - this is weird, I know
|
|
|
|
this.passedElement.value = this.passedElement.value;
|
2016-09-05 23:04:15 +02:00
|
|
|
|
2016-10-22 21:15:28 +02:00
|
|
|
// Move passed element back to original position
|
|
|
|
this.containerOuter.parentNode.insertBefore(this.passedElement, this.containerOuter);
|
|
|
|
// Remove added elements
|
|
|
|
this.containerOuter.parentNode.removeChild(this.containerOuter);
|
|
|
|
|
|
|
|
// Clear data store
|
|
|
|
this.clearStore();
|
|
|
|
|
|
|
|
// Nullify instance-specific data
|
|
|
|
this.config.templates = null;
|
2016-09-05 23:31:20 +02:00
|
|
|
|
|
|
|
// Uninitialise
|
|
|
|
this.initialised = false;
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
|
|
|
|
2016-09-27 14:44:35 +02:00
|
|
|
/**
|
|
|
|
* Render group choices into a DOM fragment and append to choice list
|
|
|
|
* @param {Array} groups Groups to add to list
|
|
|
|
* @param {Array} choices Choices to add to groups
|
|
|
|
* @param {DocumentFragment} fragment Fragment to add groups and options to (optional)
|
|
|
|
* @return {DocumentFragment} Populated options fragment
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
renderGroups(groups, choices, fragment) {
|
|
|
|
const groupFragment = fragment || document.createDocumentFragment();
|
|
|
|
const filter = this.config.sortFilter;
|
|
|
|
|
|
|
|
// If sorting is enabled, filter groups
|
|
|
|
if (this.config.shouldSort) {
|
|
|
|
groups.sort(filter);
|
|
|
|
}
|
|
|
|
|
|
|
|
groups.forEach((group) => {
|
|
|
|
// Grab options that are children of this group
|
|
|
|
const groupChoices = choices.filter((choice) => {
|
|
|
|
if (this.passedElement.type === 'select-one') {
|
|
|
|
return choice.groupId === group.id;
|
|
|
|
}
|
|
|
|
return choice.groupId === group.id && !choice.selected;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (groupChoices.length >= 1) {
|
|
|
|
const dropdownGroup = this._getTemplate('choiceGroup', group);
|
|
|
|
groupFragment.appendChild(dropdownGroup);
|
|
|
|
this.renderChoices(groupChoices, groupFragment);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return groupFragment;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render choices into a DOM fragment and append to choice list
|
|
|
|
* @param {Array} choices Choices to add to list
|
|
|
|
* @param {DocumentFragment} fragment Fragment to add choices to (optional)
|
|
|
|
* @return {DocumentFragment} Populated choices fragment
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
renderChoices(choices, fragment) {
|
|
|
|
// Create a fragment to store our list items (so we don't have to update the DOM for each item)
|
|
|
|
const choicesFragment = fragment || document.createDocumentFragment();
|
|
|
|
const filter = this.isSearching ? sortByScore : this.config.sortFilter;
|
|
|
|
|
|
|
|
// If sorting is enabled or the user is searching, filter choices
|
|
|
|
if (this.config.shouldSort || this.isSearching) {
|
|
|
|
choices.sort(filter);
|
|
|
|
}
|
|
|
|
|
|
|
|
choices.forEach((choice) => {
|
|
|
|
const dropdownItem = this._getTemplate('choice', choice);
|
|
|
|
const shouldRender = this.passedElement.type === 'select-one' || !choice.selected;
|
|
|
|
if (shouldRender) {
|
|
|
|
choicesFragment.appendChild(dropdownItem);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return choicesFragment;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render items into a DOM fragment and append to items list
|
|
|
|
* @param {Array} items Items to add to list
|
|
|
|
* @param {DocumentFragment} fragment Fragrment to add items to (optional)
|
|
|
|
* @return
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
renderItems(items, fragment) {
|
|
|
|
// Create fragment to add elements to
|
|
|
|
const itemListFragment = fragment || document.createDocumentFragment();
|
|
|
|
// Simplify store data to just values
|
|
|
|
const itemsFiltered = this.store.getItemsReducedToValues(items);
|
|
|
|
|
2016-10-26 16:43:15 +02:00
|
|
|
if (this.isTextElement) {
|
2016-09-27 14:44:35 +02:00
|
|
|
// Assign hidden input array of values
|
|
|
|
this.passedElement.setAttribute('value', itemsFiltered.join(this.config.delimiter));
|
|
|
|
} else {
|
|
|
|
const selectedOptionsFragment = document.createDocumentFragment();
|
|
|
|
|
|
|
|
// Add each list item to list
|
|
|
|
items.forEach((item) => {
|
|
|
|
// Create a standard select option
|
|
|
|
const option = this._getTemplate('option', item);
|
|
|
|
// Append it to fragment
|
|
|
|
selectedOptionsFragment.appendChild(option);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Update selected choices
|
|
|
|
this.passedElement.innerHTML = '';
|
|
|
|
this.passedElement.appendChild(selectedOptionsFragment);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add each list item to list
|
|
|
|
items.forEach((item) => {
|
|
|
|
// Create new list element
|
|
|
|
const listItem = this._getTemplate('item', item);
|
|
|
|
// Append it to list
|
|
|
|
itemListFragment.appendChild(listItem);
|
|
|
|
});
|
|
|
|
|
|
|
|
return itemListFragment;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render DOM with values
|
|
|
|
* @return
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
render() {
|
|
|
|
this.currentState = this.store.getState();
|
|
|
|
|
|
|
|
// Only render if our state has actually changed
|
|
|
|
if (this.currentState !== this.prevState) {
|
|
|
|
// Choices
|
2016-10-22 21:15:28 +02:00
|
|
|
if (this.currentState.choices !== this.prevState.choices ||
|
|
|
|
this.currentState.groups !== this.prevState.groups) {
|
2016-11-08 15:23:25 +01:00
|
|
|
if (!this.isTextElement) {
|
2016-09-27 14:44:35 +02:00
|
|
|
// Get active groups/choices
|
|
|
|
const activeGroups = this.store.getGroupsFilteredByActive();
|
|
|
|
const activeChoices = this.store.getChoicesFilteredByActive();
|
|
|
|
|
|
|
|
let choiceListFragment = document.createDocumentFragment();
|
|
|
|
|
|
|
|
// Clear choices
|
|
|
|
this.choiceList.innerHTML = '';
|
2017-01-12 15:13:21 +01:00
|
|
|
|
2016-09-27 14:44:35 +02:00
|
|
|
// Scroll back to top of choices list
|
2017-01-12 15:13:21 +01:00
|
|
|
if(this.config.resetScrollPosition){
|
|
|
|
this.choiceList.scrollTop = 0;
|
|
|
|
}
|
2016-09-27 14:44:35 +02:00
|
|
|
|
|
|
|
// If we have grouped options
|
|
|
|
if (activeGroups.length >= 1 && this.isSearching !== true) {
|
|
|
|
choiceListFragment = this.renderGroups(activeGroups, activeChoices, choiceListFragment);
|
|
|
|
} else if (activeChoices.length >= 1) {
|
|
|
|
choiceListFragment = this.renderChoices(activeChoices, choiceListFragment);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (choiceListFragment.childNodes && choiceListFragment.childNodes.length > 0) {
|
|
|
|
// If we actually have anything to add to our dropdown
|
|
|
|
// append it and highlight the first choice
|
|
|
|
this.choiceList.appendChild(choiceListFragment);
|
|
|
|
} else {
|
2016-11-08 15:23:25 +01:00
|
|
|
const activeItems = this.store.getItemsFilteredByActive();
|
|
|
|
const canAddItem = this._canAddItem(activeItems, this.input.value);
|
|
|
|
let dropdownItem = this._getTemplate('notice', this.config.noChoicesText);
|
|
|
|
|
2016-11-18 16:01:17 +01:00
|
|
|
if (this.config.addItems && canAddItem.notice) {
|
2016-11-08 15:23:25 +01:00
|
|
|
dropdownItem = this._getTemplate('notice', canAddItem.notice);
|
|
|
|
} else if (this.isSearching) {
|
|
|
|
dropdownItem = this._getTemplate('notice', this.config.noResultsText);
|
2017-02-09 14:49:30 +01:00
|
|
|
}
|
|
|
|
|
2016-09-27 14:44:35 +02:00
|
|
|
this.choiceList.appendChild(dropdownItem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Items
|
|
|
|
if (this.currentState.items !== this.prevState.items) {
|
|
|
|
const activeItems = this.store.getItemsFilteredByActive();
|
|
|
|
if (activeItems) {
|
|
|
|
// Create a fragment to store our list items
|
|
|
|
// (so we don't have to update the DOM for each item)
|
|
|
|
const itemListFragment = this.renderItems(activeItems);
|
|
|
|
|
|
|
|
// Clear list
|
|
|
|
this.itemList.innerHTML = '';
|
|
|
|
|
|
|
|
// If we have items to add
|
|
|
|
if (itemListFragment.childNodes) {
|
|
|
|
// Update list
|
|
|
|
this.itemList.appendChild(itemListFragment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.prevState = this.currentState;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
/**
|
|
|
|
* Select item (a selected item can be deleted)
|
|
|
|
* @param {Element} item Element to select
|
|
|
|
* @return {Object} Class instance
|
|
|
|
* @public
|
|
|
|
*/
|
2017-01-01 16:32:09 +01:00
|
|
|
highlightItem(item, runEvent = true) {
|
2016-09-05 23:04:15 +02:00
|
|
|
if (!item) return;
|
|
|
|
const id = item.id;
|
2016-10-19 08:33:38 +02:00
|
|
|
const groupId = item.groupId;
|
2017-01-01 16:32:09 +01:00
|
|
|
const group = groupId >= 0 ? this.store.getGroupById(groupId) : null;
|
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
this.store.dispatch(highlightItem(id, true));
|
|
|
|
|
2017-01-01 16:32:09 +01:00
|
|
|
if (runEvent) {
|
|
|
|
if(group && group.value) {
|
|
|
|
triggerEvent(this.passedElement, 'highlightItem', {
|
|
|
|
id,
|
|
|
|
value: item.value,
|
2017-03-01 20:07:22 +01:00
|
|
|
label: item.label,
|
2017-01-01 16:32:09 +01:00
|
|
|
groupValue: group.value
|
|
|
|
});
|
2016-09-05 23:04:15 +02:00
|
|
|
} else {
|
2017-01-01 16:32:09 +01:00
|
|
|
triggerEvent(this.passedElement, 'highlightItem', {
|
|
|
|
id,
|
|
|
|
value: item.value,
|
2017-03-01 20:07:22 +01:00
|
|
|
label: item.label,
|
2017-01-01 16:32:09 +01:00
|
|
|
});
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
2016-06-08 15:45:29 +02:00
|
|
|
}
|
2016-08-14 23:14:37 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deselect item
|
|
|
|
* @param {Element} item Element to de-select
|
|
|
|
* @return {Object} Class instance
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
unhighlightItem(item) {
|
|
|
|
if (!item) return;
|
|
|
|
const id = item.id;
|
2016-10-19 08:33:38 +02:00
|
|
|
const groupId = item.groupId;
|
2017-01-01 16:32:09 +01:00
|
|
|
const group = groupId >= 0 ? this.store.getGroupById(groupId) : null;
|
2016-10-19 08:33:38 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
this.store.dispatch(highlightItem(id, false));
|
|
|
|
|
2017-01-01 16:32:09 +01:00
|
|
|
if(group && group.value) {
|
|
|
|
triggerEvent(this.passedElement, 'unhighlightItem', {
|
|
|
|
id,
|
|
|
|
value: item.value,
|
2017-03-01 20:07:22 +01:00
|
|
|
label: item.label,
|
2017-01-01 16:32:09 +01:00
|
|
|
groupValue: group.value
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
triggerEvent(this.passedElement, 'unhighlightItem', {
|
|
|
|
id,
|
|
|
|
value: item.value,
|
2017-03-01 20:07:22 +01:00
|
|
|
label: item.label,
|
2017-01-01 16:32:09 +01:00
|
|
|
});
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
2016-08-14 23:14:37 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Highlight items within store
|
|
|
|
* @return {Object} Class instance
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
highlightAll() {
|
|
|
|
const items = this.store.getItems();
|
|
|
|
items.forEach((item) => {
|
|
|
|
this.highlightItem(item);
|
|
|
|
});
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deselect items within store
|
|
|
|
* @return {Object} Class instance
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
unhighlightAll() {
|
|
|
|
const items = this.store.getItems();
|
|
|
|
items.forEach((item) => {
|
|
|
|
this.unhighlightItem(item);
|
|
|
|
});
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove an item from the store by its value
|
|
|
|
* @param {String} value Value to search for
|
|
|
|
* @return {Object} Class instance
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
removeItemsByValue(value) {
|
|
|
|
if (!value || !isType('String', value)) {
|
|
|
|
console.error('removeItemsByValue: No value was passed to be removed');
|
|
|
|
return;
|
|
|
|
}
|
2016-06-08 15:45:29 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
const items = this.store.getItemsFilteredByActive();
|
|
|
|
|
|
|
|
items.forEach((item) => {
|
|
|
|
if (item.value === value) {
|
|
|
|
this._removeItem(item);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove all items from store array
|
|
|
|
* @note Removed items are soft deleted
|
|
|
|
* @param {Number} excludedId Optionally exclude item by ID
|
|
|
|
* @return {Object} Class instance
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
removeActiveItems(excludedId) {
|
|
|
|
const items = this.store.getItemsFilteredByActive();
|
|
|
|
|
|
|
|
items.forEach((item) => {
|
|
|
|
if (item.active && excludedId !== item.id) {
|
|
|
|
this._removeItem(item);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove all selected items from store
|
|
|
|
* @note Removed items are soft deleted
|
|
|
|
* @return {Object} Class instance
|
|
|
|
* @public
|
|
|
|
*/
|
2017-01-01 16:32:09 +01:00
|
|
|
removeHighlightedItems(runEvent = false) {
|
2016-09-05 23:04:15 +02:00
|
|
|
const items = this.store.getItemsFilteredByActive();
|
|
|
|
|
|
|
|
items.forEach((item) => {
|
|
|
|
if (item.highlighted && item.active) {
|
|
|
|
this._removeItem(item);
|
2016-09-20 13:57:44 +02:00
|
|
|
// If this action was performed by the user
|
2017-01-01 16:32:09 +01:00
|
|
|
// trigger the event
|
|
|
|
if (runEvent) {
|
2016-09-20 13:57:44 +02:00
|
|
|
this._triggerChange(item.value);
|
|
|
|
}
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show dropdown to user by adding active state class
|
|
|
|
* @return {Object} Class instance
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
showDropdown(focusInput = false) {
|
|
|
|
const body = document.body;
|
|
|
|
const html = document.documentElement;
|
2017-02-21 21:47:12 +01:00
|
|
|
const winHeight = Math.max(
|
|
|
|
body.scrollHeight,
|
|
|
|
body.offsetHeight,
|
|
|
|
html.clientHeight,
|
|
|
|
html.scrollHeight,
|
|
|
|
html.offsetHeight
|
|
|
|
);
|
2016-09-05 23:04:15 +02:00
|
|
|
|
|
|
|
this.containerOuter.classList.add(this.config.classNames.openState);
|
|
|
|
this.containerOuter.setAttribute('aria-expanded', 'true');
|
|
|
|
this.dropdown.classList.add(this.config.classNames.activeState);
|
2017-03-28 15:08:38 +02:00
|
|
|
this.dropdown.setAttribute('aria-expanded', 'true');
|
2016-09-05 23:04:15 +02:00
|
|
|
|
|
|
|
const dimensions = this.dropdown.getBoundingClientRect();
|
|
|
|
const dropdownPos = Math.ceil(dimensions.top + window.scrollY + dimensions.height);
|
2017-02-21 21:47:12 +01:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
// If flip is enabled and the dropdown bottom position is greater than the window height flip the dropdown.
|
2017-02-21 21:47:12 +01:00
|
|
|
let shouldFlip = false;
|
|
|
|
if (this.config.position === 'auto') {
|
|
|
|
shouldFlip = dropdownPos >= winHeight;
|
|
|
|
} else if (this.config.position === 'top') {
|
|
|
|
shouldFlip = true;
|
|
|
|
}
|
2016-09-05 23:04:15 +02:00
|
|
|
|
|
|
|
if (shouldFlip) {
|
|
|
|
this.containerOuter.classList.add(this.config.classNames.flippedState);
|
|
|
|
} else {
|
|
|
|
this.containerOuter.classList.remove(this.config.classNames.flippedState);
|
|
|
|
}
|
2016-09-04 15:11:07 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
// Optionally focus the input if we have a search input
|
|
|
|
if (focusInput && this.canSearch && document.activeElement !== this.input) {
|
|
|
|
this.input.focus();
|
2016-06-08 15:45:29 +02:00
|
|
|
}
|
|
|
|
|
2017-03-12 14:17:46 +01:00
|
|
|
triggerEvent(this.passedElement, 'showDropdown', {});
|
2017-03-02 17:13:53 +01:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
return this;
|
|
|
|
}
|
2016-08-04 14:21:24 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
/**
|
|
|
|
* Hide dropdown from user
|
|
|
|
* @return {Object} Class instance
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
hideDropdown(blurInput = false) {
|
|
|
|
// A dropdown flips if it does not have space within the page
|
|
|
|
const isFlipped = this.containerOuter.classList.contains(this.config.classNames.flippedState);
|
2016-06-08 15:45:29 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
this.containerOuter.classList.remove(this.config.classNames.openState);
|
|
|
|
this.containerOuter.setAttribute('aria-expanded', 'false');
|
|
|
|
this.dropdown.classList.remove(this.config.classNames.activeState);
|
2017-03-28 15:08:38 +02:00
|
|
|
this.dropdown.setAttribute('aria-expanded', 'false');
|
2016-06-08 15:45:29 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
if (isFlipped) {
|
|
|
|
this.containerOuter.classList.remove(this.config.classNames.flippedState);
|
|
|
|
}
|
2016-06-08 15:45:29 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
// Optionally blur the input if we have a search input
|
|
|
|
if (blurInput && this.canSearch && document.activeElement === this.input) {
|
|
|
|
this.input.blur();
|
2016-06-08 15:45:29 +02:00
|
|
|
}
|
|
|
|
|
2017-03-12 14:17:46 +01:00
|
|
|
triggerEvent(this.passedElement, 'hideDropdown', {});
|
2017-03-02 17:13:53 +01:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine whether to hide or show dropdown based on its current state
|
|
|
|
* @return {Object} Class instance
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
toggleDropdown() {
|
|
|
|
const hasActiveDropdown = this.dropdown.classList.contains(this.config.classNames.activeState);
|
|
|
|
if (hasActiveDropdown) {
|
|
|
|
this.hideDropdown();
|
|
|
|
} else {
|
|
|
|
this.showDropdown(true);
|
|
|
|
}
|
2016-06-08 15:45:29 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get value(s) of input (i.e. inputted items (text) or selected choices (select))
|
|
|
|
* @param {Boolean} valueOnly Get only values of selected items, otherwise return selected items
|
|
|
|
* @return {Array/String} selected value (select-one) or array of selected items (inputs & select-multiple)
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
getValue(valueOnly = false) {
|
|
|
|
const items = this.store.getItemsFilteredByActive();
|
|
|
|
const selectedItems = [];
|
|
|
|
|
|
|
|
items.forEach((item) => {
|
2016-10-26 16:43:15 +02:00
|
|
|
if (this.isTextElement) {
|
2016-09-05 23:04:15 +02:00
|
|
|
selectedItems.push(valueOnly ? item.value : item);
|
|
|
|
} else if (item.active) {
|
|
|
|
selectedItems.push(valueOnly ? item.value : item);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (this.passedElement.type === 'select-one') {
|
|
|
|
return selectedItems[0];
|
2016-06-08 15:45:29 +02:00
|
|
|
}
|
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
return selectedItems;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set value of input. If the input is a select box, a choice will be created and selected otherwise
|
|
|
|
* an item will created directly.
|
2017-02-17 10:27:01 +01:00
|
|
|
* @param {Array} args Array of value objects or value strings
|
2016-09-05 23:04:15 +02:00
|
|
|
* @return {Object} Class instance
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
setValue(args) {
|
|
|
|
if (this.initialised === true) {
|
2017-02-17 10:23:52 +01:00
|
|
|
// Convert args to an iterable array
|
2016-10-26 16:43:15 +02:00
|
|
|
const values = [...args],
|
2017-02-17 10:23:52 +01:00
|
|
|
passedElementType = this.passedElement.type,
|
|
|
|
handleValue = (item) => {
|
|
|
|
const itemType = getType(item);
|
|
|
|
if (itemType === 'Object') {
|
|
|
|
if (!item.value) return;
|
|
|
|
// If we are dealing with a select input, we need to create an option first
|
|
|
|
// that is then selected. For text inputs we can just add items normally.
|
|
|
|
if (passedElementType !== 'text') {
|
|
|
|
this._addChoice(true, false, item.value, item.label, -1);
|
|
|
|
} else {
|
|
|
|
this._addItem(item.value, item.label, item.id);
|
|
|
|
}
|
|
|
|
} else if (itemType === 'String') {
|
|
|
|
if (passedElementType !== 'text') {
|
|
|
|
this._addChoice(true, false, item, item, -1);
|
|
|
|
} else {
|
|
|
|
this._addItem(item);
|
|
|
|
}
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
2017-02-17 10:23:52 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
if (values.length > 1) {
|
|
|
|
values.forEach((value) => {
|
|
|
|
handleValue(value);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
handleValue(values[0]);
|
|
|
|
}
|
2016-06-08 15:45:29 +02:00
|
|
|
}
|
2016-09-05 23:04:15 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Select value of select box via the value of an existing choice
|
|
|
|
* @param {Array/String} value An array of strings of a single string
|
|
|
|
* @return {Object} Class instance
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
setValueByChoice(value) {
|
|
|
|
if (this.passedElement.type !== 'text') {
|
|
|
|
const choices = this.store.getChoices();
|
|
|
|
// If only one value has been passed, convert to array
|
|
|
|
const choiceValue = isType('Array', value) ? value : [value];
|
|
|
|
|
|
|
|
// Loop through each value and
|
|
|
|
choiceValue.forEach((val) => {
|
|
|
|
const foundChoice = choices.find((choice) => {
|
|
|
|
// Check 'value' property exists and the choice isn't already selected
|
|
|
|
return choice.value === val;
|
2016-06-08 15:45:29 +02:00
|
|
|
});
|
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
if (foundChoice) {
|
|
|
|
if (!foundChoice.selected) {
|
2016-10-18 20:39:49 +02:00
|
|
|
this._addItem(foundChoice.value, foundChoice.label, foundChoice.id, foundChoice.groupId);
|
2016-09-05 23:04:15 +02:00
|
|
|
} else {
|
|
|
|
console.warn('Attempting to select choice already selected');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
console.warn('Attempting to select choice that does not exist');
|
|
|
|
}
|
|
|
|
});
|
2016-06-08 15:45:29 +02:00
|
|
|
}
|
2016-09-05 23:04:15 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Direct populate choices
|
|
|
|
* @param {Array} choices - Choices to insert
|
|
|
|
* @param {String} value - Name of 'value' property
|
|
|
|
* @param {String} label - Name of 'label' property
|
2016-10-19 08:33:38 +02:00
|
|
|
* @param {Boolean} replaceChoices Whether existing choices should be removed
|
2016-09-05 23:04:15 +02:00
|
|
|
* @return {Object} Class instance
|
|
|
|
* @public
|
|
|
|
*/
|
2016-10-19 08:35:42 +02:00
|
|
|
setChoices(choices, value, label, replaceChoices = false) {
|
2016-09-05 23:04:15 +02:00
|
|
|
if (this.initialised === true) {
|
2016-10-26 16:43:15 +02:00
|
|
|
if (this.isSelectElement) {
|
2016-09-05 23:04:15 +02:00
|
|
|
if (!isType('Array', choices) || !value) return;
|
2016-10-19 08:33:38 +02:00
|
|
|
// Clear choices if needed
|
|
|
|
if(replaceChoices) {
|
|
|
|
this._clearChoices();
|
|
|
|
}
|
|
|
|
// Add choices if passed
|
2016-09-05 23:04:15 +02:00
|
|
|
if (choices && choices.length) {
|
|
|
|
this.containerOuter.classList.remove(this.config.classNames.loadingState);
|
|
|
|
choices.forEach((result, index) => {
|
2016-09-05 23:31:20 +02:00
|
|
|
const isSelected = result.selected ? result.selected : false;
|
|
|
|
const isDisabled = result.disabled ? result.disabled : false;
|
2016-09-05 23:04:15 +02:00
|
|
|
if (result.choices) {
|
2017-02-05 10:41:59 +01:00
|
|
|
this._addGroup(result, (result.id || null), value, label);
|
2016-09-05 23:04:15 +02:00
|
|
|
} else {
|
2016-09-05 23:31:20 +02:00
|
|
|
this._addChoice(isSelected, isDisabled, result[value], result[label]);
|
2016-06-08 15:45:29 +02:00
|
|
|
}
|
2016-09-05 23:04:15 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2016-06-08 15:45:29 +02:00
|
|
|
}
|
2016-09-05 23:04:15 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear items,choices and groups
|
|
|
|
* @note Hard delete
|
|
|
|
* @return {Object} Class instance
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
clearStore() {
|
|
|
|
this.store.dispatch(clearAll());
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set value of input to blank
|
|
|
|
* @return {Object} Class instance
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
clearInput() {
|
|
|
|
if (this.input.value) this.input.value = '';
|
|
|
|
if (this.passedElement.type !== 'select-one') {
|
2016-10-22 22:04:13 +02:00
|
|
|
this._setInputWidth();
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
|
|
|
if (this.passedElement.type !== 'text' && this.config.search) {
|
|
|
|
this.isSearching = false;
|
|
|
|
this.store.dispatch(activateChoices(true));
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2016-09-27 14:44:35 +02:00
|
|
|
/**
|
|
|
|
* Enable interaction with Choices
|
|
|
|
* @return {Object} Class instance
|
|
|
|
*/
|
|
|
|
enable() {
|
|
|
|
this.passedElement.disabled = false;
|
|
|
|
const isDisabled = this.containerOuter.classList.contains(this.config.classNames.disabledState);
|
|
|
|
if (this.initialised && isDisabled) {
|
|
|
|
this._addEventListeners();
|
|
|
|
this.passedElement.removeAttribute('disabled');
|
|
|
|
this.input.removeAttribute('disabled');
|
|
|
|
this.containerOuter.classList.remove(this.config.classNames.disabledState);
|
|
|
|
this.containerOuter.removeAttribute('aria-disabled');
|
2017-04-04 12:58:16 +02:00
|
|
|
if (this.passedElement.type === 'select-one') {
|
|
|
|
this.containerOuter.setAttribute('tabindex', '0');
|
|
|
|
}
|
2016-09-27 14:44:35 +02:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
/**
|
|
|
|
* Disable interaction with Choices
|
|
|
|
* @return {Object} Class instance
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
disable() {
|
|
|
|
this.passedElement.disabled = true;
|
2016-09-05 23:31:20 +02:00
|
|
|
const isEnabled = !this.containerOuter.classList.contains(this.config.classNames.disabledState);
|
|
|
|
if (this.initialised && isEnabled) {
|
|
|
|
this._removeEventListeners();
|
|
|
|
this.passedElement.setAttribute('disabled', '');
|
|
|
|
this.input.setAttribute('disabled', '');
|
|
|
|
this.containerOuter.classList.add(this.config.classNames.disabledState);
|
|
|
|
this.containerOuter.setAttribute('aria-disabled', 'true');
|
2017-04-04 12:58:16 +02:00
|
|
|
if (this.passedElement.type === 'select-one') {
|
|
|
|
this.containerOuter.setAttribute('tabindex', '-1');
|
|
|
|
}
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Populate options via ajax callback
|
|
|
|
* @param {Function} fn Passed
|
|
|
|
* @return {Object} Class instance
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
ajax(fn) {
|
|
|
|
if (this.initialised === true) {
|
2016-10-26 16:43:15 +02:00
|
|
|
if (this.isSelectElement) {
|
2016-09-27 10:11:22 +02:00
|
|
|
// Show loading text
|
2016-09-29 15:25:08 +02:00
|
|
|
this._handleLoadingState(true);
|
2016-09-27 21:07:32 +02:00
|
|
|
// Run callback
|
|
|
|
fn(this._ajaxCallback());
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
2016-06-08 15:45:29 +02:00
|
|
|
}
|
2016-09-05 23:04:15 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2016-09-27 14:44:35 +02:00
|
|
|
/*===== End of Public functions ======*/
|
|
|
|
|
|
|
|
/*=============================================
|
|
|
|
= Private functions =
|
|
|
|
=============================================*/
|
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
/**
|
|
|
|
* Call change callback
|
|
|
|
* @param {String} value - last added/deleted/selected value
|
|
|
|
* @return
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_triggerChange(value) {
|
|
|
|
if (!value) return;
|
|
|
|
|
2017-01-01 16:32:09 +01:00
|
|
|
triggerEvent(this.passedElement, 'change', {
|
|
|
|
value
|
|
|
|
});
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
2016-06-08 15:45:29 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
/**
|
|
|
|
* Process enter/click of an item button
|
|
|
|
* @param {Array} activeItems The currently active items
|
|
|
|
* @param {Element} element Button being interacted with
|
|
|
|
* @return
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_handleButtonAction(activeItems, element) {
|
|
|
|
if (!activeItems || !element) return;
|
2016-08-02 22:02:52 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
// If we are clicking on a button
|
|
|
|
if (this.config.removeItems && this.config.removeItemButton) {
|
|
|
|
const itemId = element.parentNode.getAttribute('data-id');
|
|
|
|
const itemToRemove = activeItems.find((item) => item.id === parseInt(itemId, 10));
|
2016-06-08 15:45:29 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
// Remove item associated with button
|
|
|
|
this._removeItem(itemToRemove);
|
|
|
|
this._triggerChange(itemToRemove.value);
|
2016-06-08 15:45:29 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
if (this.passedElement.type === 'select-one') {
|
2016-09-24 11:52:05 +02:00
|
|
|
const placeholder = this.config.placeholder ? this.config.placeholderValue || this.passedElement.getAttribute('placeholder') :
|
|
|
|
false;
|
2016-09-05 23:04:15 +02:00
|
|
|
if (placeholder) {
|
|
|
|
const placeholderItem = this._getTemplate('placeholder', placeholder);
|
|
|
|
this.itemList.appendChild(placeholderItem);
|
2016-08-21 21:52:40 +02:00
|
|
|
}
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
2016-06-08 15:45:29 +02:00
|
|
|
}
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process click of an item
|
|
|
|
* @param {Array} activeItems The currently active items
|
|
|
|
* @param {Element} element Item being interacted with
|
|
|
|
* @param {Boolean} hasShiftKey Whether the user has the shift key active
|
|
|
|
* @return
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_handleItemAction(activeItems, element, hasShiftKey = false) {
|
|
|
|
if (!activeItems || !element) return;
|
|
|
|
|
|
|
|
// If we are clicking on an item
|
|
|
|
if (this.config.removeItems && this.passedElement.type !== 'select-one') {
|
|
|
|
const passedId = element.getAttribute('data-id');
|
|
|
|
|
|
|
|
// We only want to select one item with a click
|
|
|
|
// so we deselect any items that aren't the target
|
|
|
|
// unless shift is being pressed
|
|
|
|
activeItems.forEach((item) => {
|
|
|
|
if (item.id === parseInt(passedId, 10) && !item.highlighted) {
|
|
|
|
this.highlightItem(item);
|
|
|
|
} else if (!hasShiftKey) {
|
|
|
|
if (item.highlighted) {
|
|
|
|
this.unhighlightItem(item);
|
|
|
|
}
|
2016-08-14 23:14:37 +02:00
|
|
|
}
|
2016-09-05 23:04:15 +02:00
|
|
|
});
|
2016-06-08 15:45:29 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
// Focus input as without focus, a user cannot do anything with a
|
|
|
|
// highlighted item
|
|
|
|
if (document.activeElement !== this.input) this.input.focus();
|
2016-06-08 15:45:29 +02:00
|
|
|
}
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process click of a choice
|
|
|
|
* @param {Array} activeItems The currently active items
|
|
|
|
* @param {Element} element Choice being interacted with
|
|
|
|
* @return {[type]} [description]
|
|
|
|
*/
|
|
|
|
_handleChoiceAction(activeItems, element) {
|
|
|
|
if (!activeItems || !element) return;
|
|
|
|
|
|
|
|
// If we are clicking on an option
|
|
|
|
const id = element.getAttribute('data-id');
|
|
|
|
const choice = this.store.getChoiceById(id);
|
|
|
|
const hasActiveDropdown = this.dropdown.classList.contains(this.config.classNames.activeState);
|
|
|
|
|
2017-03-28 15:41:12 +02:00
|
|
|
triggerEvent(this.passedElement, 'choice', {
|
|
|
|
choice,
|
|
|
|
});
|
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
if (choice && !choice.selected && !choice.disabled) {
|
|
|
|
const canAddItem = this._canAddItem(activeItems, choice.value);
|
|
|
|
|
|
|
|
if (canAddItem.response) {
|
2016-10-18 20:23:07 +02:00
|
|
|
this._addItem(choice.value, choice.label, choice.id, choice.groupId);
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
2016-08-02 08:45:08 +02:00
|
|
|
}
|
|
|
|
|
2016-11-18 15:27:07 +01:00
|
|
|
this._triggerChange(choice.value);
|
2016-09-05 23:04:15 +02:00
|
|
|
this.clearInput(this.passedElement);
|
2016-09-04 14:44:31 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
// We wont to close the dropdown if we are dealing with a single select box
|
|
|
|
if (hasActiveDropdown && this.passedElement.type === 'select-one') {
|
|
|
|
this.hideDropdown();
|
|
|
|
this.containerOuter.focus();
|
2016-07-31 21:02:46 +02:00
|
|
|
}
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process back space event
|
2016-09-27 10:11:22 +02:00
|
|
|
* @param {Array} activeItems items
|
2016-09-05 23:04:15 +02:00
|
|
|
* @return
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_handleBackspace(activeItems) {
|
|
|
|
if (this.config.removeItems && activeItems) {
|
|
|
|
const lastItem = activeItems[activeItems.length - 1];
|
|
|
|
const hasHighlightedItems = activeItems.some((item) => item.highlighted === true);
|
|
|
|
|
|
|
|
// If editing the last item is allowed and there are not other selected items,
|
|
|
|
// we can edit the item value. Otherwise if we can remove items, remove all selected items
|
|
|
|
if (this.config.editItems && !hasHighlightedItems && lastItem) {
|
|
|
|
this.input.value = lastItem.value;
|
2016-10-22 22:04:13 +02:00
|
|
|
this._setInputWidth();
|
2016-09-05 23:04:15 +02:00
|
|
|
this._removeItem(lastItem);
|
|
|
|
this._triggerChange(lastItem.value);
|
|
|
|
} else {
|
|
|
|
if (!hasHighlightedItems) {
|
2017-01-01 16:32:09 +01:00
|
|
|
this.highlightItem(lastItem, false);
|
2016-09-04 14:44:31 +02:00
|
|
|
}
|
2016-09-20 13:57:44 +02:00
|
|
|
this.removeHighlightedItems(true);
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
2016-07-31 22:05:17 +02:00
|
|
|
}
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates whether an item can be added by a user
|
|
|
|
* @param {Array} activeItems The currently active items
|
|
|
|
* @param {String} value Value of item to add
|
|
|
|
* @return {Object} Response: Whether user can add item
|
|
|
|
* Notice: Notice show in dropdown
|
|
|
|
*/
|
|
|
|
_canAddItem(activeItems, value) {
|
|
|
|
let canAddItem = true;
|
2016-11-07 11:27:04 +01:00
|
|
|
let notice = isType('Function', this.config.addItemText) ? this.config.addItemText(value) : this.config.addItemText;
|
2016-09-05 23:04:15 +02:00
|
|
|
|
2016-11-08 15:23:25 +01:00
|
|
|
if (this.config.addItems) {
|
2016-12-14 14:28:01 +01:00
|
|
|
const isUnique = !activeItems.some((item) => (item.value === value.trim()) || (item.label === value.trim()));
|
2016-09-05 23:04:15 +02:00
|
|
|
|
2016-11-18 16:01:17 +01:00
|
|
|
if (this.passedElement.type === 'select-multiple' || this.passedElement.type === 'text') {
|
|
|
|
if (this.config.maxItemCount > 0 && this.config.maxItemCount <= this.itemList.children.length) {
|
|
|
|
// If there is a max entry limit and we have reached that limit
|
|
|
|
// don't update
|
|
|
|
canAddItem = false;
|
|
|
|
notice = isType('Function', this.config.maxItemText) ? this.config.maxItemText(this.config.maxItemCount) : this.config.maxItemText;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
// If a user has supplied a regular expression filter
|
|
|
|
if (this.config.regexFilter) {
|
|
|
|
// Determine whether we can update based on whether
|
|
|
|
// our regular expression passes
|
|
|
|
canAddItem = this._regexFilter(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If no duplicates are allowed, and the value already exists
|
|
|
|
// in the array
|
|
|
|
if (this.config.duplicateItems === false && !isUnique) {
|
|
|
|
canAddItem = false;
|
2016-11-07 11:27:04 +01:00
|
|
|
notice = isType('Function', this.config.uniqueItemText) ? this.config.uniqueItemText(value) : this.config.uniqueItemText;
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
2016-06-08 15:45:29 +02:00
|
|
|
}
|
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
return {
|
|
|
|
response: canAddItem,
|
|
|
|
notice,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-09-27 10:11:22 +02:00
|
|
|
/**
|
|
|
|
* Apply or remove a loading state to the component.
|
|
|
|
* @param {Boolean} isLoading default value set to 'true'.
|
|
|
|
* @return
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_handleLoadingState(isLoading = true) {
|
2016-09-27 14:44:35 +02:00
|
|
|
let placeholderItem = this.itemList.querySelector(`.${this.config.classNames.placeholder}`);
|
2016-09-27 10:11:22 +02:00
|
|
|
if(isLoading) {
|
|
|
|
this.containerOuter.classList.add(this.config.classNames.loadingState);
|
|
|
|
this.containerOuter.setAttribute('aria-busy', 'true');
|
|
|
|
if (this.passedElement.type === 'select-one') {
|
2016-09-27 11:08:29 +02:00
|
|
|
if (!placeholderItem) {
|
|
|
|
placeholderItem = this._getTemplate('placeholder', this.config.loadingText);
|
|
|
|
this.itemList.appendChild(placeholderItem);
|
|
|
|
} else {
|
|
|
|
placeholderItem.innerHTML = this.config.loadingText;
|
|
|
|
}
|
2016-09-27 10:11:22 +02:00
|
|
|
} else {
|
|
|
|
this.input.placeholder = this.config.loadingText;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Remove loading states/text
|
|
|
|
this.containerOuter.classList.remove(this.config.classNames.loadingState);
|
2016-09-27 11:08:29 +02:00
|
|
|
const placeholder = this.config.placeholder ? this.config.placeholderValue || this.passedElement.getAttribute('placeholder') : false;
|
|
|
|
if (this.passedElement.type === 'select-one') {
|
|
|
|
placeholderItem.innerHTML = placeholder || '';
|
|
|
|
} else {
|
2016-09-27 10:11:22 +02:00
|
|
|
this.input.placeholder = placeholder || '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the callback used to populate component's choices in an async way.
|
2016-09-27 21:07:32 +02:00
|
|
|
* @returns {Function} The callback as a function.
|
2016-09-27 10:11:22 +02:00
|
|
|
* @private
|
|
|
|
*/
|
2016-09-27 21:07:32 +02:00
|
|
|
_ajaxCallback() {
|
2016-09-27 10:11:22 +02:00
|
|
|
return (results, value, label) => {
|
2016-09-27 21:07:32 +02:00
|
|
|
if (!results || !value) return;
|
2017-02-10 10:01:20 +01:00
|
|
|
|
2016-09-27 21:07:32 +02:00
|
|
|
const parsedResults = isType('Object', results) ? [results] : results;
|
|
|
|
|
|
|
|
if (parsedResults && isType('Array', parsedResults) && parsedResults.length) {
|
2016-09-27 10:11:22 +02:00
|
|
|
// Remove loading states/text
|
|
|
|
this._handleLoadingState(false);
|
|
|
|
// Add each result as a choice
|
2016-10-10 14:17:35 +02:00
|
|
|
parsedResults.forEach((result, index) => {
|
|
|
|
const isSelected = result.selected ? result.selected : false;
|
|
|
|
const isDisabled = result.disabled ? result.disabled : false;
|
|
|
|
if (result.choices) {
|
2017-02-05 10:41:59 +01:00
|
|
|
this._addGroup(result, (result.id || null), value, label);
|
2016-10-10 14:17:35 +02:00
|
|
|
} else {
|
|
|
|
this._addChoice(isSelected, isDisabled, result[value], result[label]);
|
|
|
|
}
|
2016-09-27 10:11:22 +02:00
|
|
|
});
|
2017-02-10 10:01:20 +01:00
|
|
|
} else {
|
|
|
|
// No results, remove loading state
|
|
|
|
this._handleLoadingState(false);
|
2016-09-27 10:11:22 +02:00
|
|
|
}
|
2017-02-10 10:01:20 +01:00
|
|
|
|
2016-09-27 10:11:22 +02:00
|
|
|
this.containerOuter.removeAttribute('aria-busy');
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
/**
|
|
|
|
* Filter choices based on search value
|
|
|
|
* @param {String} value Value to filter by
|
|
|
|
* @return
|
|
|
|
* @private
|
|
|
|
*/
|
2016-09-27 21:07:32 +02:00
|
|
|
_searchChoices(value) {
|
2016-09-27 14:44:35 +02:00
|
|
|
const newValue = isType('String', value) ? value.trim() : value;
|
|
|
|
const currentValue = isType('String', this.currentValue) ? this.currentValue.trim() : this.currentValue;
|
|
|
|
|
|
|
|
// If new value matches the desired length and is not the same as the current value with a space
|
|
|
|
if (newValue.length >= 1 && newValue !== `${currentValue} `) {
|
|
|
|
const haystack = this.store.getChoicesFilteredBySelectable();
|
|
|
|
const needle = newValue;
|
2017-04-07 09:49:15 +02:00
|
|
|
const keys = isType('Array', this.config.searchFields) ? this.config.searchFields : [this.config.searchFields];
|
2017-01-01 16:59:43 +01:00
|
|
|
const options = Object.assign(this.config.fuseOptions, { keys });
|
|
|
|
const fuse = new Fuse(haystack, options);
|
2016-09-27 14:44:35 +02:00
|
|
|
const results = fuse.search(needle);
|
2017-01-01 16:59:43 +01:00
|
|
|
|
2016-09-27 14:44:35 +02:00
|
|
|
this.currentValue = newValue;
|
|
|
|
this.highlightPosition = 0;
|
|
|
|
this.isSearching = true;
|
2016-11-08 15:23:25 +01:00
|
|
|
|
|
|
|
return results;
|
2016-09-27 14:44:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine the action when a user is searching
|
|
|
|
* @param {String} value Value entered by user
|
|
|
|
* @return
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_handleSearch(value) {
|
2016-09-05 23:04:15 +02:00
|
|
|
if (!value) return;
|
2016-09-27 21:07:32 +02:00
|
|
|
const choices = this.store.getChoices();
|
|
|
|
const hasUnactiveChoices = choices.some((option) => option.active !== true);
|
|
|
|
|
2016-09-27 10:11:22 +02:00
|
|
|
// Run callback if it is a function
|
2016-09-27 14:44:35 +02:00
|
|
|
if (this.input === document.activeElement) {
|
2016-09-27 21:07:32 +02:00
|
|
|
// Check that we have a value to search and the input was an alphanumeric character
|
|
|
|
if (value && value.length > this.config.searchFloor) {
|
2017-04-06 15:42:11 +02:00
|
|
|
// Check flag to filter search input
|
|
|
|
if (this.config.searchChoices) {
|
|
|
|
// Filter available choices
|
2017-04-10 10:19:36 +02:00
|
|
|
const results = this._searchChoices(value);
|
|
|
|
if (results) {
|
|
|
|
this.store.dispatch(filterChoices(results));
|
|
|
|
}
|
2017-04-06 15:42:11 +02:00
|
|
|
}
|
2017-01-01 16:32:09 +01:00
|
|
|
// Trigger search event
|
|
|
|
triggerEvent(this.passedElement, 'search', {
|
|
|
|
value,
|
|
|
|
});
|
2016-09-27 21:07:32 +02:00
|
|
|
} else if (hasUnactiveChoices) {
|
|
|
|
// Otherwise reset choices to active
|
|
|
|
this.isSearching = false;
|
|
|
|
this.store.dispatch(activateChoices(true));
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
2016-08-02 22:10:53 +02:00
|
|
|
}
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Trigger event listeners
|
|
|
|
* @return
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_addEventListeners() {
|
|
|
|
document.addEventListener('keyup', this._onKeyUp);
|
|
|
|
document.addEventListener('keydown', this._onKeyDown);
|
|
|
|
document.addEventListener('click', this._onClick);
|
|
|
|
document.addEventListener('touchmove', this._onTouchMove);
|
|
|
|
document.addEventListener('touchend', this._onTouchEnd);
|
|
|
|
document.addEventListener('mousedown', this._onMouseDown);
|
|
|
|
document.addEventListener('mouseover', this._onMouseOver);
|
|
|
|
|
|
|
|
if (this.passedElement.type && this.passedElement.type === 'select-one') {
|
|
|
|
this.containerOuter.addEventListener('focus', this._onFocus);
|
|
|
|
this.containerOuter.addEventListener('blur', this._onBlur);
|
2016-06-08 15:45:29 +02:00
|
|
|
}
|
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
this.input.addEventListener('input', this._onInput);
|
|
|
|
this.input.addEventListener('paste', this._onPaste);
|
|
|
|
this.input.addEventListener('focus', this._onFocus);
|
|
|
|
this.input.addEventListener('blur', this._onBlur);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-10-22 21:15:28 +02:00
|
|
|
* Remove event listeners
|
2016-09-05 23:04:15 +02:00
|
|
|
* @return
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_removeEventListeners() {
|
|
|
|
document.removeEventListener('keyup', this._onKeyUp);
|
|
|
|
document.removeEventListener('keydown', this._onKeyDown);
|
|
|
|
document.removeEventListener('click', this._onClick);
|
|
|
|
document.removeEventListener('touchmove', this._onTouchMove);
|
|
|
|
document.removeEventListener('touchend', this._onTouchEnd);
|
|
|
|
document.removeEventListener('mousedown', this._onMouseDown);
|
|
|
|
document.removeEventListener('mouseover', this._onMouseOver);
|
|
|
|
|
|
|
|
if (this.passedElement.type && this.passedElement.type === 'select-one') {
|
|
|
|
this.containerOuter.removeEventListener('focus', this._onFocus);
|
|
|
|
this.containerOuter.removeEventListener('blur', this._onBlur);
|
2016-07-31 21:02:46 +02:00
|
|
|
}
|
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
this.input.removeEventListener('input', this._onInput);
|
|
|
|
this.input.removeEventListener('paste', this._onPaste);
|
|
|
|
this.input.removeEventListener('focus', this._onFocus);
|
|
|
|
this.input.removeEventListener('blur', this._onBlur);
|
|
|
|
}
|
|
|
|
|
2016-10-22 22:04:13 +02:00
|
|
|
/**
|
|
|
|
* Set the correct input width based on placeholder
|
|
|
|
* value or input value
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
_setInputWidth() {
|
|
|
|
if (this.config.placeholder && (this.config.placeholderValue || this.passedElement.getAttribute('placeholder'))) {
|
|
|
|
// If there is a placeholder, we only want to set the width of the input when it is a greater
|
|
|
|
// length than 75% of the placeholder. This stops the input jumping around.
|
|
|
|
const placeholder = this.config.placeholder ? this.config.placeholderValue ||
|
|
|
|
this.passedElement.getAttribute('placeholder') : false;
|
|
|
|
if (this.input.value && this.input.value.length >= (placeholder.length / 1.25)) {
|
|
|
|
this.input.style.width = getWidthOfInput(this.input);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If there is no placeholder, resize input to contents
|
|
|
|
this.input.style.width = getWidthOfInput(this.input);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
/**
|
|
|
|
* Key down event
|
|
|
|
* @param {Object} e Event
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
_onKeyDown(e) {
|
|
|
|
if (e.target !== this.input && !this.containerOuter.contains(e.target)) return;
|
|
|
|
|
|
|
|
const target = e.target;
|
2016-10-26 16:43:15 +02:00
|
|
|
const passedElementType = this.passedElement.type;
|
2016-09-05 23:04:15 +02:00
|
|
|
const activeItems = this.store.getItemsFilteredByActive();
|
|
|
|
const hasFocusedInput = this.input === document.activeElement;
|
|
|
|
const hasActiveDropdown = this.dropdown.classList.contains(this.config.classNames.activeState);
|
|
|
|
const hasItems = this.itemList && this.itemList.children;
|
|
|
|
const keyString = String.fromCharCode(e.keyCode);
|
|
|
|
|
|
|
|
const backKey = 46;
|
|
|
|
const deleteKey = 8;
|
|
|
|
const enterKey = 13;
|
|
|
|
const aKey = 65;
|
|
|
|
const escapeKey = 27;
|
|
|
|
const upKey = 38;
|
|
|
|
const downKey = 40;
|
2017-01-25 14:16:58 +01:00
|
|
|
const pageUpKey = 33;
|
|
|
|
const pageDownKey = 34;
|
2016-09-05 23:04:15 +02:00
|
|
|
const ctrlDownKey = e.ctrlKey || e.metaKey;
|
|
|
|
|
|
|
|
// If a user is typing and the dropdown is not active
|
2016-10-26 16:43:15 +02:00
|
|
|
if (passedElementType !== 'text' && /[a-zA-Z0-9-_ ]/.test(keyString) && !hasActiveDropdown) {
|
2016-09-05 23:04:15 +02:00
|
|
|
this.showDropdown(true);
|
2016-08-14 17:10:53 +02:00
|
|
|
}
|
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
this.canSearch = this.config.search;
|
2016-08-14 17:10:53 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
const onAKey = () => {
|
|
|
|
// If CTRL + A or CMD + A have been pressed and there are items to select
|
|
|
|
if (ctrlDownKey && hasItems) {
|
|
|
|
this.canSearch = false;
|
|
|
|
if (this.config.removeItems && !this.input.value && this.input === document.activeElement) {
|
|
|
|
// Highlight items
|
|
|
|
this.highlightAll(this.itemList.children);
|
2016-08-14 23:14:37 +02:00
|
|
|
}
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const onEnterKey = () => {
|
2016-11-18 16:01:17 +01:00
|
|
|
const highlighted = this.dropdown.querySelector(`.${this.config.classNames.highlightedState}`);
|
2016-11-18 14:00:05 +01:00
|
|
|
|
2016-11-18 16:01:17 +01:00
|
|
|
if (hasActiveDropdown && highlighted) {
|
2016-11-18 15:27:07 +01:00
|
|
|
// If we have a highlighted choice, select it
|
2016-11-18 16:01:17 +01:00
|
|
|
this._handleChoiceAction(activeItems, highlighted);
|
2016-11-18 14:00:05 +01:00
|
|
|
} else if (passedElementType === 'select-one') {
|
|
|
|
// Open single select dropdown if it's not active
|
|
|
|
if (!hasActiveDropdown) {
|
|
|
|
this.showDropdown(true);
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
// If enter key is pressed and the input has a value
|
2016-11-08 15:23:25 +01:00
|
|
|
if (target.value) {
|
2016-09-05 23:04:15 +02:00
|
|
|
const value = this.input.value;
|
|
|
|
const canAddItem = this._canAddItem(activeItems, value);
|
|
|
|
|
|
|
|
// All is good, add
|
|
|
|
if (canAddItem.response) {
|
2016-11-18 16:01:17 +01:00
|
|
|
// Track whether we will end up adding an item
|
|
|
|
const willAddItem = this.isTextElement || (this.isSelectElement && this.config.addItems);
|
2016-11-08 15:23:25 +01:00
|
|
|
|
2016-11-18 16:01:17 +01:00
|
|
|
if (willAddItem) {
|
|
|
|
if (hasActiveDropdown) {
|
|
|
|
this.hideDropdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.isTextElement) {
|
|
|
|
this._addItem(value);
|
2016-12-14 12:13:24 +01:00
|
|
|
} else {
|
2016-12-14 14:28:01 +01:00
|
|
|
let matchingChoices = [];
|
|
|
|
let isUnique;
|
2016-12-14 13:15:31 +01:00
|
|
|
const duplicateItems = this.config.duplicateItems;
|
2016-12-14 12:13:24 +01:00
|
|
|
if (!duplicateItems) {
|
2016-12-14 14:28:01 +01:00
|
|
|
matchingChoices = this.store
|
|
|
|
.getChoices()
|
|
|
|
.filter((choice) => choice.label === value.trim());
|
|
|
|
isUnique = !this.store
|
|
|
|
.getItemsFilteredByActive()
|
|
|
|
.some((item) => item.label === value.trim());
|
2016-12-14 12:13:24 +01:00
|
|
|
}
|
2016-12-14 14:28:01 +01:00
|
|
|
if (duplicateItems || (matchingChoices.length === 0 && isUnique)) {
|
2016-12-14 12:13:24 +01:00
|
|
|
this._addChoice(true, false, value, value);
|
2016-12-14 14:28:01 +01:00
|
|
|
}
|
|
|
|
if (duplicateItems || isUnique) {
|
|
|
|
if (matchingChoices[0]) {
|
|
|
|
this._addItem(
|
|
|
|
matchingChoices[0].value,
|
|
|
|
matchingChoices[0].label,
|
|
|
|
matchingChoices[0].id
|
|
|
|
);
|
|
|
|
}
|
2016-12-14 12:13:24 +01:00
|
|
|
}
|
2016-11-18 16:01:17 +01:00
|
|
|
this.containerOuter.focus();
|
|
|
|
}
|
2016-11-08 15:23:25 +01:00
|
|
|
|
2016-11-18 16:01:17 +01:00
|
|
|
this._triggerChange(value);
|
|
|
|
this.clearInput(this.passedElement);
|
|
|
|
}
|
2016-08-14 17:10:53 +02:00
|
|
|
}
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
2016-09-05 14:48:51 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
if (target.hasAttribute('data-button')) {
|
|
|
|
this._handleButtonAction(activeItems, target);
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
2016-09-05 14:48:51 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
if (hasActiveDropdown) {
|
2016-11-28 14:12:45 +01:00
|
|
|
e.preventDefault();
|
2016-09-05 23:04:15 +02:00
|
|
|
const highlighted = this.dropdown.querySelector(`.${this.config.classNames.highlightedState}`);
|
2016-08-14 17:10:53 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
// If we have a highlighted choice
|
|
|
|
if (highlighted) {
|
|
|
|
this._handleChoiceAction(activeItems, highlighted);
|
2016-04-17 12:23:38 +02:00
|
|
|
}
|
2016-11-28 14:12:45 +01:00
|
|
|
|
2016-10-26 16:43:15 +02:00
|
|
|
} else if (passedElementType === 'select-one') {
|
2016-09-05 23:04:15 +02:00
|
|
|
// Open single select dropdown if it's not active
|
|
|
|
if (!hasActiveDropdown) {
|
|
|
|
this.showDropdown(true);
|
|
|
|
e.preventDefault();
|
2016-08-14 18:14:55 +02:00
|
|
|
}
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const onEscapeKey = () => {
|
|
|
|
if (hasActiveDropdown) {
|
|
|
|
this.toggleDropdown();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const onDirectionKey = () => {
|
|
|
|
// If up or down key is pressed, traverse through options
|
2016-10-26 16:43:15 +02:00
|
|
|
if (hasActiveDropdown || passedElementType === 'select-one') {
|
2016-09-05 23:04:15 +02:00
|
|
|
// Show dropdown if focus
|
|
|
|
if (!hasActiveDropdown) {
|
|
|
|
this.showDropdown(true);
|
2016-08-14 23:14:37 +02:00
|
|
|
}
|
2016-08-14 18:14:55 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
this.canSearch = false;
|
2016-08-08 08:40:49 +02:00
|
|
|
|
2017-01-25 14:16:58 +01:00
|
|
|
const directionInt = e.keyCode === downKey || e.keyCode === pageDownKey ? 1 : -1;
|
|
|
|
const skipKey = e.metaKey || e.keyCode === pageDownKey || e.keyCode === pageUpKey;
|
|
|
|
|
|
|
|
let nextEl;
|
|
|
|
if (skipKey) {
|
|
|
|
if (directionInt > 0) {
|
|
|
|
nextEl = Array.from(this.dropdown.querySelectorAll('[data-choice-selectable]')).pop();
|
|
|
|
} else {
|
|
|
|
nextEl = this.dropdown.querySelector('[data-choice-selectable]');
|
|
|
|
}
|
2016-09-05 23:04:15 +02:00
|
|
|
} else {
|
2017-01-25 14:16:58 +01:00
|
|
|
const currentEl = this.dropdown.querySelector(`.${this.config.classNames.highlightedState}`);
|
|
|
|
if (currentEl) {
|
|
|
|
nextEl = getAdjacentEl(currentEl, '[data-choice-selectable]', directionInt);
|
|
|
|
} else {
|
|
|
|
nextEl = this.dropdown.querySelector('[data-choice-selectable]');
|
|
|
|
}
|
2016-08-14 18:19:49 +02:00
|
|
|
}
|
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
if (nextEl) {
|
|
|
|
// We prevent default to stop the cursor moving
|
|
|
|
// when pressing the arrow
|
|
|
|
if (!isScrolledIntoView(nextEl, this.choiceList, directionInt)) {
|
|
|
|
this._scrollToChoice(nextEl, directionInt);
|
|
|
|
}
|
|
|
|
this._highlightChoice(nextEl);
|
2016-08-14 18:19:49 +02:00
|
|
|
}
|
2016-08-14 23:14:37 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
// Prevent default to maintain cursor position whilst
|
|
|
|
// traversing dropdown options
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const onDeleteKey = () => {
|
|
|
|
// If backspace or delete key is pressed and the input has no value
|
2016-10-26 16:43:15 +02:00
|
|
|
if (hasFocusedInput && !e.target.value && passedElementType !== 'select-one') {
|
2016-09-05 23:04:15 +02:00
|
|
|
this._handleBackspace(activeItems);
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Map keys to key actions
|
|
|
|
const keyDownActions = {
|
|
|
|
[aKey]: onAKey,
|
|
|
|
[enterKey]: onEnterKey,
|
|
|
|
[escapeKey]: onEscapeKey,
|
|
|
|
[upKey]: onDirectionKey,
|
2017-01-25 14:16:58 +01:00
|
|
|
[pageUpKey]: onDirectionKey,
|
2016-09-05 23:04:15 +02:00
|
|
|
[downKey]: onDirectionKey,
|
2017-01-25 14:16:58 +01:00
|
|
|
[pageDownKey]: onDirectionKey,
|
2016-09-05 23:04:15 +02:00
|
|
|
[deleteKey]: onDeleteKey,
|
|
|
|
[backKey]: onDeleteKey,
|
|
|
|
};
|
|
|
|
|
|
|
|
// If keycode has a function, run it
|
|
|
|
if (keyDownActions[e.keyCode]) {
|
|
|
|
keyDownActions[e.keyCode]();
|
2016-08-14 18:19:49 +02:00
|
|
|
}
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Key up event
|
|
|
|
* @param {Object} e Event
|
|
|
|
* @return
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_onKeyUp(e) {
|
|
|
|
if (e.target !== this.input) return;
|
|
|
|
|
|
|
|
// We are typing into a text input and have a value, we want to show a dropdown
|
|
|
|
// notice. Otherwise hide the dropdown
|
2016-10-26 16:43:15 +02:00
|
|
|
if (this.isTextElement) {
|
2016-09-05 23:04:15 +02:00
|
|
|
const hasActiveDropdown = this.dropdown.classList.contains(this.config.classNames.activeState);
|
|
|
|
const value = this.input.value;
|
|
|
|
|
|
|
|
if (value) {
|
2016-09-04 16:23:19 +02:00
|
|
|
const activeItems = this.store.getItemsFilteredByActive();
|
2016-09-05 23:04:15 +02:00
|
|
|
const canAddItem = this._canAddItem(activeItems, value);
|
2016-04-29 19:23:06 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
if (canAddItem.notice) {
|
|
|
|
const dropdownItem = this._getTemplate('notice', canAddItem.notice);
|
|
|
|
this.dropdown.innerHTML = dropdownItem.outerHTML;
|
|
|
|
}
|
2016-05-02 16:29:05 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
if (canAddItem.response === true) {
|
|
|
|
if (!hasActiveDropdown) {
|
|
|
|
this.showDropdown();
|
|
|
|
}
|
|
|
|
} else if (!canAddItem.notice && hasActiveDropdown) {
|
|
|
|
this.hideDropdown();
|
2016-03-15 23:42:10 +01:00
|
|
|
}
|
2016-09-05 23:04:15 +02:00
|
|
|
} else if (hasActiveDropdown) {
|
|
|
|
this.hideDropdown();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const backKey = 46;
|
|
|
|
const deleteKey = 8;
|
|
|
|
|
|
|
|
// If user has removed value...
|
|
|
|
if ((e.keyCode === backKey || e.keyCode === deleteKey) && !e.target.value) {
|
|
|
|
// ...and it is a multiple select input, activate choices (if searching)
|
|
|
|
if (this.passedElement.type !== 'text' && this.isSearching) {
|
|
|
|
this.isSearching = false;
|
|
|
|
this.store.dispatch(activateChoices(true));
|
|
|
|
}
|
|
|
|
} else if (this.canSearch) {
|
2016-09-27 14:44:35 +02:00
|
|
|
this._handleSearch(this.input.value);
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
2016-03-17 16:00:22 +01:00
|
|
|
}
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Input event
|
|
|
|
* @param {Object} e Event
|
|
|
|
* @return
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_onInput() {
|
|
|
|
if (this.passedElement.type !== 'select-one') {
|
2016-10-22 22:04:13 +02:00
|
|
|
this._setInputWidth();
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Touch move event
|
|
|
|
* @param {Object} e Event
|
|
|
|
* @return
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_onTouchMove() {
|
|
|
|
if (this.wasTap === true) {
|
|
|
|
this.wasTap = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Touch end event
|
|
|
|
* @param {Object} e Event
|
|
|
|
* @return
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_onTouchEnd(e) {
|
|
|
|
const target = e.target || e.touches[0].target;
|
|
|
|
const hasActiveDropdown = this.dropdown.classList.contains(this.config.classNames.activeState);
|
|
|
|
|
|
|
|
// If a user tapped within our container...
|
|
|
|
if (this.wasTap === true && this.containerOuter.contains(target)) {
|
|
|
|
// ...and we aren't dealing with a single select box, show dropdown/focus input
|
|
|
|
if ((target === this.containerOuter || target === this.containerInner) && this.passedElement.type !== 'select-one') {
|
2016-10-26 16:43:15 +02:00
|
|
|
if (this.isTextElement) {
|
2016-09-05 23:04:15 +02:00
|
|
|
// If text element, we only want to focus the input (if it isn't already)
|
|
|
|
if (document.activeElement !== this.input) {
|
|
|
|
this.input.focus();
|
|
|
|
}
|
2016-08-08 15:05:29 +02:00
|
|
|
} else {
|
2016-09-05 23:04:15 +02:00
|
|
|
if (!hasActiveDropdown) {
|
|
|
|
// If a select box, we want to show the dropdown
|
|
|
|
this.showDropdown(true);
|
|
|
|
}
|
2016-05-07 14:30:07 +02:00
|
|
|
}
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
|
|
|
// Prevents focus event firing
|
|
|
|
e.stopPropagation();
|
2016-05-04 10:02:22 +02:00
|
|
|
}
|
2016-04-22 20:45:50 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
this.wasTap = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mouse down event
|
|
|
|
* @param {Object} e Event
|
|
|
|
* @return
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_onMouseDown(e) {
|
2017-03-12 14:19:27 +01:00
|
|
|
const target = e.target;
|
2016-09-05 23:04:15 +02:00
|
|
|
if (this.containerOuter.contains(target) && target !== this.input) {
|
2017-03-12 14:19:27 +01:00
|
|
|
let foundTarget;
|
2016-09-05 23:04:15 +02:00
|
|
|
const activeItems = this.store.getItemsFilteredByActive();
|
|
|
|
const hasShiftKey = e.shiftKey;
|
|
|
|
|
2017-03-28 15:41:12 +02:00
|
|
|
if(foundTarget = findAncestorByAttrName(target, 'data-button')) {
|
|
|
|
this._handleButtonAction(activeItems, foundTarget);
|
|
|
|
} else if (foundTarget = findAncestorByAttrName(target, 'data-item')) {
|
2017-03-12 14:17:46 +01:00
|
|
|
this._handleItemAction(activeItems, foundTarget, hasShiftKey);
|
2017-03-12 14:21:00 +01:00
|
|
|
} else if (foundTarget = findAncestorByAttrName(target, 'data-choice')) {
|
2017-03-12 14:17:46 +01:00
|
|
|
this._handleChoiceAction(activeItems, foundTarget);
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
e.preventDefault();
|
2016-05-16 15:46:04 +02:00
|
|
|
}
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Click event
|
|
|
|
* @param {Object} e Event
|
|
|
|
* @return
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_onClick(e) {
|
|
|
|
const target = e.target;
|
|
|
|
const hasActiveDropdown = this.dropdown.classList.contains(this.config.classNames.activeState);
|
|
|
|
const activeItems = this.store.getItemsFilteredByActive();
|
|
|
|
|
2017-03-28 15:41:12 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
// If target is something that concerns us
|
|
|
|
if (this.containerOuter.contains(target)) {
|
|
|
|
// Handle button delete
|
|
|
|
if (target.hasAttribute('data-button')) {
|
|
|
|
this._handleButtonAction(activeItems, target);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hasActiveDropdown) {
|
2016-10-26 16:43:15 +02:00
|
|
|
if (this.isTextElement) {
|
2016-09-05 23:04:15 +02:00
|
|
|
if (document.activeElement !== this.input) {
|
|
|
|
this.input.focus();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (this.canSearch) {
|
|
|
|
this.showDropdown(true);
|
|
|
|
} else {
|
|
|
|
this.showDropdown();
|
|
|
|
this.containerOuter.focus();
|
|
|
|
}
|
2016-08-07 23:16:05 +02:00
|
|
|
}
|
2016-09-05 23:04:15 +02:00
|
|
|
} else if (this.passedElement.type === 'select-one' && target !== this.input && !this.dropdown.contains(target)) {
|
|
|
|
this.hideDropdown(true);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const hasHighlightedItems = activeItems.some((item) => item.highlighted === true);
|
|
|
|
|
|
|
|
// De-select any highlighted items
|
|
|
|
if (hasHighlightedItems) {
|
|
|
|
this.unhighlightAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove focus state
|
|
|
|
this.containerOuter.classList.remove(this.config.classNames.focusState);
|
|
|
|
|
|
|
|
// Close all other dropdowns
|
|
|
|
if (hasActiveDropdown) {
|
|
|
|
this.hideDropdown();
|
|
|
|
}
|
2016-08-07 23:09:49 +02:00
|
|
|
}
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mouse over (hover) event
|
|
|
|
* @param {Object} e Event
|
|
|
|
* @return
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_onMouseOver(e) {
|
|
|
|
// If the dropdown is either the target or one of its children is the target
|
|
|
|
if (e.target === this.dropdown || this.dropdown.contains(e.target)) {
|
|
|
|
if (e.target.hasAttribute('data-choice')) this._highlightChoice(e.target);
|
2016-08-05 21:28:21 +02:00
|
|
|
}
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Paste event
|
|
|
|
* @param {Object} e Event
|
|
|
|
* @return
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_onPaste(e) {
|
|
|
|
// Disable pasting into the input if option has been set
|
|
|
|
if (e.target === this.input && !this.config.paste) {
|
|
|
|
e.preventDefault();
|
2016-08-08 22:46:17 +02:00
|
|
|
}
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Focus event
|
|
|
|
* @param {Object} e Event
|
|
|
|
* @return
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_onFocus(e) {
|
|
|
|
const target = e.target;
|
|
|
|
// If target is something that concerns us
|
|
|
|
if (this.containerOuter.contains(target)) {
|
|
|
|
const hasActiveDropdown = this.dropdown.classList.contains(this.config.classNames.activeState);
|
|
|
|
const focusActions = {
|
|
|
|
text: () => {
|
|
|
|
if (target === this.input) {
|
|
|
|
this.containerOuter.classList.add(this.config.classNames.focusState);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'select-one': () => {
|
|
|
|
this.containerOuter.classList.add(this.config.classNames.focusState);
|
|
|
|
if (target === this.input) {
|
|
|
|
// Show dropdown if it isn't already showing
|
|
|
|
if (!hasActiveDropdown) {
|
|
|
|
this.showDropdown();
|
2016-08-19 14:11:15 +02:00
|
|
|
}
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
'select-multiple': () => {
|
|
|
|
if (target === this.input) {
|
|
|
|
// If element is a select box, the focussed element is the container and the dropdown
|
|
|
|
// isn't already open, focus and show dropdown
|
|
|
|
this.containerOuter.classList.add(this.config.classNames.focusState);
|
2016-08-19 14:11:15 +02:00
|
|
|
|
2016-08-14 23:14:37 +02:00
|
|
|
if (!hasActiveDropdown) {
|
2016-09-05 23:04:15 +02:00
|
|
|
this.showDropdown(true);
|
2016-08-01 21:00:24 +02:00
|
|
|
}
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
2016-04-29 19:06:46 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
focusActions[this.passedElement.type]();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Blur event
|
|
|
|
* @param {Object} e Event
|
|
|
|
* @return
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_onBlur(e) {
|
|
|
|
const target = e.target;
|
|
|
|
// If target is something that concerns us
|
|
|
|
if (this.containerOuter.contains(target)) {
|
|
|
|
const activeItems = this.store.getItemsFilteredByActive();
|
|
|
|
const hasActiveDropdown = this.dropdown.classList.contains(this.config.classNames.activeState);
|
|
|
|
const hasHighlightedItems = activeItems.some((item) => item.highlighted === true);
|
|
|
|
const blurActions = {
|
|
|
|
text: () => {
|
|
|
|
if (target === this.input) {
|
|
|
|
// Remove the focus state
|
|
|
|
this.containerOuter.classList.remove(this.config.classNames.focusState);
|
2016-08-01 21:00:24 +02:00
|
|
|
// De-select any highlighted items
|
2016-08-14 23:14:37 +02:00
|
|
|
if (hasHighlightedItems) {
|
2016-09-05 23:04:15 +02:00
|
|
|
this.unhighlightAll();
|
|
|
|
}
|
|
|
|
// Hide dropdown if it is showing
|
|
|
|
if (hasActiveDropdown) {
|
|
|
|
this.hideDropdown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'select-one': () => {
|
|
|
|
this.containerOuter.classList.remove(this.config.classNames.focusState);
|
|
|
|
if (target === this.containerOuter) {
|
|
|
|
// Hide dropdown if it is showing
|
|
|
|
if (hasActiveDropdown && !this.canSearch) {
|
|
|
|
this.hideDropdown();
|
2016-08-01 21:00:24 +02:00
|
|
|
}
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
2016-08-14 23:14:37 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
if (target === this.input) {
|
|
|
|
// Hide dropdown if it is showing
|
|
|
|
if (hasActiveDropdown) {
|
|
|
|
this.hideDropdown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'select-multiple': () => {
|
|
|
|
if (target === this.input) {
|
|
|
|
// Remove the focus state
|
2016-08-01 21:00:24 +02:00
|
|
|
this.containerOuter.classList.remove(this.config.classNames.focusState);
|
2016-08-14 23:14:37 +02:00
|
|
|
if (hasActiveDropdown) {
|
2016-09-05 23:04:15 +02:00
|
|
|
this.hideDropdown();
|
2016-05-11 15:51:32 +02:00
|
|
|
}
|
2016-09-05 23:04:15 +02:00
|
|
|
// De-select any highlighted items
|
|
|
|
if (hasHighlightedItems) {
|
|
|
|
this.unhighlightAll();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
2016-04-14 15:54:47 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
blurActions[this.passedElement.type]();
|
2016-04-09 12:29:56 +02:00
|
|
|
}
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests value against a regular expression
|
|
|
|
* @param {string} value Value to test
|
|
|
|
* @return {Boolean} Whether test passed/failed
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_regexFilter(value) {
|
|
|
|
if (!value) return;
|
|
|
|
const regex = this.config.regexFilter;
|
|
|
|
const expression = new RegExp(regex.source, 'i');
|
|
|
|
return expression.test(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scroll to an option element
|
|
|
|
* @param {HTMLElement} option Option to scroll to
|
|
|
|
* @param {Number} direction Whether option is above or below
|
|
|
|
* @return
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_scrollToChoice(choice, direction) {
|
|
|
|
if (!choice) return;
|
|
|
|
|
|
|
|
const dropdownHeight = this.choiceList.offsetHeight;
|
|
|
|
const choiceHeight = choice.offsetHeight;
|
|
|
|
// Distance from bottom of element to top of parent
|
|
|
|
const choicePos = choice.offsetTop + choiceHeight;
|
|
|
|
// Scroll position of dropdown
|
|
|
|
const containerScrollPos = this.choiceList.scrollTop + dropdownHeight;
|
|
|
|
// Difference between the choice and scroll position
|
|
|
|
const endPoint = direction > 0 ? ((this.choiceList.scrollTop + choicePos) - containerScrollPos) : choice.offsetTop;
|
|
|
|
|
|
|
|
const animateScroll = () => {
|
|
|
|
const strength = 4;
|
2016-10-26 16:43:15 +02:00
|
|
|
const choiceListScrollTop = this.choiceList.scrollTop;
|
2016-09-05 23:04:15 +02:00
|
|
|
let continueAnimation = false;
|
|
|
|
let easing;
|
|
|
|
let distance;
|
|
|
|
|
|
|
|
if (direction > 0) {
|
2016-10-26 16:43:15 +02:00
|
|
|
easing = (endPoint - choiceListScrollTop) / strength;
|
2016-09-05 23:04:15 +02:00
|
|
|
distance = easing > 1 ? easing : 1;
|
|
|
|
|
2016-10-26 16:43:15 +02:00
|
|
|
this.choiceList.scrollTop = choiceListScrollTop + distance;
|
|
|
|
if (choiceListScrollTop < endPoint) {
|
2016-09-05 23:04:15 +02:00
|
|
|
continueAnimation = true;
|
2016-05-02 13:23:12 +02:00
|
|
|
}
|
2016-09-05 23:04:15 +02:00
|
|
|
} else {
|
2016-10-26 16:43:15 +02:00
|
|
|
easing = (choiceListScrollTop - endPoint) / strength;
|
2016-09-05 23:04:15 +02:00
|
|
|
distance = easing > 1 ? easing : 1;
|
2016-05-02 13:23:12 +02:00
|
|
|
|
2016-10-26 16:43:15 +02:00
|
|
|
this.choiceList.scrollTop = choiceListScrollTop - distance;
|
|
|
|
if (choiceListScrollTop > endPoint) {
|
2016-09-05 23:04:15 +02:00
|
|
|
continueAnimation = true;
|
2016-05-02 22:53:21 +02:00
|
|
|
}
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
2016-04-21 15:42:57 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
if (continueAnimation) {
|
|
|
|
requestAnimationFrame((time) => {
|
|
|
|
animateScroll(time, endPoint, direction);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
requestAnimationFrame((time) => {
|
|
|
|
animateScroll(time, endPoint, direction);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Highlight choice
|
|
|
|
* @param {HTMLElement} el Element to highlight
|
|
|
|
* @return
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_highlightChoice(el) {
|
|
|
|
// Highlight first element in dropdown
|
|
|
|
const choices = Array.from(this.dropdown.querySelectorAll('[data-choice-selectable]'));
|
|
|
|
|
|
|
|
if (choices && choices.length) {
|
|
|
|
const highlightedChoices = Array.from(this.dropdown.querySelectorAll(`.${this.config.classNames.highlightedState}`));
|
|
|
|
|
|
|
|
// Remove any highlighted choices
|
|
|
|
highlightedChoices.forEach((choice) => {
|
|
|
|
choice.classList.remove(this.config.classNames.highlightedState);
|
|
|
|
choice.setAttribute('aria-selected', 'false');
|
|
|
|
});
|
|
|
|
|
|
|
|
if (el) {
|
|
|
|
// Highlight given option
|
|
|
|
el.classList.add(this.config.classNames.highlightedState);
|
|
|
|
this.highlightPosition = choices.indexOf(el);
|
|
|
|
} else {
|
|
|
|
// Highlight choice based on last known highlight location
|
|
|
|
let choice;
|
|
|
|
|
|
|
|
if (choices.length > this.highlightPosition) {
|
|
|
|
// If we have an option to highlight
|
|
|
|
choice = choices[this.highlightPosition];
|
|
|
|
} else {
|
|
|
|
// Otherwise highlight the option before
|
|
|
|
choice = choices[choices.length - 1];
|
2016-05-02 22:53:21 +02:00
|
|
|
}
|
2016-04-21 15:42:57 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
if (!choice) choice = choices[0];
|
|
|
|
choice.classList.add(this.config.classNames.highlightedState);
|
|
|
|
choice.setAttribute('aria-selected', 'true');
|
|
|
|
}
|
2016-03-24 15:42:03 +01:00
|
|
|
}
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add item to store with correct value
|
|
|
|
* @param {String} value Value to add to store
|
|
|
|
* @param {String} label Label to add to store
|
|
|
|
* @return {Object} Class instance
|
|
|
|
* @public
|
|
|
|
*/
|
2016-10-18 20:23:07 +02:00
|
|
|
_addItem(value, label, choiceId = -1, groupId = -1) {
|
2016-09-05 23:04:15 +02:00
|
|
|
let passedValue = isType('String', value) ? value.trim() : value;
|
|
|
|
const items = this.store.getItems();
|
|
|
|
const passedLabel = label || passedValue;
|
|
|
|
const passedOptionId = parseInt(choiceId, 10) || -1;
|
2017-01-01 16:32:09 +01:00
|
|
|
|
|
|
|
// Get group if group ID passed
|
|
|
|
const group = groupId >= 0 ? this.store.getGroupById(groupId) : null;
|
|
|
|
|
|
|
|
// Generate unique id
|
|
|
|
const id = items ? items.length + 1 : 1;
|
2016-09-05 23:04:15 +02:00
|
|
|
|
|
|
|
// If a prepended value has been passed, prepend it
|
|
|
|
if (this.config.prependValue) {
|
|
|
|
passedValue = this.config.prependValue + passedValue.toString();
|
2016-04-17 13:02:28 +02:00
|
|
|
}
|
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
// If an appended value has been passed, append it
|
|
|
|
if (this.config.appendValue) {
|
|
|
|
passedValue += this.config.appendValue.toString();
|
2016-03-21 19:53:26 +01:00
|
|
|
}
|
2016-04-09 12:29:56 +02:00
|
|
|
|
2016-10-18 20:39:49 +02:00
|
|
|
this.store.dispatch(addItem(passedValue, passedLabel, id, passedOptionId, groupId));
|
2016-06-22 00:06:23 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
if (this.passedElement.type === 'select-one') {
|
|
|
|
this.removeActiveItems(id);
|
2016-06-22 00:06:23 +02:00
|
|
|
}
|
|
|
|
|
2017-01-01 16:32:09 +01:00
|
|
|
// Trigger change event
|
|
|
|
if(group && group.value) {
|
|
|
|
triggerEvent(this.passedElement, 'addItem', {
|
|
|
|
id,
|
|
|
|
value: passedValue,
|
2017-03-01 15:00:22 +01:00
|
|
|
label: passedLabel,
|
2017-01-01 16:32:09 +01:00
|
|
|
groupValue: group.value,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
triggerEvent(this.passedElement, 'addItem', {
|
|
|
|
id,
|
|
|
|
value: passedValue,
|
2017-03-01 15:00:22 +01:00
|
|
|
label: passedLabel,
|
2017-01-01 16:32:09 +01:00
|
|
|
});
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
2016-06-22 00:06:23 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove item from store
|
|
|
|
* @param {Object} item Item to remove
|
|
|
|
* @param {Function} callback Callback to trigger
|
|
|
|
* @return {Object} Class instance
|
|
|
|
* @public
|
|
|
|
*/
|
2016-10-19 08:33:38 +02:00
|
|
|
_removeItem(item) {
|
2016-09-05 23:04:15 +02:00
|
|
|
if (!item || !isType('Object', item)) {
|
|
|
|
console.error('removeItem: No item object was passed to be removed');
|
|
|
|
return;
|
|
|
|
}
|
2016-06-22 00:06:23 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
const id = item.id;
|
|
|
|
const value = item.value;
|
2017-03-01 13:41:01 +01:00
|
|
|
const label = item.label;
|
2016-09-05 23:04:15 +02:00
|
|
|
const choiceId = item.choiceId;
|
2016-10-19 08:33:38 +02:00
|
|
|
const groupId = item.groupId;
|
2017-01-01 16:32:09 +01:00
|
|
|
const group = groupId >= 0 ? this.store.getGroupById(groupId) : null;
|
2016-06-22 00:06:23 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
this.store.dispatch(removeItem(id, choiceId));
|
2016-06-22 00:06:23 +02:00
|
|
|
|
2017-01-01 16:32:09 +01:00
|
|
|
if(group && group.value) {
|
|
|
|
triggerEvent(this.passedElement, 'removeItem', {
|
|
|
|
id,
|
|
|
|
value,
|
2017-03-01 13:41:01 +01:00
|
|
|
label,
|
2017-01-01 16:32:09 +01:00
|
|
|
groupValue: group.value,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
triggerEvent(this.passedElement, 'removeItem', {
|
|
|
|
id,
|
|
|
|
value,
|
2017-03-01 15:00:22 +01:00
|
|
|
label,
|
2017-01-01 16:32:09 +01:00
|
|
|
});
|
2016-06-22 00:06:23 +02:00
|
|
|
}
|
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add choice to dropdown
|
|
|
|
* @param {Boolean} isSelected Whether choice is selected
|
|
|
|
* @param {Boolean} isDisabled Whether choice is disabled
|
|
|
|
* @param {String} value Value of choice
|
|
|
|
* @param {String} Label Label of choice
|
|
|
|
* @param {Number} groupId ID of group choice is within. Negative number indicates no group
|
|
|
|
* @return
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_addChoice(isSelected, isDisabled, value, label, groupId = -1) {
|
2016-12-29 08:06:35 +01:00
|
|
|
if (typeof value === 'undefined' || value === null) return;
|
2016-09-05 23:04:15 +02:00
|
|
|
|
|
|
|
// Generate unique id
|
|
|
|
const choices = this.store.getChoices();
|
|
|
|
const choiceLabel = label || value;
|
|
|
|
const choiceId = choices ? choices.length + 1 : 1;
|
|
|
|
|
|
|
|
this.store.dispatch(addChoice(value, choiceLabel, choiceId, groupId, isDisabled));
|
|
|
|
|
2016-09-21 14:32:21 +02:00
|
|
|
if (isSelected) {
|
2016-09-05 23:04:15 +02:00
|
|
|
this._addItem(value, choiceLabel, choiceId);
|
2016-05-08 01:02:52 +02:00
|
|
|
}
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
|
|
|
|
2016-09-26 18:11:32 +02:00
|
|
|
/**
|
|
|
|
* Clear all choices added to the store.
|
|
|
|
* @return
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_clearChoices() {
|
|
|
|
this.store.dispatch(clearChoices());
|
|
|
|
}
|
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
/**
|
|
|
|
* Add group to dropdown
|
|
|
|
* @param {Object} group Group to add
|
|
|
|
* @param {Number} id Group ID
|
2016-10-10 14:17:35 +02:00
|
|
|
* @param {String} [valueKey] name of the value property on the object
|
|
|
|
* @param {String} [labelKey] name of the label property on the object
|
2016-09-05 23:04:15 +02:00
|
|
|
* @return
|
|
|
|
* @private
|
|
|
|
*/
|
2016-10-10 14:17:35 +02:00
|
|
|
_addGroup(group, id, valueKey = 'value', labelKey = 'label') {
|
2016-09-05 23:04:15 +02:00
|
|
|
const groupChoices = isType('Object', group) ? group.choices : Array.from(group.getElementsByTagName('OPTION'));
|
2017-02-05 10:41:59 +01:00
|
|
|
const groupId = id ? id : Math.floor(new Date().valueOf() * Math.random());
|
2016-09-05 23:04:15 +02:00
|
|
|
const isDisabled = group.disabled ? group.disabled : false;
|
|
|
|
|
|
|
|
if (groupChoices) {
|
|
|
|
this.store.dispatch(addGroup(group.label, groupId, true, isDisabled));
|
|
|
|
|
|
|
|
groupChoices.forEach((option) => {
|
|
|
|
const isOptDisabled = (option.disabled || (option.parentNode && option.parentNode.disabled)) || false;
|
|
|
|
const isOptSelected = option.selected ? option.selected : false;
|
|
|
|
let label;
|
|
|
|
|
|
|
|
if (isType('Object', option)) {
|
2016-10-10 14:17:35 +02:00
|
|
|
label = option[labelKey] || option[valueKey];
|
2016-05-08 01:02:52 +02:00
|
|
|
} else {
|
2016-09-05 23:04:15 +02:00
|
|
|
label = option.innerHTML;
|
2016-05-08 01:02:52 +02:00
|
|
|
}
|
|
|
|
|
2016-10-10 14:17:35 +02:00
|
|
|
this._addChoice(isOptSelected, isOptDisabled, option[valueKey], label, groupId);
|
2016-09-05 23:04:15 +02:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.store.dispatch(addGroup(group.label, group.id, false, group.disabled));
|
2016-04-16 18:06:27 +02:00
|
|
|
}
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get template from name
|
|
|
|
* @param {String} template Name of template to get
|
|
|
|
* @param {...} args Data to pass to template
|
|
|
|
* @return {HTMLElement} Template
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_getTemplate(template, ...args) {
|
|
|
|
if (!template) return;
|
|
|
|
const templates = this.config.templates;
|
|
|
|
return templates[template](...args);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create HTML element based on type and arguments
|
|
|
|
* @return
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_createTemplates() {
|
|
|
|
const classNames = this.config.classNames;
|
|
|
|
const templates = {
|
|
|
|
containerOuter: (direction) => {
|
2016-09-27 15:13:41 +02:00
|
|
|
return strToEl(`
|
2017-03-28 15:41:12 +02:00
|
|
|
<div class="${classNames.containerOuter}" data-type="${this.passedElement.type}" ${this.passedElement.type === 'select-one' ? 'tabindex="0"' : ''} aria-haspopup="true" aria-expanded="false" dir="${direction}"></div>
|
|
|
|
`);
|
2016-09-05 23:04:15 +02:00
|
|
|
},
|
|
|
|
containerInner: () => {
|
|
|
|
return strToEl(`
|
2017-03-28 15:41:12 +02:00
|
|
|
<div class="${classNames.containerInner}"></div>
|
|
|
|
`);
|
2016-09-05 23:04:15 +02:00
|
|
|
},
|
|
|
|
itemList: () => {
|
2016-09-27 15:13:41 +02:00
|
|
|
return strToEl(`
|
2017-03-28 15:41:12 +02:00
|
|
|
<div class="${classNames.list} ${this.passedElement.type === 'select-one' ? classNames.listSingle : classNames.listItems}"></div>
|
|
|
|
`);
|
2016-09-05 23:04:15 +02:00
|
|
|
},
|
|
|
|
placeholder: (value) => {
|
|
|
|
return strToEl(`
|
2017-03-28 15:41:12 +02:00
|
|
|
<div class="${classNames.placeholder}">${value}</div>
|
|
|
|
`);
|
2016-09-05 23:04:15 +02:00
|
|
|
},
|
|
|
|
item: (data) => {
|
|
|
|
if (this.config.removeItemButton) {
|
2016-09-27 15:13:41 +02:00
|
|
|
return strToEl(`
|
2017-03-28 15:41:12 +02:00
|
|
|
<div class="${classNames.item} ${data.highlighted ? classNames.highlightedState : ''} ${!data.disabled ? classNames.itemSelectable : ''}" data-item data-id="${data.id}" data-value="${data.value}" ${data.active ? 'aria-selected="true"' : ''} ${data.disabled ? 'aria-disabled="true"' : ''} data-deletable>
|
|
|
|
${data.label}<button type="button" class="${classNames.button}" data-button>Remove item</button>
|
|
|
|
</div>
|
|
|
|
`);
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
2016-09-27 15:13:41 +02:00
|
|
|
return strToEl(`
|
2016-09-05 23:47:11 +02:00
|
|
|
<div class="${classNames.item} ${data.highlighted ? classNames.highlightedState : classNames.itemSelectable}" data-item data-id="${data.id}" data-value="${data.value}" ${data.active ? 'aria-selected="true"' : ''} ${data.disabled ? 'aria-disabled="true"' : ''}>
|
|
|
|
${data.label}
|
|
|
|
</div>
|
2017-03-28 15:41:12 +02:00
|
|
|
`);
|
2016-09-05 23:04:15 +02:00
|
|
|
},
|
|
|
|
choiceList: () => {
|
2016-09-27 15:13:41 +02:00
|
|
|
return strToEl(`
|
2017-03-28 15:41:12 +02:00
|
|
|
<div class="${classNames.list}" dir="ltr" role="listbox" ${this.passedElement.type !== 'select-one' ? 'aria-multiselectable="true"' : ''}></div>
|
|
|
|
`);
|
2016-09-05 23:04:15 +02:00
|
|
|
},
|
|
|
|
choiceGroup: (data) => {
|
2016-09-27 15:13:41 +02:00
|
|
|
return strToEl(`
|
2017-03-28 15:41:12 +02:00
|
|
|
<div class="${classNames.group} ${data.disabled ? classNames.itemDisabled : ''}" data-group data-id="${data.id}" data-value="${data.value}" role="group" ${data.disabled ? 'aria-disabled="true"' : ''}>
|
|
|
|
<div class="${classNames.groupHeading}">${data.value}</div>
|
|
|
|
</div>
|
|
|
|
`);
|
2016-09-05 23:04:15 +02:00
|
|
|
},
|
|
|
|
choice: (data) => {
|
2016-09-27 15:13:41 +02:00
|
|
|
return strToEl(`
|
|
|
|
<div class="${classNames.item} ${classNames.itemChoice} ${data.disabled ? classNames.itemDisabled : classNames.itemSelectable}" data-select-text="${this.config.itemSelectText}" data-choice ${data.disabled ? 'data-choice-disabled aria-disabled="true"' : 'data-choice-selectable'} data-id="${data.id}" data-value="${data.value}" ${data.groupId > 0 ? 'role="treeitem"' : 'role="option"'}>
|
2017-03-28 15:41:12 +02:00
|
|
|
${data.label}
|
|
|
|
</div>
|
|
|
|
`);
|
2016-09-05 23:04:15 +02:00
|
|
|
},
|
|
|
|
input: () => {
|
2016-09-27 15:13:41 +02:00
|
|
|
return strToEl(`
|
|
|
|
<input type="text" class="${classNames.input} ${classNames.inputCloned}" autocomplete="off" autocapitalize="off" spellcheck="false" role="textbox" aria-autocomplete="list">
|
2017-03-28 15:41:12 +02:00
|
|
|
`);
|
2016-09-05 23:04:15 +02:00
|
|
|
},
|
|
|
|
dropdown: () => {
|
2016-09-27 15:13:41 +02:00
|
|
|
return strToEl(`
|
2017-03-28 15:41:12 +02:00
|
|
|
<div class="${classNames.list} ${classNames.listDropdown}" aria-expanded="false"></div>
|
|
|
|
`);
|
2016-09-05 23:04:15 +02:00
|
|
|
},
|
|
|
|
notice: (label) => {
|
|
|
|
return strToEl(`
|
2017-03-28 15:41:12 +02:00
|
|
|
<div class="${classNames.item} ${classNames.itemChoice}">${label}</div>
|
|
|
|
`);
|
2016-09-05 23:04:15 +02:00
|
|
|
},
|
|
|
|
option: (data) => {
|
|
|
|
return strToEl(`
|
2017-03-28 15:41:12 +02:00
|
|
|
<option value="${data.value}" selected>${data.label}</option>
|
|
|
|
`);
|
2016-09-05 23:04:15 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2016-09-30 09:40:06 +02:00
|
|
|
// User's custom templates
|
|
|
|
const callbackTemplate = this.config.callbackOnCreateTemplates;
|
|
|
|
let userTemplates = {};
|
2016-09-30 14:50:23 +02:00
|
|
|
if (callbackTemplate && isType('Function', callbackTemplate)) {
|
2016-11-07 11:37:47 +01:00
|
|
|
userTemplates = callbackTemplate.call(this, strToEl);
|
2016-09-30 09:40:06 +02:00
|
|
|
}
|
2017-01-01 16:32:09 +01:00
|
|
|
|
2016-09-30 09:40:06 +02:00
|
|
|
this.config.templates = extend(templates, userTemplates);
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create DOM structure around passed select element
|
|
|
|
* @return
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_createInput() {
|
|
|
|
const direction = this.passedElement.getAttribute('dir') || 'ltr';
|
|
|
|
const containerOuter = this._getTemplate('containerOuter', direction);
|
|
|
|
const containerInner = this._getTemplate('containerInner');
|
|
|
|
const itemList = this._getTemplate('itemList');
|
|
|
|
const choiceList = this._getTemplate('choiceList');
|
|
|
|
const input = this._getTemplate('input');
|
|
|
|
const dropdown = this._getTemplate('dropdown');
|
|
|
|
const placeholder = this.config.placeholder ? this.config.placeholderValue || this.passedElement.getAttribute('placeholder') : false;
|
|
|
|
|
|
|
|
this.containerOuter = containerOuter;
|
|
|
|
this.containerInner = containerInner;
|
|
|
|
this.input = input;
|
|
|
|
this.choiceList = choiceList;
|
|
|
|
this.itemList = itemList;
|
|
|
|
this.dropdown = dropdown;
|
|
|
|
|
|
|
|
// Hide passed input
|
|
|
|
this.passedElement.classList.add(this.config.classNames.input, this.config.classNames.hiddenState);
|
|
|
|
this.passedElement.tabIndex = '-1';
|
|
|
|
this.passedElement.setAttribute('style', 'display:none;');
|
|
|
|
this.passedElement.setAttribute('aria-hidden', 'true');
|
|
|
|
this.passedElement.setAttribute('data-choice', 'active');
|
|
|
|
|
|
|
|
// Wrap input in container preserving DOM ordering
|
|
|
|
wrap(this.passedElement, containerInner);
|
|
|
|
|
|
|
|
// Wrapper inner container with outer container
|
|
|
|
wrap(containerInner, containerOuter);
|
|
|
|
|
|
|
|
// If placeholder has been enabled and we have a value
|
|
|
|
if (placeholder) {
|
|
|
|
input.placeholder = placeholder;
|
|
|
|
if (this.passedElement.type !== 'select-one') {
|
|
|
|
input.style.width = getWidthOfInput(input);
|
|
|
|
}
|
2016-04-22 20:45:50 +02:00
|
|
|
}
|
2016-03-21 19:53:26 +01:00
|
|
|
|
2016-11-18 16:01:17 +01:00
|
|
|
// Disable text input if no entry allowed
|
|
|
|
if (!this.config.addItems && this.isTextElement) {
|
|
|
|
this.disable();
|
|
|
|
}
|
2016-03-18 13:26:38 +01:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
containerOuter.appendChild(containerInner);
|
|
|
|
containerOuter.appendChild(dropdown);
|
|
|
|
containerInner.appendChild(itemList);
|
2016-08-14 23:14:37 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
if (this.passedElement.type !== 'text') {
|
|
|
|
dropdown.appendChild(choiceList);
|
|
|
|
}
|
2016-04-29 18:11:20 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
if (this.passedElement.type === 'select-multiple' || this.passedElement.type === 'text') {
|
|
|
|
containerInner.appendChild(input);
|
|
|
|
} else if (this.canSearch) {
|
|
|
|
dropdown.insertBefore(input, dropdown.firstChild);
|
|
|
|
}
|
2016-07-02 14:04:38 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
if (this.passedElement.type === 'select-multiple' || this.passedElement.type === 'select-one') {
|
|
|
|
const passedGroups = Array.from(this.passedElement.getElementsByTagName('OPTGROUP'));
|
2016-08-14 23:14:37 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
this.highlightPosition = 0;
|
|
|
|
this.isSearching = false;
|
2016-04-14 15:54:47 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
if (passedGroups && passedGroups.length) {
|
2017-01-30 20:48:55 +01:00
|
|
|
passedGroups.forEach((group) => {
|
2017-02-05 10:41:59 +01:00
|
|
|
this._addGroup(group, (group.id || null));
|
2016-09-05 23:04:15 +02:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
const passedOptions = Array.from(this.passedElement.options);
|
2016-08-03 15:23:23 +02:00
|
|
|
const filter = this.config.sortFilter;
|
2016-09-05 23:04:15 +02:00
|
|
|
const allChoices = this.presetChoices;
|
|
|
|
|
|
|
|
// Create array of options from option elements
|
|
|
|
passedOptions.forEach((o) => {
|
|
|
|
allChoices.push({
|
|
|
|
value: o.value,
|
|
|
|
label: o.innerHTML,
|
|
|
|
selected: o.selected,
|
|
|
|
disabled: o.disabled || o.parentNode.disabled,
|
|
|
|
});
|
|
|
|
});
|
2016-05-10 10:02:59 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
// If sorting is enabled or the user is searching, filter choices
|
2016-08-31 20:18:46 +02:00
|
|
|
if (this.config.shouldSort) {
|
2016-09-05 23:04:15 +02:00
|
|
|
allChoices.sort(filter);
|
2016-08-31 20:18:46 +02:00
|
|
|
}
|
2016-04-29 16:18:53 +02:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
// Determine whether there is a selected choice
|
|
|
|
const hasSelectedChoice = allChoices.some((choice) => {
|
|
|
|
return choice.selected === true;
|
2016-08-31 20:18:46 +02:00
|
|
|
});
|
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
// Add each choice
|
|
|
|
allChoices.forEach((choice, index) => {
|
|
|
|
const isDisabled = choice.disabled ? choice.disabled : false;
|
|
|
|
const isSelected = choice.selected ? choice.selected : false;
|
|
|
|
// Pre-select first choice if it's a single select
|
|
|
|
if (this.passedElement.type === 'select-one') {
|
|
|
|
if (hasSelectedChoice || (!hasSelectedChoice && index > 0)) {
|
|
|
|
// If there is a selected choice already or the choice is not
|
|
|
|
// the first in the array, add each choice normally
|
|
|
|
this._addChoice(isSelected, isDisabled, choice.value, choice.label);
|
|
|
|
} else {
|
|
|
|
// Otherwise pre-select the first choice in the array
|
|
|
|
this._addChoice(true, false, choice.value, choice.label);
|
2016-08-31 20:18:46 +02:00
|
|
|
}
|
2016-09-05 23:04:15 +02:00
|
|
|
} else {
|
|
|
|
this._addChoice(isSelected, isDisabled, choice.value, choice.label);
|
|
|
|
}
|
2016-08-31 20:18:46 +02:00
|
|
|
});
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
2016-10-26 16:43:15 +02:00
|
|
|
} else if (this.isTextElement) {
|
2016-09-05 23:04:15 +02:00
|
|
|
// Add any preset values seperated by delimiter
|
|
|
|
this.presetItems.forEach((item) => {
|
2017-02-17 10:23:52 +01:00
|
|
|
const itemType = getType(item);
|
|
|
|
if (itemType === 'Object') {
|
2016-09-05 23:04:15 +02:00
|
|
|
if (!item.value) return;
|
|
|
|
this._addItem(item.value, item.label, item.id);
|
2017-02-17 10:23:52 +01:00
|
|
|
} else if (itemType === 'String') {
|
2016-09-05 23:04:15 +02:00
|
|
|
this._addItem(item);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-27 14:44:35 +02:00
|
|
|
/*===== End of Private functions ======*/
|
2016-08-14 23:14:37 +02:00
|
|
|
}
|
2016-03-15 23:42:10 +01:00
|
|
|
|
2016-10-18 15:15:00 +02:00
|
|
|
module.exports = Choices;
|