2016-03-17 16:00:22 +01:00
|
|
|
'use strict';
|
|
|
|
|
2016-03-30 16:04:21 +02:00
|
|
|
import { createStore } from 'redux';
|
2016-03-31 15:51:41 +02:00
|
|
|
import choices from './reducers/index.js';
|
2016-04-04 00:07:10 +02:00
|
|
|
import { addItemToStore, removeItemFromStore, selectItemFromStore } from './actions/index';
|
|
|
|
import { hasClass, wrap, getSiblings, isType, strToEl } from './lib/utils.js';
|
2016-03-16 21:24:11 +01:00
|
|
|
|
2016-03-30 16:04:21 +02:00
|
|
|
|
2016-03-18 12:05:50 +01:00
|
|
|
/**
|
|
|
|
|
|
|
|
TODO:
|
2016-03-30 16:04:21 +02:00
|
|
|
- State handling
|
2016-03-21 20:27:52 +01:00
|
|
|
- Dynamically set input width to contents
|
2016-03-18 12:05:50 +01:00
|
|
|
- Handle select input
|
|
|
|
- Handle multiple select input ?
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2016-03-18 13:26:38 +01:00
|
|
|
export class Choices {
|
2016-03-17 16:00:22 +01:00
|
|
|
constructor(options) {
|
2016-04-04 00:07:10 +02:00
|
|
|
const fakeEl = document.createElement("fakeel");
|
|
|
|
const userOptions = options || {};
|
|
|
|
const store = createStore(choices);
|
|
|
|
const defaultOptions = {
|
2016-03-17 16:00:22 +01:00
|
|
|
element: document.querySelector('[data-choice]'),
|
|
|
|
disabled: false,
|
2016-03-24 15:42:03 +01:00
|
|
|
addItems: true,
|
2016-03-22 23:47:29 +01:00
|
|
|
removeItems: true,
|
2016-03-21 19:53:26 +01:00
|
|
|
editItems: false,
|
2016-03-18 12:05:50 +01:00
|
|
|
maxItems: false,
|
|
|
|
delimiter: ',',
|
|
|
|
allowDuplicates: true,
|
2016-03-24 15:42:03 +01:00
|
|
|
regexFilter: false,
|
2016-03-18 00:10:16 +01:00
|
|
|
debug: false,
|
2016-03-17 16:00:22 +01:00
|
|
|
placeholder: false,
|
2016-03-24 15:42:03 +01:00
|
|
|
prependValue: false,
|
|
|
|
appendValue: false,
|
2016-04-04 00:07:10 +02:00
|
|
|
selectAll: true,
|
2016-03-18 13:26:38 +01:00
|
|
|
callbackOnInit: function() {},
|
|
|
|
callbackOnRender: function() {},
|
2016-03-24 00:00:32 +01:00
|
|
|
callbackOnRemoveItem: function() {},
|
|
|
|
callbackOnAddItem: function() {}
|
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-04 00:07:10 +02:00
|
|
|
this.options = this.extend(defaultOptions, userOptions || {});
|
|
|
|
this.store = store;
|
2016-03-30 16:04:21 +02:00
|
|
|
|
2016-03-17 16:00:22 +01:00
|
|
|
this.initialised = false;
|
2016-04-04 00:07:10 +02:00
|
|
|
this.supports = 'querySelector' in document && 'addEventListener' in document && 'classList' in fakeEl;
|
2016-03-17 16:00:22 +01:00
|
|
|
|
2016-04-04 00:07:10 +02:00
|
|
|
// Retrieve triggering element (i.e. element with 'data-choice' trigger)
|
2016-03-17 16:00:22 +01:00
|
|
|
this.element = this.options.element;
|
2016-03-31 15:51:41 +02:00
|
|
|
|
2016-03-17 16:00:22 +01:00
|
|
|
// If input already has values, parse the array, otherwise create a blank array
|
2016-04-04 00:07:10 +02:00
|
|
|
// Hmm, this should really map this.store
|
2016-03-18 12:13:35 +01:00
|
|
|
this.valueArray = this.element.value !== '' ? this.cleanInputValue(this.element.value) : [];
|
2016-03-31 15:51:41 +02:00
|
|
|
|
2016-03-18 13:26:38 +01:00
|
|
|
// How many values in array
|
|
|
|
this.valueCount = this.valueArray.length;
|
2016-03-17 16:00:22 +01:00
|
|
|
|
|
|
|
// Bind methods
|
|
|
|
this.onKeyDown = this.onKeyDown.bind(this);
|
2016-04-04 00:07:10 +02:00
|
|
|
this.onClick = this.onClick.bind(this);
|
2016-03-18 13:26:38 +01:00
|
|
|
|
|
|
|
this.init();
|
2016-03-17 16:00:22 +01:00
|
|
|
}
|
2016-03-16 10:03:59 +01:00
|
|
|
|
2016-03-18 12:13:35 +01:00
|
|
|
cleanInputValue(value) {
|
2016-03-18 13:26:38 +01:00
|
|
|
// Remove spaces and split with delimiter
|
2016-03-18 12:13:35 +01:00
|
|
|
return value.replace(/\s/g, '').split(this.options.delimiter);
|
|
|
|
}
|
|
|
|
|
2016-03-17 16:00:22 +01:00
|
|
|
/**
|
|
|
|
* Merges unspecified amount of objects into new object
|
|
|
|
* @private
|
|
|
|
* @return {Object} Merged object of arguments
|
|
|
|
*/
|
|
|
|
extend() {
|
|
|
|
let extended = {};
|
|
|
|
let length = arguments.length;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Merge one object into another
|
|
|
|
* @param {Object} obj Object to merge into extended object
|
|
|
|
*/
|
|
|
|
let merge = function(obj) {
|
2016-03-18 13:26:38 +01:00
|
|
|
for (let prop in obj) {
|
2016-03-17 16:00:22 +01:00
|
|
|
extended[prop] = obj[prop];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Loop through each passed argument
|
2016-03-18 13:26:38 +01:00
|
|
|
for (let i = 0; i < length; i++) {
|
2016-04-04 00:07:10 +02:00
|
|
|
// store argument at position i
|
2016-03-17 16:00:22 +01:00
|
|
|
let obj = arguments[i];
|
|
|
|
|
|
|
|
// If we are in fact dealing with an object, merge it. Otherwise throw error
|
2016-03-18 13:26:38 +01:00
|
|
|
if (isType('Object', obj)) {
|
2016-03-17 16:00:22 +01:00
|
|
|
merge(obj);
|
|
|
|
} else {
|
|
|
|
console.error('Custom options must be an object');
|
|
|
|
}
|
2016-03-15 23:42:10 +01:00
|
|
|
}
|
|
|
|
|
2016-03-17 16:00:22 +01:00
|
|
|
return extended;
|
|
|
|
};
|
2016-03-15 23:42:10 +01:00
|
|
|
|
2016-03-17 16:00:22 +01:00
|
|
|
/* State */
|
2016-03-15 23:42:10 +01:00
|
|
|
|
2016-03-17 16:00:22 +01:00
|
|
|
isOpen() {
|
2016-03-18 13:26:38 +01:00
|
|
|
|
2016-03-17 16:00:22 +01:00
|
|
|
}
|
2016-03-15 23:42:10 +01:00
|
|
|
|
2016-03-17 16:00:22 +01:00
|
|
|
isDisabled() {
|
2016-03-18 13:26:38 +01:00
|
|
|
|
2016-03-17 16:00:22 +01:00
|
|
|
}
|
2016-03-15 23:42:10 +01:00
|
|
|
|
2016-03-17 16:00:22 +01:00
|
|
|
isEmpty() {
|
2016-03-18 13:26:38 +01:00
|
|
|
return (this.valueCount.length === 0) ? true : false;
|
2016-03-17 16:00:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
clearInput() {
|
2016-03-18 13:26:38 +01:00
|
|
|
if (this.input.value) this.input.value = '';
|
2016-03-17 16:00:22 +01:00
|
|
|
}
|
2016-03-17 00:15:03 +01:00
|
|
|
|
2016-03-17 16:00:22 +01:00
|
|
|
/* Event handling */
|
|
|
|
|
2016-03-18 00:10:16 +01:00
|
|
|
onKeyUp(e) {
|
|
|
|
}
|
|
|
|
|
2016-03-17 16:00:22 +01:00
|
|
|
onKeyDown(e) {
|
2016-04-04 00:07:10 +02:00
|
|
|
const ctrlDownKey = e.ctrlKey || e.metaKey;
|
|
|
|
const deleteKey = 8 || 46;
|
|
|
|
const enterKey = 13;
|
|
|
|
const aKey = 65;
|
|
|
|
|
|
|
|
// If we are typing in the input
|
|
|
|
if(e.target === this.input) {
|
|
|
|
// If CTRL + A or CMD + A have been pressed and there are items to select
|
|
|
|
if (ctrlDownKey && e.keyCode === aKey && this.list && this.list.children) {
|
|
|
|
let handleSelectAll = () => {
|
|
|
|
if(this.options.removeItems && !this.input.value && this.options.selectAll) {
|
|
|
|
this.selectAll(this.list.children);
|
|
|
|
}
|
|
|
|
};
|
2016-03-18 00:10:16 +01:00
|
|
|
|
2016-04-04 00:07:10 +02:00
|
|
|
handleSelectAll();
|
|
|
|
}
|
2016-03-17 16:00:22 +01:00
|
|
|
|
2016-04-04 00:07:10 +02:00
|
|
|
// If enter key is pressed and the input has a value
|
|
|
|
if (e.keyCode === enterKey && e.target.value) {
|
|
|
|
let value = this.input.value;
|
2016-03-17 16:00:22 +01:00
|
|
|
|
2016-04-04 00:07:10 +02:00
|
|
|
let handleEnter = () => {
|
|
|
|
let canUpdate = true;
|
2016-03-18 00:10:16 +01:00
|
|
|
|
2016-04-04 00:07:10 +02:00
|
|
|
// If there is a max entry limit and we have reached that limit
|
|
|
|
// don't update
|
|
|
|
if (this.options.maxItems && this.options.maxItems <= this.list.children.length) {
|
2016-03-18 00:10:16 +01:00
|
|
|
canUpdate = false;
|
|
|
|
}
|
2016-03-24 15:42:03 +01:00
|
|
|
|
2016-04-04 00:07:10 +02:00
|
|
|
// If no duplicates are allowed, and the value already exists
|
|
|
|
// in the array, don't update
|
|
|
|
if (this.options.allowDuplicates === false && this.element.value) {
|
|
|
|
if (this.valueArray.indexOf(value) > -1) {
|
|
|
|
canUpdate = false;
|
2016-03-24 15:42:03 +01:00
|
|
|
}
|
2016-04-04 00:07:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// All is good, update
|
|
|
|
if (canUpdate) {
|
|
|
|
if(this.element.type === 'text') {
|
|
|
|
let canAddItem = true;
|
|
|
|
|
|
|
|
// 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.addItem(this.list, value);
|
|
|
|
this.updateInputValue(value);
|
|
|
|
this.clearInput(this.element);
|
|
|
|
}
|
2016-03-24 15:42:03 +01:00
|
|
|
}
|
2016-03-21 20:27:52 +01:00
|
|
|
|
|
|
|
}
|
2016-04-04 00:07:10 +02:00
|
|
|
};
|
2016-03-18 00:10:16 +01:00
|
|
|
|
2016-04-04 00:07:10 +02:00
|
|
|
handleEnter();
|
|
|
|
}
|
2016-03-15 23:42:10 +01:00
|
|
|
}
|
|
|
|
|
2016-03-22 23:47:29 +01:00
|
|
|
// If backspace or delete key is pressed and the input has no value
|
2016-04-04 00:07:10 +02:00
|
|
|
if (e.keyCode === deleteKey && !e.target.value) {
|
2016-03-18 13:26:38 +01:00
|
|
|
|
2016-03-17 16:00:22 +01:00
|
|
|
let handleBackspaceKey = () => {
|
2016-03-22 23:47:29 +01:00
|
|
|
if(this.options.removeItems) {
|
|
|
|
let currentListItems = this.list.querySelectorAll('.choices__item');
|
|
|
|
let selectedItems = this.list.querySelectorAll('.is-selected');
|
|
|
|
let lastItem = currentListItems[currentListItems.length - 1];
|
|
|
|
|
2016-04-04 00:07:10 +02:00
|
|
|
if(lastItem && !this.options.editItems) {
|
|
|
|
this.selectItem(lastItem);
|
2016-03-22 23:47:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// If editing the last item is allowed and there is a last item and
|
|
|
|
// there are not other selected items (minus the last item), we can edit
|
|
|
|
// the item value. Otherwise if we can remove items, remove all items
|
2016-04-04 00:07:10 +02:00
|
|
|
if(this.options.editItems && lastItem && selectedItems.length === 1) {
|
2016-03-22 23:47:29 +01:00
|
|
|
this.input.value = lastItem.innerHTML;
|
|
|
|
this.removeItem(lastItem);
|
|
|
|
} else {
|
|
|
|
this.removeAll(currentListItems);
|
|
|
|
}
|
2016-03-21 19:53:26 +01:00
|
|
|
}
|
2016-03-17 16:00:22 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
handleBackspaceKey();
|
2016-03-17 00:15:03 +01:00
|
|
|
|
2016-03-17 16:00:22 +01:00
|
|
|
e.preventDefault();
|
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-03-17 16:00:22 +01:00
|
|
|
onClick(e) {
|
2016-04-04 00:07:10 +02:00
|
|
|
if(e.target.tagName === 'LI') {
|
|
|
|
let item = e.target;
|
2016-03-17 00:15:03 +01:00
|
|
|
|
2016-04-04 00:07:10 +02:00
|
|
|
let handleClick = (item) => {
|
|
|
|
let passedId = item.getAttribute('data-choice-id');
|
|
|
|
let items = this.list.children;
|
2016-03-15 23:42:10 +01:00
|
|
|
|
2016-04-04 00:07:10 +02:00
|
|
|
// We only want to select one item with a click
|
|
|
|
// so we unselect any items that aren't the target
|
|
|
|
for (var i = 0; i < items.length; i++) {
|
|
|
|
let singleItem = items[i];
|
|
|
|
let id = singleItem.getAttribute('data-choice-id');;
|
2016-03-15 23:42:10 +01:00
|
|
|
|
2016-04-04 00:07:10 +02:00
|
|
|
if(id === passedId && !singleItem.classList.contains('is-selected')) {
|
|
|
|
this.selectItem(singleItem);
|
|
|
|
} else {
|
|
|
|
this.unselectItem(singleItem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleClick(item);
|
|
|
|
}
|
2016-03-17 16:00:22 +01:00
|
|
|
}
|
2016-03-15 23:42:10 +01:00
|
|
|
|
2016-03-17 16:00:22 +01:00
|
|
|
/* Methods */
|
2016-03-21 23:45:49 +01:00
|
|
|
setValue() {}
|
2016-03-15 23:42:10 +01:00
|
|
|
|
2016-03-21 23:45:49 +01:00
|
|
|
getValue() {}
|
2016-03-15 23:42:10 +01:00
|
|
|
|
2016-03-21 23:45:49 +01:00
|
|
|
getValues() {}
|
2016-03-15 23:42:10 +01:00
|
|
|
|
2016-03-24 15:42:03 +01:00
|
|
|
regexFilter(value) {
|
|
|
|
let expression = new RegExp(this.options.regexFilter, 'i');
|
|
|
|
let passesTest = expression.test(value);
|
|
|
|
|
|
|
|
return passesTest;
|
|
|
|
}
|
|
|
|
|
2016-04-04 00:07:10 +02:00
|
|
|
selectItem(item) {
|
|
|
|
let id = item.getAttribute('data-choice-id');
|
|
|
|
item.classList.add('is-selected');
|
|
|
|
this.store.dispatch(selectItemFromStore(id, true));
|
|
|
|
console.log(this.store.getState());
|
2016-03-24 00:00:32 +01:00
|
|
|
}
|
|
|
|
|
2016-04-04 00:07:10 +02:00
|
|
|
unselectItem(item) {
|
|
|
|
let id = item.getAttribute('data-choice-id');
|
|
|
|
item.classList.remove('is-selected');
|
|
|
|
this.store.dispatch(selectItemFromStore(id, false));
|
|
|
|
console.log(this.store.getState());
|
|
|
|
}
|
2016-03-24 00:00:32 +01:00
|
|
|
|
2016-04-04 00:07:10 +02:00
|
|
|
selectAll(items) {
|
2016-03-24 00:00:32 +01:00
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
let item = items[i];
|
2016-04-04 00:07:10 +02:00
|
|
|
this.selectItem(item);
|
2016-03-24 00:00:32 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-03-17 16:00:22 +01:00
|
|
|
updateInputValue(value) {
|
2016-03-18 13:26:38 +01:00
|
|
|
if (this.options.debug) console.debug('Update input value');
|
2016-03-15 23:42:10 +01:00
|
|
|
|
2016-03-17 16:00:22 +01:00
|
|
|
// Push new value to array
|
2016-03-18 13:26:38 +01:00
|
|
|
this.valueArray.push(value);
|
2016-03-18 12:05:50 +01:00
|
|
|
|
2016-03-17 16:00:22 +01:00
|
|
|
// Caste array to string and set it as the hidden inputs value
|
2016-03-18 12:13:35 +01:00
|
|
|
this.element.value = this.valueArray.join(this.options.delimiter);
|
2016-03-17 16:00:22 +01:00
|
|
|
}
|
2016-03-15 23:42:10 +01:00
|
|
|
|
2016-03-18 00:10:16 +01:00
|
|
|
removeInputValue(value) {
|
2016-03-18 13:26:38 +01:00
|
|
|
if (this.options.debug) console.debug('Remove input value');
|
|
|
|
|
2016-03-18 00:10:16 +01:00
|
|
|
let index = this.valueArray.indexOf(value);
|
|
|
|
this.valueArray.splice(index, 1);
|
|
|
|
|
2016-03-18 12:13:35 +01:00
|
|
|
this.element.value = this.valueArray.join(this.options.delimiter);
|
2016-03-18 00:10:16 +01:00
|
|
|
}
|
|
|
|
|
2016-03-21 20:27:52 +01:00
|
|
|
addItem(parent, value) {
|
2016-03-18 13:26:38 +01:00
|
|
|
if (this.options.debug) console.debug('Add item');
|
|
|
|
|
2016-03-24 15:42:03 +01:00
|
|
|
let passedValue = value;
|
|
|
|
|
|
|
|
// 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-03-31 15:51:41 +02:00
|
|
|
let id = this.store.getState().length + 1;
|
|
|
|
|
2016-04-04 00:07:10 +02:00
|
|
|
// Create new list element
|
|
|
|
let item = strToEl(`<li class="choices__item" data-choice-id=${id}>${passedValue}</li>`);
|
2016-03-15 23:42:10 +01:00
|
|
|
|
2016-03-17 16:00:22 +01:00
|
|
|
// Append it to list
|
2016-03-21 20:27:52 +01:00
|
|
|
parent.appendChild(item);
|
2016-03-24 00:00:32 +01:00
|
|
|
|
2016-03-24 15:42:03 +01:00
|
|
|
// Run callback if it is a function
|
2016-03-24 00:00:32 +01:00
|
|
|
if(this.options.callbackOnAddItem){
|
|
|
|
if(isType('Function', this.options.callbackOnAddItem)) {
|
|
|
|
this.options.callbackOnAddItem(item, value);
|
|
|
|
} 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-31 15:51:41 +02:00
|
|
|
|
|
|
|
this.store.dispatch(addItemToStore(passedValue, item, id));
|
|
|
|
console.log(this.store.getState());
|
2016-03-17 16:00:22 +01:00
|
|
|
}
|
2016-03-15 23:42:10 +01:00
|
|
|
|
2016-03-24 00:00:32 +01:00
|
|
|
removeItem(item) {
|
|
|
|
if(!item) {
|
|
|
|
console.error('removeItem: No item was passed to be removed');
|
|
|
|
return;
|
|
|
|
}
|
2016-03-22 15:36:01 +01:00
|
|
|
|
2016-04-04 00:07:10 +02:00
|
|
|
let id = item.getAttribute('data-choice-id');
|
2016-03-24 00:00:32 +01:00
|
|
|
let value = item.innerHTML;
|
|
|
|
item.parentNode.removeChild(item);
|
|
|
|
|
|
|
|
// Run callback
|
|
|
|
if(this.options.callbackOnRemoveItem){
|
|
|
|
if(isType('Function', this.options.callbackOnRemoveItem)) {
|
|
|
|
this.options.callbackOnRemoveItem(value);
|
|
|
|
} else {
|
2016-03-24 15:42:03 +01:00
|
|
|
console.error('callbackOnRemoveItem: Callback is not a function');
|
2016-03-22 15:36:01 +01:00
|
|
|
}
|
2016-03-24 00:00:32 +01:00
|
|
|
}
|
2016-03-31 15:51:41 +02:00
|
|
|
|
|
|
|
this.store.dispatch(removeItemFromStore(id));
|
|
|
|
console.log(this.store.getState());
|
2016-03-22 15:36:01 +01:00
|
|
|
}
|
|
|
|
|
2016-03-21 19:53:26 +01:00
|
|
|
removeAll(items) {
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
let item = items[i];
|
|
|
|
|
|
|
|
if (item.classList.contains('is-selected')) {
|
|
|
|
this.removeItem(item);
|
|
|
|
this.removeInputValue(item.textContent);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-03-17 16:00:22 +01:00
|
|
|
init() {
|
2016-03-24 15:42:03 +01:00
|
|
|
if (!this.supports) console.error('init: Your browser doesn\'nt support shit');
|
2016-03-17 16:00:22 +01:00
|
|
|
this.initialised = true;
|
|
|
|
this.render(this.element);
|
|
|
|
}
|
2016-03-16 21:24:11 +01:00
|
|
|
|
2016-03-18 12:05:50 +01:00
|
|
|
renderTextInput() {
|
2016-04-04 00:07:10 +02:00
|
|
|
/*
|
|
|
|
Template:
|
|
|
|
|
|
|
|
<div class="choices choices--active">
|
|
|
|
<div class="choices__inner">
|
|
|
|
<input id="1" type="text" data-choice="" class="choices__input choices__input--hidden" tabindex="-1" style="display:none;" aria-hidden="true">
|
|
|
|
<ul class="choices__list choices__list--items"></ul>
|
|
|
|
<input type="text" class="choices__input choices__input--cloned">
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
*/
|
|
|
|
|
|
|
|
let containerOuter = strToEl('<div class="choices choices--active"></div>');
|
|
|
|
let containerInner = strToEl('<div class="choices__inner"></div>');
|
2016-03-16 15:41:13 +01:00
|
|
|
|
2016-03-17 16:00:22 +01:00
|
|
|
// Hide passed input
|
2016-03-18 00:10:16 +01:00
|
|
|
this.element.classList.add('choices__input', 'choices__input--hidden');
|
2016-03-17 16:00:22 +01:00
|
|
|
this.element.tabIndex = '-1';
|
|
|
|
this.element.setAttribute('style', 'display:none;');
|
|
|
|
this.element.setAttribute('aria-hidden', 'true');
|
2016-03-16 21:24:11 +01:00
|
|
|
|
2016-03-18 00:10:16 +01:00
|
|
|
// Wrap input in container preserving DOM ordering
|
|
|
|
wrap(this.element, 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-04 00:07:10 +02:00
|
|
|
let list = strToEl('<ul class="choices__list choices__list--items"></ul>');
|
|
|
|
let input = strToEl('<input type="text" class="choices__input choices__input--cloned">');
|
2016-03-18 12:05:50 +01:00
|
|
|
|
2016-03-18 13:26:38 +01:00
|
|
|
if (input.placeholder) {
|
|
|
|
input.placeholder = this.element.placeholder;
|
2016-03-18 12:05:50 +01:00
|
|
|
}
|
2016-03-18 13:26:38 +01:00
|
|
|
|
2016-03-24 15:42:03 +01:00
|
|
|
if(!this.options.addItems) {
|
|
|
|
input.disabled = true;
|
|
|
|
}
|
|
|
|
|
2016-03-18 00:10:16 +01:00
|
|
|
containerInner.appendChild(list);
|
|
|
|
containerInner.appendChild(input);
|
|
|
|
containerOuter.appendChild(containerInner);
|
2016-03-17 16:00:22 +01:00
|
|
|
|
2016-03-18 00:10:16 +01:00
|
|
|
this.containerOuter = containerOuter;
|
|
|
|
this.containerInner = containerInner;
|
2016-03-17 16:00:22 +01:00
|
|
|
this.input = input;
|
|
|
|
this.list = list;
|
|
|
|
|
2016-03-18 13:26:38 +01:00
|
|
|
if (this.element.value !== '') {
|
2016-03-18 12:05:50 +01:00
|
|
|
// Add any preset values
|
|
|
|
this.valueArray.forEach((value) => {
|
2016-03-21 20:27:52 +01:00
|
|
|
this.addItem(this.list, value);
|
2016-03-17 16:00:22 +01:00
|
|
|
});
|
2016-03-15 23:42:10 +01:00
|
|
|
}
|
|
|
|
|
2016-03-18 12:05:50 +01:00
|
|
|
// Trigger event listeners
|
2016-04-04 00:07:10 +02:00
|
|
|
document.addEventListener('keydown', this.onKeyDown);
|
|
|
|
this.list.addEventListener('click', this.onClick);
|
2016-03-17 16:00:22 +01:00
|
|
|
}
|
2016-03-16 15:41:13 +01:00
|
|
|
|
2016-03-21 23:45:49 +01:00
|
|
|
|
2016-03-18 12:05:50 +01:00
|
|
|
render() {
|
2016-03-18 13:26:38 +01:00
|
|
|
if (this.options.debug) console.debug('Render');
|
2016-03-18 12:05:50 +01:00
|
|
|
|
2016-03-18 13:26:38 +01:00
|
|
|
switch (this.element.type) {
|
2016-03-18 12:05:50 +01:00
|
|
|
case "text":
|
|
|
|
this.renderTextInput();
|
|
|
|
break;
|
|
|
|
case "select-one":
|
2016-04-04 00:07:10 +02:00
|
|
|
// this.renderSelectInput();
|
2016-03-18 12:05:50 +01:00
|
|
|
break;
|
|
|
|
case "select-multiple":
|
2016-04-04 00:07:10 +02:00
|
|
|
// this.renderMultipleSelectInput();
|
2016-03-18 12:05:50 +01:00
|
|
|
break;
|
|
|
|
default:
|
2016-04-04 00:07:10 +02:00
|
|
|
this.renderTextInput();
|
2016-03-18 12:05:50 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-17 16:00:22 +01:00
|
|
|
destroy() {
|
|
|
|
this.options = null;
|
|
|
|
this.element = null;
|
2016-03-18 00:10:16 +01:00
|
|
|
this.initialised = null;
|
2016-03-17 16:00:22 +01:00
|
|
|
this.removeEventListeners(this.input);
|
|
|
|
}
|
|
|
|
};
|
2016-03-15 23:42:10 +01:00
|
|
|
|
2016-03-18 13:26:38 +01:00
|
|
|
(function(){
|
2016-03-18 12:05:50 +01:00
|
|
|
let input1 = document.getElementById(1);
|
|
|
|
let input2 = document.getElementById(2);
|
|
|
|
let input3 = document.getElementById(3);
|
2016-03-21 23:45:49 +01:00
|
|
|
let input4 = document.getElementById(4);
|
2016-03-24 15:42:03 +01:00
|
|
|
let input5 = document.getElementById(5);
|
2016-03-17 16:00:22 +01:00
|
|
|
|
2016-03-18 12:05:50 +01:00
|
|
|
let choices1 = new Choices({
|
|
|
|
element : input1,
|
2016-04-04 00:07:10 +02:00
|
|
|
// delimiter: ' ',
|
|
|
|
// maxItems: 5,
|
2016-03-31 15:51:41 +02:00
|
|
|
// callbackOnRemoveItem: function(value) {
|
|
|
|
// console.log(value);
|
|
|
|
// },
|
|
|
|
// callbackOnAddItem: function(item, value) {
|
|
|
|
// console.log(item, value);
|
|
|
|
// }
|
2016-03-18 12:05:50 +01:00
|
|
|
});
|
2016-03-15 23:42:10 +01:00
|
|
|
|
2016-04-04 00:07:10 +02:00
|
|
|
// let choices2 = new Choices({
|
|
|
|
// element : input2,
|
|
|
|
// allowDuplicates: false,
|
|
|
|
// editItems: true,
|
|
|
|
// });
|
|
|
|
|
|
|
|
// let choices3 = new Choices({
|
|
|
|
// element : input3,
|
|
|
|
// allowDuplicates: false,
|
|
|
|
// editItems: true,
|
|
|
|
// regexFilter: /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
|
|
|
|
// });
|
|
|
|
|
|
|
|
// let choices4 = new Choices({
|
|
|
|
// element : input4,
|
|
|
|
// addItems: false
|
|
|
|
// });
|
|
|
|
|
|
|
|
// let choices5 = new Choices({
|
|
|
|
// element: input5,
|
|
|
|
// prependValue: 'item-',
|
|
|
|
// appendValue: `-${Date.now()}`
|
|
|
|
// });
|
2016-03-18 13:26:38 +01:00
|
|
|
})();
|