Choices/assets/scripts/dist/choices.min.js

2 lines
47 KiB
JavaScript
Raw Normal View History

!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);t<e.length;t++)i[t]=e[t];return i}return Array.from(e)}function o(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 r=function(){function e(e,t){for(var i=0;i<t.length;i++){var n=t[i];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,i,n){return i&&e(t.prototype,i),n&&e(t,n),t}}(),a=i(2),c=i(3),l=i(4),h=n(l),u=i(5),d=n(u),p=t.Choices=function(){function e(){var t=arguments.length<=0||void 0===arguments[0]?"[data-choice]":arguments[0],i=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];if(o(this,e),(0,c.isType)("String",t)){var n=document.querySelectorAll(t);if(n.length>1)for(var s=1;s<n.length;s++){var r=n[s];new e(r,i)}}var a={items:[],maxItemCount:-1,addItems:!0,removeItems:!0,removeItemButton:!1,editItems:!1,duplicateItems:!0,delimiter:",",paste:!0,searchOptions:!0,regexFilter:null,placeholder:!0,placeholderValue:null,prependValue:null,appendValue:null,loadingText:"Loading...",templates:{},classNames:{containerOuter:"choices",containerInner:"choices__inner",input:"choices__input",inputCloned:"choices__input--cloned",list:"choices__list",listItems:"choices__list--multiple",listSingle:"choices__list--single",listDropdown:"choices__list--dropdown",item:"choices__item",itemSelectable:"choices__item--selectable",itemDisabled:"choices__item--disabled",itemChoice:"choices__item--choice",group:"choices__group",groupHeading:"choices__heading",button:"choices__button",activeState:"is-active",focusState:"is-focused",openState:"is-open",disabledState:"is-disabled",highlightedState:"is-highlighted",hiddenState:"is-hidden",flippedState:"is-flipped",loadingState:"is-loading"},callbackOnInit:function(){},callbackOnAddItem:function(e,t,i){},callbackOnRemoveItem:function(e,t,i){}};this.config=(0,c.extend)(a,i),this.store=new d.default(this.render),this.initialised=!1,this.currentState={},this.prevState={},this.currentValue="",this.passedElement=(0,c.isType)("String",t)?document.querySelector(t):t,this.highlightPosition=0,this.canSearch=this.config.searchOptions,this.presetItems=this.config.items,this.passedElement.value&&(this.presetItems=this.presetItems.concat(this.passedElement.value.split(this.config.delimiter))),this.init=this.init.bind(this),this.render=this.render.bind(this),this.destroy=this.destroy.bind(this),this.disable=this.disable.bind(this),this._onFocus=this._onFocus.bind(this),this._onBlur=this._onBlur.bind(this),this._onKeyUp=this._onKeyUp.bind(this),this._onKeyDown=this._onKeyDown.bind(this),this._onMouseDown=this._onMouseDown.bind(this),this._onMouseOver=this._onMouseOver.bind(this),this._onPaste=this._onPaste.bind(this),this._onInput=this._onInput.bind(this);var l="querySelector"in document&&"addEventListener"in document&&"classList"in document.createElement("div");l||console.error("Choices: Your browser doesn't support Choices");var h=this.passedElement&&(0,c.isElement)(this.passedElement)&&["select-one","select-multiple","text"].includes(this.passedElement.type);if(h){if("active"===this.passedElement.getAttribute("data-choice"))return;this.init()}else console.error("Incompatible input passed")}return r(e,[{key:"init",value:function(e){this.initialised!==!0&&(this.initialised=!0,this._createTemplates(),this._createInput(),this.store.subscribe(this.render),this.render(),this._addEventListeners(),(e=this.config.callbackOnInit)&&((0,c.isType)("Function",e)?e():console.error("callbackOnInit: Callback is not a function")))}},{key:"destroy",value:function(){this.passedElement.classList.remove(this.config.classNames.input,this.config.classNa
this._keyMap=null,this._prepareSearchers(),this._startSearch(),this._computeScore(),this._sort();var t=this._format();return t},n.prototype._prepareSearchers=function(){var e=this.options,t=this.pattern,i=e.searchFn,n=t.split(a),s=0,o=n.length;if(this.options.tokenize)for(this.tokenSearchers=[];o>s;s++)this.tokenSearchers.push(new i(n[s],e));this.fullSeacher=new i(t,e)},n.prototype._startSearch=function(){var e,t,i,n,s=this.options,o=s.getFn,r=this.list,a=r.length,c=this.options.keys,l=c.length,h=null;if("string"==typeof r[0])for(i=0;a>i;i++)this._analyze("",r[i],i,i);else for(this._keyMap={},i=0;a>i;i++)for(h=r[i],n=0;l>n;n++){if(e=c[n],"string"!=typeof e){if(t=1-e.weight||1,this._keyMap[e.name]={weight:t},e.weight<=0||e.weight>1)throw new Error("Key weight has to be > 0 and <= 1");e=e.name}else this._keyMap[e]={weight:1};this._analyze(e,o(h,e,[]),h,i)}},n.prototype._analyze=function(e,t,n,s){var r,c,l,h,u,d,p,f,v,m,g,y,b,_,E,I=this.options,w=!1;if(void 0!==t&&null!==t)if(c=[],"string"==typeof t){if(r=t.split(a),I.verbose&&i("---------\nKey:",e),I.verbose&&i("Record:",r),this.options.tokenize){for(l=this.tokenSearchers,h=l.length,_=0;_<this.tokenSearchers.length;_++){for(m=this.tokenSearchers[_],g=[],E=0;E<r.length;E++)y=r[E],b=m.search(y),b.isMatch?(w=!0,g.push(b.score),c.push(b.score)):(g.push(1),c.push(1));I.verbose&&i("Token scores:",g)}for(d=c[0],f=c.length,_=1;f>_;_++)d+=c[_];d/=f,I.verbose&&i("Token score average:",d)}v=this.fullSeacher.search(t),I.verbose&&i("Full text score:",v.score),p=v.score,void 0!==d&&(p=(p+d)/2),I.verbose&&i("Score average:",p),(w||v.isMatch)&&(u=this.resultMap[s],u?u.output.push({key:e,score:p,matchedIndices:v.matchedIndices}):(this.resultMap[s]={item:n,output:[{key:e,score:p,matchedIndices:v.matchedIndices}]},this.results.push(this.resultMap[s])))}else if(o(t))for(_=0;_<t.length;_++)this._analyze(e,t[_],n,s)},n.prototype._computeScore=function(){var e,t,n,s,o,r,a,c,l,h=this._keyMap,u=this.results;for(this.options.verbose&&i("\n\nComputing score:\n"),e=0;e<u.length;e++){for(n=0,s=u[e].output,o=s.length,c=1,t=0;o>t;t++)r=s[t].score,a=h?h[s[t].key].weight:1,l=r*a,1!==a?c=Math.min(c,l):(n+=l,s[t].nScore=l);1===c?u[e].score=n/o:u[e].score=c,this.options.verbose&&i(u[e])}},n.prototype._sort=function(){var e=this.options;e.shouldSort&&(e.verbose&&i("\n\nSorting...."),this.results.sort(e.sortFn))},n.prototype._format=function(){var e,t,n,s,o,r=this.options,a=r.getFn,c=[],l=this.results,h=r.include;for(r.verbose&&i("\n\nOutput:\n\n",l),s=r.id?function(e){l[e].item=a(l[e].item,r.id,[])[0]}:function(){},o=function(e){var t,i,n,s,o,r=l[e];if(h.length>0){if(t={item:r.item},-1!==h.indexOf("matches"))for(n=r.output,t.matches=[],i=0;i<n.length;i++)s=n[i],o={indices:s.matchedIndices},s.key&&(o.key=s.key),t.matches.push(o);-1!==h.indexOf("score")&&(t.score=l[e].score)}else t=r.item;return t},t=0,n=l.length;n>t;t++)s(t),e=o(t),c.push(e);return c},r.defaultOptions={location:0,distance:100,threshold:.6,maxPatternLength:32},r.prototype._calculatePatternAlphabet=function(){var e={},t=0;for(t=0;t<this.patternLen;t++)e[this.pattern.charAt(t)]=0;for(t=0;t<this.patternLen;t++)e[this.pattern.charAt(t)]|=1<<this.pattern.length-t-1;return e},r.prototype._bitapScore=function(e,t){var i=e/this.patternLen,n=Math.abs(this.options.location-t);return this.options.distance?i+n/this.options.distance:n?1:i},r.prototype.search=function(e){var t,i,n,s,o,r,c,l,h,u,d,p,f,v,m,g,y,b,_,E,I,w,S=this.options;if(e=S.caseSensitive?e:e.toLowerCase(),this.pattern===e)return{isMatch:!0,score:0,matchedIndices:[[0,e.length-1]]};if(this.patternLen>S.maxPatternLength){if(y=e.match(new RegExp(this.pattern.replace(a,"|"))),b=!!y)for(E=[],t=0,I=y.length;I>t;t++)w=y[t],E.push([e.indexOf(w),w.length-1]);return{isMatch:b,score:b?.5:1,matchedIndices:E}}for(s=S.location,n=e.length,o=S.threshold,r=e.indexOf(this.pattern,s),_=[],t=0;n>t;t++)_[t]=0;for(-1!=r&&(o=Math.min(this._bitapScore(0,r),o),r=e.lastIndexOf(this.pattern,s+this.patternLen),-1!=r&&(o=Math.min(this._bitapScore(0,r),o))),r=-1,m=1,g=[],h=this.patternLen+n,t=0;t<this.patternLen;t++){for(