Choices/assets/scripts/src/choices.js

206 lines
5.1 KiB
JavaScript
Raw Normal View History

2016-03-16 21:24:11 +01:00
import { wrap, getSiblings } from './lib/utils.js';
2016-03-15 23:42:10 +01:00
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(function() {
return factory(root);
});
} else if (typeof exports === 'object') {
module.exports = factory;
} else {
root.Choices = factory(root);
}
})(this, function (root) {
'use strict';
class Choices {
constructor() {
const fakeEl = document.createElement("fakeelement");
2016-03-16 10:03:59 +01:00
const DEFAULT_OPTIONS = {
2016-03-15 23:42:10 +01:00
element: '[data-choice]',
disabled: false,
maxItems: 0,
debug: false,
placeholder: false,
2016-03-15 23:42:10 +01:00
callbackOnInit: function(){},
callbackOnRender: function(){},
callbackOnKeyUp: function(){},
callbackOnKeyDown: function(){},
callbackOnEntry: function(){},
callbackOnRemove: function(){}
};
2016-03-16 10:03:59 +01:00
// Merge options with user options
this.options = DEFAULT_OPTIONS;
this.initialised = false;
this.supports = 'querySelector' in document && 'addEventListener' in document && 'classList' in fakeEl;
2016-03-16 10:03:59 +01:00
// Retrieve elements
this.elements = document.querySelectorAll(this.options.element);
// Bind methods
2016-03-15 23:42:10 +01:00
this.onClick = this.onClick.bind(this);
this.onKeyDown = this.onKeyDown.bind(this);
this.onChange = this.onChange.bind(this);
this.onFocus = this.onFocus.bind(this);
this.onBlur = this.onChange.bind(this);
}
/* State */
isOpen() {
}
isDisabled() {
}
isEmpty() {
}
/* Event handling */
onKeyDown(e) {
2016-03-16 21:24:11 +01:00
// let input =
console.log('Key down')
2016-03-16 21:24:11 +01:00
if(e.keyCode === 13 && e.target.value) {
this.addItem(e.target, e.target.value);
e.target.value = '';
}
2016-03-15 23:42:10 +01:00
}
onFocus(e) {
console.log('Focus')
2016-03-15 23:42:10 +01:00
}
onClick(e) {
console.log('Click')
2016-03-15 23:42:10 +01:00
}
onChange(e) {
console.log('Change')
2016-03-15 23:42:10 +01:00
}
/* Event listeners */
addEventListeners(el) {
el.addEventListener('click', this.onClick);
el.addEventListener('keydown', this.onKeyDown);
el.addEventListener('change', this.onChange);
el.addEventListener('focus', this.onFocus);
el.addEventListener('blur', this.onBlur);
2016-03-15 23:42:10 +01:00
}
removeEventListeners(el) {
el.removeEventListener('click', this.onClick);
el.removeEventListener('keydown', this.onKeyDown);
el.removeEventListener('change', this.onChange);
el.removeEventListener('focus', this.onFocus);
el.removeEventListener('blur', this.onBlur);
2016-03-15 23:42:10 +01:00
}
/* Methods */
setPlaceholder() {
}
setValue() {
}
getValue() {
}
getPlaceholder() {
}
search() {
}
2016-03-16 21:24:11 +01:00
addItem(el, value) {
console.log('Add item');
let wrapper = el.parentNode;
let list = wrapper.querySelector('.choice__list');
let item = document.createElement('li');
item.classList.add('choice__item');
item.textContent = value;
2016-03-15 23:42:10 +01:00
2016-03-16 21:24:11 +01:00
wrapper.appendChild(item);
2016-03-15 23:42:10 +01:00
}
removeItem() {
}
removeAllItems() {
}
createItemList() {
}
init() {
if(!this.supports) console.error('Your browser doesn\'nt support shit');
this.initialised = true;
let els = this.elements;
for (let i = els.length - 1; i >= 0; i--) {
let el = els[i];
this.render(el);
}
}
render(el) {
2016-03-15 23:42:10 +01:00
console.log('Render');
let wrapper = document.createElement('div');
2016-03-16 21:24:11 +01:00
let input = document.createElement('input');
let list = document.createElement('ul');
wrapper.classList.add('choice', 'choice--active');
el.classList.add('choice__input', 'choice__input--original');
el.tabIndex = '-1';
el.setAttribute('style', 'display:none;');
wrap(el, wrapper);
2016-03-16 21:24:11 +01:00
list.classList.add('choice__list');
input.type = 'text';
input.classList.add('choice__input', 'choice__input--cloned');
2016-03-16 21:24:11 +01:00
wrapper.appendChild(list);
wrapper.appendChild(input);
2016-03-16 21:24:11 +01:00
this.addEventListeners(input);
2016-03-15 23:42:10 +01:00
}
destroy() {
this.options = null;
this.elements = null;
let els = this.elements;
for (let i = els.length - 1; i >= 0; i--) {
let el = els[i];
2016-03-15 23:42:10 +01:00
this.removeEventListeners(el);
}
2016-03-15 23:42:10 +01:00
}
};
var choices = new Choices();
choices.init();
console.log(choices);
2016-03-15 23:42:10 +01:00
});