2016-03-17 16:00:22 +01:00
|
|
|
'use strict';
|
|
|
|
|
2016-04-28 16:30:43 +02:00
|
|
|
import { addItem, removeItem, selectItem, addOption, filterOptions, activateOptions, addGroup } from './actions/index';
|
2016-05-02 16:29:05 +02:00
|
|
|
import { isScrolledIntoView, getAdjacentEl, findAncestor, wrap, isType, strToEl, extend, getWidthOfInput, debounce } from './lib/utils.js';
|
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-04-13 15:40:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Choices
|
|
|
|
*
|
|
|
|
* To do:
|
2016-05-16 15:46:04 +02:00
|
|
|
* - Pagination
|
|
|
|
* - Single select box search in dropdown
|
2016-04-13 15:40:41 +02:00
|
|
|
*/
|
2016-03-18 13:26:38 +01:00
|
|
|
export class Choices {
|
2016-04-29 19:06:46 +02:00
|
|
|
constructor(element = '[data-choice]', userOptions = {}) {
|
2016-04-17 13:09:46 +02:00
|
|
|
|
2016-04-10 22:23:42 +02:00
|
|
|
// 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)) {
|
2016-04-12 23:42:56 +02:00
|
|
|
const elements = document.querySelectorAll(element);
|
2016-04-10 22:23:42 +02:00
|
|
|
if(elements.length > 1) {
|
|
|
|
for (let i = 1; i < elements.length; i++) {
|
2016-05-03 22:31:05 +02:00
|
|
|
const el = elements[i];
|
|
|
|
new Choices(el, userOptions);
|
2016-04-10 22:23:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-04 00:07:10 +02:00
|
|
|
const defaultOptions = {
|
2016-04-07 14:57:57 +02:00
|
|
|
items: [],
|
2016-03-24 15:42:03 +01:00
|
|
|
addItems: true,
|
2016-03-22 23:47:29 +01:00
|
|
|
removeItems: true,
|
2016-05-11 15:51:32 +02:00
|
|
|
removeButton: false,
|
2016-03-21 19:53:26 +01:00
|
|
|
editItems: false,
|
2016-03-18 12:05:50 +01:00
|
|
|
maxItems: false,
|
|
|
|
delimiter: ',',
|
|
|
|
allowDuplicates: true,
|
2016-04-14 15:54:47 +02:00
|
|
|
allowPaste: true,
|
2016-04-22 10:02:42 +02:00
|
|
|
allowSearch: true,
|
2016-03-24 15:42:03 +01:00
|
|
|
regexFilter: false,
|
2016-04-15 10:19:02 +02:00
|
|
|
placeholder: true,
|
|
|
|
placeholderValue: '',
|
2016-03-24 15:42:03 +01:00
|
|
|
prependValue: false,
|
|
|
|
appendValue: false,
|
2016-04-04 00:07:10 +02:00
|
|
|
selectAll: true,
|
2016-06-01 19:56:08 +02:00
|
|
|
loadingText: 'Loading...',
|
2016-05-07 13:36:50 +02:00
|
|
|
templates: {},
|
2016-04-10 23:54:56 +02:00
|
|
|
classNames: {
|
2016-04-11 15:13:50 +02:00
|
|
|
containerOuter: 'choices',
|
|
|
|
containerInner: 'choices__inner',
|
|
|
|
input: 'choices__input',
|
|
|
|
inputCloned: 'choices__input--cloned',
|
|
|
|
list: 'choices__list',
|
2016-05-07 13:36:50 +02:00
|
|
|
listItems: 'choices__list--multiple',
|
|
|
|
listSingle: 'choices__list--single',
|
2016-04-11 15:13:50 +02:00
|
|
|
listDropdown: 'choices__list--dropdown',
|
|
|
|
item: 'choices__item',
|
|
|
|
itemSelectable: 'choices__item--selectable',
|
2016-04-12 23:42:56 +02:00
|
|
|
itemDisabled: 'choices__item--disabled',
|
|
|
|
itemOption: 'choices__item--option',
|
2016-04-16 18:06:27 +02:00
|
|
|
group: 'choices__group',
|
|
|
|
groupHeading : 'choices__heading',
|
2016-05-11 15:51:32 +02:00
|
|
|
button: 'choices__button',
|
2016-04-11 15:13:50 +02:00
|
|
|
activeState: 'is-active',
|
2016-05-05 22:46:56 +02:00
|
|
|
focusState: 'is-focused',
|
|
|
|
openState: 'is-open',
|
2016-04-11 15:13:50 +02:00
|
|
|
disabledState: 'is-disabled',
|
2016-04-26 15:36:02 +02:00
|
|
|
highlightedState: 'is-highlighted',
|
2016-04-11 15:13:50 +02:00
|
|
|
hiddenState: 'is-hidden',
|
2016-04-21 15:42:57 +02:00
|
|
|
flippedState: 'is-flipped',
|
2016-05-12 00:17:22 +02:00
|
|
|
selectedState: 'is-selected',
|
2016-04-10 23:54:56 +02:00
|
|
|
},
|
2016-05-08 01:02:52 +02:00
|
|
|
callbackOnInit: () => {},
|
|
|
|
callbackOnRemoveItem: () => {},
|
|
|
|
callbackOnAddItem: () => {}
|
2016-03-17 16:00:22 +01:00
|
|
|
};
|
2016-03-15 23:42:10 +01:00
|
|
|
|
2016-03-17 16:00:22 +01:00
|
|
|
// Merge options with user options
|
2016-04-29 19:06:46 +02:00
|
|
|
this.options = extend(defaultOptions, userOptions);
|
2016-04-04 22:44:32 +02:00
|
|
|
|
|
|
|
// Create data store
|
2016-05-04 15:31:29 +02:00
|
|
|
this.store = new Store(this.render);
|
|
|
|
|
|
|
|
// State tracking
|
2016-06-01 19:45:35 +02:00
|
|
|
this.initialised = false;
|
2016-05-04 15:31:29 +02:00
|
|
|
this.currentState = {};
|
2016-06-01 19:45:35 +02:00
|
|
|
this.prevState = {};
|
2016-03-30 16:04:21 +02:00
|
|
|
|
2016-04-04 00:07:10 +02:00
|
|
|
// Retrieve triggering element (i.e. element with 'data-choice' trigger)
|
2016-04-10 22:23:42 +02:00
|
|
|
this.passedElement = isType('String', element) ? document.querySelector(element) : element;
|
2016-03-31 15:51:41 +02:00
|
|
|
|
2016-05-02 22:39:33 +02:00
|
|
|
this.highlightPosition = 0;
|
|
|
|
|
2016-06-01 19:45:35 +02:00
|
|
|
// Assign preset items from passed object first
|
|
|
|
this.presetItems = this.options.items;
|
|
|
|
// Then add any values passed from attribute
|
|
|
|
if(this.passedElement.value !== '') {
|
|
|
|
this.presetItems = this.presetItems.concat(this.passedElement.value.split(this.options.delimiter));
|
2016-04-07 14:57:57 +02:00
|
|
|
}
|
|
|
|
|
2016-03-17 16:00:22 +01:00
|
|
|
// Bind methods
|
2016-06-01 19:45:35 +02:00
|
|
|
this.init = this.init.bind(this);
|
|
|
|
this.render = this.render.bind(this);
|
2016-04-08 10:07:41 +02:00
|
|
|
this.destroy = this.destroy.bind(this);
|
2016-05-08 01:02:52 +02:00
|
|
|
this.disable = this.disable.bind(this);
|
2016-04-29 19:23:06 +02:00
|
|
|
|
|
|
|
// Bind event handlers
|
2016-06-01 19:45:35 +02:00
|
|
|
this.onFocus = this.onFocus.bind(this);
|
|
|
|
this.onBlur = this.onBlur.bind(this);
|
|
|
|
this.onKeyUp = this.onKeyUp.bind(this);
|
|
|
|
this.onKeyDown = this.onKeyDown.bind(this);
|
2016-05-08 13:22:56 +02:00
|
|
|
this.onMouseDown = this.onMouseDown.bind(this);
|
2016-05-02 13:23:12 +02:00
|
|
|
this.onMouseOver = this.onMouseOver.bind(this);
|
2016-06-01 19:45:35 +02:00
|
|
|
this.onPaste = this.onPaste.bind(this);
|
|
|
|
this.onInput = this.onInput.bind(this);
|
2016-04-29 16:18:53 +02:00
|
|
|
|
2016-05-07 13:36:50 +02:00
|
|
|
// Cutting the mustard
|
|
|
|
const cuttingTheMustard = 'querySelector' in document && 'addEventListener' in document && 'classList' in document.createElement("div");
|
|
|
|
if (!cuttingTheMustard) console.error('Choices: Your browser doesn\'t support Choices');
|
|
|
|
|
|
|
|
// Input type check
|
|
|
|
const inputTypes = ['select-one', 'select-multiple', 'text'];
|
|
|
|
const canInit = this.passedElement && inputTypes.includes(this.passedElement.type);
|
|
|
|
|
|
|
|
if(canInit) {
|
|
|
|
// Let's have it large
|
|
|
|
this.init();
|
|
|
|
} else {
|
|
|
|
console.error('Choices: Incompatible input passed');
|
|
|
|
}
|
2016-03-17 16:00:22 +01:00
|
|
|
}
|
2016-04-29 19:06:46 +02:00
|
|
|
|
2016-04-04 22:44:32 +02:00
|
|
|
/**
|
2016-04-29 19:06:46 +02:00
|
|
|
* Process enter key event
|
|
|
|
* @param {Array} activeItems Items that are currently active
|
|
|
|
* @return
|
2016-04-04 22:44:32 +02:00
|
|
|
*/
|
2016-04-29 19:06:46 +02:00
|
|
|
handleEnter(activeItems, value) {
|
2016-04-17 12:23:38 +02:00
|
|
|
let canUpdate = true;
|
|
|
|
|
2016-05-08 01:02:52 +02:00
|
|
|
if(this.options.addItems) {
|
2016-05-18 23:40:32 +02:00
|
|
|
if (this.options.maxItems && this.options.maxItems <= this.itemList.children.length) {
|
2016-05-08 01:02:52 +02:00
|
|
|
// If there is a max entry limit and we have reached that limit
|
|
|
|
// don't update
|
2016-04-17 12:23:38 +02:00
|
|
|
canUpdate = false;
|
2016-05-08 01:02:52 +02:00
|
|
|
} else if(this.options.allowDuplicates === false && this.passedElement.value) {
|
|
|
|
// If no duplicates are allowed, and the value already exists
|
|
|
|
// in the array, don't update
|
|
|
|
canUpdate = !activeItems.some((item) => item.value === value );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
canUpdate = false;
|
|
|
|
}
|
2016-04-17 12:23:38 +02:00
|
|
|
|
2016-05-08 01:02:52 +02:00
|
|
|
if (canUpdate) {
|
|
|
|
let canAddItem = true;
|
2016-04-17 12:23:38 +02:00
|
|
|
|
2016-05-08 01:02:52 +02:00
|
|
|
// If a user has supplied a regular expression filter
|
|
|
|
if(this.options.regexFilter) {
|
|
|
|
// Determine whether we can update based on whether
|
|
|
|
// our regular expression passes
|
|
|
|
canAddItem = this.regexFilter(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
// All is good, add
|
|
|
|
if(canAddItem) {
|
|
|
|
this.toggleDropdown();
|
|
|
|
this.addItem(value);
|
|
|
|
this.clearInput(this.passedElement);
|
2016-04-17 12:23:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-04-29 19:06:46 +02:00
|
|
|
/**
|
|
|
|
* Process back space event
|
|
|
|
* @param {Array} Active items
|
|
|
|
* @return
|
|
|
|
*/
|
2016-05-07 13:36:50 +02:00
|
|
|
handleBackspace(activeItems) {
|
2016-04-17 12:23:38 +02:00
|
|
|
if(this.options.removeItems && activeItems) {
|
|
|
|
const lastItem = activeItems[activeItems.length - 1];
|
2016-05-11 15:25:34 +02:00
|
|
|
const hasSelectedItems = activeItems.some((item) => item.selected === true);
|
2016-04-17 12:23:38 +02:00
|
|
|
|
|
|
|
// 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
|
2016-04-17 13:09:46 +02:00
|
|
|
if(this.options.editItems && !hasSelectedItems && lastItem) {
|
2016-04-17 12:23:38 +02:00
|
|
|
this.input.value = lastItem.value;
|
|
|
|
this.removeItem(lastItem);
|
|
|
|
} else {
|
2016-05-11 15:25:34 +02:00
|
|
|
if(!hasSelectedItems) { this.selectItem(lastItem); }
|
|
|
|
this.removeSelectedItems();
|
2016-04-17 12:23:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-04-29 19:06:46 +02:00
|
|
|
/**
|
|
|
|
* Key down event
|
2016-04-04 22:44:32 +02:00
|
|
|
* @param {Object} e Event
|
|
|
|
* @return
|
|
|
|
*/
|
2016-03-17 16:00:22 +01:00
|
|
|
onKeyDown(e) {
|
2016-04-29 19:23:06 +02:00
|
|
|
if(e.target !== this.input) return;
|
2016-05-07 13:36:50 +02:00
|
|
|
|
2016-04-04 00:07:10 +02:00
|
|
|
const ctrlDownKey = e.ctrlKey || e.metaKey;
|
2016-06-01 19:45:35 +02:00
|
|
|
const backKey = 46;
|
|
|
|
const deleteKey = 8;
|
|
|
|
const enterKey = 13;
|
|
|
|
const aKey = 65;
|
|
|
|
const escapeKey = 27;
|
|
|
|
const upKey = 38;
|
|
|
|
const downKey = 40;
|
|
|
|
|
|
|
|
const activeItems = this.store.getItemsFilteredByActive();
|
|
|
|
const activeOptions = this.store.getOptionsFilteredByActive();
|
|
|
|
|
|
|
|
const hasFocusedInput = this.input === document.activeElement;
|
2016-05-07 14:30:07 +02:00
|
|
|
const hasActiveDropdown = this.dropdown.classList.contains(this.options.classNames.activeState);
|
2016-06-01 19:45:35 +02:00
|
|
|
const hasItems = this.itemList && this.itemList.children;
|
|
|
|
const keyString = String.fromCharCode(event.keyCode);
|
2016-04-04 00:07:10 +02:00
|
|
|
|
2016-04-29 19:23:06 +02:00
|
|
|
// If a user is typing and the dropdown is not active
|
2016-05-07 14:30:07 +02:00
|
|
|
if(this.passedElement.type !== 'text' && /[a-zA-Z0-9-_ ]/.test(keyString) && !hasActiveDropdown) {
|
2016-05-08 01:02:52 +02:00
|
|
|
this.showDropdown();
|
2016-04-29 19:23:06 +02:00
|
|
|
}
|
2016-04-26 15:36:02 +02:00
|
|
|
|
2016-04-29 19:23:06 +02:00
|
|
|
switch (e.keyCode) {
|
|
|
|
case aKey:
|
|
|
|
// If CTRL + A or CMD + A have been pressed and there are items to select
|
|
|
|
if(ctrlDownKey && hasItems) {
|
|
|
|
if(this.options.removeItems && !this.input.value && this.options.selectAll && this.input === document.activeElement) {
|
2016-05-18 23:40:32 +02:00
|
|
|
this.selectAll(this.itemList.children);
|
2016-04-29 16:18:53 +02:00
|
|
|
}
|
2016-04-29 19:23:06 +02:00
|
|
|
}
|
|
|
|
break;
|
2016-05-02 16:29:05 +02:00
|
|
|
|
2016-04-29 19:23:06 +02:00
|
|
|
case enterKey:
|
2016-05-11 15:25:34 +02:00
|
|
|
if(this.passedElement.type === 'select-one') this.toggleDropdown();
|
2016-05-08 01:02:52 +02:00
|
|
|
|
2016-04-29 19:23:06 +02:00
|
|
|
// If enter key is pressed and the input has a value
|
|
|
|
if(e.target.value && this.passedElement.type === 'text') {
|
|
|
|
const value = this.input.value;
|
|
|
|
this.handleEnter(activeItems, value);
|
|
|
|
}
|
|
|
|
|
2016-05-08 01:02:52 +02:00
|
|
|
if(hasActiveDropdown) {
|
2016-04-29 19:23:06 +02:00
|
|
|
const highlighted = this.dropdown.querySelector(`.${this.options.classNames.highlightedState}`);
|
|
|
|
|
|
|
|
if(highlighted) {
|
2016-05-02 16:29:05 +02:00
|
|
|
const value = highlighted.getAttribute('data-value');
|
2016-04-29 19:23:06 +02:00
|
|
|
const label = highlighted.innerHTML;
|
2016-06-01 19:45:35 +02:00
|
|
|
const id = highlighted.getAttribute('data-id');
|
2016-04-29 19:23:06 +02:00
|
|
|
this.addItem(value, label, id);
|
2016-05-02 16:29:05 +02:00
|
|
|
this.clearInput(this.passedElement);
|
2016-04-29 16:18:53 +02:00
|
|
|
}
|
2016-04-29 19:23:06 +02:00
|
|
|
}
|
|
|
|
break;
|
2016-05-02 16:29:05 +02:00
|
|
|
|
2016-04-29 19:23:06 +02:00
|
|
|
case escapeKey:
|
2016-05-08 01:02:52 +02:00
|
|
|
if(hasActiveDropdown) {
|
2016-04-29 19:23:06 +02:00
|
|
|
this.toggleDropdown();
|
|
|
|
}
|
|
|
|
break;
|
2016-05-02 16:29:05 +02:00
|
|
|
|
2016-04-29 19:23:06 +02:00
|
|
|
case downKey:
|
|
|
|
case upKey:
|
|
|
|
// If up or down key is pressed, traverse through options
|
2016-05-08 01:02:52 +02:00
|
|
|
if(hasActiveDropdown) {
|
2016-06-01 19:45:35 +02:00
|
|
|
const currentEl = this.dropdown.querySelector(`.${this.options.classNames.highlightedState}`);
|
2016-05-02 22:39:33 +02:00
|
|
|
const directionInt = e.keyCode === downKey ? 1 : -1;
|
2016-05-02 14:22:53 +02:00
|
|
|
let nextEl;
|
2016-04-28 16:30:43 +02:00
|
|
|
|
2016-05-02 14:22:53 +02:00
|
|
|
if(currentEl) {
|
2016-05-02 22:39:33 +02:00
|
|
|
nextEl = getAdjacentEl(currentEl, '[data-option-selectable]', directionInt);
|
2016-05-02 14:22:53 +02:00
|
|
|
} else {
|
2016-05-02 16:29:05 +02:00
|
|
|
nextEl = this.dropdown.querySelector('[data-option-selectable]');
|
2016-04-29 19:23:06 +02:00
|
|
|
}
|
2016-05-02 14:22:53 +02:00
|
|
|
|
|
|
|
if(nextEl) {
|
2016-05-03 22:31:05 +02:00
|
|
|
// We prevent default to stop the cursor moving
|
|
|
|
// when pressing the arrow
|
2016-05-02 22:39:33 +02:00
|
|
|
if(!isScrolledIntoView(nextEl, this.dropdown, directionInt)) {
|
|
|
|
this.scrollToOption(nextEl, directionInt);
|
2016-05-02 16:29:05 +02:00
|
|
|
}
|
2016-05-03 22:31:05 +02:00
|
|
|
this.highlightOption(nextEl);
|
2016-04-28 16:30:43 +02:00
|
|
|
}
|
2016-04-29 19:23:06 +02:00
|
|
|
}
|
|
|
|
break
|
2016-05-02 16:29:05 +02:00
|
|
|
|
2016-04-29 19:23:06 +02:00
|
|
|
case backKey:
|
|
|
|
case deleteKey:
|
|
|
|
// If backspace or delete key is pressed and the input has no value
|
2016-05-08 13:22:56 +02:00
|
|
|
if(hasFocusedInput && !e.target.value) {
|
2016-05-07 13:36:50 +02:00
|
|
|
this.handleBackspace(activeItems);
|
2016-04-29 19:23:06 +02:00
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
break;
|
2016-05-02 16:29:05 +02:00
|
|
|
|
2016-04-29 19:23:06 +02:00
|
|
|
default:
|
|
|
|
break;
|
2016-03-15 23:42:10 +01:00
|
|
|
}
|
2016-03-17 16:00:22 +01:00
|
|
|
}
|
2016-03-15 23:42:10 +01:00
|
|
|
|
2016-04-29 19:06:46 +02:00
|
|
|
/**
|
|
|
|
* Key up event
|
|
|
|
* @param {Object} e Event
|
|
|
|
* @return
|
|
|
|
*/
|
2016-04-22 20:45:50 +02:00
|
|
|
onKeyUp(e) {
|
2016-04-29 19:23:06 +02:00
|
|
|
if(e.target !== this.input) return;
|
2016-05-07 13:36:50 +02:00
|
|
|
|
2016-05-07 14:30:07 +02:00
|
|
|
// We are typing into a text input and have a value, we want to show a dropdown
|
|
|
|
// notice. Otherwise hide the dropdown
|
|
|
|
if(this.passedElement.type === 'text') {
|
2016-05-10 10:02:59 +02:00
|
|
|
const hasActiveDropdown = this.dropdown.classList.contains(this.options.classNames.activeState);
|
2016-05-07 15:14:05 +02:00
|
|
|
let dropdownItem;
|
2016-05-07 14:34:59 +02:00
|
|
|
if(this.input.value) {
|
2016-05-16 15:46:04 +02:00
|
|
|
|
|
|
|
const activeItems = this.store.getItemsFilteredByActive();
|
|
|
|
const isUnique = !activeItems.some((item) => item.value === this.input.value);
|
|
|
|
|
2016-05-18 23:40:32 +02:00
|
|
|
if (this.options.maxItems && this.options.maxItems <= this.itemList.children.length) {
|
2016-05-16 15:46:04 +02:00
|
|
|
dropdownItem = this.getTemplate('notice', `Only ${ this.options.maxItems } options can be added.`);
|
|
|
|
} else if(!this.options.allowDuplicates && !isUnique) {
|
|
|
|
dropdownItem = this.getTemplate('notice', `Only unique values can be added.`);
|
2016-05-07 15:14:05 +02:00
|
|
|
} else {
|
2016-05-08 13:22:56 +02:00
|
|
|
dropdownItem = this.getTemplate('notice', `Add "${ this.input.value }"`);
|
2016-05-07 15:14:05 +02:00
|
|
|
}
|
|
|
|
|
2016-05-08 13:22:56 +02:00
|
|
|
if((this.options.regexFilter && this.regexFilter(this.input.value)) || !this.options.regexFilter) {
|
|
|
|
this.dropdown.innerHTML = dropdownItem.outerHTML;
|
|
|
|
if(!this.dropdown.classList.contains(this.options.classNames.activeState)) {
|
|
|
|
this.showDropdown();
|
|
|
|
}
|
2016-05-07 14:30:07 +02:00
|
|
|
}
|
2016-05-08 13:22:56 +02:00
|
|
|
|
2016-05-07 14:30:07 +02:00
|
|
|
} else {
|
2016-05-10 10:02:59 +02:00
|
|
|
if(hasActiveDropdown) this.hideDropdown();
|
2016-05-07 14:30:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-08 01:02:52 +02:00
|
|
|
if(this.options.allowSearch) {
|
2016-04-29 19:23:06 +02:00
|
|
|
if(this.input === document.activeElement) {
|
2016-06-01 19:45:35 +02:00
|
|
|
const options = this.store.getOptions();
|
2016-05-08 01:02:52 +02:00
|
|
|
const hasUnactiveOptions = options.some((option) => option.active !== true);
|
2016-05-03 15:55:38 +02:00
|
|
|
|
2016-05-03 22:31:05 +02:00
|
|
|
// Check that have a value to search
|
2016-05-04 15:31:29 +02:00
|
|
|
if(this.input.value && options.length) {
|
2016-05-08 01:02:52 +02:00
|
|
|
const handleFilter = () => {
|
2016-05-16 15:46:04 +02:00
|
|
|
if(this.input.value.length >= 1) {
|
2016-05-04 15:31:29 +02:00
|
|
|
const haystack = this.store.getOptionsFiltedBySelectable();
|
2016-06-01 19:45:35 +02:00
|
|
|
const needle = this.input.value;
|
2016-05-04 15:31:29 +02:00
|
|
|
|
|
|
|
const fuse = new Fuse(haystack, {
|
2016-05-03 22:31:05 +02:00
|
|
|
keys: ['label', 'value'],
|
|
|
|
shouldSort: true,
|
|
|
|
include: 'score',
|
|
|
|
});
|
2016-05-04 15:31:29 +02:00
|
|
|
|
|
|
|
const results = fuse.search(needle);
|
|
|
|
|
2016-05-16 15:46:04 +02:00
|
|
|
this.highlightPosition = 0;
|
2016-05-03 22:31:05 +02:00
|
|
|
this.isSearching = true;
|
|
|
|
this.store.dispatch(filterOptions(results));
|
|
|
|
}
|
2016-05-08 01:02:52 +02:00
|
|
|
};
|
|
|
|
|
2016-05-03 22:31:05 +02:00
|
|
|
handleFilter();
|
2016-04-29 19:23:06 +02:00
|
|
|
} else if(hasUnactiveOptions) {
|
|
|
|
// Otherwise reset options to active
|
2016-05-03 22:31:05 +02:00
|
|
|
this.isSearching = false;
|
2016-04-29 19:23:06 +02:00
|
|
|
this.store.dispatch(activateOptions());
|
2016-04-22 20:45:50 +02:00
|
|
|
}
|
2016-04-29 19:23:06 +02:00
|
|
|
}
|
|
|
|
}
|
2016-05-04 10:02:22 +02:00
|
|
|
}
|
2016-04-22 20:45:50 +02:00
|
|
|
|
2016-05-16 15:46:04 +02:00
|
|
|
onInput(e) {
|
2016-05-18 23:40:32 +02:00
|
|
|
if(this.passedElement.type !== 'select-one') {
|
|
|
|
this.input.style.width = getWidthOfInput(this.input);
|
|
|
|
}
|
2016-05-16 15:46:04 +02:00
|
|
|
}
|
|
|
|
|
2016-04-29 19:06:46 +02:00
|
|
|
/**
|
|
|
|
* Click event
|
2016-04-04 22:44:32 +02:00
|
|
|
* @param {Object} e Event
|
|
|
|
* @return
|
|
|
|
*/
|
2016-05-08 13:22:56 +02:00
|
|
|
onMouseDown(e) {
|
2016-05-11 15:51:32 +02:00
|
|
|
// If not a right click
|
|
|
|
if(e.button !== 2) {
|
|
|
|
const activeItems = this.store.getItemsFilteredByActive();
|
2016-04-17 13:09:46 +02:00
|
|
|
|
2016-05-11 15:51:32 +02:00
|
|
|
// If click is affecting a child node of our element
|
|
|
|
if(this.containerOuter.contains(e.target)) {
|
2016-05-10 10:02:59 +02:00
|
|
|
|
2016-05-11 15:51:32 +02:00
|
|
|
// Prevent blur event triggering causing dropdown to close
|
|
|
|
// in a race condition
|
|
|
|
e.preventDefault();
|
2016-05-08 13:22:56 +02:00
|
|
|
|
2016-05-11 15:51:32 +02:00
|
|
|
const hasShiftKey = e.shiftKey ? true : false;
|
2016-05-08 13:22:56 +02:00
|
|
|
|
2016-05-11 15:51:32 +02:00
|
|
|
if(this.passedElement.type !== 'text' && !this.dropdown.classList.contains(this.options.classNames.activeState)) {
|
|
|
|
// For select inputs we always want to show the dropdown if it isn't already showing
|
|
|
|
this.showDropdown();
|
|
|
|
}
|
2016-05-07 14:30:07 +02:00
|
|
|
|
2016-06-01 19:56:08 +02:00
|
|
|
// If input is not in focus, it ought to be
|
|
|
|
if(this.input !== document.activeElement) {
|
|
|
|
this.input.focus();
|
|
|
|
}
|
|
|
|
|
2016-05-11 15:51:32 +02:00
|
|
|
if(e.target.hasAttribute('data-button')) {
|
|
|
|
if(this.options.removeItems && this.options.removeButton) {
|
2016-06-01 19:45:35 +02:00
|
|
|
const itemId = e.target.parentNode.getAttribute('data-id');
|
2016-05-11 15:51:32 +02:00
|
|
|
const itemToRemove = activeItems.find((item) => item.id === parseInt(itemId));
|
|
|
|
this.removeItem(itemToRemove);
|
|
|
|
}
|
|
|
|
} else if(e.target.hasAttribute('data-item')) {
|
|
|
|
// If we are clicking on an item
|
|
|
|
if(this.options.removeItems) {
|
|
|
|
const passedId = e.target.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) && !item.selected) {
|
|
|
|
this.selectItem(item);
|
|
|
|
} else if(!hasShiftKey) {
|
|
|
|
this.deselectItem(item);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else if(e.target.hasAttribute('data-option')) {
|
|
|
|
// If we are clicking on an option
|
|
|
|
const options = this.store.getOptionsFilteredByActive();
|
|
|
|
const id = e.target.getAttribute('data-id');
|
|
|
|
const option = options.find((option) => option.id === parseInt(id));
|
|
|
|
|
|
|
|
if(!option.selected && !option.disabled) {
|
|
|
|
this.addItem(option.value, option.label, option.id);
|
|
|
|
if(this.passedElement.type === 'select-one') {
|
|
|
|
this.toggleDropdown();
|
2016-05-07 13:36:50 +02:00
|
|
|
}
|
|
|
|
}
|
2016-04-12 21:16:36 +02:00
|
|
|
}
|
2016-04-12 15:31:07 +02:00
|
|
|
|
2016-05-11 15:51:32 +02:00
|
|
|
} else {
|
|
|
|
// Click is outside of our element so close dropdown and de-select items
|
|
|
|
const hasActiveDropdown = this.dropdown.classList.contains(this.options.classNames.activeState);
|
2016-06-01 19:45:35 +02:00
|
|
|
const hasSelectedItems = activeItems.some((item) => item.selected === true);
|
2016-04-29 19:06:46 +02:00
|
|
|
|
2016-05-11 15:51:32 +02:00
|
|
|
// De-select any selected items
|
|
|
|
if(hasSelectedItems) this.deselectAll();
|
|
|
|
|
|
|
|
// Remove focus state
|
|
|
|
this.containerOuter.classList.remove(this.options.classNames.focusState);
|
2016-05-02 22:53:21 +02:00
|
|
|
|
2016-05-11 15:51:32 +02:00
|
|
|
// Close all other dropdowns
|
|
|
|
if(hasActiveDropdown) this.toggleDropdown();
|
|
|
|
}
|
2016-04-09 12:29:56 +02:00
|
|
|
}
|
2016-04-14 15:54:47 +02:00
|
|
|
}
|
|
|
|
|
2016-04-29 19:06:46 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Paste event
|
|
|
|
* @param {Object} e Event
|
|
|
|
* @return
|
|
|
|
*/
|
2016-04-14 15:54:47 +02:00
|
|
|
onPaste(e) {
|
2016-06-01 19:56:08 +02:00
|
|
|
if(e.target !== this.input) return;
|
2016-04-21 15:42:57 +02:00
|
|
|
// Disable pasting into the input if option has been set
|
2016-04-14 15:54:47 +02:00
|
|
|
if(!this.options.allowPaste) {
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
2016-04-09 12:29:56 +02:00
|
|
|
}
|
|
|
|
|
2016-05-02 13:23:12 +02:00
|
|
|
/**
|
|
|
|
* Mouse over (hover) event
|
|
|
|
* @param {Object} e Event
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
onMouseOver(e) {
|
2016-05-07 14:30:07 +02:00
|
|
|
// If the dropdown is either the target or one of its children is the target
|
|
|
|
if((e.target === this.dropdown || findAncestor(e.target, this.options.classNames.listDropdown))) {
|
2016-05-02 16:29:05 +02:00
|
|
|
if(e.target.hasAttribute('data-option')) {
|
2016-05-02 22:39:33 +02:00
|
|
|
this.highlightOption(e.target);
|
2016-05-02 14:22:53 +02:00
|
|
|
}
|
2016-05-02 13:23:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-29 19:06:46 +02:00
|
|
|
/**
|
|
|
|
* Focus event
|
|
|
|
* @param {Object} e Event
|
|
|
|
* @return
|
|
|
|
*/
|
2016-04-21 15:42:57 +02:00
|
|
|
onFocus(e) {
|
2016-05-08 01:02:52 +02:00
|
|
|
const hasActiveDropdown = this.dropdown.classList.contains(this.options.classNames.activeState);
|
|
|
|
if(e.target === this.input && !hasActiveDropdown) {
|
2016-05-05 22:46:56 +02:00
|
|
|
this.containerOuter.classList.add(this.options.classNames.focusState);
|
2016-05-08 01:02:52 +02:00
|
|
|
if(this.passedElement.type === 'select-one' || this.passedElement.type === 'select-multiple'){
|
|
|
|
this.showDropdown();
|
|
|
|
}
|
2016-05-02 22:53:21 +02:00
|
|
|
}
|
2016-04-21 15:42:57 +02:00
|
|
|
}
|
|
|
|
|
2016-04-29 19:06:46 +02:00
|
|
|
/**
|
|
|
|
* Blur event
|
|
|
|
* @param {Object} e Event
|
|
|
|
* @return
|
|
|
|
*/
|
2016-04-21 15:42:57 +02:00
|
|
|
onBlur(e) {
|
2016-05-07 14:30:07 +02:00
|
|
|
const hasActiveDropdown = this.dropdown.classList.contains(this.options.classNames.activeState);
|
2016-05-08 01:02:52 +02:00
|
|
|
if(e.target === this.input && !hasActiveDropdown) {
|
2016-05-05 22:46:56 +02:00
|
|
|
this.containerOuter.classList.remove(this.options.classNames.focusState);
|
2016-05-08 01:02:52 +02:00
|
|
|
} else {
|
|
|
|
this.hideDropdown();
|
2016-05-02 22:53:21 +02:00
|
|
|
}
|
2016-04-21 15:42:57 +02:00
|
|
|
}
|
|
|
|
|
2016-04-04 22:44:32 +02:00
|
|
|
/**
|
|
|
|
* Set value of input to blank
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
clearInput() {
|
|
|
|
if (this.input.value) this.input.value = '';
|
2016-05-18 23:40:32 +02:00
|
|
|
if(this.passedElement.type !== 'select-one') {
|
|
|
|
this.input.style.width = getWidthOfInput(this.input);
|
|
|
|
}
|
2016-04-04 22:44:32 +02:00
|
|
|
}
|
2016-03-15 23:42:10 +01:00
|
|
|
|
2016-04-04 22:44:32 +02:00
|
|
|
/**
|
|
|
|
* Tests value against a regular expression
|
2016-05-02 22:39:33 +02:00
|
|
|
* @param {string} value Value to test
|
|
|
|
* @return {Boolean} Whether test passed/failed
|
2016-04-04 22:44:32 +02:00
|
|
|
*/
|
2016-03-24 15:42:03 +01:00
|
|
|
regexFilter(value) {
|
2016-05-10 10:02:59 +02:00
|
|
|
if(!value) return;
|
2016-04-12 23:42:56 +02:00
|
|
|
const expression = new RegExp(this.options.regexFilter, 'i');
|
2016-05-08 01:02:52 +02:00
|
|
|
return expression.test(value);
|
2016-03-24 15:42:03 +01:00
|
|
|
}
|
|
|
|
|
2016-05-02 22:39:33 +02:00
|
|
|
/**
|
|
|
|
* Scroll to an option element
|
|
|
|
* @param {HTMLElement} option Option to scroll to
|
|
|
|
* @param {Number} direction Whether option is above or below
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
scrollToOption(option, direction) {
|
|
|
|
if(!option) return;
|
2016-05-03 22:31:05 +02:00
|
|
|
|
2016-06-01 19:45:35 +02:00
|
|
|
const dropdownHeight = this.dropdown.offsetHeight;
|
|
|
|
const optionHeight = option.offsetHeight;
|
|
|
|
|
2016-05-02 22:39:33 +02:00
|
|
|
// Distance from bottom of element to top of parent
|
2016-06-01 19:45:35 +02:00
|
|
|
const optionPos = option.offsetTop + optionHeight;
|
|
|
|
|
2016-05-03 22:31:05 +02:00
|
|
|
// Scroll position of dropdown
|
|
|
|
const containerScrollPos = this.dropdown.scrollTop + dropdownHeight;
|
2016-06-01 19:45:35 +02:00
|
|
|
|
2016-05-03 22:31:05 +02:00
|
|
|
// Difference between the option and scroll position
|
2016-05-05 22:31:09 +02:00
|
|
|
let endPoint;
|
|
|
|
|
|
|
|
const animateScroll = (time, endPoint, direction) => {
|
|
|
|
let continueAnimation = false;
|
|
|
|
let easing, distance;
|
|
|
|
const strength = 4;
|
|
|
|
|
|
|
|
if(direction > 0) {
|
|
|
|
easing = (endPoint - this.dropdown.scrollTop)/strength;
|
|
|
|
distance = easing > 1 ? easing : 1;
|
|
|
|
|
|
|
|
this.dropdown.scrollTop = this.dropdown.scrollTop + distance;
|
|
|
|
if(this.dropdown.scrollTop < endPoint) {
|
|
|
|
continueAnimation = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
easing = (this.dropdown.scrollTop - endPoint)/strength;
|
|
|
|
distance = easing > 1 ? easing : 1;
|
|
|
|
|
|
|
|
this.dropdown.scrollTop = this.dropdown.scrollTop - distance;
|
|
|
|
if(this.dropdown.scrollTop > endPoint) {
|
|
|
|
continueAnimation = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(continueAnimation) {
|
|
|
|
requestAnimationFrame((time) => {
|
|
|
|
animateScroll(time, endPoint, direction);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2016-05-03 22:31:05 +02:00
|
|
|
|
|
|
|
// Scroll dropdown to top of option
|
2016-05-02 22:39:33 +02:00
|
|
|
if(direction > 0) {
|
2016-05-05 22:31:09 +02:00
|
|
|
endPoint = (this.dropdown.scrollTop + optionPos) - containerScrollPos;
|
|
|
|
requestAnimationFrame((time) => {
|
|
|
|
animateScroll(time, endPoint, 1);
|
|
|
|
});
|
2016-05-02 22:39:33 +02:00
|
|
|
} else {
|
2016-05-05 22:31:09 +02:00
|
|
|
endPoint = option.offsetTop;
|
|
|
|
requestAnimationFrame((time) => {
|
|
|
|
animateScroll(time, endPoint, -1);
|
|
|
|
});
|
2016-05-02 22:39:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-08 13:22:56 +02:00
|
|
|
/**
|
|
|
|
* Highlight option element
|
|
|
|
* @param {HTMLElement} el Element to highlight
|
|
|
|
* @return
|
|
|
|
*/
|
2016-05-02 22:39:33 +02:00
|
|
|
highlightOption(el) {
|
|
|
|
// Highlight first element in dropdown
|
|
|
|
const options = Array.from(this.dropdown.querySelectorAll('[data-option-selectable]'));
|
|
|
|
|
2016-05-03 22:31:05 +02:00
|
|
|
if(options.length) {
|
|
|
|
const highlightedOptions = Array.from(this.dropdown.querySelectorAll(`.${this.options.classNames.highlightedState}`));
|
|
|
|
|
|
|
|
// Remove any highlighted options
|
|
|
|
highlightedOptions.forEach((el) => {
|
|
|
|
el.classList.remove(this.options.classNames.highlightedState);
|
|
|
|
});
|
2016-05-05 22:31:09 +02:00
|
|
|
|
2016-05-03 22:31:05 +02:00
|
|
|
if(el){
|
|
|
|
// Highlight given option
|
|
|
|
el.classList.add(this.options.classNames.highlightedState);
|
|
|
|
this.highlightPosition = options.indexOf(el);
|
|
|
|
} else {
|
|
|
|
// Highlight option based on last known highlight location
|
|
|
|
let el;
|
2016-05-02 22:39:33 +02:00
|
|
|
|
2016-05-03 22:31:05 +02:00
|
|
|
if(options.length > this.highlightPosition) {
|
2016-05-05 22:31:09 +02:00
|
|
|
// If we have an option to highlight
|
2016-05-03 22:31:05 +02:00
|
|
|
el = options[this.highlightPosition];
|
|
|
|
} else {
|
2016-05-05 22:31:09 +02:00
|
|
|
// Otherwise highlight the option before
|
2016-05-03 22:31:05 +02:00
|
|
|
el = options[options.length - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!el) el = options[0];
|
|
|
|
el.classList.add(this.options.classNames.highlightedState);
|
|
|
|
}
|
2016-05-02 22:39:33 +02:00
|
|
|
}
|
2016-05-03 22:31:05 +02:00
|
|
|
|
2016-05-02 22:39:33 +02:00
|
|
|
}
|
|
|
|
|
2016-04-04 22:44:32 +02:00
|
|
|
/**
|
|
|
|
* Select item (a selected item can be deleted)
|
|
|
|
* @param {Element} item Element to select
|
|
|
|
* @return
|
|
|
|
*/
|
2016-04-04 00:07:10 +02:00
|
|
|
selectItem(item) {
|
2016-04-12 21:16:36 +02:00
|
|
|
if(!item) return;
|
2016-04-12 23:42:56 +02:00
|
|
|
const id = item.id;
|
2016-04-12 15:31:07 +02:00
|
|
|
this.store.dispatch(selectItem(id, true));
|
2016-03-24 00:00:32 +01:00
|
|
|
}
|
|
|
|
|
2016-04-04 22:44:32 +02:00
|
|
|
/**
|
|
|
|
* Deselect item
|
|
|
|
* @param {Element} item Element to de-select
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
deselectItem(item) {
|
2016-04-12 21:16:36 +02:00
|
|
|
if(!item) return;
|
2016-04-12 23:42:56 +02:00
|
|
|
const id = item.id;
|
2016-04-12 15:31:07 +02:00
|
|
|
this.store.dispatch(selectItem(id, false));
|
2016-04-04 00:07:10 +02:00
|
|
|
}
|
2016-03-24 00:00:32 +01:00
|
|
|
|
2016-04-04 22:44:32 +02:00
|
|
|
/**
|
2016-05-02 16:29:05 +02:00
|
|
|
* Select items within store
|
2016-04-04 22:44:32 +02:00
|
|
|
* @return
|
|
|
|
*/
|
2016-04-12 21:16:36 +02:00
|
|
|
selectAll() {
|
2016-05-04 10:02:22 +02:00
|
|
|
const items = this.store.getItems();
|
2016-04-12 21:16:36 +02:00
|
|
|
items.forEach((item) => {
|
2016-04-04 00:07:10 +02:00
|
|
|
this.selectItem(item);
|
2016-04-12 21:16:36 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-05-02 16:29:05 +02:00
|
|
|
/**
|
|
|
|
* Deselect items within store
|
|
|
|
* @return
|
|
|
|
*/
|
2016-04-12 21:16:36 +02:00
|
|
|
deselectAll() {
|
2016-05-04 10:02:22 +02:00
|
|
|
const items = this.store.getItems();
|
2016-04-12 21:16:36 +02:00
|
|
|
items.forEach((item) => {
|
|
|
|
this.deselectItem(item);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-04-04 22:44:32 +02:00
|
|
|
/**
|
|
|
|
* Add item to store with correct value
|
|
|
|
* @param {String} value Value to add to store
|
|
|
|
*/
|
2016-04-16 18:06:27 +02:00
|
|
|
addItem(value, label, optionId = -1, callback = this.options.callbackOnAddItem) {
|
2016-06-01 19:45:35 +02:00
|
|
|
const items = this.store.getItems();
|
|
|
|
let passedValue = value.trim();
|
|
|
|
let passedLabel = label || passedValue;
|
2016-04-17 13:02:28 +02:00
|
|
|
let passedOptionId = optionId || -1;
|
2016-03-24 15:42:03 +01:00
|
|
|
|
|
|
|
// If a prepended value has been passed, prepend it
|
|
|
|
if(this.options.prependValue) {
|
|
|
|
passedValue = this.options.prependValue + passedValue.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If an appended value has been passed, append it
|
|
|
|
if(this.options.appendValue) {
|
|
|
|
passedValue = passedValue + this.options.appendValue.toString();
|
|
|
|
}
|
|
|
|
|
2016-04-04 15:43:22 +02:00
|
|
|
// Generate unique id
|
2016-05-04 15:31:29 +02:00
|
|
|
const id = items ? items.length + 1 : 1;
|
2016-03-31 15:51:41 +02:00
|
|
|
|
2016-04-28 21:49:49 +02:00
|
|
|
this.store.dispatch(addItem(passedValue, passedLabel, id, passedOptionId));
|
2016-04-11 15:13:50 +02:00
|
|
|
|
2016-05-07 13:36:50 +02:00
|
|
|
if(this.passedElement.type === 'select-one') {
|
|
|
|
this.removeActiveItems(id);
|
|
|
|
}
|
|
|
|
|
2016-03-24 15:42:03 +01:00
|
|
|
// Run callback if it is a function
|
2016-04-12 15:10:07 +02:00
|
|
|
if(callback){
|
|
|
|
if(isType('Function', callback)) {
|
2016-05-08 01:02:52 +02:00
|
|
|
callback(id, passedValue, this.passedElement);
|
2016-03-24 00:00:32 +01:00
|
|
|
} else {
|
2016-03-24 15:42:03 +01:00
|
|
|
console.error('callbackOnAddItem: Callback is not a function');
|
2016-03-24 00:00:32 +01:00
|
|
|
}
|
|
|
|
}
|
2016-03-17 16:00:22 +01:00
|
|
|
}
|
2016-03-15 23:42:10 +01:00
|
|
|
|
2016-04-04 22:44:32 +02:00
|
|
|
/**
|
|
|
|
* Remove item from store
|
|
|
|
* @param
|
|
|
|
*/
|
2016-04-12 20:45:41 +02:00
|
|
|
removeItem(item, callback = this.options.callbackOnRemoveItem) {
|
2016-04-17 13:02:28 +02:00
|
|
|
if(!item || !isType('Object', item)) {
|
|
|
|
console.error('removeItem: No item object was passed to be removed');
|
2016-03-24 00:00:32 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-03-22 15:36:01 +01:00
|
|
|
|
2016-06-01 19:45:35 +02:00
|
|
|
const id = item.id;
|
|
|
|
const value = item.value;
|
2016-04-17 13:02:28 +02:00
|
|
|
const optionId = item.optionId;
|
2016-04-08 10:07:41 +02:00
|
|
|
|
2016-04-29 16:18:53 +02:00
|
|
|
this.store.dispatch(removeItem(id, optionId));
|
|
|
|
|
2016-04-12 20:45:41 +02:00
|
|
|
// Run callback
|
|
|
|
if(callback){
|
2016-05-04 15:31:29 +02:00
|
|
|
if(!isType('Function', callback)) console.error('callbackOnRemoveItem: Callback is not a function'); return;
|
2016-05-08 01:02:52 +02:00
|
|
|
callback(id, value, this.passedElement);
|
2016-04-08 10:07:41 +02:00
|
|
|
}
|
2016-03-22 15:36:01 +01:00
|
|
|
}
|
|
|
|
|
2016-04-29 19:06:46 +02:00
|
|
|
/**
|
|
|
|
* Remove an item from the store by its value
|
|
|
|
* @param {String} value Value to search for
|
|
|
|
* @return
|
|
|
|
*/
|
2016-04-17 13:02:28 +02:00
|
|
|
removeItemsByValue(value) {
|
2016-05-04 15:31:29 +02:00
|
|
|
if(!value || !isType('String', value)) console.error('removeItemsByValue: No value was passed to be removed'); return;
|
2016-04-17 13:02:28 +02:00
|
|
|
|
2016-05-04 10:02:22 +02:00
|
|
|
const items = this.store.getItemsFilteredByActive();
|
2016-04-17 13:02:28 +02:00
|
|
|
|
|
|
|
items.forEach((item) => {
|
|
|
|
if(item.value === value) {
|
|
|
|
this.removeItem(item);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-04-04 22:44:32 +02:00
|
|
|
/**
|
2016-04-29 19:06:46 +02:00
|
|
|
* Remove all items from store array
|
|
|
|
* Note: removed items are soft deleted
|
2016-04-12 15:54:07 +02:00
|
|
|
* @param {Boolean} selectedOnly Optionally remove only selected items
|
2016-04-04 22:44:32 +02:00
|
|
|
* @return
|
|
|
|
*/
|
2016-05-07 13:36:50 +02:00
|
|
|
removeActiveItems(excludedId) {
|
2016-05-04 10:02:22 +02:00
|
|
|
const items = this.store.getItemsFilteredByActive();
|
2016-03-21 19:53:26 +01:00
|
|
|
|
2016-04-12 15:54:07 +02:00
|
|
|
items.forEach((item) => {
|
2016-05-07 13:36:50 +02:00
|
|
|
if(item.active && excludedId !== item.id) {
|
|
|
|
this.removeItem(item);
|
|
|
|
}
|
2016-04-17 12:23:38 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-04-29 19:06:46 +02:00
|
|
|
/**
|
|
|
|
* Remove all selected items from store
|
|
|
|
* Note: removed items are soft deleted
|
|
|
|
* @return
|
|
|
|
*/
|
2016-05-07 13:36:50 +02:00
|
|
|
removeSelectedItems() {
|
2016-05-04 10:02:22 +02:00
|
|
|
const items = this.store.getItemsFilteredByActive();
|
2016-04-17 12:23:38 +02:00
|
|
|
|
|
|
|
items.forEach((item) => {
|
|
|
|
if(item.selected && item.active) {
|
|
|
|
this.removeItem(item);
|
2016-03-21 19:53:26 +01:00
|
|
|
}
|
2016-04-12 15:54:07 +02:00
|
|
|
});
|
2016-03-21 19:53:26 +01:00
|
|
|
}
|
2016-04-09 12:29:56 +02:00
|
|
|
|
2016-05-08 01:02:52 +02:00
|
|
|
/**
|
|
|
|
* Add option to dropdown
|
|
|
|
* @param {Object} option Option to add
|
|
|
|
* @param {Number} groupId ID of the options group
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
addOption(option, value, label, groupId = -1) {
|
2016-05-08 13:22:56 +02:00
|
|
|
if(!value) return
|
|
|
|
|
|
|
|
if(!label) { label = value; }
|
|
|
|
|
2016-05-08 01:02:52 +02:00
|
|
|
// Generate unique id
|
2016-06-01 19:45:35 +02:00
|
|
|
const options = this.store.getOptions();
|
|
|
|
const id = options.length + 1;
|
2016-05-08 01:02:52 +02:00
|
|
|
const isDisabled = option && (option.disabled || option.parentNode.disabled);
|
|
|
|
|
|
|
|
this.store.dispatch(addOption(value, label, id, groupId, isDisabled));
|
|
|
|
|
|
|
|
if(option && option.selected && !isDisabled) {
|
|
|
|
this.addItem(value, label, id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add group to dropdown
|
|
|
|
* @param {Object} group Group to add
|
|
|
|
* @param {Number} index Whether this is the first group to add
|
|
|
|
*/
|
|
|
|
addGroup(group, id, isFirst) {
|
|
|
|
const groupOptions = Array.from(group.getElementsByTagName('OPTION'));
|
2016-06-01 19:45:35 +02:00
|
|
|
const groupId = id;
|
2016-05-08 01:02:52 +02:00
|
|
|
|
|
|
|
if(groupOptions) {
|
|
|
|
this.store.dispatch(addGroup(group.label, groupId, true, group.disabled));
|
|
|
|
|
|
|
|
groupOptions.forEach((option, optionIndex) => {
|
|
|
|
// We want to pre-highlight the first option
|
|
|
|
const highlighted = isFirst && optionIndex === 0 ? true : false;
|
|
|
|
this.addOption(option, option.value, option.innerHTML, groupId);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.store.dispatch(addGroup(group.label, group.id, false, group.disabled));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-29 19:06:46 +02:00
|
|
|
/**
|
|
|
|
* Show dropdown to user by adding active state class
|
|
|
|
* @return
|
|
|
|
*/
|
2016-05-05 22:46:56 +02:00
|
|
|
showDropdown() {
|
|
|
|
this.containerOuter.classList.add(this.options.classNames.openState);
|
2016-04-22 10:02:42 +02:00
|
|
|
this.dropdown.classList.add(this.options.classNames.activeState);
|
2016-05-05 22:46:56 +02:00
|
|
|
|
2016-04-21 15:42:57 +02:00
|
|
|
const dimensions = this.dropdown.getBoundingClientRect();
|
2016-04-29 19:06:46 +02:00
|
|
|
const shouldFlip = dimensions.top + dimensions.height >= document.body.offsetHeight;
|
|
|
|
|
|
|
|
// Whether or not the dropdown should appear above or below input
|
|
|
|
if(shouldFlip) {
|
2016-05-07 13:48:27 +02:00
|
|
|
this.containerOuter.classList.add(this.options.classNames.flippedState);
|
2016-04-21 15:42:57 +02:00
|
|
|
} else {
|
2016-05-07 13:48:27 +02:00
|
|
|
this.containerOuter.classList.remove(this.options.classNames.flippedState);
|
2016-04-21 15:42:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-29 19:06:46 +02:00
|
|
|
/**
|
|
|
|
* Hide dropdown from user
|
|
|
|
* @return {[type]} [description]
|
|
|
|
*/
|
2016-04-21 15:42:57 +02:00
|
|
|
hideDropdown() {
|
2016-04-22 10:02:42 +02:00
|
|
|
// A dropdown flips if it does not have space below the input
|
2016-05-07 13:48:27 +02:00
|
|
|
const isFlipped = this.containerOuter.classList.contains(this.options.classNames.flippedState);
|
2016-04-22 10:02:42 +02:00
|
|
|
|
2016-05-05 22:46:56 +02:00
|
|
|
this.containerOuter.classList.remove(this.options.classNames.openState);
|
2016-04-22 10:02:42 +02:00
|
|
|
this.dropdown.classList.remove(this.options.classNames.activeState);
|
2016-04-21 15:42:57 +02:00
|
|
|
|
|
|
|
if(isFlipped) {
|
2016-05-07 13:48:27 +02:00
|
|
|
this.containerOuter.classList.remove(this.options.classNames.flippedState);
|
2016-04-09 12:29:56 +02:00
|
|
|
}
|
2016-04-21 15:42:57 +02:00
|
|
|
}
|
|
|
|
|
2016-04-29 19:06:46 +02:00
|
|
|
/**
|
|
|
|
* Determine whether to hide or show dropdown based on its current state
|
|
|
|
* @return
|
|
|
|
*/
|
2016-04-21 15:42:57 +02:00
|
|
|
toggleDropdown() {
|
2016-04-22 10:02:42 +02:00
|
|
|
const isActive = this.dropdown.classList.contains(this.options.classNames.activeState);
|
2016-04-09 12:29:56 +02:00
|
|
|
|
2016-05-08 13:22:56 +02:00
|
|
|
isActive ? this.hideDropdown() : this.showDropdown();
|
2016-04-09 12:29:56 +02:00
|
|
|
}
|
|
|
|
|
2016-05-08 01:02:52 +02:00
|
|
|
/**
|
|
|
|
* Disable
|
|
|
|
* @return {[type]} [description]
|
2016-04-29 19:06:46 +02:00
|
|
|
*/
|
2016-05-08 01:02:52 +02:00
|
|
|
disable() {
|
|
|
|
this.passedElement.disabled = true;
|
|
|
|
if(this.initialised) {
|
|
|
|
this.input.disabled = true;
|
|
|
|
this.containerOuter.classList.add(this.options.classNames.disabledState);
|
2016-04-14 15:43:36 +02:00
|
|
|
}
|
2016-04-09 12:29:56 +02:00
|
|
|
}
|
2016-04-13 15:20:08 +02:00
|
|
|
|
2016-05-08 13:22:56 +02:00
|
|
|
/**
|
|
|
|
* Populate options via ajax callback
|
|
|
|
* @param {Function} fn Passed
|
|
|
|
* @return {[type]} [description]
|
|
|
|
*/
|
2016-05-08 01:02:52 +02:00
|
|
|
ajax(fn) {
|
2016-05-08 13:22:56 +02:00
|
|
|
this.containerOuter.classList.add('is-loading');
|
2016-06-01 19:56:08 +02:00
|
|
|
this.input.placeholder = this.options.loadingText;
|
2016-05-08 13:22:56 +02:00
|
|
|
|
|
|
|
const callback = (results, value, label) => {
|
2016-05-08 01:02:52 +02:00
|
|
|
if(results && results.length) {
|
2016-05-08 13:22:56 +02:00
|
|
|
this.containerOuter.classList.remove('is-loading');
|
|
|
|
this.input.placeholder = "";
|
|
|
|
results.forEach((result, index) => {
|
2016-05-08 01:02:52 +02:00
|
|
|
// Add each result to option dropdown
|
2016-05-08 13:22:56 +02:00
|
|
|
if(index === 0) {
|
|
|
|
this.addItem(result[value], result[label], index);
|
|
|
|
}
|
|
|
|
this.addOption(null, result[value], result[label]);
|
2016-05-08 01:02:52 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2016-04-29 19:06:46 +02:00
|
|
|
|
2016-05-08 01:02:52 +02:00
|
|
|
fn(callback);
|
2016-04-16 18:06:27 +02:00
|
|
|
}
|
|
|
|
|
2016-05-08 01:02:52 +02:00
|
|
|
/**
|
|
|
|
* Get template from name
|
|
|
|
* @param {String} template Name of template to get
|
|
|
|
* @param {...} args Data to pass to template
|
|
|
|
* @return {HTMLElement} Template
|
|
|
|
*/
|
2016-05-04 15:31:29 +02:00
|
|
|
getTemplate(template, ...args) {
|
|
|
|
if(!template) return;
|
|
|
|
const templates = this.options.templates;
|
|
|
|
return templates[template](...args);
|
2016-04-16 18:06:27 +02:00
|
|
|
}
|
2016-04-22 20:45:50 +02:00
|
|
|
|
2016-04-29 19:06:46 +02:00
|
|
|
/**
|
2016-05-04 15:31:29 +02:00
|
|
|
* Create HTML element based on type and arguments
|
2016-05-08 01:02:52 +02:00
|
|
|
* @return
|
2016-04-29 19:06:46 +02:00
|
|
|
*/
|
2016-05-04 15:31:29 +02:00
|
|
|
createTemplates() {
|
|
|
|
const classNames = this.options.classNames;
|
|
|
|
const templates = {
|
|
|
|
containerOuter: () => {
|
2016-05-07 13:36:50 +02:00
|
|
|
return strToEl(`<div class="${ classNames.containerOuter }" data-type="${ this.passedElement.type }"></div>`);
|
2016-05-04 15:31:29 +02:00
|
|
|
},
|
|
|
|
containerInner: () => {
|
|
|
|
return strToEl(`<div class="${ classNames.containerInner }"></div>`);
|
|
|
|
},
|
2016-05-18 23:40:32 +02:00
|
|
|
itemList: () => {
|
2016-05-07 13:36:50 +02:00
|
|
|
return strToEl(`<div class="${ classNames.list } ${ this.passedElement.type === 'select-one' ? classNames.listSingle : classNames.listItems }"></div>`);
|
2016-05-04 15:31:29 +02:00
|
|
|
},
|
2016-05-18 23:40:32 +02:00
|
|
|
optionList: () => {
|
|
|
|
return strToEl(`<div class="${ classNames.list }"></div>`);
|
|
|
|
},
|
2016-05-04 15:31:29 +02:00
|
|
|
input: () => {
|
|
|
|
return strToEl(`<input type="text" class="${ classNames.input } ${ classNames.inputCloned }">`);
|
|
|
|
},
|
|
|
|
dropdown: () => {
|
|
|
|
return strToEl(`<div class="${ classNames.list } ${ classNames.listDropdown }"></div>`);
|
|
|
|
},
|
2016-05-16 15:46:04 +02:00
|
|
|
notice: (label, clickable) => {
|
2016-05-04 15:31:29 +02:00
|
|
|
return strToEl(`<div class="${ classNames.item } ${ classNames.itemOption }">${ label }</div>`);
|
|
|
|
},
|
2016-05-11 15:25:34 +02:00
|
|
|
selectOption: (data) => {
|
|
|
|
return strToEl(`<option value="${ data.value }" selected>${ data.label.trim() }</option>`);
|
|
|
|
},
|
2016-05-04 15:31:29 +02:00
|
|
|
option: (data) => {
|
|
|
|
return strToEl(`
|
|
|
|
<div class="${ classNames.item } ${ classNames.itemOption } ${ data.disabled ? classNames.itemDisabled : classNames.itemSelectable }" data-option ${ data.disabled ? 'data-option-disabled' : 'data-option-selectable' } data-id="${ data.id }" data-value="${ data.value }">
|
|
|
|
${ data.label }
|
|
|
|
</div>
|
|
|
|
`);
|
|
|
|
},
|
|
|
|
optgroup: (data) => {
|
|
|
|
return strToEl(`
|
|
|
|
<div class="${ classNames.group } ${ data.disabled ? classNames.itemDisabled : '' }" data-group data-id="${ data.id }" data-value="${ data.value }">
|
|
|
|
<div class="${ classNames.groupHeading }">${ data.value }</div>
|
|
|
|
</div>
|
|
|
|
`);
|
|
|
|
},
|
|
|
|
item: (data) => {
|
2016-05-11 15:51:32 +02:00
|
|
|
if(this.options.removeButton) {
|
|
|
|
return strToEl(`
|
2016-05-16 15:53:56 +02:00
|
|
|
<div class="${ classNames.item } ${ data.selected ? classNames.selectedState : ''} ${ !data.disabled ? classNames.itemSelectable : '' }" data-item data-id="${ data.id }" data-value="${ data.value }" data-deletable>
|
2016-05-11 15:51:32 +02:00
|
|
|
${ data.label }
|
|
|
|
<button class="${ classNames.button }" data-button>Remove item</button>
|
|
|
|
</div>
|
|
|
|
`);
|
|
|
|
} else {
|
|
|
|
return strToEl(`
|
|
|
|
<div class="${ classNames.item } ${ data.selected ? classNames.selectedState : classNames.itemSelectable }" data-item data-id="${ data.id }" data-value="${ data.value }">
|
|
|
|
${ data.label }
|
|
|
|
</div>
|
|
|
|
`);
|
|
|
|
}
|
2016-05-04 15:31:29 +02:00
|
|
|
},
|
|
|
|
};
|
2016-04-22 20:45:50 +02:00
|
|
|
|
2016-05-04 15:31:29 +02:00
|
|
|
this.options.templates = extend(this.options.templates, templates);
|
2016-04-22 20:45:50 +02:00
|
|
|
}
|
2016-03-21 19:53:26 +01:00
|
|
|
|
2016-04-08 23:33:13 +02:00
|
|
|
/**
|
|
|
|
* Create DOM structure around passed select element
|
|
|
|
* @return
|
|
|
|
*/
|
2016-04-29 18:11:20 +02:00
|
|
|
generateInput() {
|
2016-05-02 16:29:05 +02:00
|
|
|
const containerOuter = this.getTemplate('containerOuter');
|
|
|
|
const containerInner = this.getTemplate('containerInner');
|
2016-06-01 19:45:35 +02:00
|
|
|
const itemList = this.getTemplate('itemList');
|
|
|
|
const optionList = this.getTemplate('optionList');
|
|
|
|
const input = this.getTemplate('input');
|
|
|
|
const dropdown = this.getTemplate('dropdown');
|
2016-03-16 15:41:13 +01:00
|
|
|
|
2016-05-08 01:02:52 +02:00
|
|
|
this.containerOuter = containerOuter;
|
|
|
|
this.containerInner = containerInner;
|
2016-06-01 19:45:35 +02:00
|
|
|
this.input = input;
|
|
|
|
this.optionList = optionList;
|
|
|
|
this.itemList = itemList;
|
|
|
|
this.dropdown = dropdown;
|
2016-05-08 01:02:52 +02:00
|
|
|
|
2016-03-17 16:00:22 +01:00
|
|
|
// Hide passed input
|
2016-04-11 15:13:50 +02:00
|
|
|
this.passedElement.classList.add(this.options.classNames.input, this.options.classNames.hiddenState);
|
2016-04-10 22:23:42 +02:00
|
|
|
this.passedElement.tabIndex = '-1';
|
|
|
|
this.passedElement.setAttribute('style', 'display:none;');
|
|
|
|
this.passedElement.setAttribute('aria-hidden', 'true');
|
2016-05-08 01:02:52 +02:00
|
|
|
this.passedElement.removeAttribute('data-choice');
|
2016-03-16 21:24:11 +01:00
|
|
|
|
2016-03-18 00:10:16 +01:00
|
|
|
// Wrap input in container preserving DOM ordering
|
2016-04-10 22:23:42 +02:00
|
|
|
wrap(this.passedElement, containerInner);
|
2016-03-18 13:26:38 +01:00
|
|
|
|
2016-03-18 12:05:50 +01:00
|
|
|
// Wrapper inner container with outer container
|
2016-03-18 00:10:16 +01:00
|
|
|
wrap(containerInner, containerOuter);
|
2016-04-29 18:11:20 +02:00
|
|
|
|
2016-04-15 10:19:02 +02:00
|
|
|
// If placeholder has been enabled and we have a value
|
2016-05-08 13:22:56 +02:00
|
|
|
if (this.options.placeholder && (this.options.placeholderValue || this.passedElement.placeholder)) {
|
|
|
|
if(this.passedElement.type !== 'select-one') {
|
|
|
|
const placeholder = this.options.placeholderValue || this.passedElement.placeholder;
|
|
|
|
input.placeholder = placeholder;
|
|
|
|
input.style.width = getWidthOfInput(input);
|
|
|
|
}
|
2016-03-18 12:05:50 +01:00
|
|
|
}
|
2016-03-18 13:26:38 +01:00
|
|
|
|
2016-05-18 23:40:32 +02:00
|
|
|
if(!this.options.addItems) this.disable();
|
2016-03-24 15:42:03 +01:00
|
|
|
|
2016-04-04 23:52:49 +02:00
|
|
|
containerOuter.appendChild(containerInner);
|
2016-05-07 14:30:07 +02:00
|
|
|
containerOuter.appendChild(dropdown);
|
2016-05-18 23:40:32 +02:00
|
|
|
containerInner.appendChild(itemList);
|
|
|
|
dropdown.appendChild(optionList);
|
|
|
|
|
|
|
|
if(this.passedElement.type === 'select-multiple' || this.passedElement.type === 'text') {
|
|
|
|
containerInner.appendChild(input);
|
2016-06-01 19:56:08 +02:00
|
|
|
} else if(this.options.allowSearch) {
|
2016-05-18 23:40:32 +02:00
|
|
|
dropdown.insertBefore(input, dropdown.firstChild);
|
|
|
|
}
|
2016-04-29 18:11:20 +02:00
|
|
|
|
2016-05-07 13:36:50 +02:00
|
|
|
if(this.passedElement.type === 'select-multiple' || this.passedElement.type === 'select-one') {
|
2016-04-29 18:11:20 +02:00
|
|
|
this.highlightPosition = 0;
|
|
|
|
|
2016-05-07 14:30:07 +02:00
|
|
|
const passedGroups = Array.from(this.passedElement.getElementsByTagName('OPTGROUP'));
|
|
|
|
|
2016-05-03 22:31:05 +02:00
|
|
|
this.isSearching = false;
|
2016-04-29 18:11:20 +02:00
|
|
|
|
2016-05-08 13:22:56 +02:00
|
|
|
if(passedGroups && passedGroups.length) {
|
2016-04-29 18:11:20 +02:00
|
|
|
passedGroups.forEach((group, index) => {
|
2016-04-29 19:06:46 +02:00
|
|
|
const isFirst = index === 0 ? true : false;
|
|
|
|
this.addGroup(group, index, isFirst);
|
2016-04-29 18:11:20 +02:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
const passedOptions = Array.from(this.passedElement.options);
|
|
|
|
passedOptions.forEach((option) => {
|
2016-05-08 01:02:52 +02:00
|
|
|
this.addOption(option, option.value, option.innerHTML);
|
2016-04-29 18:11:20 +02:00
|
|
|
});
|
|
|
|
}
|
2016-05-07 13:36:50 +02:00
|
|
|
|
2016-04-29 18:11:20 +02:00
|
|
|
} else if(this.passedElement.type === 'text') {
|
|
|
|
// Add any preset values seperated by delimiter
|
2016-06-01 19:45:35 +02:00
|
|
|
this.presetItems.forEach((item) => {
|
|
|
|
if(isType('Object', item)) {
|
|
|
|
if(!item.value) return;
|
|
|
|
this.addItem(item.value, item.label, item.id);
|
|
|
|
} else if(isType('String', item)) {
|
|
|
|
this.addItem(item);
|
|
|
|
}
|
2016-04-29 18:11:20 +02:00
|
|
|
});
|
|
|
|
}
|
2016-04-14 15:54:47 +02:00
|
|
|
}
|
|
|
|
|
2016-05-04 15:31:29 +02:00
|
|
|
renderGroups(groups, options, fragment) {
|
2016-05-10 10:02:59 +02:00
|
|
|
const groupFragment = fragment || document.createDocumentFragment();
|
|
|
|
|
2016-05-04 15:31:29 +02:00
|
|
|
groups.forEach((group, i) => {
|
|
|
|
// Grab options that are children of this group
|
|
|
|
const groupOptions = options.filter((option) => {
|
2016-05-08 13:27:08 +02:00
|
|
|
if(this.passedElement.type === 'select-one') {
|
|
|
|
return option.groupId === group.id
|
|
|
|
} else {
|
|
|
|
return option.groupId === group.id && !option.selected;
|
|
|
|
}
|
2016-05-04 15:31:29 +02:00
|
|
|
});
|
2016-04-22 20:45:50 +02:00
|
|
|
|
2016-05-04 15:31:29 +02:00
|
|
|
if(groupOptions.length >= 1) {
|
|
|
|
const dropdownGroup = this.getTemplate('optgroup', group);
|
2016-04-25 15:59:58 +02:00
|
|
|
|
2016-05-10 10:02:59 +02:00
|
|
|
groupFragment.appendChild(dropdownGroup);
|
2016-04-29 16:18:53 +02:00
|
|
|
|
2016-05-10 10:02:59 +02:00
|
|
|
this.renderOptions(groupOptions, groupFragment);
|
2016-04-28 21:49:49 +02:00
|
|
|
}
|
2016-05-04 15:31:29 +02:00
|
|
|
});
|
2016-05-18 23:40:32 +02:00
|
|
|
|
|
|
|
return groupFragment;
|
2016-05-04 15:31:29 +02:00
|
|
|
}
|
2016-04-04 22:44:32 +02:00
|
|
|
|
2016-05-04 15:31:29 +02:00
|
|
|
renderOptions(options, fragment) {
|
2016-05-10 10:02:59 +02:00
|
|
|
// Create a fragment to store our list items (so we don't have to update the DOM for each item)
|
|
|
|
const optsFragment = fragment || document.createDocumentFragment();
|
|
|
|
|
2016-05-04 15:31:29 +02:00
|
|
|
options.forEach((option, i) => {
|
2016-05-07 13:36:50 +02:00
|
|
|
const dropdownItem = this.getTemplate('option', option);
|
2016-05-10 10:02:59 +02:00
|
|
|
|
2016-05-07 13:36:50 +02:00
|
|
|
if(this.passedElement.type === 'select-one') {
|
2016-05-10 10:02:59 +02:00
|
|
|
optsFragment.appendChild(dropdownItem);
|
2016-05-07 13:36:50 +02:00
|
|
|
} else if(!option.selected) {
|
2016-05-10 10:02:59 +02:00
|
|
|
optsFragment.appendChild(dropdownItem);
|
2016-05-07 13:36:50 +02:00
|
|
|
}
|
2016-05-04 15:31:29 +02:00
|
|
|
});
|
2016-05-18 23:40:32 +02:00
|
|
|
|
|
|
|
return optsFragment;
|
2016-05-04 15:31:29 +02:00
|
|
|
}
|
2016-04-13 15:40:41 +02:00
|
|
|
|
2016-05-04 15:31:29 +02:00
|
|
|
renderItems(items, fragment) {
|
2016-06-01 19:45:35 +02:00
|
|
|
// Create fragment to add elements to
|
2016-05-10 10:02:59 +02:00
|
|
|
const itemListFragment = fragment || document.createDocumentFragment();
|
2016-05-04 15:31:29 +02:00
|
|
|
// Simplify store data to just values
|
2016-06-01 19:45:35 +02:00
|
|
|
const itemsFiltered = this.store.getItemsReducedToValues(items);
|
2016-04-04 15:43:22 +02:00
|
|
|
|
2016-05-11 15:25:34 +02:00
|
|
|
if(this.passedElement.type === 'text') {
|
|
|
|
// Assign hidden input array of values
|
2016-05-12 00:17:22 +02:00
|
|
|
this.passedElement.setAttribute('value', itemsFiltered.join(this.options.delimiter));
|
2016-05-11 15:25:34 +02:00
|
|
|
} else {
|
|
|
|
const selectedOptionsFragment = document.createDocumentFragment();
|
|
|
|
|
|
|
|
// Add each list item to list
|
|
|
|
items.forEach((item) => {
|
2016-05-11 15:29:26 +02:00
|
|
|
// Create a standard select option
|
2016-05-11 15:25:34 +02:00
|
|
|
const option = this.getTemplate('selectOption', item);
|
|
|
|
|
2016-05-11 15:29:26 +02:00
|
|
|
// Append it to fragment
|
2016-05-11 15:25:34 +02:00
|
|
|
selectedOptionsFragment.appendChild(option);
|
|
|
|
});
|
|
|
|
|
2016-05-11 15:29:26 +02:00
|
|
|
// Update selected options
|
2016-05-11 15:25:34 +02:00
|
|
|
this.passedElement.innerHTML = "";
|
|
|
|
this.passedElement.appendChild(selectedOptionsFragment);
|
|
|
|
}
|
2016-04-04 15:43:22 +02:00
|
|
|
|
2016-05-04 15:31:29 +02:00
|
|
|
// Add each list item to list
|
|
|
|
items.forEach((item) => {
|
|
|
|
// Create new list element
|
|
|
|
const listItem = this.getTemplate('item', item);
|
2016-04-13 15:40:41 +02:00
|
|
|
|
2016-05-04 15:31:29 +02:00
|
|
|
// Append it to list
|
2016-05-10 10:02:59 +02:00
|
|
|
itemListFragment.appendChild(listItem);
|
2016-05-04 15:31:29 +02:00
|
|
|
});
|
2016-04-04 15:43:22 +02:00
|
|
|
|
2016-05-07 14:34:59 +02:00
|
|
|
// Clear list
|
2016-05-18 23:40:32 +02:00
|
|
|
this.itemList.innerHTML = '';
|
2016-05-07 14:34:59 +02:00
|
|
|
|
|
|
|
// Update list
|
2016-05-18 23:40:32 +02:00
|
|
|
this.itemList.appendChild(itemListFragment);
|
2016-05-02 16:29:05 +02:00
|
|
|
}
|
|
|
|
|
2016-05-02 13:23:12 +02:00
|
|
|
/**
|
2016-05-04 15:31:29 +02:00
|
|
|
* Render DOM with values
|
|
|
|
* @return
|
2016-05-02 13:23:12 +02:00
|
|
|
*/
|
2016-05-04 15:31:29 +02:00
|
|
|
render() {
|
|
|
|
this.currentState = this.store.getState();
|
|
|
|
|
|
|
|
if(this.currentState !== this.prevState) {
|
2016-05-11 15:25:34 +02:00
|
|
|
|
2016-05-11 15:29:26 +02:00
|
|
|
// Options
|
2016-05-07 13:36:50 +02:00
|
|
|
if((this.currentState.options !== this.prevState.options || this.currentState.groups !== this.prevState.groups)) {
|
|
|
|
if(this.passedElement.type === 'select-multiple' || this.passedElement.type === 'select-one') {
|
|
|
|
// Get active groups/options
|
2016-06-01 19:45:35 +02:00
|
|
|
const activeGroups = this.store.getGroupsFilteredByActive();
|
|
|
|
const activeOptions = this.store.getOptionsFilteredByActive();
|
|
|
|
|
2016-05-10 10:02:59 +02:00
|
|
|
const optListFragment = document.createDocumentFragment();
|
2016-05-07 13:36:50 +02:00
|
|
|
|
|
|
|
// Clear options
|
2016-05-18 23:40:32 +02:00
|
|
|
this.optionList.innerHTML = '';
|
2016-05-07 13:36:50 +02:00
|
|
|
|
|
|
|
// If we have grouped options
|
|
|
|
if(activeGroups.length >= 1 && this.isSearching !== true) {
|
2016-05-10 10:02:59 +02:00
|
|
|
this.renderGroups(activeGroups, activeOptions, optListFragment);
|
2016-05-07 13:36:50 +02:00
|
|
|
} else if(activeOptions.length >= 1) {
|
2016-05-10 10:02:59 +02:00
|
|
|
this.renderOptions(activeOptions, optListFragment);
|
2016-05-07 13:36:50 +02:00
|
|
|
}
|
|
|
|
|
2016-05-10 10:02:59 +02:00
|
|
|
if(optListFragment.children.length) {
|
|
|
|
// If we actually have anything to add to our dropdown
|
|
|
|
// append it and highlight the first option
|
2016-05-18 23:40:32 +02:00
|
|
|
this.optionList.appendChild(optListFragment);
|
2016-05-07 13:36:50 +02:00
|
|
|
this.highlightOption();
|
|
|
|
} else {
|
2016-05-10 10:02:59 +02:00
|
|
|
// Otherwise show a notice
|
|
|
|
const dropdownItem = this.isSearching ? this.getTemplate('notice', 'No results found') : this.getTemplate('notice', 'No options to select');
|
|
|
|
|
2016-05-18 23:40:32 +02:00
|
|
|
this.optionList.appendChild(dropdownItem);
|
2016-05-07 13:36:50 +02:00
|
|
|
}
|
2016-05-04 15:31:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-11 15:29:26 +02:00
|
|
|
// Items
|
2016-05-04 15:31:29 +02:00
|
|
|
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)
|
2016-05-10 10:02:59 +02:00
|
|
|
this.renderItems(activeItems);
|
2016-05-04 15:31:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.prevState = this.currentState;
|
|
|
|
}
|
2016-05-02 13:23:12 +02:00
|
|
|
}
|
|
|
|
|
2016-04-04 22:44:32 +02:00
|
|
|
/**
|
2016-04-29 18:11:20 +02:00
|
|
|
* Trigger event listeners
|
2016-04-29 19:06:46 +02:00
|
|
|
* @return
|
2016-04-04 22:44:32 +02:00
|
|
|
*/
|
2016-04-29 18:11:20 +02:00
|
|
|
addEventListeners() {
|
|
|
|
document.addEventListener('keyup', this.onKeyUp);
|
|
|
|
document.addEventListener('keydown', this.onKeyDown);
|
2016-05-08 13:22:56 +02:00
|
|
|
document.addEventListener('mousedown', this.onMouseDown);
|
2016-05-02 13:23:12 +02:00
|
|
|
document.addEventListener('mouseover', this.onMouseOver);
|
2016-04-29 18:11:20 +02:00
|
|
|
|
2016-05-16 15:46:04 +02:00
|
|
|
this.input.addEventListener('input', this.onInput);
|
2016-05-08 13:22:56 +02:00
|
|
|
this.input.addEventListener('paste', this.onPaste);
|
2016-04-29 18:11:20 +02:00
|
|
|
this.input.addEventListener('focus', this.onFocus);
|
|
|
|
this.input.addEventListener('blur', this.onBlur);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroy event listeners
|
2016-04-29 19:06:46 +02:00
|
|
|
* @return
|
2016-04-29 18:11:20 +02:00
|
|
|
*/
|
|
|
|
removeEventListeners() {
|
|
|
|
document.removeEventListener('keyup', this.onKeyUp);
|
|
|
|
document.removeEventListener('keydown', this.onKeyDown);
|
2016-05-08 13:22:56 +02:00
|
|
|
document.removeEventListener('mousedown', this.onMouseDown);
|
2016-05-02 13:23:12 +02:00
|
|
|
document.removeEventListener('mouseover', this.onMouseOver);
|
2016-05-08 13:22:56 +02:00
|
|
|
|
2016-05-16 15:46:04 +02:00
|
|
|
this.input.removeEventListener('input', this.onInput);
|
2016-05-08 13:22:56 +02:00
|
|
|
this.input.removeEventListener('paste', this.onPaste);
|
2016-04-29 18:11:20 +02:00
|
|
|
this.input.removeEventListener('focus', this.onFocus);
|
|
|
|
this.input.removeEventListener('blur', this.onBlur);
|
2016-03-18 12:05:50 +01:00
|
|
|
}
|
|
|
|
|
2016-04-04 22:44:32 +02:00
|
|
|
/**
|
|
|
|
* Initialise Choices
|
|
|
|
* @return
|
|
|
|
*/
|
2016-05-12 00:17:22 +02:00
|
|
|
init(callback) {
|
|
|
|
if(this.initialised !== true) {
|
2016-05-08 01:02:52 +02:00
|
|
|
|
2016-05-12 00:17:22 +02:00
|
|
|
this.initialised = true;
|
2016-05-02 16:29:05 +02:00
|
|
|
|
2016-05-12 00:17:22 +02:00
|
|
|
// Create required elements
|
|
|
|
this.createTemplates();
|
2016-04-17 13:02:28 +02:00
|
|
|
|
2016-05-12 00:17:22 +02:00
|
|
|
// Generate input markup
|
|
|
|
this.generateInput();
|
2016-04-17 13:02:28 +02:00
|
|
|
|
2016-05-12 00:17:22 +02:00
|
|
|
this.store.subscribe(this.render);
|
2016-04-17 13:02:28 +02:00
|
|
|
|
2016-05-12 00:17:22 +02:00
|
|
|
// Render any items
|
|
|
|
this.render();
|
2016-04-26 15:36:02 +02:00
|
|
|
|
2016-05-12 00:17:22 +02:00
|
|
|
// Trigger event listeners
|
|
|
|
this.addEventListeners();
|
|
|
|
|
|
|
|
// Run callback if it is a function
|
|
|
|
if(callback = this.options.callbackOnInit){
|
|
|
|
if(isType('Function', callback)) {
|
|
|
|
callback();
|
|
|
|
} else {
|
|
|
|
console.error('callbackOnInit: Callback is not a function');
|
|
|
|
}
|
2016-04-12 15:10:07 +02:00
|
|
|
}
|
|
|
|
}
|
2016-04-04 22:44:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroy Choices and nullify values
|
|
|
|
* @return
|
|
|
|
*/
|
2016-03-17 16:00:22 +01:00
|
|
|
destroy() {
|
2016-05-07 15:14:05 +02:00
|
|
|
this.passedElement.classList.remove(this.options.classNames.input, this.options.classNames.hiddenState);
|
|
|
|
this.passedElement.tabIndex = '';
|
|
|
|
this.passedElement.removeAttribute('style', 'display:none;');
|
|
|
|
this.passedElement.removeAttribute('aria-hidden');
|
|
|
|
|
|
|
|
this.containerOuter.outerHTML = this.passedElement.outerHTML;
|
|
|
|
|
2016-04-26 15:36:02 +02:00
|
|
|
this.passedElement = null;
|
2016-06-01 19:45:35 +02:00
|
|
|
this.userOptions = null;
|
|
|
|
this.options = null;
|
|
|
|
this.store = null;
|
2016-05-07 15:14:05 +02:00
|
|
|
|
|
|
|
this.removeEventListeners();
|
2016-03-17 16:00:22 +01:00
|
|
|
}
|
|
|
|
};
|
2016-03-15 23:42:10 +01:00
|
|
|
|
2016-04-25 19:00:30 +02:00
|
|
|
window.Choices = module.exports = Choices;
|