芝麻web文件管理V1.00
编辑当前文件:/home2/sdektunc/.trash/media.2/system/js/fields/joomla-field-fancy-select.min.js
window.customElements.define("joomla-field-fancy-select",class extends HTMLElement{get allowCustom(){return this.hasAttribute("allow-custom")}get remoteSearch(){return this.hasAttribute("remote-search")}get url(){return this.getAttribute("url")}get termKey(){return this.getAttribute("term-key")||"term"}get minTermLength(){return parseInt(this.getAttribute("min-term-length"),10)||1}get newItemPrefix(){return this.getAttribute("new-item-prefix")||""}get placeholder(){return this.getAttribute("placeholder")}get searchPlaceholder(){return this.getAttribute("search-placeholder")}get value(){return this.choicesInstance.getValue(!0)}set value(e){this.choicesInstance.setChoiceByValue(e)}constructor(){if(super(),this.keyCode={ENTER:13},!Joomla)throw new Error("Joomla API is not properly initiated");if(!window.Choices)throw new Error("JoomlaFieldFancySelect requires Choices.js to work");this.choicesCache={},this.activeXHR=null,this.choicesInstance=null,this.isDisconnected=!1}connectedCallback(){if(window.Choices||"complete"===document.readyState)this.doConnect();else{const e=()=>{this.doConnect(),window.removeEventListener("load",e)};window.addEventListener("load",e)}}doConnect(){if(this.select=this.querySelector("select"),!this.select)throw new Error("JoomlaFieldFancySelect requires
element to work");if(this.choicesInstance)this.isDisconnected&&(this.choicesInstance.init(),this.isDisconnected=!1);else{if(this.isDisconnected=!1,this.select.multiple&&this.placeholder){const e=document.createElement("option");e.setAttribute("placeholder",""),e.textContent=this.placeholder,this.select.appendChild(e)}if(this.choicesInstance=new Choices(this.select,{placeholderValue:this.placeholder,searchPlaceholderValue:this.searchPlaceholder,removeItemButton:!0,searchFloor:this.minTermLength,searchResultLimit:parseInt(this.select.dataset.maxResults,10)||10,renderChoiceLimit:parseInt(this.select.dataset.maxRender,10)||-1,shouldSort:!1,fuseOptions:{threshold:.3},noResultsText:Joomla.Text._("JGLOBAL_SELECT_NO_RESULTS_MATCH","No results found"),noChoicesText:Joomla.Text._("JGLOBAL_SELECT_NO_RESULTS_MATCH","No results found"),itemSelectText:Joomla.Text._("JGLOBAL_SELECT_PRESS_TO_SELECT","Press to select"),classNames:{button:"choices__button_joomla"}}),this.allowCustom){const e=this.choicesInstance._highlightChoice;this.choicesInstance._highlightChoice=t=>{t&&e.call(this.choicesInstance,t)},this.addEventListener("mouseleave",(()=>{if(!this.choicesInstance.dropdown.isActive)return;Array.from(this.choicesInstance.dropdown.element.querySelectorAll(`.${this.choicesInstance.config.classNames.highlightedState}`)).forEach((e=>{e.classList.remove(this.choicesInstance.config.classNames.highlightedState),e.setAttribute("aria-selected","false")})),this.choicesInstance._highlightPosition=0})),this.addEventListener("keydown",(e=>{if(e.keyCode!==this.keyCode.ENTER||e.target!==this.choicesInstance.input.element)return;if(e.preventDefault(),this.choicesInstance._highlightPosition||!e.target.value)return;if(this.choicesInstance.dropdown.element.querySelector(`.${this.choicesInstance.config.classNames.highlightedState}`))return;const t=e.target.value.toLowerCase();let s=!1;if(this.choicesInstance.config.choices.some((e=>(e.value.toLowerCase()===t||e.label.toLowerCase()===t)&&(s=e.value,!0))),!1===s&&Object.keys(this.choicesCache).some((e=>(e.toLowerCase()===t||this.choicesCache[e].toLowerCase()===t)&&(s=e,!0))),!1!==s)return this.choicesInstance.setChoiceByValue(s),e.target.value=null,void this.choicesInstance.hideDropdown();this.choicesInstance.setChoices([{value:this.newItemPrefix+e.target.value,label:e.target.value,selected:!0,customProperties:{value:e.target.value}}],"value","label",!1),this.choicesCache[e.target.value]=e.target.value,e.target.value=null,this.choicesInstance.hideDropdown()}))}if(this.remoteSearch&&this.url){this.choicesInstance.config.choices.forEach((e=>{this.choicesCache[e.value]=e.label}));const e=300;let t=null;this.select.addEventListener("search",(()=>{clearTimeout(t),t=setTimeout(this.requestLookup.bind(this),e)}))}}}disconnectedCallback(){this.choicesInstance&&(this.choicesInstance.destroy(),this.isDisconnected=!0),this.activeXHR&&(this.activeXHR.abort(),this.activeXHR=null)}requestLookup(){let{url:e}=this;e+=-1===e.indexOf("?")?"?":"&",e+=`${encodeURIComponent(this.termKey)}=${encodeURIComponent(this.choicesInstance.input.value)}`,this.activeXHR&&this.activeXHR.abort(),this.activeXHR=Joomla.request({url:e,onSuccess:e=>{this.activeXHR=null;const t=e?JSON.parse(e):[];if(!t.length)return;let s;for(let e=t.length-1;e>=0;e--)s=t[e],s.value=""+s.value,this.choicesCache[s.value]?t.splice(e,1):this.choicesCache[s.value]=s.text;t.length&&this.choicesInstance.setChoices(t,"value","text",!1)},onError:()=>{this.activeXHR=null}})}disableAllOptions(){const{choices:e}=this.choicesInstance._store;e.forEach(((t,s)=>{e[s].disabled=!0,e[s].selected=!1})),this.choicesInstance.clearStore(),this.choicesInstance.setChoices(e,"value","label",!0)}enableAllOptions(){const{choices:e}=this.choicesInstance._store,t=this.choicesInstance.getValue(!0);e.forEach(((t,s)=>{e[s].disabled=!1})),this.choicesInstance.clearStore(),this.choicesInstance.setChoices(e,"value","label",!0),this.value=t}disableByValue(e){const{choices:t}=this.choicesInstance._store,s=this.choicesInstance.getValue(!0);t.forEach(((s,i)=>{s.value===e&&(t[i].disabled=!0,t[i].selected=!1)}));const i=s.indexOf(e);i>-1&&s.slice(i,1),this.choicesInstance.clearStore(),this.choicesInstance.setChoices(t,"value","label",!0),this.value=s}enableByValue(e){const{choices:t}=this.choicesInstance._store,s=this.choicesInstance.getValue(!0);t.forEach(((s,i)=>{s.value===e&&(t[i].disabled=!1)})),this.choicesInstance.clearStore(),this.choicesInstance.setChoices(t,"value","label",!0),this.value=s}});