From 04ab5f6a98fa6d6585723c38eb86d4df7118b401 Mon Sep 17 00:00:00 2001 From: Josh Johnson Date: Mon, 25 Apr 2016 18:00:30 +0100 Subject: [PATCH] Attach choices to window --- assets/scripts/dist/bundle.js | 4 +- assets/scripts/src/choices.js | 69 +--------------------------------- index.html | 70 +++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 70 deletions(-) diff --git a/assets/scripts/dist/bundle.js b/assets/scripts/dist/bundle.js index 135a4bd..905046c 100644 --- a/assets/scripts/dist/bundle.js +++ b/assets/scripts/dist/bundle.js @@ -1,2 +1,2 @@ -!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){return e&&e.__esModule?e:{"default":e}}function s(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;n1)for(var d=1;d=document.body.offsetHeight?this.dropdown.classList.add(this.options.classNames.flippedState):this.dropdown.classList.remove(this.options.classNames.flippedState)}},{key:"hideDropdown",value:function(){var e=this.dropdown.classList.contains(this.options.classNames.flippedState);this.dropdown.classList.remove(this.options.classNames.activeState),e&&this.dropdown.classList.remove(this.options.classNames.flippedState)}},{key:"toggleDropdown",value:function(){if(this.dropdown){var e=this.dropdown.classList.contains(this.options.classNames.activeState);e?this.hideDropdown():this.showDropdown()}}},{key:"addOption",value:function(e){var t=arguments.length<=1||void 0===arguments[1]?-1:arguments[1],n=this.store.getState(),i=n.options.length+1,s=e.value,o=e.innerHTML,r=e.selected;this.store.dispatch((0,l.addOption)(s,o,i,t)),r&&(this.selectOption(i),this.addItem(s,o,i))}},{key:"addGroup",value:function(e,t,n,i){this.store.dispatch((0,l.addGroup)(e,t,n,i))}},{key:"getItems",value:function(){var e=this.store.getState();return e.items}},{key:"getItemsFilteredByActive",value:function(){var e=this.getItems(),t=e.filter(function(e){return e.active===!0},[]);return t}},{key:"getItemsReducedToValues",value:function(){var e=this.getItems(),t=e.reduce(function(e,t){return e.push(t.value),e},[]);return t}},{key:"getOptions",value:function(){var e=this.store.getState();return e.options}},{key:"getOptionsFilteredByActive",value:function(){var e=this.getOptions(),t=e.filter(function(e){return e.active===!0},[]);return t}},{key:"getOptionsFiltedBySelectable",value:function(){var e=this.getOptions(),t=e.filter(function(e){return e.disabled===!1},[]);return t}},{key:"getGroups",value:function(){var e=this.store.getState();return e.groups}},{key:"getGroupsFilteredByActive",value:function(){var e=this.getGroups(),t=this.getOptions(),n=e.filter(function(e){var n=e.active===!0&&e.disabled===!1,i=t.some(function(e){return e.active===!0&&e.disabled===!1});return!(!n||!i)},[]);return n}},{key:"generateTextInput",value:function(){var e=this,t=(0,u.strToEl)('
'),n=(0,u.strToEl)('
');this.passedElement.classList.add(this.options.classNames.input,this.options.classNames.hiddenState),this.passedElement.tabIndex="-1",this.passedElement.setAttribute("style","display:none;"),this.passedElement.setAttribute("aria-hidden","true"),(0,u.wrap)(this.passedElement,n),(0,u.wrap)(n,t);var i=(0,u.strToEl)('
    '),s=(0,u.strToEl)('');if(this.options.placeholder){var o=this.options.placeholderValue||this.passedElement.placeholder;o&&(s.placeholder=o,s.style.width=(0,u.getWidthOfInput)(s))}this.options.addItems||(s.disabled=!0,t.classList.add(this.options.classNames.disabledState)),t.appendChild(n),n.appendChild(i),n.appendChild(s),this.containerOuter=t,this.containerInner=n,this.input=s,this.list=i,this.inputType="text",this.presetItems.forEach(function(t){e.addItem(t)})}},{key:"generateMultipleSelectInput",value:function(){var e=this,t=(0,u.strToEl)('
    '),n=(0,u.strToEl)('
    ');this.passedElement.classList.add(this.options.classNames.input,this.options.classNames.hiddenState),this.passedElement.tabIndex="-1",this.passedElement.setAttribute("style","display:none;"),this.passedElement.setAttribute("aria-hidden","true"),(0,u.wrap)(this.passedElement,n),(0,u.wrap)(n,t);var i=(0,u.strToEl)('
      '),s=(0,u.strToEl)(''),o=(0,u.strToEl)('
      ');this.options.placeholder&&this.options.placeholderValue&&(s.placeholder=this.options.placeholderValue,s.style.width=(0,u.getWidthOfInput)(s)),this.options.addItems||(s.disabled=!0),t.appendChild(n),t.appendChild(o),n.appendChild(i),n.appendChild(s),this.containerOuter=t,this.containerInner=n,this.input=s,this.list=i,this.dropdown=o,this.inputType="multipleSelect";var r=Array.from(this.passedElement.getElementsByTagName("OPTGROUP"));if(r.length)r.forEach(function(t,n){var i=Array.from(t.getElementsByTagName("OPTION")),s=n;i?(e.addGroup(t.label,s,!0,t.disabled),i.forEach(function(t){e.addOption(t,s)})):e.addGroup(t.label,s,!1,t.disabled)});else{var a=Array.from(this.passedElement.options);a.forEach(function(t){e.addOption(t)})}}},{key:"addEventListeners",value:function(){document.addEventListener("keyup",this.onKeyUp),document.addEventListener("keydown",this.onKeyDown),document.addEventListener("click",this.onClick),document.addEventListener("paste",this.onPaste),this.input.addEventListener("focus",this.onFocus),this.input.addEventListener("blur",this.onBlur)}},{key:"removeEventListeners",value:function(){document.removeEventListener("keyup",this.onKeyUp),document.removeEventListener("keydown",this.onKeyDown),document.removeEventListener("click",this.onClick),document.removeEventListener("paste",this.onPaste),this.input.removeEventListener("focus",this.onFocus),this.input.removeEventListener("blur",this.onBlur)}},{key:"render",value:function(){var e=this,t=arguments.length<=0||void 0===arguments[0]?this.options.callbackOnRender:arguments[0],n=this.options.classNames,i=this.getItemsFilteredByActive();"multipleSelect"===this.inputType&&!function(){var t=e.getOptionsFilteredByActive(),i=e.getGroupsFilteredByActive();console.log(i),e.dropdown.innerHTML="";var s=document.createDocumentFragment();if(i.length>=1)i.forEach(function(e,i){var o=t.filter(function(t){return t.groupId===e.id}),r=(0,u.strToEl)('\n
      \n
      '+e.value+"
      \n
      \n ");o.forEach(function(e){var t=(0,u.strToEl)('\n
      \n '+e.label+"\n
      \n ");r.appendChild(t)}),s.appendChild(r)});else if(t.length>=1)t.forEach(function(e){var t=(0,u.strToEl)('\n
      \n '+e.label+"\n
      \n ");s.appendChild(t)});else{var o=(0,u.strToEl)('
      No options to select
      ');s.appendChild(o)}e.dropdown.appendChild(s)}(),i&&!function(){var t=e.getItemsReducedToValues();e.passedElement.value=t.join(e.options.delimiter),e.list.innerHTML="";var s=document.createDocumentFragment();i.forEach(function(t){var i=(0,u.strToEl)('\n
    • \n '+t.label+"\n
    • \n ");s.appendChild(i)}),e.list.appendChild(s)}(),t&&((0,u.isType)("Function",t)?t(i):console.error("callbackOnRender: Callback is not a function"))}},{key:"renderInput",value:function(e){switch(this.options.debug&&console.debug("Render"),e.type){case"text":this.generateTextInput();break;case"select-one":break;case"select-multiple":this.generateMultipleSelectInput();break;default:console.error("renderInput: Input type is not supported")}}},{key:"init",value:function(){var e=arguments.length<=0||void 0===arguments[0]?this.options.callbackOnInit:arguments[0];this.initialised=!0,this.renderInput(this.passedElement),this.store.subscribe(this.render),this.render(),this.addEventListeners(),e&&((0,u.isType)("Function",e)?e():console.error("callbackOnInit: Callback is not a function"))}},{key:"destroy",value:function(){this.options=null,this.passedElement=null,this.initialised=null}}]),e}();document.addEventListener("DOMContentLoaded",function(){var e=document.getElementById("choices-1"),t=(new h(e,{delimiter:" ",editItems:!0,maxItems:5}),new h("#choices-2",{allowPaste:!1,allowDuplicates:!1,editItems:!0}),new h("#choices-3",{allowDuplicates:!1,editItems:!0,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,}))$/}),new h("#choices-4",{addItems:!1,removeItems:!1}),new h("#choices-5",{prependValue:"item-",appendValue:"-"+Date.now()}));t.removeAllItems();new h("#choices-6",{items:["josh@joshuajohnson.co.uk","joe@bloggs.co.uk"]}),new h("#choices-7",{}),new h("[data-choice]",{placeholderValue:"This is a placeholder set in the config"})})},function(e,t,n){"use strict";function i(e){return e&&e.__esModule?e:{"default":e}}t.__esModule=!0,t.compose=t.applyMiddleware=t.bindActionCreators=t.combineReducers=t.createStore=void 0;var s=n(3),o=i(s),r=n(7),a=i(r),c=n(9),l=i(c),u=n(10),d=i(u),p=n(11),h=i(p),f=n(8);i(f);t.createStore=o.default,t.combineReducers=a.default,t.bindActionCreators=l.default,t.applyMiddleware=d.default,t.compose=h.default},function(e,t,n){"use strict";function i(e){return e&&e.__esModule?e:{"default":e}}function s(e,t,n){function i(){f===h&&(f=h.slice())}function o(){return p}function c(e){if("function"!=typeof e)throw new Error("Expected listener to be a function.");var t=!0;return i(),f.push(e),function(){if(t){t=!1,i();var n=f.indexOf(e);f.splice(n,1)}}}function l(e){if(!(0,r.default)(e))throw new Error("Actions must be plain objects. Use custom middleware for async actions.");if("undefined"==typeof e.type)throw new Error('Actions may not have an undefined "type" property. Have you misspelled a constant?');if(v)throw new Error("Reducers may not dispatch actions.");try{v=!0,p=d(p,e)}finally{v=!1}for(var t=h=f,n=0;nn;n++)t[n]=arguments[n];return function(e){return function(n,i,s){var r=e(n,i,s),c=r.dispatch,l=[],u={getState:r.getState,dispatch:function(e){return c(e)}};return l=t.map(function(e){return e(u)}),c=a.default.apply(void 0,l)(r.dispatch),o({},r,{dispatch:c})}}}var o=Object.assign||function(e){for(var t=1;tn;n++)t[n]=arguments[n];return function(){if(0===t.length)return arguments.length<=0?void 0:arguments[0];var e=t[t.length-1],n=t.slice(0,-1);return n.reduceRight(function(e,t){return t(e)},e.apply(void 0,arguments))}}t.__esModule=!0,t.default=n},function(e,t,n){"use strict";function i(e){return e&&e.__esModule?e:{"default":e}}Object.defineProperty(t,"__esModule",{value:!0});var s=n(2),o=n(13),r=i(o),a=n(14),c=i(a),l=n(15),u=i(l),d=(0,s.combineReducers)({items:r.default,groups:c.default,options:u.default});t.default=d},function(e,t){"use strict";function n(e){if(Array.isArray(e)){for(var t=0,n=Array(e.length);t-1?e.map(function(e){return e.id===parseInt(t.optionId)&&(e.selected=t.selected),e}):e;case"FILTER_OPTIONS":var i=t.results.items,s=e.map(function(e,t){return e.active=i.some(function(e){return e.id===t}),e});return s;case"ACTIVATE_OPTIONS":return e.map(function(e){return e.active=t.active,e});default:return e}};t.default=i},function(e,t){"use strict";Object.defineProperty(t,"__esModule",{value:!0});t.addItem=function(e,t,n,i){return{type:"ADD_ITEM",value:e,label:t,id:n,optionId:i}},t.removeItem=function(e,t){return{type:"REMOVE_ITEM",id:e,optionId:t}},t.selectItem=function(e,t){return{type:"SELECT_ITEM",id:e,selected:t}},t.addOption=function(e,t,n,i){return{type:"ADD_OPTION",value:e,label:t,id:n,groupId:i}},t.selectOption=function(e,t){return{type:"SELECT_OPTION",id:e,selected:t}},t.filterOptions=function(e){return{type:"FILTER_OPTIONS",results:e}},t.activateOptions=function(){var e=arguments.length<=0||void 0===arguments[0]?!0:arguments[0];return{type:"ACTIVATE_OPTIONS",active:e}},t.addGroup=function(e,t,n,i){return{type:"ADD_GROUP",value:e,id:t,active:n,disabled:i}}},function(e,t){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var n=(t.hasClass=function(e,t){return new RegExp(" "+t+" ").test(" "+e.className+" ")},t.capitalise=function(e){return e.replace(/\w\S*/g,function(e){return e.charAt(0).toUpperCase()+e.substr(1).toLowerCase()})},t.isType=function(e,t){var n=Object.prototype.toString.call(t).slice(8,-1);return void 0!==t&&null!==t&&n===e}),i=(t.extend=function o(){for(var e={},t=!1,i=arguments.length,s=function(i){for(var s in i)Object.prototype.hasOwnProperty.call(i,s)&&(t&&n("Object",i[s])?e[s]=o(!0,e[s],i[s]):e[s]=i[s])},r=0;i>r;r++){var a=arguments[r];n("Object",a)?s(a):console.error("Custom options must be an object")}return e},t.whichTransitionEvent=function(){var e,t=document.createElement("fakeelement"),n={transition:"transitionend",OTransition:"oTransitionEnd",MozTransition:"transitionend",WebkitTransition:"webkitTransitionEnd"};for(e in n)if(void 0!==t.style[e])return n[e]},t.whichAnimationEvent=function(){var e,t=document.createElement("fakeelement"),n={animation:"animationend",OAnimation:"oAnimationEnd",MozAnimation:"animationend",WebkitAnimation:"webkitAnimationEnd"};for(e in n)if(void 0!==t.style[e])return n[e]}),s=(t.getParentsUntil=function(e,t,n){for(var i=[];e&&e!==document;e=e.parentNode){if(t){var s=t.charAt(0);if("."===s&&e.classList.contains(t.substr(1)))break;if("#"===s&&e.id===t.substr(1))break;if("["===s&&e.hasAttribute(t.substr(1,t.length-1)))break;if(e.tagName.toLowerCase()===t)break}if(n){var o=n.charAt(0);"."===o&&e.classList.contains(n.substr(1))&&i.push(e),"#"===o&&e.id===n.substr(1)&&i.push(e),"["===o&&e.hasAttribute(n.substr(1,n.length-1))&&i.push(e),e.tagName.toLowerCase()===n&&i.push(e)}else i.push(e)}return 0===i.length?null:i},t.wrap=function(e,t){return t=t||document.createElement("div"),e.nextSibling?e.parentNode.insertBefore(t,e.nextSibling):e.parentNode.appendChild(t),t.appendChild(e)},t.getSiblings=function(e){for(var t=[],n=e.parentNode.firstChild;n;n=n.nextSibling)1===n.nodeType&&n!==e&&t.push(n);return t},t.findAncestor=function(e,t){for(;(e=e.parentElement)&&!e.classList.contains(t););return e},t.debounce=function(e,t,n){var i;return function(){var s=this,o=arguments,r=function(){i=null,n||e.apply(s,o)},a=n&&!i;clearTimeout(i),i=setTimeout(r,t),a&&e.apply(s,o)}},t.getElemDistance=function(e){var t=0;if(e.offsetParent)do t+=e.offsetTop,e=e.offsetParent;while(e);return 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 n=i(),s=function o(){e.classList.remove(t),e.removeEventListener(n,o,!1)};e.classList.add(t),e.addEventListener(n,s,!1)},t.getRandomNumber=function(e,t){return Math.floor(Math.random()*(t-e)+e)},t.strToEl=function(){var e=document.createElement("div");return function(t){var n;for(e.innerHTML=t,n=e.children[0];e.firstChild;)e.removeChild(e.firstChild);return n}}());t.getWidthOfInput=function(e){var t=arguments.length<=1||void 0===arguments[1]?20:arguments[1],n=e.value||e.placeholder,i=e.offsetWidth;if(n){var o=s(''+n+"");o.style.position="absolute",o.style.top="-9999px",o.style.left="-9999px",o.style.padding="0",o.style.width="auto",document.body.appendChild(o),o.offsetWidth>t&&o.offsetWidth!=e.offsetWidth&&(i=o.offsetWidth+t/4),document.body.removeChild(o)}return i+"px"}},function(e,t,n){var i,s;!function(o,r){i=r,s="function"==typeof i?i.call(t,n,t,e):i,!(void 0!==s&&(e.exports=s))}(this,function(){var e=function(e,t){this.items=e,this.settings=t||{diacritics:!0}};e.prototype.tokenize=function(e){if(e=i(String(e||"").toLowerCase()),!e||!e.length)return[];var t,n,o,a,c=[],l=e.split(/ +/);for(t=0,n=l.length;n>t;t++){if(o=s(l[t]),this.settings.diacritics)for(a in r)r.hasOwnProperty(a)&&(o=o.replace(new RegExp(a,"g"),r[a]));c.push({string:l[t],regex:new RegExp(o,"i")})}return c},e.prototype.iterator=function(e,t){var n;n=o(e)?Array.prototype.forEach||function(e){for(var t=0,n=this.length;n>t;t++)e(this[t],t,this)}:function(e){for(var t in this)this.hasOwnProperty(t)&&e(this[t],t,this)},n.apply(e,[t])},e.prototype.getScoreFunction=function(e,t){var n,i,s,o;n=this,e=n.prepareSearch(e,t),s=e.tokens,i=e.options.fields,o=s.length;var r=function(e,t){var n,i;return e?(e=String(e||""),i=e.search(t.regex),-1===i?0:(n=t.string.length/e.length,0===i&&(n+=.5),n)):0},a=function(){var e=i.length;return e?1===e?function(e,t){return r(t[i[0]],e); -}:function(t,n){for(var s=0,o=0;e>s;s++)o+=r(n[i[s]],t);return o/e}:function(){return 0}}();return o?1===o?function(e){return a(s[0],e)}:"and"===e.options.conjunction?function(e){for(var t,n=0,i=0;o>n;n++){if(t=a(s[n],e),0>=t)return 0;i+=t}return i/o}:function(e){for(var t=0,n=0;o>t;t++)n+=a(s[t],e);return n/o}:function(){return 0}},e.prototype.getSortFunction=function(e,n){var i,s,o,r,a,c,l,u,d,p,h;if(o=this,e=o.prepareSearch(e,n),h=!e.query&&n.sort_empty||n.sort,d=function(e,t){return"$score"===e?t.score:o.items[t.id][e]},a=[],h)for(i=0,s=h.length;s>i;i++)(e.query||"$score"!==h[i].field)&&a.push(h[i]);if(e.query){for(p=!0,i=0,s=a.length;s>i;i++)if("$score"===a[i].field){p=!1;break}p&&a.unshift({field:"$score",direction:"desc"})}else for(i=0,s=a.length;s>i;i++)if("$score"===a[i].field){a.splice(i,1);break}for(u=[],i=0,s=a.length;s>i;i++)u.push("desc"===a[i].direction?-1:1);return c=a.length,c?1===c?(r=a[0].field,l=u[0],function(e,n){return l*t(d(r,e),d(r,n))}):function(e,n){var i,s,o;for(i=0;c>i;i++)if(o=a[i].field,s=u[i]*t(d(o,e),d(o,n)))return s;return 0}:null},e.prototype.prepareSearch=function(e,t){if("object"==typeof e)return e;t=n({},t);var i=t.fields,s=t.sort,r=t.sort_empty;return i&&!o(i)&&(t.fields=[i]),s&&!o(s)&&(t.sort=[s]),r&&!o(r)&&(t.sort_empty=[r]),{options:t,query:String(e||"").toLowerCase(),tokens:this.tokenize(e),total:0,items:[]}},e.prototype.search=function(e,t){var n,i,s,o,r=this;return i=this.prepareSearch(e,t),t=i.options,e=i.query,o=t.score||r.getScoreFunction(i),e.length?r.iterator(r.items,function(e,s){n=o(e),(t.filter===!1||n>0)&&i.items.push({score:n,id:s})}):r.iterator(r.items,function(e,t){i.items.push({score:1,id:t})}),s=r.getSortFunction(i,t),s&&i.items.sort(s),i.total=i.items.length,"number"==typeof t.limit&&(i.items=i.items.slice(0,t.limit)),i};var t=function(e,t){return"number"==typeof e&&"number"==typeof t?e>t?1:t>e?-1:0:(e=a(String(e||"")),t=a(String(t||"")),e>t?1:t>e?-1:0)},n=function(e,t){var n,i,s,o;for(n=1,i=arguments.length;i>n;n++)if(o=arguments[n])for(s in o)o.hasOwnProperty(s)&&(e[s]=o[s]);return e},i=function(e){return(e+"").replace(/^\s+|\s+$|/g,"")},s=function(e){return(e+"").replace(/([.?*+^$[\]\\(){}|-])/g,"\\$1")},o=Array.isArray||"undefined"!=typeof $&&$.isArray||function(e){return"[object Array]"===Object.prototype.toString.call(e)},r={a:"[aÀÁÂÃÄÅàáâãäåĀāąĄ]",c:"[cÇçćĆčČ]",d:"[dđĐďĎð]",e:"[eÈÉÊËèéêëěĚĒēęĘ]",i:"[iÌÍÎÏìíîïĪī]",l:"[lłŁ]",n:"[nÑñňŇńŃ]",o:"[oÒÓÔÕÕÖØòóôõöøŌō]",r:"[rřŘ]",s:"[sŠšśŚ]",t:"[tťŤ]",u:"[uÙÚÛÜùúûüůŮŪū]",y:"[yŸÿýÝ]",z:"[zŽžżŻźŹ]"},a=function(){var e,t,n,i,s="",o={};for(n in r)if(r.hasOwnProperty(n))for(i=r[n].substring(2,r[n].length-1),s+=i,e=0,t=i.length;t>e;e++)o[i.charAt(e)]=n;var a=new RegExp("["+s+"]","g");return function(e){return e.replace(a,function(e){return o[e]}).toLowerCase()}}();return 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){return e&&e.__esModule?e:{"default":e}}function s(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;n1)for(var d=1;d=document.body.offsetHeight?this.dropdown.classList.add(this.options.classNames.flippedState):this.dropdown.classList.remove(this.options.classNames.flippedState)}},{key:"hideDropdown",value:function(){var e=this.dropdown.classList.contains(this.options.classNames.flippedState);this.dropdown.classList.remove(this.options.classNames.activeState),e&&this.dropdown.classList.remove(this.options.classNames.flippedState)}},{key:"toggleDropdown",value:function(){if(this.dropdown){var e=this.dropdown.classList.contains(this.options.classNames.activeState);e?this.hideDropdown():this.showDropdown()}}},{key:"addOption",value:function(e){var t=arguments.length<=1||void 0===arguments[1]?-1:arguments[1],n=this.store.getState(),i=n.options.length+1,s=e.value,o=e.innerHTML,r=e.selected;this.store.dispatch((0,l.addOption)(s,o,i,t)),r&&(this.selectOption(i),this.addItem(s,o,i))}},{key:"addGroup",value:function(e,t,n,i){this.store.dispatch((0,l.addGroup)(e,t,n,i))}},{key:"getItems",value:function(){var e=this.store.getState();return e.items}},{key:"getItemsFilteredByActive",value:function(){var e=this.getItems(),t=e.filter(function(e){return e.active===!0},[]);return t}},{key:"getItemsReducedToValues",value:function(){var e=this.getItems(),t=e.reduce(function(e,t){return e.push(t.value),e},[]);return t}},{key:"getOptions",value:function(){var e=this.store.getState();return e.options}},{key:"getOptionsFilteredByActive",value:function(){var e=this.getOptions(),t=e.filter(function(e){return e.active===!0},[]);return t}},{key:"getOptionsFiltedBySelectable",value:function(){var e=this.getOptions(),t=e.filter(function(e){return e.disabled===!1},[]);return t}},{key:"getGroups",value:function(){var e=this.store.getState();return e.groups}},{key:"getGroupsFilteredByActive",value:function(){var e=this.getGroups(),t=this.getOptions(),n=e.filter(function(e){var n=e.active===!0&&e.disabled===!1,i=t.some(function(e){return e.active===!0&&e.disabled===!1});return!(!n||!i)},[]);return n}},{key:"generateTextInput",value:function(){var e=this,t=(0,u.strToEl)('
      '),n=(0,u.strToEl)('
      ');this.passedElement.classList.add(this.options.classNames.input,this.options.classNames.hiddenState),this.passedElement.tabIndex="-1",this.passedElement.setAttribute("style","display:none;"),this.passedElement.setAttribute("aria-hidden","true"),(0,u.wrap)(this.passedElement,n),(0,u.wrap)(n,t);var i=(0,u.strToEl)('
        '),s=(0,u.strToEl)('');if(this.options.placeholder){var o=this.options.placeholderValue||this.passedElement.placeholder;o&&(s.placeholder=o,s.style.width=(0,u.getWidthOfInput)(s))}this.options.addItems||(s.disabled=!0,t.classList.add(this.options.classNames.disabledState)),t.appendChild(n),n.appendChild(i),n.appendChild(s),this.containerOuter=t,this.containerInner=n,this.input=s,this.list=i,this.inputType="text",this.presetItems.forEach(function(t){e.addItem(t)})}},{key:"generateMultipleSelectInput",value:function(){var e=this,t=(0,u.strToEl)('
        '),n=(0,u.strToEl)('
        ');this.passedElement.classList.add(this.options.classNames.input,this.options.classNames.hiddenState),this.passedElement.tabIndex="-1",this.passedElement.setAttribute("style","display:none;"),this.passedElement.setAttribute("aria-hidden","true"),(0,u.wrap)(this.passedElement,n),(0,u.wrap)(n,t);var i=(0,u.strToEl)('
          '),s=(0,u.strToEl)(''),o=(0,u.strToEl)('
          ');this.options.placeholder&&this.options.placeholderValue&&(s.placeholder=this.options.placeholderValue,s.style.width=(0,u.getWidthOfInput)(s)),this.options.addItems||(s.disabled=!0),t.appendChild(n),t.appendChild(o),n.appendChild(i),n.appendChild(s),this.containerOuter=t,this.containerInner=n,this.input=s,this.list=i,this.dropdown=o,this.inputType="multipleSelect";var r=Array.from(this.passedElement.getElementsByTagName("OPTGROUP"));if(r.length)r.forEach(function(t,n){var i=Array.from(t.getElementsByTagName("OPTION")),s=n;i?(e.addGroup(t.label,s,!0,t.disabled),i.forEach(function(t){e.addOption(t,s)})):e.addGroup(t.label,s,!1,t.disabled)});else{var a=Array.from(this.passedElement.options);a.forEach(function(t){e.addOption(t)})}}},{key:"addEventListeners",value:function(){document.addEventListener("keyup",this.onKeyUp),document.addEventListener("keydown",this.onKeyDown),document.addEventListener("click",this.onClick),document.addEventListener("paste",this.onPaste),this.input.addEventListener("focus",this.onFocus),this.input.addEventListener("blur",this.onBlur)}},{key:"removeEventListeners",value:function(){document.removeEventListener("keyup",this.onKeyUp),document.removeEventListener("keydown",this.onKeyDown),document.removeEventListener("click",this.onClick),document.removeEventListener("paste",this.onPaste),this.input.removeEventListener("focus",this.onFocus),this.input.removeEventListener("blur",this.onBlur)}},{key:"render",value:function(){var e=this,t=arguments.length<=0||void 0===arguments[0]?this.options.callbackOnRender:arguments[0],n=this.options.classNames,i=this.getItemsFilteredByActive();"multipleSelect"===this.inputType&&!function(){var t=e.getOptionsFilteredByActive(),i=e.getGroupsFilteredByActive();console.log(i),e.dropdown.innerHTML="";var s=document.createDocumentFragment();if(i.length>=1)i.forEach(function(e,i){var o=t.filter(function(t){return t.groupId===e.id}),r=(0,u.strToEl)('\n
          \n
          '+e.value+"
          \n
          \n ");o.forEach(function(e){var t=(0,u.strToEl)('\n
          \n '+e.label+"\n
          \n ");r.appendChild(t)}),s.appendChild(r)});else if(t.length>=1)t.forEach(function(e){var t=(0,u.strToEl)('\n
          \n '+e.label+"\n
          \n ");s.appendChild(t)});else{var o=(0,u.strToEl)('
          No options to select
          ');s.appendChild(o)}e.dropdown.appendChild(s)}(),i&&!function(){var t=e.getItemsReducedToValues();e.passedElement.value=t.join(e.options.delimiter),e.list.innerHTML="";var s=document.createDocumentFragment();i.forEach(function(t){var i=(0,u.strToEl)('\n
        • \n '+t.label+"\n
        • \n ");s.appendChild(i)}),e.list.appendChild(s)}(),t&&((0,u.isType)("Function",t)?t(i):console.error("callbackOnRender: Callback is not a function"))}},{key:"renderInput",value:function(e){switch(this.options.debug&&console.debug("Render"),e.type){case"text":this.generateTextInput();break;case"select-one":break;case"select-multiple":this.generateMultipleSelectInput();break;default:console.error("renderInput: Input type is not supported")}}},{key:"init",value:function(){var e=arguments.length<=0||void 0===arguments[0]?this.options.callbackOnInit:arguments[0];this.initialised=!0,this.renderInput(this.passedElement),this.store.subscribe(this.render),this.render(),this.addEventListeners(),e&&((0,u.isType)("Function",e)?e():console.error("callbackOnInit: Callback is not a function"))}},{key:"destroy",value:function(){this.options=null,this.passedElement=null,this.initialised=null}}]),e}();window.Choices=e.exports=h},function(e,t,n){"use strict";function i(e){return e&&e.__esModule?e:{"default":e}}t.__esModule=!0,t.compose=t.applyMiddleware=t.bindActionCreators=t.combineReducers=t.createStore=void 0;var s=n(3),o=i(s),r=n(7),a=i(r),c=n(9),l=i(c),u=n(10),d=i(u),p=n(11),h=i(p),f=n(8);i(f);t.createStore=o.default,t.combineReducers=a.default,t.bindActionCreators=l.default,t.applyMiddleware=d.default,t.compose=h.default},function(e,t,n){"use strict";function i(e){return e&&e.__esModule?e:{"default":e}}function s(e,t,n){function i(){f===h&&(f=h.slice())}function o(){return p}function c(e){if("function"!=typeof e)throw new Error("Expected listener to be a function.");var t=!0;return i(),f.push(e),function(){if(t){t=!1,i();var n=f.indexOf(e);f.splice(n,1)}}}function l(e){if(!(0,r.default)(e))throw new Error("Actions must be plain objects. Use custom middleware for async actions.");if("undefined"==typeof e.type)throw new Error('Actions may not have an undefined "type" property. Have you misspelled a constant?');if(v)throw new Error("Reducers may not dispatch actions.");try{v=!0,p=d(p,e)}finally{v=!1}for(var t=h=f,n=0;nn;n++)t[n]=arguments[n];return function(e){return function(n,i,s){var r=e(n,i,s),c=r.dispatch,l=[],u={getState:r.getState,dispatch:function(e){return c(e)}};return l=t.map(function(e){return e(u)}),c=a.default.apply(void 0,l)(r.dispatch),o({},r,{dispatch:c})}}}var o=Object.assign||function(e){for(var t=1;tn;n++)t[n]=arguments[n];return function(){if(0===t.length)return arguments.length<=0?void 0:arguments[0];var e=t[t.length-1],n=t.slice(0,-1);return n.reduceRight(function(e,t){return t(e)},e.apply(void 0,arguments))}}t.__esModule=!0,t.default=n},function(e,t,n){"use strict";function i(e){return e&&e.__esModule?e:{"default":e}}Object.defineProperty(t,"__esModule",{value:!0});var s=n(2),o=n(13),r=i(o),a=n(14),c=i(a),l=n(15),u=i(l),d=(0,s.combineReducers)({items:r.default,groups:c.default,options:u.default});t.default=d},function(e,t){"use strict";function n(e){if(Array.isArray(e)){for(var t=0,n=Array(e.length);t-1?e.map(function(e){return e.id===parseInt(t.optionId)&&(e.selected=t.selected),e}):e;case"FILTER_OPTIONS":var i=t.results.items,s=e.map(function(e,t){return e.active=i.some(function(e){return e.id===t}),e});return s;case"ACTIVATE_OPTIONS":return e.map(function(e){return e.active=t.active,e});default:return e}};t.default=i},function(e,t){"use strict";Object.defineProperty(t,"__esModule",{value:!0});t.addItem=function(e,t,n,i){return{type:"ADD_ITEM",value:e,label:t,id:n,optionId:i}},t.removeItem=function(e,t){return{type:"REMOVE_ITEM",id:e,optionId:t}},t.selectItem=function(e,t){return{type:"SELECT_ITEM",id:e,selected:t}},t.addOption=function(e,t,n,i){return{type:"ADD_OPTION",value:e,label:t,id:n,groupId:i}},t.selectOption=function(e,t){return{type:"SELECT_OPTION",id:e,selected:t}},t.filterOptions=function(e){return{type:"FILTER_OPTIONS",results:e}},t.activateOptions=function(){var e=arguments.length<=0||void 0===arguments[0]?!0:arguments[0];return{type:"ACTIVATE_OPTIONS",active:e}},t.addGroup=function(e,t,n,i){return{type:"ADD_GROUP",value:e,id:t,active:n,disabled:i}}},function(e,t){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var n=(t.hasClass=function(e,t){return new RegExp(" "+t+" ").test(" "+e.className+" ")},t.capitalise=function(e){return e.replace(/\w\S*/g,function(e){return e.charAt(0).toUpperCase()+e.substr(1).toLowerCase()})},t.isType=function(e,t){var n=Object.prototype.toString.call(t).slice(8,-1);return void 0!==t&&null!==t&&n===e}),i=(t.extend=function o(){for(var e={},t=!1,i=arguments.length,s=function(i){for(var s in i)Object.prototype.hasOwnProperty.call(i,s)&&(t&&n("Object",i[s])?e[s]=o(!0,e[s],i[s]):e[s]=i[s])},r=0;i>r;r++){var a=arguments[r];n("Object",a)?s(a):console.error("Custom options must be an object")}return e},t.whichTransitionEvent=function(){var e,t=document.createElement("fakeelement"),n={transition:"transitionend",OTransition:"oTransitionEnd",MozTransition:"transitionend",WebkitTransition:"webkitTransitionEnd"};for(e in n)if(void 0!==t.style[e])return n[e]},t.whichAnimationEvent=function(){var e,t=document.createElement("fakeelement"),n={animation:"animationend",OAnimation:"oAnimationEnd",MozAnimation:"animationend",WebkitAnimation:"webkitAnimationEnd"};for(e in n)if(void 0!==t.style[e])return n[e]}),s=(t.getParentsUntil=function(e,t,n){for(var i=[];e&&e!==document;e=e.parentNode){if(t){var s=t.charAt(0);if("."===s&&e.classList.contains(t.substr(1)))break;if("#"===s&&e.id===t.substr(1))break;if("["===s&&e.hasAttribute(t.substr(1,t.length-1)))break;if(e.tagName.toLowerCase()===t)break}if(n){var o=n.charAt(0);"."===o&&e.classList.contains(n.substr(1))&&i.push(e),"#"===o&&e.id===n.substr(1)&&i.push(e),"["===o&&e.hasAttribute(n.substr(1,n.length-1))&&i.push(e),e.tagName.toLowerCase()===n&&i.push(e)}else i.push(e)}return 0===i.length?null:i},t.wrap=function(e,t){return t=t||document.createElement("div"),e.nextSibling?e.parentNode.insertBefore(t,e.nextSibling):e.parentNode.appendChild(t),t.appendChild(e)},t.getSiblings=function(e){for(var t=[],n=e.parentNode.firstChild;n;n=n.nextSibling)1===n.nodeType&&n!==e&&t.push(n);return t},t.findAncestor=function(e,t){for(;(e=e.parentElement)&&!e.classList.contains(t););return e},t.debounce=function(e,t,n){var i;return function(){var s=this,o=arguments,r=function(){i=null,n||e.apply(s,o)},a=n&&!i;clearTimeout(i),i=setTimeout(r,t),a&&e.apply(s,o)}},t.getElemDistance=function(e){var t=0;if(e.offsetParent)do t+=e.offsetTop,e=e.offsetParent;while(e);return 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 n=i(),s=function o(){e.classList.remove(t),e.removeEventListener(n,o,!1)};e.classList.add(t),e.addEventListener(n,s,!1)},t.getRandomNumber=function(e,t){return Math.floor(Math.random()*(t-e)+e)},t.strToEl=function(){var e=document.createElement("div");return function(t){var n;for(e.innerHTML=t,n=e.children[0];e.firstChild;)e.removeChild(e.firstChild);return n}}());t.getWidthOfInput=function(e){var t=arguments.length<=1||void 0===arguments[1]?20:arguments[1],n=e.value||e.placeholder,i=e.offsetWidth;if(n){var o=s(''+n+"");o.style.position="absolute",o.style.top="-9999px",o.style.left="-9999px",o.style.padding="0",o.style.width="auto",document.body.appendChild(o),o.offsetWidth>t&&o.offsetWidth!=e.offsetWidth&&(i=o.offsetWidth+t/4),document.body.removeChild(o)}return i+"px"}},function(e,t,n){var i,s;!function(o,r){i=r,s="function"==typeof i?i.call(t,n,t,e):i,!(void 0!==s&&(e.exports=s))}(this,function(){var e=function(e,t){this.items=e,this.settings=t||{diacritics:!0}};e.prototype.tokenize=function(e){if(e=i(String(e||"").toLowerCase()),!e||!e.length)return[];var t,n,o,a,c=[],l=e.split(/ +/);for(t=0,n=l.length;n>t;t++){if(o=s(l[t]),this.settings.diacritics)for(a in r)r.hasOwnProperty(a)&&(o=o.replace(new RegExp(a,"g"),r[a]));c.push({string:l[t],regex:new RegExp(o,"i")})}return c},e.prototype.iterator=function(e,t){var n;n=o(e)?Array.prototype.forEach||function(e){for(var t=0,n=this.length;n>t;t++)e(this[t],t,this)}:function(e){for(var t in this)this.hasOwnProperty(t)&&e(this[t],t,this)},n.apply(e,[t])},e.prototype.getScoreFunction=function(e,t){var n,i,s,o;n=this,e=n.prepareSearch(e,t),s=e.tokens,i=e.options.fields,o=s.length;var r=function(e,t){var n,i;return e?(e=String(e||""),i=e.search(t.regex),-1===i?0:(n=t.string.length/e.length,0===i&&(n+=.5),n)):0},a=function(){var e=i.length;return e?1===e?function(e,t){return r(t[i[0]],e)}:function(t,n){for(var s=0,o=0;e>s;s++)o+=r(n[i[s]],t);return o/e}:function(){return 0}}();return o?1===o?function(e){return a(s[0],e)}:"and"===e.options.conjunction?function(e){for(var t,n=0,i=0;o>n;n++){if(t=a(s[n],e),0>=t)return 0;i+=t}return i/o}:function(e){for(var t=0,n=0;o>t;t++)n+=a(s[t],e);return n/o}:function(){return 0}},e.prototype.getSortFunction=function(e,n){var i,s,o,r,a,c,l,u,d,p,h;if(o=this,e=o.prepareSearch(e,n),h=!e.query&&n.sort_empty||n.sort,d=function(e,t){return"$score"===e?t.score:o.items[t.id][e]},a=[],h)for(i=0,s=h.length;s>i;i++)(e.query||"$score"!==h[i].field)&&a.push(h[i]);if(e.query){for(p=!0,i=0,s=a.length;s>i;i++)if("$score"===a[i].field){p=!1;break}p&&a.unshift({field:"$score",direction:"desc" +})}else for(i=0,s=a.length;s>i;i++)if("$score"===a[i].field){a.splice(i,1);break}for(u=[],i=0,s=a.length;s>i;i++)u.push("desc"===a[i].direction?-1:1);return c=a.length,c?1===c?(r=a[0].field,l=u[0],function(e,n){return l*t(d(r,e),d(r,n))}):function(e,n){var i,s,o;for(i=0;c>i;i++)if(o=a[i].field,s=u[i]*t(d(o,e),d(o,n)))return s;return 0}:null},e.prototype.prepareSearch=function(e,t){if("object"==typeof e)return e;t=n({},t);var i=t.fields,s=t.sort,r=t.sort_empty;return i&&!o(i)&&(t.fields=[i]),s&&!o(s)&&(t.sort=[s]),r&&!o(r)&&(t.sort_empty=[r]),{options:t,query:String(e||"").toLowerCase(),tokens:this.tokenize(e),total:0,items:[]}},e.prototype.search=function(e,t){var n,i,s,o,r=this;return i=this.prepareSearch(e,t),t=i.options,e=i.query,o=t.score||r.getScoreFunction(i),e.length?r.iterator(r.items,function(e,s){n=o(e),(t.filter===!1||n>0)&&i.items.push({score:n,id:s})}):r.iterator(r.items,function(e,t){i.items.push({score:1,id:t})}),s=r.getSortFunction(i,t),s&&i.items.sort(s),i.total=i.items.length,"number"==typeof t.limit&&(i.items=i.items.slice(0,t.limit)),i};var t=function(e,t){return"number"==typeof e&&"number"==typeof t?e>t?1:t>e?-1:0:(e=a(String(e||"")),t=a(String(t||"")),e>t?1:t>e?-1:0)},n=function(e,t){var n,i,s,o;for(n=1,i=arguments.length;i>n;n++)if(o=arguments[n])for(s in o)o.hasOwnProperty(s)&&(e[s]=o[s]);return e},i=function(e){return(e+"").replace(/^\s+|\s+$|/g,"")},s=function(e){return(e+"").replace(/([.?*+^$[\]\\(){}|-])/g,"\\$1")},o=Array.isArray||"undefined"!=typeof $&&$.isArray||function(e){return"[object Array]"===Object.prototype.toString.call(e)},r={a:"[aÀÁÂÃÄÅàáâãäåĀāąĄ]",c:"[cÇçćĆčČ]",d:"[dđĐďĎð]",e:"[eÈÉÊËèéêëěĚĒēęĘ]",i:"[iÌÍÎÏìíîïĪī]",l:"[lłŁ]",n:"[nÑñňŇńŃ]",o:"[oÒÓÔÕÕÖØòóôõöøŌō]",r:"[rřŘ]",s:"[sŠšśŚ]",t:"[tťŤ]",u:"[uÙÚÛÜùúûüůŮŪū]",y:"[yŸÿýÝ]",z:"[zŽžżŻźŹ]"},a=function(){var e,t,n,i,s="",o={};for(n in r)if(r.hasOwnProperty(n))for(i=r[n].substring(2,r[n].length-1),s+=i,e=0,t=i.length;t>e;e++)o[i.charAt(e)]=n;var a=new RegExp("["+s+"]","g");return function(e){return e.replace(a,function(e){return o[e]}).toLowerCase()}}();return e})}]); \ No newline at end of file diff --git a/assets/scripts/src/choices.js b/assets/scripts/src/choices.js index 5a635c8..2f034bf 100644 --- a/assets/scripts/src/choices.js +++ b/assets/scripts/src/choices.js @@ -1026,71 +1026,4 @@ export class Choices { } }; -document.addEventListener('DOMContentLoaded', () => { - const firstElement = document.getElementById('choices-1'); - const choices1 = new Choices(firstElement, { - delimiter: ' ', - editItems: true, - maxItems: 5, - // callbackOnRemoveItem: function(value) { - // console.log(value); - // }, - // callbackOnAddItem: function(id, value) { - // console.log(id, value); - // }, - // callbackOnRender: function(items) { - // console.log(items); - // } - }); - - const choices2 = new Choices('#choices-2', { - allowPaste: false, - allowDuplicates: false, - editItems: true, - }); - - const choices3 = new Choices('#choices-3', { - 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,}))$/, - }); - - const choices4 = new Choices('#choices-4', { - addItems: false, - removeItems: false, - }); - - const choices5 = new Choices('#choices-5', { - prependValue: 'item-', - appendValue: `-${Date.now()}`, - // callbackOnRender: function(items, options) { - // console.log(items); - // }, - }); - - choices5.removeAllItems(); - - const choices6 = new Choices('#choices-6', { - items: ['josh@joshuajohnson.co.uk', 'joe@bloggs.co.uk'], - // callbackOnRender: function(items, options, groups) { - // console.log(items); - // }, - }); - - const choices7 = new Choices('#choices-7', { - // callbackOnRender: function(items, options) { - // console.log(items); - // }, - }); - - const choicesMultiple = new Choices('[data-choice]', { - placeholderValue: 'This is a placeholder set in the config', - // callbackOnRender: function(items, options, groups) { - // console.log(options); - // }, - }); - - // choices6.addItem('josh2@joshuajohnson.co.uk', null, null, () => { console.log('Custom add item callback')}); - // choices6.removeItemsByValue('josh@joshuajohnson.co.uk'); - // console.log(choices6.getItemById(3)); -}); \ No newline at end of file +window.Choices = module.exports = Choices; \ No newline at end of file diff --git a/index.html b/index.html index d551df2..cb21683 100644 --- a/index.html +++ b/index.html @@ -70,5 +70,75 @@ + \ No newline at end of file