diff --git a/assets/scripts/dist/bundle.js b/assets/scripts/dist/bundle.js index fd0cf92..588e542 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 s=n[i]={exports:{},id:i,loaded:!1};return e[i].call(s.exports,s,s.exports,t),s.loaded=!0,s.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 s=function(){function e(e,t){for(var n=0;ni;i++){var s=arguments[i];(0,o.isType)("Object",s)?n(s):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,i=8,s=13,o=65;if(n&&e.keyCode===o&&this.list&&this.list.children){var a=function(){if(t.options.removeItems)for(var e=0;e-1&&(n=!1),n&&"text"===t.element.type&&(t.addItem(t.list,e),t.updateInputValue(e),t.clearInput(t.element),t.unselectAll(t.list.children))};n()}(),e.keyCode===i&&!e.target.value){var r=function(){if(t.options.removeItems){var e=t.list.querySelectorAll(".choices__item"),n=t.list.querySelectorAll(".is-selected"),i=e[e.length-1];i&&i.classList.add("is-selected"),t.options.editItems&&i&&n.length<=1?(t.input.value=i.innerHTML,t.removeItem(i)):t.removeAll(e)}};r(),e.preventDefault()}}},{key:"onFocus",value:function(e){}},{key:"onClick",value:function(e){}},{key:"onChange",value:function(e){}},{key:"addEventListeners",value:function(e){e.addEventListener("click",this.onClick),e.addEventListener("keyup",this.onKeyUp),e.addEventListener("keydown",this.onKeyDown),e.addEventListener("change",this.onChange),e.addEventListener("focus",this.onFocus),e.addEventListener("blur",this.onBlur)}},{key:"removeEventListeners",value:function(e){e.removeEventListener("click",this.onClick),e.removeEventListener("keyup",this.onKeyUp),e.removeEventListener("keydown",this.onKeyDown),e.removeEventListener("change",this.onChange),e.removeEventListener("focus",this.onFocus),e.removeEventListener("blur",this.onBlur)}},{key:"setValue",value:function(){}},{key:"getValue",value:function(){}},{key:"getValues",value:function(){}},{key:"getPlaceholder",value:function(){}},{key:"updateInputValue",value:function(e){this.options.debug&&console.debug("Update input value"),this.valueArray.push(e),this.element.value=this.valueArray.join(this.options.delimiter)}},{key:"removeInputValue",value:function(e){this.options.debug&&console.debug("Remove input value");var t=this.valueArray.indexOf(e);this.valueArray.splice(t,1),this.element.value=this.valueArray.join(this.options.delimiter)}},{key:"addItem",value:function(e,t){this.options.debug&&console.debug("Add item");var n=document.createElement("li");n.classList.add("choices__item"),n.textContent=t,e.appendChild(n)}},{key:"unselectAll",value:function(e){for(var t=0;t=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(),s=function o(){e.classList.remove(t),e.removeEventListener(i,o,!1)};e.classList.add(t),e.addEventListener(i,s,!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 s=n[i]={exports:{},id:i,loaded:!1};return e[i].call(s.exports,s,s.exports,t),s.loaded=!0,s.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 s=function(){function e(e,t){for(var n=0;ni;i++){var s=arguments[i];(0,o.isType)("Object",s)?n(s):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,i=8,s=13,o=65;if(n&&e.keyCode===o&&this.list&&this.list.children){var a=function(){t.options.removeItems&&t.selectAll(t.list.children)};a()}if(e.keyCode===s&&e.target.value&&!function(){var e=t.input.value,n=function(){var n=!0;t.options.maxItems&&t.options.maxItems<=t.list.children.length&&(n=!1),t.options.allowDuplicates===!1&&t.element.value&&t.valueArray.indexOf(e)>-1&&(n=!1),n&&"text"===t.element.type&&(t.addItem(t.list,e),t.updateInputValue(e),t.clearInput(t.element),t.unselectAll(t.list.children))};n()}(),e.keyCode===i&&!e.target.value){var l=function(){if(t.options.removeItems){var e=t.list.querySelectorAll(".choices__item"),n=t.list.querySelectorAll(".is-selected"),i=e[e.length-1];i&&i.classList.add("is-selected"),t.options.editItems&&i&&n.length<=1?(t.input.value=i.innerHTML,t.removeItem(i)):t.removeAll(e)}};l(),e.preventDefault()}}},{key:"onFocus",value:function(e){}},{key:"onClick",value:function(e){}},{key:"onChange",value:function(e){}},{key:"addEventListeners",value:function(e){e.addEventListener("click",this.onClick),e.addEventListener("keyup",this.onKeyUp),e.addEventListener("keydown",this.onKeyDown),e.addEventListener("change",this.onChange),e.addEventListener("focus",this.onFocus),e.addEventListener("blur",this.onBlur)}},{key:"removeEventListeners",value:function(e){e.removeEventListener("click",this.onClick),e.removeEventListener("keyup",this.onKeyUp),e.removeEventListener("keydown",this.onKeyDown),e.removeEventListener("change",this.onChange),e.removeEventListener("focus",this.onFocus),e.removeEventListener("blur",this.onBlur)}},{key:"setValue",value:function(){}},{key:"getValue",value:function(){}},{key:"getValues",value:function(){}},{key:"getPlaceholder",value:function(){}},{key:"selectAll",value:function(e){for(var t=0;t=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(),s=function o(){e.classList.remove(t),e.removeEventListener(i,o,!1)};e.classList.add(t),e.addEventListener(i,s,!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 b41b4b1..fab39fc 100644 --- a/assets/scripts/src/choices.js +++ b/assets/scripts/src/choices.js @@ -28,10 +28,8 @@ export class Choices { placeholder: false, callbackOnInit: function() {}, callbackOnRender: function() {}, - callbackOnKeyUp: function() {}, - callbackOnKeyDown: function() {}, - callbackOnEntry: function() {}, - callbackOnRemove: function() {} + callbackOnRemoveItem: function() {}, + callbackOnAddItem: function() {} }; // Merge options with user options @@ -129,14 +127,7 @@ export class Choices { if (CTRLDOWN_KEY && e.keyCode === A_KEY && this.list && this.list.children) { let handleSelectAll = () => { if(this.options.removeItems) { - for (let i = 0; i < this.list.children.length; i++) { - let listItem = this.list.children[i]; - - // Select any items that have not already been selected - if(!listItem.classList.contains('is-selected')) { - listItem.classList.add('is-selected'); - } - } + this.selectAll(this.list.children); } }; @@ -147,7 +138,7 @@ export class Choices { if (e.keyCode === ENTER_KEY && e.target.value) { let value = this.input.value; - let handleENTER_KEY = () => { + let handleEnter = () => { let canUpdate = true; // If there is a max entry limit and we have reached that limit @@ -178,7 +169,7 @@ export class Choices { } }; - handleENTER_KEY(); + handleEnter(); } // If backspace or delete key is pressed and the input has no value @@ -253,6 +244,27 @@ export class Choices { getPlaceholder() {} + selectAll(items) { + for (let i = 0; i < items.length; i++) { + let item = items[i]; + + if (!item.classList.contains('is-selected')) { + item.classList.add('is-selected'); + } + }; + } + + + unselectAll(items) { + for (let i = 0; i < items.length; i++) { + let item = items[i]; + + if (item.classList.contains('is-selected')) { + item.classList.remove('is-selected'); + } + }; + } + updateInputValue(value) { if (this.options.debug) console.debug('Update input value'); @@ -282,16 +294,34 @@ export class Choices { // Append it to list parent.appendChild(item); + + // Run callback + if(this.options.callbackOnAddItem){ + if(isType('Function', this.options.callbackOnAddItem)) { + this.options.callbackOnAddItem(item, value); + } else { + console.error('Callback is not a function'); + } + } } - unselectAll(items) { - for (let i = 0; i < items.length; i++) { - let item = items[i]; + removeItem(item) { + if(!item) { + console.error('removeItem: No item was passed to be removed'); + return; + } - if (item.classList.contains('is-selected')) { - item.classList.remove('is-selected'); + 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 { + console.error('Callback is not a function'); } - }; + } } removeAll(items) { @@ -305,9 +335,7 @@ export class Choices { }; } - removeItem(item) { - if (item) item.parentNode.removeChild(item); - } + init() { if (!this.supports) console.error('Your browser doesn\'nt support shit'); @@ -475,6 +503,12 @@ export class Choices { element : input1, delimiter: ' ', maxItems: 5, + callbackOnRemoveItem: function(value) { + console.log(value); + }, + callbackOnAddItem: function(item, value) { + console.log(item, value); + } }); let choices2 = new Choices({ @@ -487,7 +521,6 @@ export class Choices { element : input3 }); - let choices4 = new Choices({ element : input4 });