3 lines
3.8 KiB
JavaScript
Vendored
3 lines
3.8 KiB
JavaScript
Vendored
/*! multi.js 17-02-2021 */
|
|
|
|
var multi=function(){var e=function(e,t){var n=t.limit;if(n>-1){for(var a=0,i=0;i<e.options.length;i++)e.options[i].selected&&a++;if(a===n){this.disabled_limit=!0,"function"==typeof t.limit_reached&&t.limit_reached();for(i=0;i<e.options.length;i++){(d=e.options[i]).selected||d.setAttribute("disabled",!0)}}else if(this.disabled_limit){for(i=0;i<e.options.length;i++){var d;"false"===(d=e.options[i]).getAttribute("data-origin-disabled")&&d.removeAttribute("disabled")}this.disabled_limit=!1}}},t=function(t,n,a){var i,d,r,l=t.options[n.target.getAttribute("multi-index")];l.disabled||(l.selected=!l.selected,e(t,a),i="change",d=t,(r=document.createEvent("HTMLEvents")).initEvent(i,!1,!0),d.dispatchEvent(r))},n=function(e,t){if(e.wrapper.selected.innerHTML="",e.wrapper.non_selected.innerHTML="",t.non_selected_header&&t.selected_header){var n=document.createElement("div"),a=document.createElement("div");n.className="header",a.className="header",n.innerText=t.non_selected_header,a.innerText=t.selected_header,e.wrapper.non_selected.appendChild(n),e.wrapper.selected.appendChild(a)}if(e.wrapper.search)var i=e.wrapper.search.value;for(var d=null,r=null,l=0;l<e.options.length;l++){var s=e.options[l],o=s.value,c=s.textContent||s.innerText,p=document.createElement("a");if(p.tabIndex=0,p.className="item",p.innerText=c,p.setAttribute("role","button"),p.setAttribute("data-value",o),p.setAttribute("multi-index",l),s.disabled&&(p.className+=" disabled"),s.selected){p.className+=" selected";var u=p.cloneNode(!0);e.wrapper.selected.appendChild(u)}if("OPTGROUP"==s.parentNode.nodeName&&s.parentNode!=r){if(r=s.parentNode,(d=document.createElement("div")).className="item-group",s.parentNode.label){var m=document.createElement("span");m.innerHTML=s.parentNode.label,m.className="group-label",d.appendChild(m)}e.wrapper.non_selected.appendChild(d)}s.parentNode==e&&(d=null,r=null),(!i||i&&c.toLowerCase().indexOf(i.toLowerCase())>-1)&&(null!=d?d.appendChild(p):e.wrapper.non_selected.appendChild(p))}if(t.hide_empty_groups){var h=document.getElementsByClassName("item-group");for(l=0;l<h.length;l++)h[l].childElementCount<2&&(h[l].style.display="none")}};return function(a,i){if((i=void 0!==i?i:{}).enable_search=void 0===i.enable_search||i.enable_search,i.search_placeholder=void 0!==i.search_placeholder?i.search_placeholder:"Search...",i.non_selected_header=void 0!==i.non_selected_header?i.non_selected_header:null,i.selected_header=void 0!==i.selected_header?i.selected_header:null,i.limit=void 0!==i.limit?parseInt(i.limit):-1,isNaN(i.limit)&&(i.limit=-1),i.hide_empty_groups=void 0!==i.hide_empty_groups&&i.hide_empty_groups,null==a.dataset.multijs&&"SELECT"==a.nodeName&&a.multiple){a.style.display="none",a.setAttribute("data-multijs",!0);var d=document.createElement("div");if(d.className="multi-wrapper",i.enable_search){var r=document.createElement("input");r.className="search-input",r.type="text",r.setAttribute("placeholder",i.search_placeholder),r.setAttribute("title",i.search_placeholder),r.addEventListener("input",function(){n(a,i)}),d.appendChild(r),d.search=r}var l=document.createElement("div");l.className="non-selected-wrapper";var s=document.createElement("div");s.className="selected-wrapper",d.addEventListener("click",function(e){e.target.getAttribute("multi-index")&&t(a,e,i)}),d.addEventListener("keypress",function(e){var n=32===e.keyCode||13===e.keyCode;e.target.getAttribute("multi-index")&&n&&(e.preventDefault(),t(a,e,i))}),d.appendChild(l),d.appendChild(s),d.non_selected=l,d.selected=s,a.wrapper=d,a.parentNode.insertBefore(d,a.nextSibling);for(var o=0;o<a.options.length;o++){var c=a.options[o];c.setAttribute("data-origin-disabled",c.disabled)}e(a,i),n(a,i),a.addEventListener("change",function(){n(a,i)})}}}();"undefined"!=typeof jQuery&&function(e){e.fn.multi=function(t){return t=void 0!==t?t:{},this.each(function(){var n=e(this);multi(n.get(0),t)})}}(jQuery); |