Просмотр файла motorcms-v2.0.2-max/public/assets/js/bootstrap-tags.min.js

Размер файла: 14.97Kb
var g={allowNew:!1,showAllSuggestions:!1,badgeStyle:"primary",allowClear:!1,clearEnd:!1,selected:[],regex:"",separator:[],max:0,clearLabel:"Clear",searchLabel:"Type a value",keepOpen:!1,baseClass:"",placeholder:"",addOnBlur:!1,suggestionsThreshold:1,maximumItems:0,autoselectFirst:!0,updateOnSelect:!1,fullWidth:!1,labelField:"label",valueField:"value",server:"",serverParams:{},liveServer:!1,noCache:!0,debounceTime:300,notFoundMessage:"",onRenderItem:(o,t)=>t,onSelectItem:o=>{},onServerResponse:o=>o.json()},y="is-loading",E="is-active",u=["is-active","bg-primary","text-white"],f="data-value",v="next",S="prev",A="form-control-focus",b=new WeakMap,w=0;function C(o,t=300){let e;return(...s)=>{clearTimeout(e),e=setTimeout(()=>{o.apply(this,s)},t)}}function x(o,t=null){let e=document.createElement("span");document.body.appendChild(e),e.style.fontSize=t||"inherit",e.style.height="auto",e.style.width="auto",e.style.position="absolute",e.style.whiteSpace="no-wrap",e.innerHTML=o;let s=Math.ceil(e.clientWidth)+8;return document.body.removeChild(e),s}function L(o){return o.normalize("NFD").replace(/[\u0300-\u036f]/g,"")}function I(o,t){return o.parentNode.insertBefore(t,o.nextSibling)}var m=class{constructor(t,e={}){b.set(t,this),w++,this.l=t,this.A(e),this.o=!1,this.w=C(()=>{this.b(!0)},this.s.debounceTime),this.h=!0,this.c=[],this.L(),this.n=document.createElement("div"),this.a=document.createElement("div"),this.n.appendChild(this.a),I(this.l,this.n),this.E(),this.C(),this.x(),this.I(),this.k(),this.resetState(),this.e.addEventListener("focus",this),this.e.addEventListener("blur",this),this.e.addEventListener("input",this),this.e.addEventListener("keydown",this),this.i.addEventListener("mousemove",this),this.F()}static init(t="select[multiple]",e={}){let s=document.querySelectorAll(t);for(let i=0;i<s.length;i++)m.getInstance(s[i])||new m(s[i],e)}static getInstance(t){if(b.has(t))return b.get(t)}dispose(){this.e.removeEventListener("focus",this),this.e.removeEventListener("blur",this),this.e.removeEventListener("input",this),this.e.removeEventListener("keydown",this),this.i.removeEventListener("mousemove",this),this.l.style.display="block",this.n.parentElement.removeChild(this.n),this.parentForm&&this.parentForm.removeEventListener("reset",this.reset),b.delete(this.l)}handleEvent(t){this[`on${t.type}`](t)}A(t={}){this.s=Object.assign({},g);let e={...t,...this.l.dataset},s=i=>["true","false","1","0",!0,!1].includes(i)&&!!JSON.parse(i);for(let[i,n]of Object.entries(g)){if(e[i]===void 0)continue;let r=e[i];switch(typeof n){case"number":this.s[i]=parseInt(r);break;case"boolean":this.s[i]=s(r);break;case"string":this.s[i]=r.toString();break;case"object":if(Array.isArray(n)){let a=r.includes("|")?"|":",";this.s[i]=typeof r=="string"?r.split(a):r}else this.s[i]=typeof r=="string"?JSON.parse(r):r;break;case"function":this.s[i]=typeof r=="string"?window[r]:r;break;default:this.s[i]=r;break}}this.s.placeholder||(this.s.placeholder=this.O())}L(){for(this.overflowParent=null,this.parentForm=this.l.parentElement;this.parentForm&&(this.parentForm.style.overflow==="hidden"&&(this.overflowParent=this.parentForm),this.parentForm=this.parentForm.parentElement,!(this.parentForm&&this.parentForm.nodeName=="FORM")););this.reset=this.reset.bind(this),this.parentForm&&this.parentForm.addEventListener("reset",this.reset)}O(){if(this.l.hasAttribute("placeholder"))return this.l.getAttribute("placeholder");if(this.l.dataset.placeholder)return this.l.dataset.placeholder;let t=this.l.querySelector("option");return t?(t.hasAttribute("selected")&&t.removeAttribute("selected"),t.value?"":t.textContent):""}E(){this.l.style.position="absolute",this.l.style.left="-9999px",this.l.addEventListener("focus",t=>{this.e.focus()})}k(){this.i=document.createElement("ul"),this.i.classList.add("dropdown-menu","tags-menu","p-0"),this.i.setAttribute("id","tags-menu-"+w),this.i.setAttribute("role","menu"),this.i.style.maxHeight="280px",this.s.fullWidth||(this.i.style.maxWidth="360px"),this.i.style.overflowY="auto",this.i.addEventListener("mouseenter",t=>{this.o=!1}),this.n.appendChild(this.i),this.e.setAttribute("aria-controls",this.i.getAttribute("id"))}C(){this.n.classList.add("form-control","dropdown"),this.l.classList.contains("form-select-lg")&&this.n.classList.add("form-control-lg"),this.l.classList.contains("form-select-sm")&&this.n.classList.add("form-control-sm"),this.overflowParent&&(this.n.style.position="inherit"),this.g()===4&&(this.n.style.height="auto")}x(){this.a.addEventListener("click",e=>{this.isDisabled()||this.e.style.visibility!="hidden"&&this.e.focus()});let t=this.l.selectedOptions??[];for(let e=0;e<t.length;e++){let s=t[e];!s.value||(this.c.push(s),this.addItem(s.textContent,s.value))}}I(){this.e=document.createElement("input"),this.e.type="text",this.e.autocomplete="off",this.e.spellcheck=!1,this.e.ariaAutoComplete="list",this.e.ariaExpanded="false",this.e.ariaHasPopup="menu",this.e.setAttribute("role","combobox"),this.e.ariaLabel=this.s.searchLabel,this.e.style.backgroundColor="transparent",this.e.style.color="currentColor",this.e.style.border="0",this.e.style.outline="0",this.e.style.maxWidth="100%",this.resetSearchInput(!0),this.a.appendChild(this.e)}onfocus(t){this.n.classList.add(A),this.m()}onblur(t){let e=this.getSelection(),s={selection:e?e.dataset.value:null,input:this.e.value};this.n.classList.remove(A),this.d(),this.s.keepOpen&&this.resetSearchInput(),this.s.addOnBlur&&this.s.allowNew&&this.canAdd(s.input)&&(this.addItem(s.input),this.resetSearchInput()),this.h&&this.l.dispatchEvent(new CustomEvent("tags.blur",{bubbles:!0,detail:s}))}oninput(t){let e=this.e.value;if(e){let s=e.slice(-1);if(this.s.separator.length&&this.s.separator.includes(s)){this.e.value=this.e.value.slice(0,-1),this.u(this.e.value,null);return}}this.r(),this.m()}onkeydown(t){let e=t.keyCode||t.key,s=t.target;switch(t.keyCode==229&&(e=s.value.charAt(s.selectionStart-1).charCodeAt(0)),e){case 13:case"Enter":t.preventDefault();let i=this.getSelection();if(i)i.click();else if(this.s.allowNew&&this.e.value){let n=this.e.value;this.u(n,null)}break;case 38:case"ArrowUp":t.preventDefault(),this.o=!0,this.p(S);break;case 40:case"ArrowDown":t.preventDefault(),this.o=!0,this.p(v);break;case 8:case"Backspace":this.e.value.length==0&&(this.removeLastItem(),this.r(),this.m());break;case 27:case"Escape":this.e.focus(),this.d();break}}onmousemove(t){this.o=!1}resetState(){this.isDisabled()?(this.n.setAttribute("readonly",""),this.e.setAttribute("disabled","")):(this.n.hasAttribute("readonly")&&this.n.removeAttribute("readonly"),this.e.hasAttribute("disabled")&&this.e.removeAttribute("disabled"))}resetSuggestions(){let t=Array.from(this.l.querySelectorAll("option")).filter(e=>!e.disabled).map(e=>({value:e.getAttribute("value"),label:e.textContent}));this.S(t)}b(t=!1){this.f&&this.f.abort(),this.f=new AbortController;let e=Object.assign({},this.s.serverParams);if(e.query=this.e.value,this.s.noCache&&(e.t=Date.now()),e.related){let i=document.getElementById(e.related);i&&(e.related=i.value)}let s=new URLSearchParams(e).toString();this.n.classList.add(y),fetch(this.s.server+"?"+s,{signal:this.f.signal}).then(i=>this.s.onServerResponse(i)).then(i=>{let n=i.data||i;this.S(n),this.f=null,t&&this.v()}).catch(i=>{i.name!=="AbortError"&&console.error(i)}).finally(i=>{this.n.classList.remove(y)})}u(t,e=null,s={}){!this.canAdd(t,e)||(this.addItem(t,e,s),this.s.keepOpen?this.v():this.resetSearchInput())}p(t=v){let e=this.getSelection(),s=null;if(e){let i=t===v?"nextSibling":"previousSibling";s=e.parentNode;do s=s[i];while(s&&s.style.display==="none");s?(e.classList.remove(...u),t===S?s.parentNode.scrollTop=s.offsetTop-s.parentNode.offsetTop:s.offsetTop>s.parentNode.offsetHeight-s.offsetHeight&&(s.parentNode.scrollTop+=s.offsetHeight)):e&&(s=e.parentElement)}else for(s=this.i.firstChild;s&&s.style.display==="none";)s=s.nextSibling;if(s){let i=s.querySelector("a");i.classList.add(...u),this.e.setAttribute("aria-activedescendant",i.getAttribute("id")),this.s.updateOnSelect&&(this.e.value=i.dataset.label,this.r())}else this.e.setAttribute("aria-activedescendant","");return s}r(){this.e.value?this.e.size=this.e.value.length:this.getSelectedValues().length?(this.e.placeholder="",this.e.size=1):(this.e.size=this.s.placeholder.length>0?this.s.placeholder.length:1,this.e.placeholder=this.s.placeholder);let t=this.e.value||this.e.placeholder,e=window.getComputedStyle(this.n).fontSize,s=x(t,e);this.e.style.minWidth=s+"px"}S(t){for(;this.i.lastChild;)this.i.removeChild(this.i.lastChild);for(let e=0;e<t.length;e++){let s=t[e];if(!s[this.s.valueField])continue;let i=s[this.s.valueField],n=s[this.s.labelField];if(!this.s.liveServer&&(s.selected||this.s.selected.includes(i))){this.c.push({value:i,textContent:n,dataset:s.data}),this.u(n,i,s.data);continue}let r=this.s.onRenderItem(s,n),a=document.createElement("li");a.setAttribute("role","presentation");let l=document.createElement("a");a.append(l),l.setAttribute("id",this.i.getAttribute("id")+"-"+e),l.classList.add("dropdown-item","text-truncate"),l.setAttribute(f,i),l.setAttribute("data-label",n),l.setAttribute("href","#"),l.textContent=r,this.i.appendChild(a),l.addEventListener("mouseenter",h=>{this.o||(this.removeSelection(),a.querySelector("a").classList.add(...u))}),l.addEventListener("mousedown",h=>{h.preventDefault()}),l.addEventListener("click",h=>{h.preventDefault(),this.u(r,i,s.data),this.s.onSelectItem(s)})}if(this.s.notFoundMessage){let e=document.createElement("li");e.setAttribute("role","presentation"),e.classList.add("tags-not-found"),e.innerHTML=`<span class="dropdown-item">${this.s.notFoundMessage}</span>`,this.i.appendChild(e)}}reset(){this.removeAll(),this.h=!1;for(let t=0;t<this.c.length;t++){let e=this.c[t];this.addItem(e.textContent,e.value,e.dataset)}this.r(),this.h=!0}resetSearchInput(t=!1){this.e.value="",this.r(),t||(this.d(),this.e===document.activeElement&&this.e.dispatchEvent(new Event("input"))),this.s.max&&this.getSelectedValues().length>=this.s.max?this.e.style.visibility="hidden":this.e.style.visibility=="hidden"&&(this.e.style.visibility="visible"),this.isSingle()&&!t&&document.activeElement.blur()}getSelectedValues(){let t=this.l.querySelectorAll("option[selected]");return Array.from(t).map(e=>e.value)}T(){return this.isDisabled()?!1:this.e.value.length>=this.s.suggestionsThreshold}m(){if(!this.T()){this.d();return}this.s.liveServer?this.w():this.v()}v(){if(this.e.style.visibility=="hidden")return;let t=L(this.e.value).toLowerCase(),e=this.getSelectedValues(),s=this.i.querySelectorAll("li"),i=0,n=null,r=!1;for(let a=0;a<s.length;a++){let l=s[a],h=l.querySelector("a");if(!h){l.style.display="none";continue}if(h.classList.remove(...u),e.indexOf(h.getAttribute(f))!=-1){l.style.display="none";continue}r=!0;let c=L(l.textContent).toLowerCase(),d=t.length>0?c.indexOf(t)>=0:!0;this.s.showAllSuggestions||d?(i++,l.style.display="list-item",!n&&d&&(n=l),this.s.maximumItems>0&&i>this.s.maximumItems&&(l.style.display="none")):l.style.display="none"}if(n||this.s.showAllSuggestions?(this.n.classList.remove("is-invalid"),n&&this.s.autoselectFirst&&this.p(v)):!this.s.allowNew&&!(t.length===0&&!r)?this.n.classList.add("is-invalid"):this.s.regex&&this.isInvalid()&&this.n.classList.remove("is-invalid"),i===0||this.isInvalid())if(this.s.notFoundMessage){let a=this.i.querySelector(".tags-not-found");a.style.display="block"}else this.d();else this.i.classList.add("show"),this.e.ariaExpanded="true",this.N()}N(){if(this.s.fullWidth)this.i.style.left=-1+"px",this.i.style.width=this.n.offsetWidth+"px";else{let t=this.e.offsetLeft,e=document.body.offsetWidth-1,s=30,i=e-(t+this.i.offsetWidth)-s;i<0&&(t=t+i),this.i.style.left=t+"px";let n=document.body.offsetHeight,r=this.e.getBoundingClientRect().y+window.pageYOffset+this.i.offsetHeight;n-r<0?this.i.style.transform="translateY(calc(-100% - "+s+"px))":this.i.style.transform="none"}}d(){this.i.classList.remove("show"),this.n.classList.remove("is-invalid"),this.e.ariaExpanded="false",this.removeSelection()}g(){let t=5;return window.jQuery&&$.fn.tooltip!=null&&$.fn.tooltip.Constructor!=null&&(t=parseInt($.fn.tooltip.Constructor.VERSION.charAt(0))),t}D(t){let e=Array.from(this.l.querySelectorAll("option")).find(s=>s.textContent==t);return!!(e&&e.getAttribute("selected"))}F(){this.s.server&&!this.s.liveServer?this.b():this.resetSuggestions()}q(t){return new RegExp(this.s.regex.trim()).test(t)}getSelection(){return this.i.querySelector("a."+E)}removeSelection(){let t=this.getSelection();t&&t.classList.remove(...u)}getActiveSelection(){return this.getSelection()}removeActiveSelection(){return this.removeSelection()}removeAll(){this.getSelectedValues().forEach(e=>{this.removeItem(e,!0)}),this.r()}removeLastItem(t=!1){let e=this.a.querySelectorAll("span");if(!e.length)return;let s=e[e.length-1];this.removeItem(s.getAttribute(f),t)}isDisabled(){return this.l.hasAttribute("disabled")||this.l.disabled||this.l.hasAttribute("readonly")}isInvalid(){return this.n.classList.contains("is-invalid")}isSingle(){return!this.l.hasAttribute("multiple")}canAdd(t,e=null){return e||(e=t),!t||this.isDisabled()||!this.isSingle()&&this.D(t)||this.s.max&&this.getSelectedValues().length>=this.s.max?!1:this.s.regex&&!this.q(t)?(this.n.classList.add("is-invalid"),!1):!0}addItem(t,e=null,s={}){e||(e=t),this.isSingle()&&this.getSelectedValues().length&&this.removeLastItem(!0);let i=this.g(),n=this.l.querySelector('option[value="'+e+'"]');n&&(s=n.dataset);let r=t,a=document.createElement("span"),l=["badge"],h=this.s.badgeStyle;if(s.badgeStyle&&(h=s.badgeStyle),s.badgeClass&&l.push(...s.badgeClass.split(" ")),this.s.baseClass?(i===5?l.push("me-2"):l.push("mr-2"),l.push(...this.s.baseClass.split(" "))):i===5?l=[...l,"me-2","bg-"+h,"mw-100"]:l=[...l,"mr-2","badge-"+h],a.classList.add(...l),a.setAttribute(f,e),this.s.allowClear){let c=l.includes("text-dark")?"btn-close":"btn-close-white",d,p;this.s.clearEnd?(d=i===5?"ms-2":"ml-2",p=i===5?"float-end":"float:right;"):(d=i===5?"me-2":"mr-2",p=i===5?"float-start":"float:left;"),r=(i===5?'<button type="button" style="font-size:0.65em" class="'+d+" "+p+" btn-close "+c+'" aria-label="'+this.s.clearLabel+'"></button>':'<button type="button" style="font-size:1em;'+p+'text-shadow:none;color:currentColor;transform:scale(1.2)" class="'+d+' close" aria-label="'+this.s.clearLabel+'"><span aria-hidden="true">&times;</span></button>')+r}if(a.innerHTML=r,this.a.insertBefore(a,this.e),this.s.allowClear&&a.querySelector("button").addEventListener("click",c=>{c.preventDefault(),c.stopPropagation(),this.isDisabled()||(this.removeItem(e),document.activeElement.blur(),this.r())}),!n){n=document.createElement("option"),n.value=e,n.textContent=t;for(let[c,d]of Object.entries(s))n.dataset[c]=d;this.l.appendChild(n)}n.setAttribute("selected","selected"),n.selected=!0,this.h&&this.l.dispatchEvent(new Event("change",{bubbles:!0}))}removeItem(t,e=!1){let s=this.a.querySelector("span["+f+'="'+t+'"]');if(!s)return;s.remove();let i=this.l.querySelector('option[value="'+t+'"]');i&&(i.removeAttribute("selected"),i.selected=!1,this.h&&!e&&this.l.dispatchEvent(new Event("change",{bubbles:!0}))),this.e.style.visibility=="hidden"&&this.s.max&&this.getSelectedValues().length<this.s.max&&(this.e.style.visibility="visible")}},F=m;export{F as default};