From 6228463c71f3329b1f2c59e07fe10927be107633 Mon Sep 17 00:00:00 2001 From: Josh Johnson Date: Fri, 18 Mar 2016 12:26:38 +0000 Subject: [PATCH] Build --- assets/scripts/dist/bundle.js | 2 +- assets/scripts/src/choices.js | 111 +++++++++++++++--------------- assets/styles/css/choices.css | 4 +- assets/styles/css/choices.min.css | 2 +- assets/styles/scss/choices.scss | 2 + 5 files changed, 62 insertions(+), 59 deletions(-) diff --git a/assets/scripts/dist/bundle.js b/assets/scripts/dist/bundle.js index d0c3f82..398fb57 100644 --- a/assets/scripts/dist/bundle.js +++ b/assets/scripts/dist/bundle.js @@ -1 +1 @@ -!function(e){function t(i){if(n[i])return n[i].exports;var o=n[i]={exports:{},id:i,loaded:!1};return e[i].call(o.exports,o,o.exports,t),o.loaded=!0,o.exports}var n={};return t.m=e,t.c=n,t.p="/assets/scripts/dist/",t(0)}([function(e,t,n){e.exports=n(1)},function(e,t,n){"use strict";function i(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var o=function(){function e(e,t){for(var n=0;ni;i++){var o=arguments[i];(0,s.isType)("Object",o)?n(o):console.error("Custom options must be an object")}return e}},{key:"isOpen",value:function(){}},{key:"isDisabled",value:function(){}},{key:"isEmpty",value:function(){}},{key:"clearInput",value:function(){this.input.value&&(this.input.value="")}},{key:"onKeyUp",value:function(e){}},{key:"onKeyDown",value:function(e){var t=this,n=e.ctrlKey||e.metaKey;if(n&&65===e.keyCode)for(var i=0;i-1&&(n=!1),n&&(t.addItem(e),t.updateInputValue(e),t.clearInput(t.element))};n()}(),(8===e.keyCode||46===e.keyCode)&&!e.target.value){var s=function(){var e=t.list.querySelectorAll(".choices__item"),n=e[e.length-1];n.classList.add("is-selected");for(var i=0;i=0?t:0},t.getElementOffset=function(e,t){var n=t;return n>1&&(n=1),n>0&&(n=0),Math.max(e.offsetHeight*n)},t.getScrollPosition=function(e){return"bottom"===e?Math.max((window.scrollY||window.pageYOffset)+(window.innerHeight||document.documentElement.clientHeight)):window.scrollY||window.pageYOffset},t.isInView=function(e,t,n){return this.getScrollPosition(t)>this.getElemDistance(e)+this.getElementOffset(e,n)},t.stripHTML=function(e){var t=document.createElement("DIV");return t.innerHTML=e,t.textContent||t.innerText||""},t.addAnimation=function(e,t){var i=n(),o=function s(){e.classList.remove(t),e.removeEventListener(i,s,!1)};e.classList.add(t),e.addEventListener(i,o,!1)},t.getRandomNumber=function(e,t){return Math.floor(Math.random()*(t-e)+e)}}]); \ No newline at end of file +!function(e){function t(i){if(n[i])return n[i].exports;var o=n[i]={exports:{},id:i,loaded:!1};return e[i].call(o.exports,o,o.exports,t),o.loaded=!0,o.exports}var n={};return t.m=e,t.c=n,t.p="/assets/scripts/dist/",t(0)}([function(e,t,n){e.exports=n(1)},function(e,t,n){"use strict";function i(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(t,"__esModule",{value:!0}),t.Choices=void 0;var o=function(){function e(e,t){for(var n=0;ni;i++){var o=arguments[i];(0,s.isType)("Object",o)?n(o):console.error("Custom options must be an object")}return e}},{key:"isOpen",value:function(){}},{key:"isDisabled",value:function(){}},{key:"isEmpty",value:function(){return 0===this.valueCount.length}},{key:"clearInput",value:function(){this.input.value&&(this.input.value="")}},{key:"onKeyUp",value:function(e){}},{key:"onKeyDown",value:function(e){var t=this,n=e.ctrlKey||e.metaKey;if(n&&65===e.keyCode)for(var i=0;i-1&&(n=!1),n&&(t.addItem(e),t.updateInputValue(e),t.clearInput(t.element))};n()}(),(8===e.keyCode||46===e.keyCode)&&!e.target.value){var s=function(){var e=t.list.querySelectorAll(".choices__item"),n=e[e.length-1];n.classList.add("is-selected");for(var i=0;i=0?t:0},t.getElementOffset=function(e,t){var n=t;return n>1&&(n=1),n>0&&(n=0),Math.max(e.offsetHeight*n)},t.getScrollPosition=function(e){return"bottom"===e?Math.max((window.scrollY||window.pageYOffset)+(window.innerHeight||document.documentElement.clientHeight)):window.scrollY||window.pageYOffset},t.isInView=function(e,t,n){return this.getScrollPosition(t)>this.getElemDistance(e)+this.getElementOffset(e,n)},t.stripHTML=function(e){var t=document.createElement("DIV");return t.innerHTML=e,t.textContent||t.innerText||""},t.addAnimation=function(e,t){var i=n(),o=function s(){e.classList.remove(t),e.removeEventListener(i,s,!1)};e.classList.add(t),e.addEventListener(i,o,!1)},t.getRandomNumber=function(e,t){return Math.floor(Math.random()*(t-e)+e)}}]); \ No newline at end of file diff --git a/assets/scripts/src/choices.js b/assets/scripts/src/choices.js index be796d3..fc63fc9 100644 --- a/assets/scripts/src/choices.js +++ b/assets/scripts/src/choices.js @@ -10,9 +10,9 @@ import { hasClass, wrap, getSiblings, isType } from './lib/utils.js'; */ -class Choices { +export class Choices { constructor(options) { - const fakeEl = document.createElement("fakeelement"); + const FAKE_EL = document.createElement("FAKE_ELement"); const USER_OPTIONS = options || {}; const DEFAULT_OPTIONS = { element: document.querySelector('[data-choice]'), @@ -23,23 +23,25 @@ class Choices { allowDuplicates: true, debug: false, placeholder: false, - callbackOnInit: function(){}, - callbackOnRender: function(){}, - callbackOnKeyUp: function(){}, - callbackOnKeyDown: function(){}, - callbackOnEntry: function(){}, - callbackOnRemove: function(){} + callbackOnInit: function() {}, + callbackOnRender: function() {}, + callbackOnKeyUp: function() {}, + callbackOnKeyDown: function() {}, + callbackOnEntry: function() {}, + callbackOnRemove: function() {} }; // Merge options with user options this.options = this.extend(DEFAULT_OPTIONS, USER_OPTIONS || {}); this.initialised = false; - this.supports = 'querySelector' in document && 'addEventListener' in document && 'classList' in fakeEl; + this.supports = 'querySelector' in document && 'addEventListener' in document && 'classList' in FAKE_EL; // Retrieve elements this.element = this.options.element; // If input already has values, parse the array, otherwise create a blank array this.valueArray = this.element.value !== '' ? this.cleanInputValue(this.element.value) : []; + // How many values in array + this.valueCount = this.valueArray.length; // Bind methods this.onClick = this.onClick.bind(this); @@ -47,9 +49,12 @@ class Choices { this.onChange = this.onChange.bind(this); this.onFocus = this.onFocus.bind(this); this.onBlur = this.onChange.bind(this); + + this.init(); } cleanInputValue(value) { + // Remove spaces and split with delimiter return value.replace(/\s/g, '').split(this.options.delimiter); } @@ -67,18 +72,18 @@ class Choices { * @param {Object} obj Object to merge into extended object */ let merge = function(obj) { - for(let prop in obj) { + for (let prop in obj) { extended[prop] = obj[prop]; } }; // Loop through each passed argument - for(let i = 0; i < length; i++) { + for (let i = 0; i < length; i++) { // Store argument at position i let obj = arguments[i]; // If we are in fact dealing with an object, merge it. Otherwise throw error - if(isType('Object', obj)) { + if (isType('Object', obj)) { merge(obj); } else { console.error('Custom options must be an object'); @@ -91,19 +96,19 @@ class Choices { /* State */ isOpen() { - + } isDisabled() { - + } isEmpty() { - + return (this.valueCount.length === 0) ? true : false; } clearInput() { - if(this.input.value) this.input.value = ''; + if (this.input.value) this.input.value = ''; } /* Event handling */ @@ -116,7 +121,7 @@ class Choices { let ctrlDown = e.ctrlKey || e.metaKey; // Handle select all - if(ctrlDown && e.keyCode === 65) { + if (ctrlDown && e.keyCode === 65) { for (let i = 0; i < this.list.children.length; i++) { let listItem = this.list.children[i]; @@ -125,7 +130,7 @@ class Choices { } // Handle enter key - if(e.keyCode === 13 && e.target.value) { + if (e.keyCode === 13 && e.target.value) { let value = this.input.value; let handleEnterKey = () => { @@ -133,41 +138,41 @@ class Choices { // 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) { + if (this.options.maxItems && this.options.maxItems <= this.list.children.length) { canUpdate = false; } // 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) { + if (this.options.allowDuplicates === false && this.element.value) { + if (this.valueArray.indexOf(value) > -1) { canUpdate = false; } } // All is good, update - if(canUpdate) { + if (canUpdate) { this.addItem(value); this.updateInputValue(value); this.clearInput(this.element); } }; - handleEnterKey(); + handleEnterKey(); } - if((e.keyCode === 8 || e.keyCode === 46) && !e.target.value) { - + if ((e.keyCode === 8 || e.keyCode === 46) && !e.target.value) { + let handleBackspaceKey = () => { let currentListItems = this.list.querySelectorAll('.choices__item'); let lastItem = currentListItems[currentListItems.length - 1]; lastItem.classList.add('is-selected'); - + for (let i = 0; i < currentListItems.length; i++) { let listItem = currentListItems[i]; - if(listItem.classList.contains('is-selected')) { + if (listItem.classList.contains('is-selected')) { this.removeItem(listItem); this.removeInputValue(listItem.textContent); } @@ -235,10 +240,10 @@ class Choices { } updateInputValue(value) { - if(this.options.debug) console.debug('Update input value'); + if (this.options.debug) console.debug('Update input value'); // Push new value to array - this.valueArray.push(value); + this.valueArray.push(value); // Caste array to string and set it as the hidden inputs value this.element.value = this.valueArray.join(this.options.delimiter); @@ -247,8 +252,8 @@ class Choices { } removeInputValue(value) { - if(this.options.debug) console.debug('Remove input value'); - + if (this.options.debug) console.debug('Remove input value'); + let index = this.valueArray.indexOf(value); this.valueArray.splice(index, 1); @@ -256,8 +261,8 @@ class Choices { } addItem(value) { - if(this.options.debug) console.debug('Add item'); - + if (this.options.debug) console.debug('Add item'); + // // Create new list element let item = document.createElement('li'); item.classList.add('choices__item'); @@ -268,21 +273,21 @@ class Choices { } removeItem(item) { - if(item) item.parentNode.removeChild(item); + if (item) item.parentNode.removeChild(item); } - + init() { - if(!this.supports) console.error('Your browser doesn\'nt support shit'); + if (!this.supports) console.error('Your browser doesn\'nt support shit'); this.initialised = true; this.render(this.element); } renderTextInput() { let containerOuter = document.createElement('div'); - containerOuter.className = 'choices choices--active'; + containerOuter.className = 'choices choices--active'; let containerInner = document.createElement('div'); - containerInner.className = 'choices__inner'; + containerInner.className = 'choices__inner'; // Hide passed input this.element.classList.add('choices__input', 'choices__input--hidden'); @@ -292,21 +297,21 @@ class Choices { // Wrap input in container preserving DOM ordering wrap(this.element, containerInner); - + // Wrapper inner container with outer container wrap(containerInner, containerOuter); let list = document.createElement('ul'); - list.className = 'choices__list'; + list.className = 'choices__list'; let input = document.createElement('input'); - input.type = 'text'; - input.className = 'choices__input choices__input--cloned'; + input.type = 'text'; + input.className = 'choices__input choices__input--cloned'; - if(input.placeholder) { - input.placeholder = this.element.placeholder; + if (input.placeholder) { + input.placeholder = this.element.placeholder; } - + containerInner.appendChild(list); containerInner.appendChild(input); containerOuter.appendChild(containerInner); @@ -316,7 +321,7 @@ class Choices { this.input = input; this.list = list; - if(this.element.value !== '') { + if (this.element.value !== '') { // Add any preset values this.valueArray.forEach((value) => { this.addItem(value); @@ -332,9 +337,9 @@ class Choices { } render() { - if(this.options.debug) console.debug('Render'); + if (this.options.debug) console.debug('Render'); - switch(this.element.type) { + switch (this.element.type) { case "text": this.renderTextInput(); break; @@ -359,8 +364,7 @@ class Choices { } }; -window.addEventListener('load', function() { - +(function(){ let input1 = document.getElementById(1); let input2 = document.getElementById(2); let input3 = document.getElementById(3); @@ -379,9 +383,4 @@ window.addEventListener('load', function() { let choices3 = new Choices({ element : input3 }); - - choices1.init(); - choices2.init(); - choices3.init(); - -}); \ No newline at end of file +})(); \ No newline at end of file diff --git a/assets/styles/css/choices.css b/assets/styles/css/choices.css index 017a321..b575dba 100644 --- a/assets/styles/css/choices.css +++ b/assets/styles/css/choices.css @@ -62,6 +62,8 @@ label { background-color: #00BCD4; text-shadow: 0px 1px 0px #008fa1; border: 1px solid #00a5bb; - color: #FFFFFF; } + box-shadow: 0px 1px 1px rgba(0, 0, 0, 0.2); + color: #FFFFFF; + cursor: pointer; } .choices__item.is-selected { background-color: #00a5bb; } diff --git a/assets/styles/css/choices.min.css b/assets/styles/css/choices.min.css index 99c19fe..f475a39 100644 --- a/assets/styles/css/choices.min.css +++ b/assets/styles/css/choices.min.css @@ -1 +1 @@ -*,:after,:before{box-sizing:border-box}body,html{margin:0;height:100%;widows:100%}html{font-size:62.5%}body{background-color:#fafafa;font-family:'Helvetica Neue','Helvetica','Arial',sans-serif;font-size:1.6rem;color:#222;padding:2.4rem}label{display:block;margin-bottom:.8rem}.choices{margin-bottom:2.4rem;position:relative}.choices__inner{background-color:#fff;padding:.75rem .75rem .375rem;border:1px solid #ddd;border-radius:.25rem;font-size:1.4rem}.choices__inner:focus{outline:1px solid #00bcd4;outline-offset:-1px}.choices__list{margin:0;padding-left:0;list-style-type:none;display:inline}.choices__input{font-size:1.4rem;padding:0;margin-bottom:.5rem;display:inline-block;vertical-align:baseline;border:0;border-radius:0;max-width:100%}.choices__input:focus{outline:0}.choices__item{display:inline-block;border-radius:.4rem;padding:.4rem .8rem;font-size:1.4rem;margin-right:.375rem;margin-bottom:.375rem;background-color:#00bcd4;text-shadow:0 1px 0 #008fa1;border:1px solid #00a5bb;color:#fff}.choices__item.is-selected{background-color:#00a5bb} \ No newline at end of file +*,:after,:before{box-sizing:border-box}body,html{margin:0;height:100%;widows:100%}html{font-size:62.5%}body{background-color:#fafafa;font-family:'Helvetica Neue','Helvetica','Arial',sans-serif;font-size:1.6rem;color:#222;padding:2.4rem}label{display:block;margin-bottom:.8rem}.choices{margin-bottom:2.4rem;position:relative}.choices__inner{background-color:#fff;padding:.75rem .75rem .375rem;border:1px solid #ddd;border-radius:.25rem;font-size:1.4rem}.choices__inner:focus{outline:1px solid #00bcd4;outline-offset:-1px}.choices__list{margin:0;padding-left:0;list-style-type:none;display:inline}.choices__input{font-size:1.4rem;padding:0;margin-bottom:.5rem;display:inline-block;vertical-align:baseline;border:0;border-radius:0;max-width:100%}.choices__input:focus{outline:0}.choices__item{display:inline-block;border-radius:.4rem;padding:.4rem .8rem;font-size:1.4rem;margin-right:.375rem;margin-bottom:.375rem;background-color:#00bcd4;text-shadow:0 1px 0 #008fa1;border:1px solid #00a5bb;box-shadow:0 1px 1px rgba(0,0,0,.2);color:#fff;cursor:pointer}.choices__item.is-selected{background-color:#00a5bb} \ No newline at end of file diff --git a/assets/styles/scss/choices.scss b/assets/styles/scss/choices.scss index cbed131..46549a8 100644 --- a/assets/styles/scss/choices.scss +++ b/assets/styles/scss/choices.scss @@ -71,6 +71,8 @@ label { background-color: #00BCD4; text-shadow: 0px 1px 0px darken(#00BCD4, 10%); border: 1px solid darken(#00BCD4, 5%); + box-shadow: 0px 1px 1px rgba(#000000, 0.2); color: #FFFFFF; + cursor: pointer; &.is-selected { background-color: darken(#00BCD4, 5%); } } \ No newline at end of file