\n '):(0,c.strToEl)('\n \n ')},containerInner:function(){return(0,c.strToEl)('')},itemList:function(){return(0,c.strToEl)('')},item:function(i){return e.config.removeItemButton&&"select-one"!==e.passedElement.type?(0,c.strToEl)('\n \n '+i.label+'\n \n
\n '):(0,c.strToEl)('\n \n '+i.label+"\n
\n ")},choiceList:function(){return(0,c.strToEl)('')},choiceGroup:function(e){return(0,c.strToEl)('\n \n ")},choice:function(e){return e.groupId>0?(0,c.strToEl)('\n \n '+e.label+"\n
\n "):(0,c.strToEl)('\n \n '+e.label+"\n
\n ")},input:function(){return(0,c.strToEl)('')},dropdown:function(){return(0,c.strToEl)('')},notice:function(e,i){return(0,c.strToEl)(''+e+"
")},option:function(e){return(0,c.strToEl)('")}};this.config.templates=i}},{key:"_createInput",value:function(){var e=this,t=this._getTemplate("containerOuter"),i=this._getTemplate("containerInner"),n=this._getTemplate("itemList"),s=this._getTemplate("choiceList"),o=this._getTemplate("input"),r=this._getTemplate("dropdown");if(this.containerOuter=t,this.containerInner=i,this.input=o,this.choiceList=s,this.itemList=n,this.dropdown=r,this.passedElement.classList.add(this.config.classNames.input,this.config.classNames.hiddenState),this.passedElement.tabIndex="-1",this.passedElement.setAttribute("style","display:none;"),this.passedElement.setAttribute("aria-hidden","true"),this.passedElement.setAttribute("data-choice","active"),(0,c.wrap)(this.passedElement,i),(0,c.wrap)(i,t),this.config.placeholder&&(this.config.placeholderValue||this.passedElement.getAttribute("placeholder"))){var a=this.config.placeholderValue||this.passedElement.getAttribute("placeholder");o.placeholder=a,"select-one"!==this.passedElement.type&&(o.style.width=(0,c.getWidthOfInput)(o))}if(this.config.addItems||this.disable(),t.appendChild(i),t.appendChild(r),i.appendChild(n),r.appendChild(s),"select-multiple"===this.passedElement.type||"text"===this.passedElement.type?i.appendChild(o):this.config.search&&r.insertBefore(o,r.firstChild),"select-multiple"===this.passedElement.type||"select-one"===this.passedElement.type){var l=Array.from(this.passedElement.getElementsByTagName("OPTGROUP"));this.highlightPosition=0,this.isSearching=!1,l&&l.length?l.forEach(function(t,i){var n=0===i;e._addGroup(t,i,n)}):!function(){var t=Array.from(e.passedElement.options),i=[];t.forEach(function(e){i.push({value:e.value,label:e.innerHTML,selected:e.selected,disabled:e.disabled||e.parentNode.disabled})}),i.concat(e.presetChoices).forEach(function(t,i){0===i?e._addChoice(!0,!!t.disabled&&t.disabled,t.value,t.label):e._addChoice(!!t.selected&&t.selected,!!t.disabled&&t.disabled,t.value,t.label)})}()}else"text"===this.passedElement.type&&this.presetItems.forEach(function(t){if((0,c.isType)("Object",t)){if(!t.value)return;e._addItem(t.value,t.label,t.id)}else(0,c.isType)("String",t)&&e._addItem(t)})}},{key:"renderGroups",value:function(e,t,i){var n=this,s=i||document.createDocumentFragment(),o=this.config.sortFilter;return e.sort(o).forEach(function(e,i){var o=t.filter(function(t){return"select-one"===n.passedElement.type?t.groupId===e.id:t.groupId===e.id&&!t.selected});if(o.length>=1){var r=n._getTemplate("choiceGroup",e);s.appendChild(r),n.renderChoices(o,s)}}),s}},{key:"renderChoices",value:function(e,t){var i=this,n=t||document.createDocumentFragment(),s=this.isSearching?c.sortByScore:this.config.sortFilter;return e.sort(s).forEach(function(e,t){var s=i._getTemplate("choice",e);"select-one"===i.passedElement.type?n.appendChild(s):e.selected||n.appendChild(s)}),n}},{key:"renderItems",value:function(e,t){var i=this,n=t||document.createDocumentFragment(),s=this.store.getItemsReducedToValues(e);return"text"===this.passedElement.type?this.passedElement.setAttribute("value",s.join(this.config.delimiter)):!function(){var t=document.createDocumentFragment();e.forEach(function(e){var n=i._getTemplate("option",e);t.appendChild(n)}),i.passedElement.innerHTML="",i.passedElement.appendChild(t)}(),e.forEach(function(e){var t=i._getTemplate("item",e);n.appendChild(t)}),n}},{key:"render",value:function(){if(this.currentState=this.store.getState(),this.currentState!==this.prevState){if(!(this.currentState.choices===this.prevState.choices&&this.currentState.groups===this.prevState.groups||"select-multiple"!==this.passedElement.type&&"select-one"!==this.passedElement.type)){var e=this.store.getGroupsFilteredByActive(),t=this.store.getChoicesFilteredByActive(),i=document.createDocumentFragment();if(this.choiceList.innerHTML="",this.choiceList.scrollTop=0,e.length>=1&&this.isSearching!==!0?i=this.renderGroups(e,t,i):t.length>=1&&(i=this.renderChoices(t,i)),i.childNodes&&i.childNodes.length>0)this.choiceList.appendChild(i),this._highlightChoice();else{var n=this.isSearching?this._getTemplate("notice","No results found"):this._getTemplate("notice","No choices to choose from");this.choiceList.appendChild(n)}}if(this.currentState.items!==this.prevState.items){var s=this.store.getItemsFilteredByActive();if(s){var o=this.renderItems(s);this.itemList.innerHTML="",o.children&&o.children.length&&this.itemList.appendChild(o)}}this.prevState=this.currentState}}},{key:"_addEventListeners",value:function(){document.addEventListener("keyup",this._onKeyUp),document.addEventListener("keydown",this._onKeyDown),document.addEventListener("mousedown",this._onMouseDown),document.addEventListener("mouseover",this._onMouseOver),this.passedElement.type&&"select-one"===this.passedElement.type&&(this.containerOuter.addEventListener("focus",this._onFocus),this.containerOuter.addEventListener("blur",this._onBlur)),this.input.addEventListener("input",this._onInput),this.input.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("mousedown",this._onMouseDown),document.removeEventListener("mouseover",this._onMouseOver),this.passedElement.type&&"select-one"===this.passedElement.type&&(this.containerOuter.removeEventListener("focus",this._onFocus),this.containerOuter.removeEventListener("blur",this._onBlur)),this.input.removeEventListener("input",this._onInput),this.input.removeEventListener("paste",this._onPaste),this.input.removeEventListener("focus",this._onFocus),this.input.removeEventListener("blur",this._onBlur)}}]),e}();window.Choices=e.exports=p},function(e,t){"use strict";Array.from||(Array.from=function(){var e=Object.prototype.toString,t=function(t){return"function"==typeof t||"[object Function]"===e.call(t)},i=function(e){var t=Number(e);return isNaN(t)?0:0!==t&&isFinite(t)?(t>0?1:-1)*Math.floor(Math.abs(t)):t},n=Math.pow(2,53)-1,s=function(e){var t=i(e);return Math.min(Math.max(t,0),n)};return function(e){var i=this,n=Object(e);if(null==e)throw new TypeError("Array.from requires an array-like object - not null or undefined");var o,r=arguments.length>1?arguments[1]:void 0;if("undefined"!=typeof r){if(!t(r))throw new TypeError("Array.from: when provided, the second argument must be a function");arguments.length>2&&(o=arguments[2])}for(var a,c=s(n.length),l=t(i)?Object(new i(c)):new Array(c),u=0;u=0?t:0},t.getElementOffset=function(e,t){var i=t;return i>1&&(i=1),i>0&&(i=0),Math.max(e.offsetHeight*i)},t.getAdjacentEl=function(e,t){var i=arguments.length<=2||void 0===arguments[2]?1:arguments[2];if(e&&t){var n=e.parentNode.parentNode,s=Array.from(n.querySelectorAll(t)),o=s.indexOf(e),r=i>0?1:-1;return s[o+r]}},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,i){return this.getScrollPosition(t)>this.getElemDistance(e)+this.getElementOffset(e,i)},t.isScrolledIntoView=function(e,t){var i=arguments.length<=2||void 0===arguments[2]?1:arguments[2];if(e){var n=void 0;return n=i>0?t.scrollTop+t.offsetHeight>=e.offsetTop+e.offsetHeight:e.offsetTop>=t.scrollTop}},t.stripHTML=function(e){var t=document.createElement("DIV");return t.innerHTML=e,t.textContent||t.innerText||""},t.addAnimation=function(e,t){var i=s(),n=function o(){e.classList.remove(t),e.removeEventListener(i,o,!1)};e.classList.add(t),e.addEventListener(i,n,!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 i;for(e.innerHTML=t,i=e.children[0];e.firstChild;)e.removeChild(e.firstChild);return i}}());t.getWidthOfInput=function(e){var t=e.value||e.placeholder,i=e.offsetWidth;if(t){var n=o(""+t+"");n.style.position="absolute",n.style.padding="0",n.style.top="-9999px",n.style.left="-9999px",n.style.width="auto",n.style.whiteSpace="pre",document.body.appendChild(n),t&&n.offsetWidth!==e.offsetWidth&&(i=n.offsetWidth+4),document.body.removeChild(n)}return i+"px"},t.sortByAlpha=function(e,t){var i=(e.label||e.value).toLowerCase(),n=(t.label||t.value).toLowerCase();return in?1:0},t.sortByScore=function(e,t){return e.score-t.score}},function(e,t,i){!function(t){"use strict";function i(){console.log.apply(console,arguments)}function n(e,t){var i,n,s,o;for(this.list=e,this.options=t=t||{},i=0,o=["sort","shouldSort","verbose","tokenize"],n=o.length;i1)throw new Error("Key weight has to be > 0 and <= 1");e=e.name}else this._keyMap[e]={weight:1};this._analyze(e,o(u,e,[]),u,i)}},n.prototype._analyze=function(e,t,n,s){var r,a,c,l,u,h,d,p,f,v,m,g,y,b=this.options,_=!1;if(void 0!==t&&null!==t)if(a=[],"string"==typeof t){if(r=t.split(b.tokenSeparator),b.verbose&&i("---------\nKey:",e),this.options.tokenize){for(g=0;g0){if(t={item:r.item},u.indexOf("matches")!==-1)for(n=r.output,t.matches=[],i=0;iI.maxPatternLength){if(g=e.match(new RegExp(this.pattern.replace(I.tokenSeparator,"|"))),y=!!g)for(_=[],t=0,E=g.length;t=u;i--)if(f=this.patternAlphabet[e.charAt(i-1)],f&&(b[i-1]=1),0===t?d[i]=(d[i+1]<<1|1)&f:d[i]=(d[i+1]<<1|1)&f|((p[i+1]|p[i])<<1|1)|p[i+1],d[i]&this.matchmask&&(v=this._bitapScore(t,i-1),v<=o)){if(o=v,r=i-1,m.push(r),!(r>s))break;u=Math.max(1,2*s-r)}if(this._bitapScore(t+1,s)>o)break;p=d}return _=this._getMatchedIndices(b),{isMatch:r>=0,score:0===v?.001:v,matchedIndices:_}},r.prototype._getMatchedIndices=function(e){for(var t,i=[],n=-1,s=-1,o=0,r=e.length;o-1&&(n=e.map(function(e){return e.id===parseInt(t.choiceId)&&(e.selected=!0),e})),{v:n};case"REMOVE_ITEM":return t.choiceId>-1?{v:e.map(function(e){return e.id===parseInt(t.choiceId)&&(e.selected=!1),e})}:{v:e};case"FILTER_CHOICES":var s=t.results,o=e.map(function(e,t){return e.active=s.some(function(t){if(t.item.id===e.id)return e.score=t.score,!0}),e});return{v:o};case"ACTIVATE_CHOICES":return{v:e.map(function(e){return e.active=t.active,e})};default:return{v:e}}}();if("object"===("undefined"==typeof s?"undefined":n(s)))return s.v};t.default=s}]);
\ No newline at end of file
+!function(e){function t(n){if(i[n])return i[n].exports;var s=i[n]={exports:{},id:n,loaded:!1};return e[n].call(s.exports,s,s.exports,t),s.loaded=!0,s.exports}var i={};return t.m=e,t.c=i,t.p="/assets/scripts/dist/",t(0)}([function(e,t,i){e.exports=i(1)},function(e,t,i){"use strict";function n(e){return e&&e.__esModule?e:{"default":e}}function s(e){if(Array.isArray(e)){for(var t=0,i=Array(e.length);t1)for(var o=1;o=document.body.offsetHeight;return t?this.containerOuter.classList.add(this.config.classNames.flippedState):this.containerOuter.classList.remove(this.config.classNames.flippedState),this}},{key:"hideDropdown",value:function(){var e=this.containerOuter.classList.contains(this.config.classNames.flippedState);return this.containerOuter.classList.remove(this.config.classNames.openState),this.containerOuter.setAttribute("aria-expanded","false"),this.dropdown.classList.remove(this.config.classNames.activeState),e&&this.containerOuter.classList.remove(this.config.classNames.flippedState),this}},{key:"toggleDropdown",value:function(){return this.dropdown.classList.contains(this.config.classNames.activeState)?this.hideDropdown():this.showDropdown(),this}},{key:"getValue",value:function(){var e=this,t=!(arguments.length<=0||void 0===arguments[0])&&arguments[0],i=this.store.getItemsFilteredByActive(),n=[];return i.forEach(function(i){"text"===e.passedElement.type?n.push(t?i.value:i):i.active&&n.push(t?i.value:i)}),"select-one"==this.passedElement.type?n[0]:n}},{key:"setValue",value:function(e){var t=this;if(this.initialised===!0){var i=[].concat(s(e));i.forEach(function(e,i){if((0,c.isType)("Object",e)){if(!e.value)return;"text"!==t.passedElement.type?t._addChoice(!0,!1,e.value,e.label,-1):t._addItem(e.value,e.label,e.id)}else(0,c.isType)("String",e)&&("text"!==t.passedElement.type?t._addChoice(!0,!1,e,e,-1):t._addItem(e))})}return this}},{key:"setValueByChoice",value:function(e){var t=this;return"text"!==this.passedElement.type&&!function(){var i=t.store.getChoices();(0,c.isType)("Array",e)||(e=[e]),e.forEach(function(e,n){var s=i.find(function(t){return t.value===e});s?s.selected?console.warn("Attempting to select choice already selected"):t._addItem(s.value,s.label,s.id):console.warn("Attempting to select choice that does not exist")})}(),this}},{key:"setChoices",value:function(e,t,i){var n=this;if(this.initialised===!0&&("select-one"===this.passedElement.type||"select-multiple"===this.passedElement.type)){if(!(0,c.isType)("Array",e)||!t)return;e&&e.length&&(this.containerOuter.classList.remove(this.config.classNames.loadingState),e.forEach(function(e,s){if(e.choices){var r=0===s;n._addGroup(e,s,r)}else n._addChoice(!!e.selected&&e.selected,!!e.disabled&&e.disabled,e[t],e[i])}))}return this}},{key:"clearStore",value:function(){return this.store.dispatch((0,a.clearAll)()),this}},{key:"disable",value:function(){return this.passedElement.disabled=!0,this.initialised&&(this.containerOuter.classList.contains(this.config.classNames.disabledState)||(this.input.disabled=!0,this.containerOuter.classList.add(this.config.classNames.disabledState),this.containerOuter.setAttribute("aria-disabled","true"))),this}},{key:"enable",value:function(){return this.passedElement.disabled=!1,this.initialised&&this.containerOuter.classList.contains(this.config.classNames.disabledState)&&(this.input.disabled=!1,this.containerOuter.classList.remove(this.config.classNames.disabledState),this.containerOuter.removeAttribute("aria-disabled")),this}},{key:"ajax",value:function(e){var t=this;if(this.initialised===!0&&("select-one"===this.passedElement.type||"select-multiple"===this.passedElement.type)){if(this.containerOuter.classList.add(this.config.classNames.loadingState),this.containerOuter.setAttribute("aria-busy","true"),"select-one"===this.passedElement.type){var i=this._getTemplate("item",{id:-1,value:"Loading",label:this.config.loadingText,active:!0});this.itemList.appendChild(i)}var n=function(e,i,n){if((0,c.isType)("Array",e)&&i){if(e&&e.length){t.containerOuter.classList.remove(t.config.classNames.loadingState);t.config.sortFilter;e.forEach(function(e,s){0===s&&"select-one"===t.passedElement.type?t._addChoice(!0,!1,e[i],e[n]):t._addChoice(!1,!1,e[i],e[n])})}t.containerOuter.removeAttribute("aria-busy")}};e(n)}return this}},{key:"clearInput",value:function(){return this.input.value&&(this.input.value=""),"select-one"!==this.passedElement.type&&(this.input.style.width=(0,c.getWidthOfInput)(this.input)),this}},{key:"_triggerChange",value:function(e){if(e){if(this.config.callbackOnChange){var t=this.config.callbackOnChange;(0,c.isType)("Function",t)?t(e,this.passedElement):console.error("callbackOnChange: Callback is not a function")}"select-one"===this.passedElement.type&&(this._focusAndHideDropdown=!0,this.containerOuter.focus())}}},{key:"_handleEnter",value:function(e,t){var i=!0;if(this.config.addItems?this.config.maxItemCount&&this.config.maxItemCount>0&&this.config.maxItemCount<=this.itemList.children.length?i=!1:this.config.duplicateItems===!1&&this.passedElement.value&&(i=!e.some(function(e){return e.value===t})):i=!1,i){var n=!0;this.config.regexFilter&&(n=this._regexFilter(t)),n&&(this.toggleDropdown(),this._addItem(t),this._triggerChange(t),this.clearInput(this.passedElement))}}},{key:"_handleBackspace",value:function(e){if(this.config.removeItems&&e){var t=e[e.length-1],i=e.some(function(e){return e.highlighted===!0});this.config.editItems&&!i&&t?(this.input.value=t.value,this._removeItem(t),this._triggerChange(t.value)):(i||this.highlightItem(t),this.removeHighlightedItems())}}},{key:"_onKeyDown",value:function(e){if(e.target===this.input||e.target===this.containerOuter){var t=e.ctrlKey||e.metaKey,i=46,n=8,s=13,r=65,o=27,l=38,u=40,h=this.store.getItemsFilteredByActive(),d=(this.store.getChoicesFilteredByActive(),this.input===document.activeElement),p=this.dropdown.classList.contains(this.config.classNames.activeState),f=this.itemList&&this.itemList.children,v=String.fromCharCode(e.keyCode);switch("text"!==this.passedElement.type&&/[a-zA-Z0-9-_ ]/.test(v)&&!p&&this.showDropdown(),this.canSearch=this.config.search,e.keyCode){case r:t&&f&&(this.canSearch=!1,this.config.removeItems&&!this.input.value&&this.input===document.activeElement&&this.highlightAll(this.itemList.children));break;case s:if(e.target.value&&"text"===this.passedElement.type){var m=this.input.value;this._handleEnter(h,m)}if(p||"select-one"!==this.passedElement.type||(e.preventDefault(),this.showDropdown(),this.canSearch&&this.input.focus()),p){var g=this.dropdown.querySelector("."+this.config.classNames.highlightedState);if(g){var y=g.getAttribute("data-value"),b=g.innerHTML,_=g.getAttribute("data-id");this._addItem(y,b,_),this._triggerChange(y),this.clearInput(this.passedElement),"select-one"===this.passedElement.type&&(this.isSearching=!1,this.store.dispatch((0,a.activateChoices)()),this.toggleDropdown())}}break;case o:p&&this.toggleDropdown();break;case u:case l:if(p||"select-one"===this.passedElement.type){p||(this.showDropdown(),this.canSearch&&this.input.focus());var E=this.dropdown.querySelector("."+this.config.classNames.highlightedState),w=e.keyCode===u?1:-1,I=void 0;this.canSearch=!1,I=E?(0,c.getAdjacentEl)(E,"[data-option-selectable]",w):this.dropdown.querySelector("[data-option-selectable]"),I&&((0,c.isScrolledIntoView)(I,this.choiceList,w)||this._scrollToChoice(I,w),this._highlightChoice(I)),e.preventDefault()}break;case i:case n:d&&!e.target.value&&"select-one"!==this.passedElement.type&&(this._handleBackspace(h),e.preventDefault())}}}},{key:"_onKeyUp",value:function(e){var t=this;if(e.target===this.input){String.fromCharCode(e.keyCode);if("text"===this.passedElement.type){var i=this.dropdown.classList.contains(this.config.classNames.activeState),n=void 0;if(this.input.value){var s=this.store.getItemsFilteredByActive(),r=!s.some(function(e){return e.value===t.input.value});n=this.config.maxItemCount&&this.config.maxItemCount>0&&this.config.maxItemCount<=this.itemList.children.length?this._getTemplate("notice","Only "+this.config.maxItemCount+" options can be added."):this.config.duplicateItems||r?this._getTemplate("notice",'Add "'+this.input.value+'"'):this._getTemplate("notice","Only unique values can be added."),(this.config.regexFilter&&this._regexFilter(this.input.value)||!this.config.regexFilter)&&(this.dropdown.innerHTML=n.outerHTML,this.dropdown.classList.contains(this.config.classNames.activeState)||this.showDropdown())}else i&&this.hideDropdown()}if(this.canSearch&&this.input===document.activeElement){var o=this.store.getChoices(),l=o.some(function(e){return e.active!==!0});if(this.input.value&&this.input.value.length>1){var h=function(){var e=(0,c.isType)("String",t.input.value)?t.input.value.trim():t.input.value,i=(0,c.isType)("String",t.currentValue)?t.currentValue.trim():t.currentValue;if(e.length>=1&&e!==i+" "){var n=t.store.getChoicesFiltedBySelectable(),s=e,r=(0,c.isType)("Array",t.config.sortFields)?t.config.sortFields:[t.config.sortFields],o=new u.default(n,{keys:r,shouldSort:!0,include:"score"}),l=o.search(s);t.currentValue=e,t.highlightPosition=0,t.isSearching=!0,t.store.dispatch((0,a.filterChoices)(l))}};h()}else l&&(this.isSearching=!1,this.store.dispatch((0,a.activateChoices)(!0)))}}}},{key:"_onInput",value:function(e){"select-one"!==this.passedElement.type&&(this.input.style.width=(0,c.getWidthOfInput)(this.input))}},{key:"_onMouseDown",value:function(e){var t=this,i=this.store.getItemsFilteredByActive(),n=e.target||e.touches[0].target;if(this.containerOuter.contains(n))!function(){e.preventDefault();var s=!!e.shiftKey;t.dropdown.classList.contains(t.config.classNames.activeState)?"text"!==t.passedElement.type&&t.dropdown.classList.contains(t.config.classNames.activeState)&&e.target===t.containerInner&&t.hideDropdown():"text"!==t.passedElement.type?(t.showDropdown(),("select-multiple"===t.passedElement.type||t.canSearch)&&t.input.focus()):t.input!==document.activeElement&&t.input.focus(),n.hasAttribute("data-button")?t.config.removeItems&&t.config.removeItemButton&&!function(){var e=n.parentNode.getAttribute("data-id"),s=i.find(function(t){return t.id===parseInt(e)});t._removeItem(s),t._triggerChange(s.value)}():n.hasAttribute("data-item")?t.config.removeItems&&!function(){var e=n.getAttribute("data-id");i.forEach(function(i){i.id!==parseInt(e)||i.highlighted?s||t.unhighlightItem(i):t.highlightItem(i)})}():n.hasAttribute("data-option")&&!function(){var e=n.getAttribute("data-id"),i=t.store.getChoicesFilteredByActive(),s=i.find(function(t){return t.id===parseInt(e)});s.selected||s.disabled||(t._addItem(s.value,s.label,s.id),t._triggerChange(s.value),"select-one"===t.passedElement.type&&(t.input.value="",t.isSearching=!1,t.store.dispatch((0,a.activateChoices)(!0)),t.toggleDropdown()))}()}();else{var s=this.dropdown.classList.contains(this.config.classNames.activeState),r=i.some(function(e){return e.highlighted===!0});r&&this.unhighlightAll(),this.containerOuter.classList.remove(this.config.classNames.focusState),s&&this.toggleDropdown()}}},{key:"_onMouseOver",value:function(e){(e.target===this.dropdown||(0,c.findAncestor)(e.target,this.config.classNames.listDropdown))&&e.target.hasAttribute("data-option")&&this._highlightChoice(e.target)}},{key:"_onPaste",value:function(e){e.target===this.input&&(this.config.paste||e.preventDefault())}},{key:"_onFocus",value:function(e){var t=this.dropdown.classList.contains(this.config.classNames.activeState);e.target!==this.input||t?"select-one"!==this.passedElement.type||e.target!==this.containerOuter||t||(this.containerOuter.classList.add(this.config.classNames.focusState),this.showDropdown(),this._focusAndHideDropdown||this.input.focus(),this._focusAndHideDropdown=!1):(this.containerOuter.classList.add(this.config.classNames.focusState),"select-one"!==this.passedElement.type&&"select-multiple"!==this.passedElement.type||this.showDropdown())}},{key:"_onBlur",value:function(e){var t=this.dropdown.classList.contains(this.config.classNames.activeState);e.target!==this.input&&e.target!==this.containerOuter||(this.containerOuter.classList.remove(this.config.classNames.focusState),t&&(e.target===this.input||e.target===this.containerOuter&&!this.config.search)&&this.hideDropdown())}},{key:"_regexFilter",value:function(e){if(e){var t=this.config.regexFilter,i=new RegExp(t.source,"i");return i.test(e)}}},{key:"_scrollToChoice",value:function(e,t){var i=this;if(e){var n=this.choiceList.offsetHeight,s=e.offsetHeight,r=e.offsetTop+s,o=this.choiceList.scrollTop+n,a=t>0?this.choiceList.scrollTop+r-o:e.offsetTop,c=function l(e,t,n){var s=!1,r=void 0,o=void 0,a=4;n>0?(r=(t-i.choiceList.scrollTop)/a,o=r>1?r:1,i.choiceList.scrollTop=i.choiceList.scrollTop+o,i.choiceList.scrollTop1?r:1,i.choiceList.scrollTop=i.choiceList.scrollTop-o,i.choiceList.scrollTop>t&&(s=!0)),s&&requestAnimationFrame(function(e){l(e,t,n)})};requestAnimationFrame(function(e){c(e,a,t)})}}},{key:"_highlightChoice",value:function(e){var t=this,i=Array.from(this.dropdown.querySelectorAll("[data-option-selectable]"));if(i&&i.length){var n=Array.from(this.dropdown.querySelectorAll("."+this.config.classNames.highlightedState));if(n.forEach(function(e){e.classList.remove(t.config.classNames.highlightedState),e.setAttribute("aria-selected","false")}),e)e.classList.add(this.config.classNames.highlightedState),this.highlightPosition=i.indexOf(e);else{var s=void 0;s=i.length>this.highlightPosition?i[this.highlightPosition]:i[i.length-1],s||(s=i[0]),s.classList.add(this.config.classNames.highlightedState),s.setAttribute("aria-selected","true")}}}},{key:"_addItem",value:function(e,t){var i=arguments.length<=2||void 0===arguments[2]?-1:arguments[2],n=this.store.getItems(),s=(0,c.isType)("String",e)?e.trim():e,r=t||s,o=parseInt(i)||-1;this.config.prependValue&&(s=this.config.prependValue+s.toString()),this.config.appendValue&&(s+=this.config.appendValue.toString());var l=n?n.length+1:1;if(this.store.dispatch((0,a.addItem)(s,r,l,o)),"select-one"===this.passedElement.type&&this.removeActiveItems(l),this.config.callbackOnAddItem){var u=this.config.callbackOnAddItem;(0,c.isType)("Function",u)?u(l,s,this.passedElement):console.error("callbackOnAddItem: Callback is not a function")}return this}},{key:"_removeItem",value:function(e){var t=arguments.length<=1||void 0===arguments[1]?this.config.callbackOnRemoveItem:arguments[1];if(!e||!(0,c.isType)("Object",e))return void console.error("removeItem: No item object was passed to be removed");var i=e.id,n=e.value,s=e.choiceId;if(this.store.dispatch((0,a.removeItem)(i,s)),t){if(!(0,c.isType)("Function",t))return void console.error("callbackOnRemoveItem: Callback is not a function");t(i,n,this.passedElement)}return this}},{key:"_addChoice",value:function(e,t,i,n){var s=arguments.length<=4||void 0===arguments[4]?-1:arguments[4];if(i){n||(n=i);var r=this.store.getChoices(),o=r?r.length+1:1;this.store.dispatch((0,a.addChoice)(i,n,o,s,t)),e&&!t&&this._addItem(i,n,o)}}},{key:"_addGroup",value:function(e,t,i){var n=this,s=(0,c.isType)("Object",e)?e.choices:Array.from(e.getElementsByTagName("OPTION")),r=t,o=!!e.disabled&&e.disabled;s?(this.store.dispatch((0,a.addGroup)(e.label,r,!0,o)),s.forEach(function(e,t){var i=e.disabled||e.parentNode&&e.parentNode.disabled||!1,s=!!e.selected&&e.selected,o=void 0;o=(0,c.isType)("Object",e)?e.label||e.value:e.innerHTML,n._addChoice(s,i,e.value,o,r)})):this.store.dispatch((0,a.addGroup)(e.label,e.id,!1,e.disabled))}},{key:"_getTemplate",value:function(e){if(e){for(var t=this.config.templates,i=arguments.length,n=Array(i>1?i-1:0),s=1;s\n '):(0,c.strToEl)('\n \n ')},containerInner:function(){return(0,c.strToEl)('')},itemList:function(){return(0,c.strToEl)('')},item:function(i){return e.config.removeItemButton&&"select-one"!==e.passedElement.type?(0,c.strToEl)('\n \n '+i.label+'\n \n
\n '):(0,c.strToEl)('\n \n '+i.label+"\n
\n ")},choiceList:function(){return(0,c.strToEl)('')},choiceGroup:function(e){return(0,c.strToEl)('\n \n ")},choice:function(e){return e.groupId>0?(0,c.strToEl)('\n \n '+e.label+"\n
\n "):(0,c.strToEl)('\n \n '+e.label+"\n
\n ")},input:function(){return(0,c.strToEl)('')},dropdown:function(){return(0,c.strToEl)('')},notice:function(e,i){return(0,c.strToEl)(''+e+"
")},option:function(e){return(0,c.strToEl)('")}};this.config.templates=i}},{key:"_createInput",value:function(){var e=this,t=this._getTemplate("containerOuter"),i=this._getTemplate("containerInner"),n=this._getTemplate("itemList"),s=this._getTemplate("choiceList"),r=this._getTemplate("input"),o=this._getTemplate("dropdown");if(this.containerOuter=t,this.containerInner=i,this.input=r,this.choiceList=s,this.itemList=n,this.dropdown=o,this.passedElement.classList.add(this.config.classNames.input,this.config.classNames.hiddenState),this.passedElement.tabIndex="-1",this.passedElement.setAttribute("style","display:none;"),this.passedElement.setAttribute("aria-hidden","true"),this.passedElement.setAttribute("data-choice","active"),(0,c.wrap)(this.passedElement,i),(0,c.wrap)(i,t),this.config.placeholder&&(this.config.placeholderValue||this.passedElement.getAttribute("placeholder"))){var a=this.config.placeholderValue||this.passedElement.getAttribute("placeholder");r.placeholder=a,"select-one"!==this.passedElement.type&&(r.style.width=(0,c.getWidthOfInput)(r))}if(this.config.addItems||this.disable(),t.appendChild(i),t.appendChild(o),i.appendChild(n),o.appendChild(s),"select-multiple"===this.passedElement.type||"text"===this.passedElement.type?i.appendChild(r):this.config.search&&o.insertBefore(r,o.firstChild),"select-multiple"===this.passedElement.type||"select-one"===this.passedElement.type){var l=Array.from(this.passedElement.getElementsByTagName("OPTGROUP"));this.highlightPosition=0,this.isSearching=!1,l&&l.length?l.forEach(function(t,i){var n=0===i;e._addGroup(t,i,n)}):!function(){var t=Array.from(e.passedElement.options),i=[];t.forEach(function(e){i.push({value:e.value,label:e.innerHTML,selected:e.selected,disabled:e.disabled||e.parentNode.disabled})}),i.concat(e.presetChoices).forEach(function(t,i){0===i?e._addChoice(!0,!!t.disabled&&t.disabled,t.value,t.label):e._addChoice(!!t.selected&&t.selected,!!t.disabled&&t.disabled,t.value,t.label)})}()}else"text"===this.passedElement.type&&this.presetItems.forEach(function(t){if((0,c.isType)("Object",t)){if(!t.value)return;e._addItem(t.value,t.label,t.id)}else(0,c.isType)("String",t)&&e._addItem(t)})}},{key:"renderGroups",value:function(e,t,i){var n=this,s=i||document.createDocumentFragment(),r=this.config.sortFilter;return e.sort(r).forEach(function(e,i){var r=t.filter(function(t){return"select-one"===n.passedElement.type?t.groupId===e.id:t.groupId===e.id&&!t.selected});if(r.length>=1){var o=n._getTemplate("choiceGroup",e);s.appendChild(o),n.renderChoices(r,s)}}),s}},{key:"renderChoices",value:function(e,t){var i=this,n=t||document.createDocumentFragment(),s=this.isSearching?c.sortByScore:this.config.sortFilter;return e.sort(s).forEach(function(e,t){var s=i._getTemplate("choice",e);"select-one"===i.passedElement.type?n.appendChild(s):e.selected||n.appendChild(s)}),n}},{key:"renderItems",value:function(e,t){var i=this,n=t||document.createDocumentFragment(),s=this.store.getItemsReducedToValues(e);return"text"===this.passedElement.type?this.passedElement.setAttribute("value",s.join(this.config.delimiter)):!function(){var t=document.createDocumentFragment();e.forEach(function(e){var n=i._getTemplate("option",e);t.appendChild(n)}),i.passedElement.innerHTML="",i.passedElement.appendChild(t)}(),e.forEach(function(e){var t=i._getTemplate("item",e);n.appendChild(t)}),n}},{key:"render",value:function(){if(this.currentState=this.store.getState(),this.currentState!==this.prevState){if(!(this.currentState.choices===this.prevState.choices&&this.currentState.groups===this.prevState.groups||"select-multiple"!==this.passedElement.type&&"select-one"!==this.passedElement.type)){var e=this.store.getGroupsFilteredByActive(),t=this.store.getChoicesFilteredByActive(),i=document.createDocumentFragment();if(this.choiceList.innerHTML="",this.choiceList.scrollTop=0,e.length>=1&&this.isSearching!==!0?i=this.renderGroups(e,t,i):t.length>=1&&(i=this.renderChoices(t,i)),i.childNodes&&i.childNodes.length>0)this.choiceList.appendChild(i),this._highlightChoice();else{var n=this.isSearching?this._getTemplate("notice","No results found"):this._getTemplate("notice","No choices to choose from");this.choiceList.appendChild(n)}}if(this.currentState.items!==this.prevState.items){var s=this.store.getItemsFilteredByActive();if(s){var r=this.renderItems(s);this.itemList.innerHTML="",r.children&&r.children.length&&this.itemList.appendChild(r)}}this.prevState=this.currentState}}},{key:"_addEventListeners",value:function(){document.addEventListener("keyup",this._onKeyUp),document.addEventListener("keydown",this._onKeyDown),document.addEventListener("mousedown",this._onMouseDown),document.addEventListener("mouseover",this._onMouseOver),this.passedElement.type&&"select-one"===this.passedElement.type&&(this.containerOuter.addEventListener("focus",this._onFocus),this.containerOuter.addEventListener("blur",this._onBlur)),this.input.addEventListener("input",this._onInput),this.input.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("mousedown",this._onMouseDown),document.removeEventListener("mouseover",this._onMouseOver),this.passedElement.type&&"select-one"===this.passedElement.type&&(this.containerOuter.removeEventListener("focus",this._onFocus),this.containerOuter.removeEventListener("blur",this._onBlur)),this.input.removeEventListener("input",this._onInput),this.input.removeEventListener("paste",this._onPaste),this.input.removeEventListener("focus",this._onFocus),this.input.removeEventListener("blur",this._onBlur)}}]),e}();window.Choices=e.exports=p},function(e,t){"use strict";Array.from||(Array.from=function(){var e=Object.prototype.toString,t=function(t){return"function"==typeof t||"[object Function]"===e.call(t)},i=function(e){var t=Number(e);return isNaN(t)?0:0!==t&&isFinite(t)?(t>0?1:-1)*Math.floor(Math.abs(t)):t},n=Math.pow(2,53)-1,s=function(e){var t=i(e);return Math.min(Math.max(t,0),n)};return function(e){var i=this,n=Object(e);if(null==e)throw new TypeError("Array.from requires an array-like object - not null or undefined");var r,o=arguments.length>1?arguments[1]:void 0;if("undefined"!=typeof o){if(!t(o))throw new TypeError("Array.from: when provided, the second argument must be a function");arguments.length>2&&(r=arguments[2])}for(var a,c=s(n.length),l=t(i)?Object(new i(c)):new Array(c),u=0;u>>0,s=arguments[1],r=0;r=0?t:0},t.getElementOffset=function(e,t){var i=t;return i>1&&(i=1),i>0&&(i=0),Math.max(e.offsetHeight*i)},t.getAdjacentEl=function(e,t){var i=arguments.length<=2||void 0===arguments[2]?1:arguments[2];if(e&&t){var n=e.parentNode.parentNode,s=Array.from(n.querySelectorAll(t)),r=s.indexOf(e),o=i>0?1:-1;return s[r+o]}},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,i){return this.getScrollPosition(t)>this.getElemDistance(e)+this.getElementOffset(e,i)},t.isScrolledIntoView=function(e,t){var i=arguments.length<=2||void 0===arguments[2]?1:arguments[2];if(e){var n=void 0;return n=i>0?t.scrollTop+t.offsetHeight>=e.offsetTop+e.offsetHeight:e.offsetTop>=t.scrollTop}},t.stripHTML=function(e){var t=document.createElement("DIV");return t.innerHTML=e,t.textContent||t.innerText||""},t.addAnimation=function(e,t){var i=s(),n=function r(){e.classList.remove(t),e.removeEventListener(i,r,!1)};e.classList.add(t),e.addEventListener(i,n,!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 i;for(e.innerHTML=t,i=e.children[0];e.firstChild;)e.removeChild(e.firstChild);return i}}());t.getWidthOfInput=function(e){var t=e.value||e.placeholder,i=e.offsetWidth;if(t){var n=r(""+t+"");n.style.position="absolute",n.style.padding="0",n.style.top="-9999px",n.style.left="-9999px",n.style.width="auto",n.style.whiteSpace="pre",document.body.appendChild(n),t&&n.offsetWidth!==e.offsetWidth&&(i=n.offsetWidth+4),document.body.removeChild(n)}return i+"px"},t.sortByAlpha=function(e,t){var i=(e.label||e.value).toLowerCase(),n=(t.label||t.value).toLowerCase();return in?1:0},t.sortByScore=function(e,t){return e.score-t.score}},function(e,t,i){!function(t){"use strict";function i(){console.log.apply(console,arguments)}function n(e,t){var i,n,s,r;for(this.list=e,this.options=t=t||{},i=0,r=["sort","shouldSort","verbose","tokenize"],n=r.length;i1)throw new Error("Key weight has to be > 0 and <= 1");e=e.name}else this._keyMap[e]={weight:1};this._analyze(e,r(u,e,[]),u,i)}},n.prototype._analyze=function(e,t,n,s){var o,a,c,l,u,h,d,p,f,v,m,g,y,b=this.options,_=!1;if(void 0!==t&&null!==t)if(a=[],"string"==typeof t){if(o=t.split(b.tokenSeparator),b.verbose&&i("---------\nKey:",e),this.options.tokenize){for(g=0;g0){if(t={item:o.item},u.indexOf("matches")!==-1)for(n=o.output,t.matches=[],i=0;iI.maxPatternLength){if(g=e.match(new RegExp(this.pattern.replace(I.tokenSeparator,"|"))),y=!!g)for(_=[],t=0,E=g.length;t=u;i--)if(f=this.patternAlphabet[e.charAt(i-1)],f&&(b[i-1]=1),0===t?d[i]=(d[i+1]<<1|1)&f:d[i]=(d[i+1]<<1|1)&f|((p[i+1]|p[i])<<1|1)|p[i+1],d[i]&this.matchmask&&(v=this._bitapScore(t,i-1),v<=r)){if(r=v,o=i-1,m.push(o),!(o>s))break;u=Math.max(1,2*s-o)}if(this._bitapScore(t+1,s)>r)break;p=d}return _=this._getMatchedIndices(b),{isMatch:o>=0,score:0===v?.001:v,matchedIndices:_}},o.prototype._getMatchedIndices=function(e){for(var t,i=[],n=-1,s=-1,r=0,o=e.length;r-1&&(n=e.map(function(e){return e.id===parseInt(t.choiceId)&&(e.selected=!0),e})),{v:n};case"REMOVE_ITEM":return t.choiceId>-1?{v:e.map(function(e){return e.id===parseInt(t.choiceId)&&(e.selected=!1),e})}:{v:e};case"FILTER_CHOICES":var s=t.results,r=e.map(function(e,t){return e.active=s.some(function(t){if(t.item.id===e.id)return e.score=t.score,!0}),e});return{v:r};case"ACTIVATE_CHOICES":return{v:e.map(function(e){return e.active=t.active,e})};default:return{v:e}}}();if("object"===("undefined"==typeof s?"undefined":n(s)))return s.v};t.default=s}]);
\ No newline at end of file
diff --git a/assets/scripts/src/choices.js b/assets/scripts/src/choices.js
index 25de418..111d814 100644
--- a/assets/scripts/src/choices.js
+++ b/assets/scripts/src/choices.js
@@ -261,7 +261,10 @@ export class Choices {
* @public
*/
removeItemsByValue(value) {
- if(!value || !isType('String', value)) console.error('removeItemsByValue: No value was passed to be removed'); return;
+ if(!value || !isType('String', value)) {
+ console.error('removeItemsByValue: No value was passed to be removed');
+ return;
+ }
const items = this.store.getItemsFilteredByActive();
@@ -276,7 +279,7 @@ export class Choices {
/**
* Remove all items from store array
- * Note: removed items are soft deleted
+ * @note Removed items are soft deleted
* @param {Number} excludedId Optionally exclude item by ID
* @return {Object} Class instance
* @public
@@ -295,7 +298,7 @@ export class Choices {
/**
* Remove all selected items from store
- * Note: removed items are soft deleted
+ * @note Removed items are soft deleted
* @return {Object} Class instance
* @public
*/
@@ -322,7 +325,6 @@ export class Choices {
this.dropdown.classList.add(this.config.classNames.activeState);
-
const dimensions = this.dropdown.getBoundingClientRect();
const shouldFlip = this.config.flip ? dimensions.top + dimensions.height >= document.body.offsetHeight : false;
@@ -450,13 +452,10 @@ export class Choices {
// Loop through each value and
value.forEach((val, index) => {
+
const foundChoice = choices.find((choice) => {
// Check 'value' property exists and the choice isn't already selected
- if(choice.value === val) {
- return true;
- } else {
- return false;
- }
+ return choice.value === val;
});
if(foundChoice) {
@@ -503,11 +502,12 @@ export class Choices {
}
/**
- * Clear value of inputs
+ * Clear items,choices and groups
+ * @note Hard delete
* @return {Object} Class instance
* @public
*/
- clearValue() {
+ clearStore() {
this.store.dispatch(clearAll());
return this;
}
@@ -1218,6 +1218,7 @@ export class Choices {
/**
* Add item to store with correct value
* @param {String} value Value to add to store
+ * @param {String} label Label to add to store
* @return {Object} Class instance
* @public
*/
@@ -1261,7 +1262,8 @@ export class Choices {
/**
* Remove item from store
- * @param
+ * @param {Object} item Item to remove
+ * @param {Function} callback Callback to trigger
* @return {Object} Class instance
* @public
*/
@@ -1279,7 +1281,10 @@ export class Choices {
// Run callback
if(callback){
- if(!isType('Function', callback)) console.error('callbackOnRemoveItem: Callback is not a function'); return;
+ if(!isType('Function', callback)) {
+ console.error('callbackOnRemoveItem: Callback is not a function');
+ return;
+ }
callback(id, value, this.passedElement);
}
@@ -1287,7 +1292,12 @@ export class Choices {
}
/**
- * Add choice to dropdoww
+ * Add choice to dropdown
+ * @param {Boolean} isSelected Whether choice is selected
+ * @param {Boolean} isDisabled Whether choice is disabled
+ * @param {String} value Value of choice
+ * @param {String} Label Label of choice
+ * @param {Number} groupId ID of group choice is within. Negative number indicates no group
* @return
* @private
*/
@@ -1310,7 +1320,8 @@ export class Choices {
/**
* Add group to dropdown
* @param {Object} group Group to add
- * @param {Number} index Whether this is the first group to add
+ * @param {Number} id Group ID
+ * @param {Boolean} isFirst Whether this is the first group to add
* @return
* @private
*/
diff --git a/assets/scripts/src/reducers/groups.js b/assets/scripts/src/reducers/groups.js
index 45cee4a..73da46c 100644
--- a/assets/scripts/src/reducers/groups.js
+++ b/assets/scripts/src/reducers/groups.js
@@ -1,5 +1,3 @@
-import { sortByAlpha } from './../lib/utils.js';
-
const groups = (state = [], action) => {
switch (action.type) {
case 'ADD_GROUP':
diff --git a/tests/spec/choices_spec.js b/tests/spec/choices_spec.js
index f7c0394..63ee039 100644
--- a/tests/spec/choices_spec.js
+++ b/tests/spec/choices_spec.js
@@ -87,7 +87,7 @@ describe('Choices', function() {
expect(this.choices.setValue).toEqual(jasmine.any(Function));
expect(this.choices.setValueByChoice).toEqual(jasmine.any(Function));
expect(this.choices.setChoices).toEqual(jasmine.any(Function));
- expect(this.choices.clearValue).toEqual(jasmine.any(Function));
+ expect(this.choices.clearStore).toEqual(jasmine.any(Function));
expect(this.choices.disable).toEqual(jasmine.any(Function));
expect(this.choices.enable).toEqual(jasmine.any(Function));
expect(this.choices.ajax).toEqual(jasmine.any(Function));
@@ -181,7 +181,7 @@ describe('Choices', function() {
it('should open the choice list on focussing', function() {
this.choices.input.focus();
- expect(this.choices.dropdown.classList).toContain('is-active');
+ expect(this.choices.dropdown.classList).toContain(this.choices.config.classNames.activeState);
});
it('should select the first choice', function() {
@@ -308,25 +308,197 @@ describe('Choices', function() {
});
});
- describe('should handle AJAX-populated choices', function() {
+ describe('should handle public methods on select input types', function() {
beforeEach(function() {
this.input = document.createElement('select');
this.input.className = 'js-choices';
- this.input.multiple = false;
+ this.input.multiple = true;
this.input.placeholder = 'Placeholder text';
- for (let i = 1; i < 4; i++) {
+ for (let i = 1; i < 10; i++) {
const option = document.createElement('option');
option.value = `Value ${i}`;
option.innerHTML = `Value ${i}`;
+
+ if(i % 2) {
+ option.selected = true;
+ }
this.input.appendChild(option);
}
document.body.appendChild(this.input);
-
this.choices = new Choices(this.input);
+ });
+
+ it('should handle highlightItem()', function() {
+ const items = this.choices.currentState.items;
+ const randomItem = items[Math.floor(Math.random()*items.length)];
+
+ this.choices.highlightItem(randomItem);
+
+ expect(randomItem.highlighted).toBe(true);
+ });
+
+ it('should handle unhighlightItem()', function() {
+ const items = this.choices.currentState.items;
+ const randomItem = items[Math.floor(Math.random()*items.length)];
+
+ this.choices.unhighlightItem(randomItem);
+
+ expect(randomItem.highlighted).toBe(false);
+ });
+
+ it('should handle highlightAll()', function() {
+ const items = this.choices.currentState.items;
+
+ this.choices.highlightAll();
+
+ const unhighlightedItems = items.some((item) => item.highlighted === false);
+
+ expect(unhighlightedItems).toBe(false);
+ });
+
+ it('should handle unhighlightAll()', function() {
+ const items = this.choices.currentState.items;
+
+ this.choices.unhighlightAll();
+
+ const highlightedItems = items.some((item) => item.highlighted === true);
+
+ expect(highlightedItems).toBe(false);
+ });
+
+ it('should handle removeHighlightedItems()', function() {
+ const items = this.choices.currentState.items;
+ this.choices.highlightAll();
+ this.choices.removeHighlightedItems();
+
+ const activeItems = items.some((item) => item.active === true);
+
+ expect(activeItems).toBe(false);
+ });
+
+ it('should handle showDropdown()', function() {
+ this.choices.showDropdown();
+ const hasOpenState = this.choices.containerOuter.classList.contains(this.choices.config.classNames.openState);
+ const hasAttr = this.choices.containerOuter.getAttribute('aria-expanded') === 'true';
+ const hasActiveState = this.choices.dropdown.classList.contains(this.choices.config.classNames.activeState);
+ expect(hasOpenState && hasAttr && hasActiveState).toBe(true);
+ });
+
+ it('should handle hideDropdown()', function() {
+ this.choices.showDropdown();
+ this.choices.hideDropdown();
+ const hasOpenState = this.choices.containerOuter.classList.contains(this.choices.config.classNames.openState);
+ const hasAttr = this.choices.containerOuter.getAttribute('aria-expanded') === 'true';
+ const hasActiveState = this.choices.dropdown.classList.contains(this.choices.config.classNames.activeState);
+
+ expect(hasOpenState && hasAttr && hasActiveState).toBe(false);
+ });
+
+ it('should handle toggleDropdown()', function() {
+ spyOn(this.choices, 'hideDropdown');
+ this.choices.showDropdown();
+ this.choices.toggleDropdown();
+ expect(this.choices.hideDropdown).toHaveBeenCalled();
+ });
+
+ it('should handle hideDropdown()', function() {
+ this.choices.showDropdown();
+ expect(this.choices.containerOuter.classList).toContain(this.choices.config.classNames.openState);
+ });
+
+ it('should handle getValue()', function() {
+ const valueObjects = this.choices.getValue();
+ const valueStrings = this.choices.getValue(true);
+
+ expect(valueStrings[0]).toEqual(jasmine.any(String));
+ expect(valueObjects[0]).toEqual(jasmine.any(Object));
+ expect(valueObjects).toEqual(jasmine.any(Array));
+ expect(valueObjects.length).toEqual(5);
+ });
+
+ it('should handle setValue()', function() {
+ this.choices.setValue(['Set value 1', 'Set value 2', 'Set value 3']);
+ const valueStrings = this.choices.getValue(true);
+
+ expect(valueStrings[valueStrings.length - 1]).toBe('Set value 3');
+ expect(valueStrings[valueStrings.length - 2]).toBe('Set value 2');
+ expect(valueStrings[valueStrings.length - 3]).toBe('Set value 1');
+ });
+
+ it('should handle setValueByChoice()', function() {
+ const choices = this.choices.store.getChoicesFilteredByActive();
+ const randomChoice = choices[Math.floor(Math.random()*choices.length)];
+
+ this.choices.highlightAll();
+ this.choices.removeHighlightedItems();
+ this.choices.setValueByChoice(randomChoice.value);
+
+ const value = this.choices.getValue(true);
+
+ expect(value[0]).toBe(randomChoice.value);
+ });
+
+ it('should handle setChoices()', function() {
+ this.choices.setChoices([{
+ label: 'Group one',
+ id: 1,
+ disabled: false,
+ choices: [
+ {value: 'Child One', label: 'Child One', selected: true},
+ {value: 'Child Two', label: 'Child Two', disabled: true},
+ {value: 'Child Three', label: 'Child Three'},
+ ]
+ },
+ {
+ label: 'Group two',
+ id: 2,
+ disabled: false,
+ choices: [
+ {value: 'Child Four', label: 'Child Four', disabled: true},
+ {value: 'Child Five', label: 'Child Five'},
+ {value: 'Child Six', label: 'Child Six'},
+ ]
+ }], 'value', 'label');
+
+
+ const groups = this.choices.currentState.groups;
+ const choices = this.choices.currentState.choices;
+
+ expect(groups[groups.length - 1].value).toEqual('Group two');
+ expect(groups[groups.length - 2].value).toEqual('Group one');
+ expect(choices[choices.length - 1].value).toEqual('Child Six');
+ expect(choices[choices.length - 2].value).toEqual('Child Five');
+ });
+
+ it('should handle clearStore()', function() {
+ this.choices.clearStore();
+
+ expect(this.choices.currentState.items).toEqual([]);
+ expect(this.choices.currentState.choices).toEqual([]);
+ expect(this.choices.currentState.groups).toEqual([]);
+ });
+
+ it('should handle disable()', function() {
+ this.choices.disable();
+
+ expect(this.choices.input.disabled).toBe(true);
+ expect(this.choices.containerOuter.classList.contains(this.choices.config.classNames.disabledState)).toBe(true);
+ expect(this.choices.containerOuter.getAttribute('aria-disabled')).toBe('true');
+ });
+
+ it('should handle enable()', function() {
+ this.choices.enable();
+
+ expect(this.choices.input.disabled).toBe(false);
+ expect(this.choices.containerOuter.classList.contains(this.choices.config.classNames.disabledState)).toBe(false);
+ expect(this.choices.containerOuter.hasAttribute('aria-disabled')).toBe(false);
+ });
+
+ it('should handle ajax()', function() {
spyOn(this.choices, 'ajax');
this.choices.ajax((callback) => {
@@ -340,10 +512,36 @@ describe('Choices', function() {
console.log(error);
});
});
- });
-
- it('should call ajax()', function() {
+
expect(this.choices.ajax).toHaveBeenCalledWith(jasmine.any(Function));
});
});
+
+ describe('should handle public methods on text input types', function() {
+ beforeEach(function() {
+ this.input = document.createElement('input');
+ this.input.type = "text";
+ this.input.className = 'js-choices';
+ this.input.value = "Value 1, Value 2, Value 3, Value 4";
+
+ document.body.appendChild(this.input);
+ this.choices = new Choices(this.input);
+ });
+
+ it('should handle clearInput()', function() {
+ this.choices.clearInput();
+ expect(this.choices.input.value).toBe('');
+ });
+
+ it('should handle removeItemsByValue()', function() {
+ const items = this.choices.currentState.items;
+ const randomItem = items[Math.floor(Math.random()*items.length)];
+
+ this.choices.removeItemsByValue(randomItem.value);
+
+ expect(randomItem.active).toBe(false);
+
+ });
+ });
+
});