diff --git a/hybrid-dropdown.js b/hybrid-dropdown.js index 10e5efe..023b29b 100755 --- a/hybrid-dropdown.js +++ b/hybrid-dropdown.js @@ -147,6 +147,8 @@ class HybridDDError extends Error { settings, //user settings. cnfg //element data attribtues, precede over others to allow HTML script overrides. ); + //make sure selectedValues are strings... + if(_.opt.selectedValues) _.opt.selectedValues = _.opt.selectedValues.map(String); //nake sure we have proper functions ['selectedLabel','optionLabel'].forEach(s=>{ if(!_.opt[s] || !(_.opt[s] instanceof Function) || 1!=_.opt[s].length){ @@ -392,6 +394,7 @@ class HybridDDError extends Error { kids = Object.entries(o[1]); kids.splice(Object.keys(o[1]).indexOf('label')); hasChildren = (kids.length>0); + isSelected = _.opt.selectedValues.indexOf(val) >=0; }else kids = Object.entries(o[1]); break; default: diff --git a/min/hybrid-dropdown.min.js b/min/hybrid-dropdown.min.js index 71e91af..295ce98 100644 --- a/min/hybrid-dropdown.min.js +++ b/min/hybrid-dropdown.min.js @@ -1 +1 @@ -class HybridDDError extends Error{constructor(e){super(e),this.name="HybridDropdownError"}}!function(e){"function"==typeof define&&define.amd?define(e):"object"==typeof exports?module.exports=e():e()}(function(){let e="undefined"!=typeof window?window:this,t=e.HybridDropdown=function(e,t){if(!(e&&e instanceof Element))throw new HybridDDError("HybridDropdown requires a DOM element to intialise.");if(e.classList.contains("hybridddised")&&e._hybriddd)return console.log("WARNING: attempting instantiate element already converted to Hybrid Dropdown"),e._hybriddd;if(e.classList.contains("hybrid-dropdown"))return console.log("WARNING: attempting instantiate Hybrid Dropdown element."),e;let i=this,d=1,o=!0,l=e.getAttribute("tabindex");switch(i.isDS=!1,cnfg=Object.assign({},e.dataset),["class","id","name"].forEach(t=>{if(e.hasAttribute(t)){let i="field"+t.charAt(0).toUpperCase()+t.slice(1);cnfg[i]=e.getAttribute(t),e.removeAttribute(t)}}),!0){case"SELECT"===e.nodeName:e.multiple&&(d=-1),e.style="visibility:hidden",o=!1;break;default:if(i.isDS=!0,!t.dataSet){if(e.querySelector("script"))try{cnfg.dataSet=JSON.parse(e.querySelector("script").innerHTML)}catch(e){console.log(e.name+":"+e.message),console.log("HybridDropdown ERROR: missing or malformed json dataset")}else console.log("HybridDropdown ERROR: missing json dataset"),cnfg.dataSet=null}!cnfg.id&&cnfg.fieldName&&(cnfg.id=cnfg.fieldName),e.classList.add("hybriddd-custom")}if(cnfg&&(e._hybriddd=i),i.el=e,i.el.classList.add("hybridddised"),Object.keys(cnfg).forEach(e=>{switch(e){case"limitSelection":cnfg[e]=parseInt(cnfg[e]);break;case"treeView":case"negative":case"colourise":case"checkboxes":cnfg[e]="true"==cnfg[e]}}),i.opt=Object.assign({},{dropdown:"vertical",limitSelection:d,optionLabel:function(e){return""+e+""},selectedLabel:function(e){let t=Object.keys(e);return e[t[0]]+(t.length>1?"[...]":"")},defaultText:"---",treeView:!1,treeGlue:"/",fieldName:"hybriddd",backgroundColor:"",color:"",negative:!1,colourise:!0,checkboxes:o,tabIndex:l||0,listOption:null,selectedValues:[],fieldId:"",fieldClass:""},t,cnfg),["selectedLabel","optionLabel"].forEach(e=>{if(!(i.opt[e]&&i.opt[e]instanceof Function&&1==i.opt[e].length))throw new HybridDDError(`${e} setting must be a function with 1 argument.`)}),i.opt.listOption&&(!(i.opt.listOption instanceof Function)||2!=i.opt.listOption.length))throw new HybridDDError("listOption setting must be a function with 2 arguments.");return i.opt.treeView&&1==i.opt.limitSelection&&(i.opt.limitSelection=-1),i.multi=1!=i.opt.limitSelection,i.opt.fieldId||(i.opt.fieldId=i.opt.fieldName),i.multi&&i.opt.fieldName&&i.opt.fieldName.indexOf("[]")<0&&(i.opt.fieldName+="[]"),i.init(!0,!0,!0),i},i=t.prototype;return i.init=function(e,t,i){let d=this;if(e){d.hdd=null,d.isDS?(d.hdd=d.el,d.el.hasAttribute("tabindex")||d.hdd.setAttribute("tabindex",d.opt.tabIndex)):(d.hdd=document.createElement("div"),d.el.parentNode.insertBefore(d.hdd,d.el.nextSibling),d.hdd.style["margin-left"]="-"+d.el.getBoundingClientRect().width+"px",d.hdd.setAttribute("tabindex",d.opt.tabIndex)),d.hdd.setAttribute("id",d.opt.fieldId);let e=d.el.closest("label");e&&e.setAttribute("for",d.opt.fieldId),d.hdd.setAttribute("class",d.opt.fieldClass),d.hdd.classList.add("hybrid-dropdown"),d.hdd.selected=document.createElement("div"),d.hdd.appendChild(d.hdd.selected),d.hdd.selected.classList.add("hybriddd-selected"),d.hdd.ddlist=document.createElement("ul"),d.hdd.appendChild(d.hdd.ddlist),d.hdd.ddlist.classList.add("hybriddd-options"),d.listenForBulk=!1,d.listenModClick=!1,d.hdd.classList.add("hybriddd-"+d.opt.dropdown)}if(t){let e=null;d.hdd.options={},d.hindex=[],d.sindex=[],d.value={};try{d.isDS?(d.hdd.classList.add("hybriddd-custom"),d.opt.dataSet?e=d.buildOptionList(Object.entries(d.opt.dataSet).reverse(),0):(d.hdd.selected.innerHTML="json error",e=[])):e=d.buildOptionList(d.el.children,0),d.hdd.ddlist.replaceChildren(...e)}catch(e){if(!(e instanceof HybridDDError))throw e;console.log(e.name+":"+e.message),d.hdd.selected.innerHTML="json error"}}e&&(d.isDS||d.event(d.el,"add",{change:d.updateFromOriginal.bind(d),focus:d.originalElementFocus.bind(d)}),d.change=d.inputChange.bind(d),d.event(d.hdd.ddlist,"add",{change:d.change}),d.open=d.openSelect.bind(d),d.event(d.hdd,"add",{click:d.open}),d.close=d.closeSelect.bind(d,!0),d.blur=d.blurField.bind(d),d.hover=d.optionHover.bind(d),d.keyNav=d.keyboardNavigate.bind(d),d.event(d.hdd,"add",{keydown:d.keyNav}),d.refresh=d.refreshHybrid.bind(d),d.modClick=d.optionModClick.bind(d),d.emit("hybrid-dd-init")),d.opt.checkboxes&&d.hdd.classList.add("show-cb"),i&&d.colourise()},i.colourise=function(){let t=this,i=!1;if(!t.opt.colourise)return;if(!(e.hdd||t.opt.backgroundColor&&t.opt.color)){let d,o=!1,l=t.hdd;for(e.hdd={};!o&&l;){if(d=e.getComputedStyle(l,null),!e.hdd.bgColor&&!t.opt.backgroundColor)switch(d["background-color"]){case"transparent":case"":case"rgba(0, 0, 0, 0)":l=l.parentElement;break;default:e.hdd.bgColor=d["background-color"]}if(!e.hdd.color&&!t.opt.color)switch(d.color){case"transparent":case"":case"rgba(0, 0, 0, 0)":l=l.parentElement;break;default:e.hdd.color=d.color}e.hdd.bgColor&&e.hdd.color&&(o=!0),i=!0}o||(e.hdd.color||(e.hdd.color="#fff"),e.hdd.bgColor&&(e.hdd.bgColor="#5d5d5d"))}let d=t.opt.backgroundColor?t.opt.backgroundColor:e.hdd.bgColor,o=t.opt.color?t.opt.color:e.hdd.color;if(t.hdd.style["background-color"]=t.opt.negative?o:d,t.hdd.style.color=t.opt.negative?d:o,i){let e=document.createElement("style");e.setAttribute("id","hybriddd-colours"),e.type="text/css",e.innerText=`.hybriddd-option.active > label:hover,.hybriddd-option.hover > label,.hybriddd-option > label:hover{color:${d};background-color:${o}}:hover > input:checked + .hybridddl > .hybridddcb::before {color:${o}}ul.hybriddd-options::-webkit-scrollbar-track {background:${d}} ul.hybriddd-options::-webkit-scrollbar-thumb, ul.hybriddd-options::-webkit-scrollbar{background:${o}}`,document.head.appendChild(e)}if(t.opt.backgroundColor||t.opt.color||t.opt.negative){if(t.opt.negative){let e=d;d=o,o=e}let e=document.createElement("style"),i=t.hdd.getAttribute("id");e.setAttribute("id",i+"-css"),e.type="text/css",e.innerText=`#${i} .hybriddd-option.active > label:hover,#${i} .hybriddd-option.hover > label,#${i} .hybriddd-option > label:hover{color:${d};background-color:${o}}#${i} :hover > input:checked + .hybridddl > .hybridddcb::before {color:${o}} #${i} ul.hybriddd-options::-webkit-scrollbar-track {background:${d}} #${i} ul.hybriddd-options::-webkit-scrollbar-thumb,#${i} ul.hybriddd-options::-webkit-scrollbar{background:${o}}`,document.head.appendChild(e)}},i.refreshHybrid=function(e={}){let t=this,i=!1,d=!1;if(e.hasOwnProperty("negative")||e.hasOwnProperty("colourise")||e.color||e.backgroundColor){d=!0;let e=t.el.getAttribute("id"),i=document.querySelector("style#"+e+"-css");i&&i.remove()}e.listOption&&(i=!0,e.listOption instanceof Function&&2==e.listOption.length||(console.log("Hybriddd refresh error: listOption setting must be a function with 2 arguments."),e.listOption=null)),t.opt=Object.assign({},t.opt,e),t.init(!1,i,d)},i.buildOptionList=function(e,t,i=""){let d=this,o=[],l=d.multi?"checkbox":"radio",n=d.opt.fieldName?' name="'+d.opt.fieldName+'"':"";if([].forEach.call(e,(e,s)=>{if(d.opt.listOption&&!0!==d.opt.listOption(e,s))return;let r,c,a=document.createElement("li"),h=!1,u=!1,p=!1,b=[],f="",y="";if(d.isDS)switch(r=e[0],c=null,!0){case e[1]instanceof Object&&!e[1].length:case 0===t&&e[1]instanceof Array:p=h=!0,e[1].label?(h=!1,c=e[0],f="hybridddis",r=d.opt.optionLabel(e[1].label),(b=Object.entries(e[1])).splice(Object.keys(e[1]).indexOf("label")),p=b.length>0):b=Object.entries(e[1]);break;default:f="hybridddis",c=e[0],r=d.opt.optionLabel(e[1]),u=d.opt.selectedValues.indexOf(c)>=0}else switch(e.nodeName){case"OPTGROUP":p=h=!0,r=e.label,b=e.children;break;default:f="hybridddin",r=e.text,c=e.value,u=e.selected}switch(h){case!0:a.innerHTML=""+r+"",a.classList.add("hybriddd-group");break;default:if(y="",u&&(d.value[c]=r,d.sindex.push(c),y=" checked"),a.setAttribute("tabindex","-1"),a.innerHTML=``,a.classList.value="hybriddd-option"+(u?" active":""),d.isDS||(a.classList.value+=` ${e.classList.value}`),d.hdd.options[i+c])throw new HybridDDError("Option list has duplicate value: "+c);d.hdd.options[i+c]=a}if(p){d.opt.treeView&&c&&(i+=c+d.opt.treeGlue);let e=d.buildOptionList(b,t+1,i),o=document.createElement("ul");o.replaceChildren(...e),a.appendChild(o)}o[o.length]=a}),0==t){d.empty()&&d.reset();let e=Object.keys(d.value);e.length>1&&e.indexOf("")>=0&&delete d.value[""],d.hdd.selected.innerHTML=d.opt.selectedLabel(d.value)}return o},i.empty=function(){let e=this;for(let t in e.value)return!1;return!0},i.reset=function(){let e=this;e.sindex=[],e.hdd.options[""]?(e.hdd.options[""].classList.add("active"),e.value[""]=e.hdd.options[""].querySelector(".hybridddl").innerHTML,e.hdd.options[""].querySelector("input").checked=!0,e.sindex.push("")):e.value[""]=e.opt.defaultText},i.event=function(e,t,i){var d=e[t+"EventListener"].bind(e);Object.keys(i).forEach(function(e){["mouseenter","mouseleave"].indexOf(e)>=0?d(e,i[e],!0):d(e,i[e])})},i.updateFromOriginal=function(){let e=this,t=[];if(!e.isDS){if(e.multi){let i=!0;for(let d=0;d0?t.indexOf(e[e.length-1]):-1;return i0?t.indexOf(e[0]):t.length;return i>=0&&i--,i<0&&(i=t.length-1),t[i]},i.keyboardNavigate=function(){let e,t=this,i=arguments[0];if(!t.el.disabled&&i&&i.keyCode)switch(i.preventDefault(),!0){case 40==i.keyCode&&"keydown"==i.type:t.hdd.classList.contains("active")?((t.hindex.length>0&&!t.multi||!i.shiftKey)&&t.clearClass("hover"),e=t.nextOption(t.hindex),t.hdd.options[e].classList.add("hover"),t.scroll(e,"down"),t.multi&&i.shiftKey?(t.hindex.push(e),t.opt.limitSelection>0&&(t.hindex=t.hindex.slice(-1*t.opt.limitSelection))):t.hindex[0]=e):1==t.opt.limitSelection?(e=t.nextOption(t.sindex),t.toggleValue([e],!0)):(t.hdd.classList.remove("focus"),t.open());break;case 38==i.keyCode&&"keydown"==i.type:if(t.hdd.classList.contains("active"))(t.hindex.length>0&&!t.multi||!i.shiftKey)&&t.clearClass("hover"),e=t.prevOption(t.hindex),t.hdd.options[e].classList.add("hover"),t.scroll(e,"up"),t.multi&&i.shiftKey?(t.hindex.push(e),t.opt.limitSelection>0&&(t.hindex=t.hindex.slice(-1*t.opt.limitSelection))):t.hindex[0]=e;else if(1==t.opt.limitSelection){let e=t.prevOption(t.sindex);t.toggleValue([e],!0)}else t.hdd.classList.remove("focus"),t.open();break;case 13==i.keyCode&&"keydown"==i.type:case 32==i.keyCode&&"keydown"==i.type:!1===t.hdd.classList.contains("active")?(t.hdd.classList.remove("focus"),t.open()):(t.hindex.length>0&&t.toggleValue(t.hindex,!0),t.closeSelect(!1),t.hdd.classList.add("focus"));break;case 27==i.keyCode&&"keydown"==i.type:t.hdd.classList.contains("active")&&t.closeSelect(!1),t.blurField();break;case 9==i.keyCode&&"keydown"==i.type:t.hdd.classList.contains("active")&&t.closeSelect(!1),t.blurField();let d,o,l=1*t.el.getAttribute("tabindex"),n=t.el.closest("form");if(null===n&&(n=document),null!=l&&""!=l)l+=1,d=n.querySelector('[tabindex="'+l+'"]');else if(n!==document)for(l in o=t.el,t.isDS&&(l=(o=Object.keys(t.hdd.options))[o.length-1],o=t.hdd.options[l].querySelector("input")),n.elements)if(n.elements[l]===o){do{l++}while(lo.bottom)switch(t){case"up":o.top>d.top?i.hdd.ddlist.scrollTop-=d.height:o.bottomd.top&&(i.hdd.ddlist.scrollTop=0)}},i.inputChange=function(){let e=this,t=arguments[0],i=[],d=!1;if(t&&t.target){if(d=t.target.classList.contains("mod-ctrl"),"change"==t.type&&d)return t.target.classList.remove("mod-ctrl"),!1;if(e.hindex.length>0&&(i=[...e.hindex]),t.target.checked&&""==t.target.value)d||e.removeValue([...e.sindex],!0);else if(e.opt.treeView&&(e.opt.limitSelection<0||e.opt.limitSelection>e.sindex.length)){let o,l,n=!0,s=t.target.closest(".hybriddd-option"),r=s.querySelectorAll("input");for(l of r.values())l.checked=t.target.checked,d||(t.target.checked?i.push(l.value):e.removeValue([l.value])),l.closest(".hybriddd-option").classList.remove("partial");for(;s;)l=s.querySelector("input"),n||l.checked||s.querySelectorAll("label input").length-s.querySelectorAll("label input:checked").length!=1||(l.checked=!0,i.push(l.value),s.classList.remove("partial")),r=s.querySelectorAll("input:checked"),o=s.querySelectorAll("input"),l.checked&&1==r.length&&o.length>1?(s.classList.remove("partial"),l.checked=!1,d||e.removeValue([l.value])):r.length>0&&o.length!=r.length?(s.classList.add("partial"),d||e.removeValue([l.value])):s.classList.remove("partial"),s=s.parentNode.closest(".hybriddd-option"),n=!1;d||e.addValue(i,!0)}else d||(i.push(t.target.value),t.target.checked?e.addValue(i,!0):e.removeValue(i,!0));switch(!0){case"hybrid-ddi-change"==t.type:case t.shiftKey&&1==t.shiftKey:break;default:e.closeSelect(!1)}}},i.toggleValue=function(e,t){let i=this;i.hdd.options[e[0]].querySelector("input").checked?i.removeValue(e,t):i.addValue(e,t)},i.addValue=function(e,t=!1){let i=this;switch(i.opt.limitSelection){case 1:i.sindex.length>0&&i.hdd.options[i.sindex[0]].classList.remove("active"),i.sindex=e;break;default:if(1==i.sindex.length&&0==i.sindex[0].length?(i.hdd.options[""].classList.remove("active"),i.hdd.options[""].querySelector("input").checked=!1,i.sindex=e):i.sindex=i.sindex.concat(e),i.opt.limitSelection>0){for(let e=i.opt.limitSelection;e{i.value[e]=i.hdd.options[e].querySelector(".hybridddl").innerHTML,i.hdd.options[e].querySelector("input").checked=!0,i.hdd.options[e].classList.add("active")}),i.empty()&&i.reset(),i.isDS||i.updateOriginal(),i.hdd.selected.innerHTML=i.opt.selectedLabel(i.value),t&&i.emit("change")},i.removeValue=function(e,t=!1){let i,d=this;e.forEach(e=>{d.hdd.options[e].classList.remove("active"),d.hdd.options[e].querySelector("input").checked=!1,(i=d.sindex.indexOf(e))>=0&&(d.sindex.splice(i,1),delete d.value[e])}),0==d.sindex.length&&(d.hdd.options[""]?(d.sindex=[""],d.value={"":d.hdd.options[""].querySelector(".hybridddl").innerHTML},d.hdd.options[""].querySelector("input").checked=!0,d.hdd.options[""].classList.add("active")):d.value={"":d.opt.defaultText}),d.empty()&&d.reset(),d.isDS||d.updateOriginal(),d.hdd.selected.innerHTML=d.opt.selectedLabel(d.value),t&&d.emit("change")},i.updateOriginal=function(){let e=this;Object.keys(e.value).forEach(t=>{e.el.selectedIndex=-1,e.el.querySelector('option[value="'+t+'"]').selected=!0})},i.optionHover=function(){let e,t,i=this,d=arguments[0];if(d.shiftKey){if(t=d.target.classList.contains("hybriddd-option")?d.target:d.target.closest(".hybriddd-option")){switch(e=t.querySelector("input").value,d.type){case"mouseleave":i.event(i.hdd.ddlist,"remove",{mouseleave:i.hover}),e&&(i.hindex=[e]);break;case"mouseenter":i.hindex.includes(e)?(e=i.hindex[i.hindex.length-1],i.hindex=i.hindex.slice(0,-1)):e&&i.hindex.push(e),i.opt.limitSelection>0&&(i.hindex=i.hindex.slice(0,i.opt.limitSelection))}e&&!i.sindex.includes(e)&&i.hdd.options[e].classList.add("hover")}}else i.hindex.length>0&&(i.clearClass("hover"),i.hindex=[])},i.clearClass=function(e){let t=this;"string"==typeof e&&e.length>0&&t.hdd.querySelectorAll("."+e).forEach(t=>{t.classList.remove(e)})},i.openSelect=function(){let e=this,t=arguments[0];if(!e.el.disabled){if(t&&t.target){if(t.target.classList.contains("hybriddd-options")||t.target.closest(".hybriddd-options"))return;t.stopPropagation(),e.emit("hybrid-dd-"+t.type)}e.hdd.classList.contains("active")?e.closeSelect():(e.hdd.classList.add("active"),e.hdd.ddlist.style["min-width"]||(e.hdd.ddlist.style["min-width"]=e.hdd.ddlist.offsetWidth+12+"px"),e.event(document,"add",{click:e.close}),e.event(e.hdd,"add",{keyup:e.keyNav}),e.event(document,"add",{"hybrid-dd-click":e.close}))}},i.closeSelect=function(e){let t=this,i=arguments[0],d=arguments[1];if(!(i&&i.target&&i.target.classList.contains("hybriddd-option"))){if(d){if(d.target.isSameNode(t.el))return;if(t.multi&&(d.ctrlKey||d.shiftKey))return;if(d.target.parentNode.classList&&d.target.parentNode.classList.contains("hybriddd-group"))return;if(d.target.closest(".hybriddd-option"))return}t.hdd.classList.remove("active"),t.clearClass("hover"),t.hindex=[],t.event(document,"remove",{click:t.close}),t.event(document,"remove",{"hybrid-dd-click":t.close}),t.event(t.hdd,"remove",{keyup:t.keyNav}),t.listenForBulk&&(t.event(t.hdd.ddlist,"remove",{mouseenter:t.hover}),t.listenForBulk=!1),e&&t.blur()}},t}); +class HybridDDError extends Error{constructor(e){super(e),this.name="HybridDropdownError"}}!function(e){"function"==typeof define&&define.amd?define(e):"object"==typeof exports?module.exports=e():e()}(function(){let e="undefined"!=typeof window?window:this,t=e.HybridDropdown=function(e,t){if(!(e&&e instanceof Element))throw new HybridDDError("HybridDropdown requires a DOM element to intialise.");if(e.classList.contains("hybridddised")&&e._hybriddd)return console.log("WARNING: attempting instantiate element already converted to Hybrid Dropdown"),e._hybriddd;if(e.classList.contains("hybrid-dropdown"))return console.log("WARNING: attempting instantiate Hybrid Dropdown element."),e;let i=this,d=1,o=!0,l=e.getAttribute("tabindex");switch(i.isDS=!1,cnfg=Object.assign({},e.dataset),["class","id","name"].forEach(t=>{if(e.hasAttribute(t)){let i="field"+t.charAt(0).toUpperCase()+t.slice(1);cnfg[i]=e.getAttribute(t),e.removeAttribute(t)}}),!0){case"SELECT"===e.nodeName:e.multiple&&(d=-1),e.style="visibility:hidden",o=!1;break;default:if(i.isDS=!0,!t.dataSet){if(e.querySelector("script"))try{cnfg.dataSet=JSON.parse(e.querySelector("script").innerHTML)}catch(e){console.log(e.name+":"+e.message),console.log("HybridDropdown ERROR: missing or malformed json dataset")}else console.log("HybridDropdown ERROR: missing json dataset"),cnfg.dataSet=null}!cnfg.id&&cnfg.fieldName&&(cnfg.id=cnfg.fieldName),e.classList.add("hybriddd-custom")}if(cnfg&&(e._hybriddd=i),i.el=e,i.el.classList.add("hybridddised"),Object.keys(cnfg).forEach(e=>{switch(e){case"limitSelection":cnfg[e]=parseInt(cnfg[e]);break;case"treeView":case"negative":case"colourise":case"checkboxes":cnfg[e]="true"==cnfg[e]}}),i.opt=Object.assign({},{dropdown:"vertical",limitSelection:d,optionLabel:function(e){return""+e+""},selectedLabel:function(e){let t=Object.keys(e);return e[t[0]]+(t.length>1?"[...]":"")},defaultText:"---",treeView:!1,treeGlue:"/",fieldName:"hybriddd",backgroundColor:"",color:"",negative:!1,colourise:!0,checkboxes:o,tabIndex:l||0,listOption:null,selectedValues:[],fieldId:"",fieldClass:""},t,cnfg),i.opt.selectedValues&&(i.opt.selectedValues=i.opt.selectedValues.map(String)),["selectedLabel","optionLabel"].forEach(e=>{if(!(i.opt[e]&&i.opt[e]instanceof Function&&1==i.opt[e].length))throw new HybridDDError(`${e} setting must be a function with 1 argument.`)}),i.opt.listOption&&(!(i.opt.listOption instanceof Function)||2!=i.opt.listOption.length))throw new HybridDDError("listOption setting must be a function with 2 arguments.");return i.opt.treeView&&1==i.opt.limitSelection&&(i.opt.limitSelection=-1),i.multi=1!=i.opt.limitSelection,i.opt.fieldId||(i.opt.fieldId=i.opt.fieldName),i.multi&&i.opt.fieldName&&i.opt.fieldName.indexOf("[]")<0&&(i.opt.fieldName+="[]"),i.init(!0,!0,!0),i},i=t.prototype;return i.init=function(e,t,i){let d=this;if(e){d.hdd=null,d.isDS?(d.hdd=d.el,d.el.hasAttribute("tabindex")||d.hdd.setAttribute("tabindex",d.opt.tabIndex)):(d.hdd=document.createElement("div"),d.el.parentNode.insertBefore(d.hdd,d.el.nextSibling),d.hdd.style["margin-left"]="-"+d.el.getBoundingClientRect().width+"px",d.hdd.setAttribute("tabindex",d.opt.tabIndex)),d.hdd.setAttribute("id",d.opt.fieldId);let e=d.el.closest("label");e&&e.setAttribute("for",d.opt.fieldId),d.hdd.setAttribute("class",d.opt.fieldClass),d.hdd.classList.add("hybrid-dropdown"),d.hdd.selected=document.createElement("div"),d.hdd.appendChild(d.hdd.selected),d.hdd.selected.classList.add("hybriddd-selected"),d.hdd.ddlist=document.createElement("ul"),d.hdd.appendChild(d.hdd.ddlist),d.hdd.ddlist.classList.add("hybriddd-options"),d.listenForBulk=!1,d.listenModClick=!1,d.hdd.classList.add("hybriddd-"+d.opt.dropdown)}if(t){let e=null;d.hdd.options={},d.hindex=[],d.sindex=[],d.value={};try{d.isDS?(d.hdd.classList.add("hybriddd-custom"),d.opt.dataSet?e=d.buildOptionList(Object.entries(d.opt.dataSet).reverse(),0):(d.hdd.selected.innerHTML="json error",e=[])):e=d.buildOptionList(d.el.children,0),d.hdd.ddlist.replaceChildren(...e)}catch(e){if(!(e instanceof HybridDDError))throw e;console.log(e.name+":"+e.message),d.hdd.selected.innerHTML="json error"}}e&&(d.isDS||d.event(d.el,"add",{change:d.updateFromOriginal.bind(d),focus:d.originalElementFocus.bind(d)}),d.change=d.inputChange.bind(d),d.event(d.hdd.ddlist,"add",{change:d.change}),d.open=d.openSelect.bind(d),d.event(d.hdd,"add",{click:d.open}),d.close=d.closeSelect.bind(d,!0),d.blur=d.blurField.bind(d),d.hover=d.optionHover.bind(d),d.keyNav=d.keyboardNavigate.bind(d),d.event(d.hdd,"add",{keydown:d.keyNav}),d.refresh=d.refreshHybrid.bind(d),d.modClick=d.optionModClick.bind(d),d.emit("hybrid-dd-init")),d.opt.checkboxes&&d.hdd.classList.add("show-cb"),i&&d.colourise()},i.colourise=function(){let t=this,i=!1;if(!t.opt.colourise)return;if(!(e.hdd||t.opt.backgroundColor&&t.opt.color)){let d,o=!1,l=t.hdd;for(e.hdd={};!o&&l;){if(d=e.getComputedStyle(l,null),!e.hdd.bgColor&&!t.opt.backgroundColor)switch(d["background-color"]){case"transparent":case"":case"rgba(0, 0, 0, 0)":l=l.parentElement;break;default:e.hdd.bgColor=d["background-color"]}if(!e.hdd.color&&!t.opt.color)switch(d.color){case"transparent":case"":case"rgba(0, 0, 0, 0)":l=l.parentElement;break;default:e.hdd.color=d.color}e.hdd.bgColor&&e.hdd.color&&(o=!0),i=!0}o||(e.hdd.color||(e.hdd.color="#fff"),e.hdd.bgColor&&(e.hdd.bgColor="#5d5d5d"))}let d=t.opt.backgroundColor?t.opt.backgroundColor:e.hdd.bgColor,o=t.opt.color?t.opt.color:e.hdd.color;if(t.hdd.style["background-color"]=t.opt.negative?o:d,t.hdd.style.color=t.opt.negative?d:o,i){let e=document.createElement("style");e.setAttribute("id","hybriddd-colours"),e.type="text/css",e.innerText=`.hybriddd-option.active > label:hover,.hybriddd-option.hover > label,.hybriddd-option > label:hover{color:${d};background-color:${o}}:hover > input:checked+.hybridddcb::before {background-color:${d}}ul.hybriddd-options::-webkit-scrollbar-track {background:${d}} ul.hybriddd-options::-webkit-scrollbar-thumb, ul.hybriddd-options::-webkit-scrollbar{background:${o}}`,document.head.appendChild(e)}if(t.opt.backgroundColor||t.opt.color||t.opt.negative){if(t.opt.negative){let e=d;d=o,o=e}let e=document.createElement("style"),i=t.hdd.getAttribute("id");e.setAttribute("id",i+"-css"),e.type="text/css",e.innerText=`#${i} .hybriddd-option.active > label:hover,#${i} .hybriddd-option.hover > label,#${i} .hybriddd-option > label:hover{color:${d};background-color:${o}}#${i} :hover > input:checked + .hybridddl > .hybridddcb::before {color:${o}} #${i} ul.hybriddd-options::-webkit-scrollbar-track {background:${d}} #${i} ul.hybriddd-options::-webkit-scrollbar-thumb,#${i} ul.hybriddd-options::-webkit-scrollbar{background:${o}}`,document.head.appendChild(e)}},i.refreshHybrid=function(e={}){let t=this,i=!1,d=!1;if(e.hasOwnProperty("negative")||e.hasOwnProperty("colourise")||e.color||e.backgroundColor){d=!0;let e=t.el.getAttribute("id"),i=document.querySelector("style#"+e+"-css");i&&i.remove()}e.listOption&&(i=!0,e.listOption instanceof Function&&2==e.listOption.length||(console.log("Hybriddd refresh error: listOption setting must be a function with 2 arguments."),e.listOption=null)),t.opt=Object.assign({},t.opt,e),t.init(!1,i,d)},i.buildOptionList=function(e,t,i=""){let d=this,o=[],l=d.multi?"checkbox":"radio",n=d.opt.fieldName?' name="'+d.opt.fieldName+'"':"";if([].forEach.call(e,(e,s)=>{if(d.opt.listOption&&!0!==d.opt.listOption(e,s))return;let r,c,a=document.createElement("li"),h=!1,u=!1,p=!1,b=[],f="",y="";if(d.isDS)switch(r=e[0],c=null,!0){case e[1]instanceof Object&&!e[1].length:case 0===t&&e[1]instanceof Array:p=h=!0,e[1].label?(h=!1,c=e[0],f="hybridddis",r=d.opt.optionLabel(e[1].label),(b=Object.entries(e[1])).splice(Object.keys(e[1]).indexOf("label")),p=b.length>0,u=d.opt.selectedValues.indexOf(c)>=0):b=Object.entries(e[1]);break;default:f="hybridddis",c=e[0],r=d.opt.optionLabel(e[1]),u=d.opt.selectedValues.indexOf(c)>=0}else switch(e.nodeName){case"OPTGROUP":p=h=!0,r=e.label,b=e.children;break;default:f="hybridddin",r=e.text,c=e.value,u=e.selected}switch(h){case!0:a.innerHTML=""+r+"",a.classList.add("hybriddd-group");break;default:if(y="",u&&(d.value[c]=r,d.sindex.push(c),y=" checked"),a.setAttribute("tabindex","-1"),a.innerHTML=``,a.classList.value="hybriddd-option"+(u?" active":""),d.isDS||(a.classList.value+=` ${e.classList.value}`),d.hdd.options[i+c])throw new HybridDDError("Option list has duplicate value: "+c);d.hdd.options[i+c]=a}if(p){d.opt.treeView&&c&&(i+=c+d.opt.treeGlue);let e=d.buildOptionList(b,t+1,i),o=document.createElement("ul");o.replaceChildren(...e),a.appendChild(o)}o[o.length]=a}),0==t){d.empty()&&d.reset();let e=Object.keys(d.value);e.length>1&&e.indexOf("")>=0&&delete d.value[""],d.hdd.selected.innerHTML=d.opt.selectedLabel(d.value)}return o},i.empty=function(){let e=this;for(let t in e.value)return!1;return!0},i.reset=function(){let e=this;e.sindex=[],e.hdd.options[""]?(e.hdd.options[""].classList.add("active"),e.value[""]=e.hdd.options[""].querySelector(".hybridddl").innerHTML,e.hdd.options[""].querySelector("input").checked=!0,e.sindex.push("")):e.value[""]=e.opt.defaultText},i.event=function(e,t,i){var d=e[t+"EventListener"].bind(e);Object.keys(i).forEach(function(e){["mouseenter","mouseleave"].indexOf(e)>=0?d(e,i[e],!0):d(e,i[e])})},i.updateFromOriginal=function(){let e=this,t=[];if(!e.isDS){if(e.multi){let i=!0;for(let d=0;d0?t.indexOf(e[e.length-1]):-1;return i0?t.indexOf(e[0]):t.length;return i>=0&&i--,i<0&&(i=t.length-1),t[i]},i.keyboardNavigate=function(){let e,t=this,i=arguments[0];if(!t.el.disabled&&i&&i.keyCode)switch(i.preventDefault(),!0){case 40==i.keyCode&&"keydown"==i.type:t.hdd.classList.contains("active")?((t.hindex.length>0&&!t.multi||!i.shiftKey)&&t.clearClass("hover"),e=t.nextOption(t.hindex),t.hdd.options[e].classList.add("hover"),t.scroll(e,"down"),t.multi&&i.shiftKey?(t.hindex.push(e),t.opt.limitSelection>0&&(t.hindex=t.hindex.slice(-1*t.opt.limitSelection))):t.hindex[0]=e):1==t.opt.limitSelection?(e=t.nextOption(t.sindex),t.toggleValue([e],!0)):(t.hdd.classList.remove("focus"),t.open());break;case 38==i.keyCode&&"keydown"==i.type:if(t.hdd.classList.contains("active"))(t.hindex.length>0&&!t.multi||!i.shiftKey)&&t.clearClass("hover"),e=t.prevOption(t.hindex),t.hdd.options[e].classList.add("hover"),t.scroll(e,"up"),t.multi&&i.shiftKey?(t.hindex.push(e),t.opt.limitSelection>0&&(t.hindex=t.hindex.slice(-1*t.opt.limitSelection))):t.hindex[0]=e;else if(1==t.opt.limitSelection){let e=t.prevOption(t.sindex);t.toggleValue([e],!0)}else t.hdd.classList.remove("focus"),t.open();break;case 13==i.keyCode&&"keydown"==i.type:case 32==i.keyCode&&"keydown"==i.type:!1===t.hdd.classList.contains("active")?(t.hdd.classList.remove("focus"),t.open()):(t.hindex.length>0&&t.toggleValue(t.hindex,!0),t.closeSelect(!1),t.hdd.classList.add("focus"));break;case 27==i.keyCode&&"keydown"==i.type:t.hdd.classList.contains("active")&&t.closeSelect(!1),t.blurField();break;case 9==i.keyCode&&"keydown"==i.type:t.hdd.classList.contains("active")&&t.closeSelect(!1),t.blurField();let d,o,l=1*t.el.getAttribute("tabindex"),n=t.el.closest("form");if(null===n&&(n=document),null!=l&&""!=l)l+=1,d=n.querySelector('[tabindex="'+l+'"]');else if(n!==document)for(l in o=t.el,t.isDS&&(l=(o=Object.keys(t.hdd.options))[o.length-1],o=t.hdd.options[l].querySelector("input")),n.elements)if(n.elements[l]===o){do{l++}while(lo.bottom)switch(t){case"up":o.top>d.top?i.hdd.ddlist.scrollTop-=d.height:o.bottomd.top&&(i.hdd.ddlist.scrollTop=0)}},i.inputChange=function(){let e=this,t=arguments[0],i=[],d=!1;if(t&&t.target){if(d=t.target.classList.contains("mod-ctrl"),"change"==t.type&&d)return t.target.classList.remove("mod-ctrl"),!1;if(e.hindex.length>0&&(i=[...e.hindex]),t.target.checked&&""==t.target.value)d||e.removeValue([...e.sindex],!0);else if(e.opt.treeView&&(e.opt.limitSelection<0||e.opt.limitSelection>e.sindex.length)){let o,l,n=!0,s=t.target.closest(".hybriddd-option"),r=s.querySelectorAll("input");for(l of r.values())l.checked=t.target.checked,d||(t.target.checked?i.push(l.value):e.removeValue([l.value])),l.closest(".hybriddd-option").classList.remove("partial");for(;s;)l=s.querySelector("input"),n||l.checked||s.querySelectorAll("label input").length-s.querySelectorAll("label input:checked").length!=1||(l.checked=!0,i.push(l.value),s.classList.remove("partial")),r=s.querySelectorAll("input:checked"),o=s.querySelectorAll("input"),l.checked&&1==r.length&&o.length>1?(s.classList.remove("partial"),l.checked=!1,d||e.removeValue([l.value])):r.length>0&&o.length!=r.length?(s.classList.add("partial"),d||e.removeValue([l.value])):s.classList.remove("partial"),s=s.parentNode.closest(".hybriddd-option"),n=!1;d||e.addValue(i,!0)}else d||(i.push(t.target.value),t.target.checked?e.addValue(i,!0):e.removeValue(i,!0));switch(!0){case"hybrid-ddi-change"==t.type:case t.shiftKey&&1==t.shiftKey:break;default:e.closeSelect(!1)}}},i.toggleValue=function(e,t){let i=this;i.hdd.options[e[0]].querySelector("input").checked?i.removeValue(e,t):i.addValue(e,t)},i.addValue=function(e,t=!1){let i=this;switch(i.opt.limitSelection){case 1:i.sindex.length>0&&i.hdd.options[i.sindex[0]].classList.remove("active"),i.sindex=e;break;default:if(1==i.sindex.length&&0==i.sindex[0].length?(i.hdd.options[""].classList.remove("active"),i.hdd.options[""].querySelector("input").checked=!1,i.sindex=e):i.sindex=i.sindex.concat(e),i.opt.limitSelection>0){for(let e=i.opt.limitSelection;e{i.value[e]=i.hdd.options[e].querySelector(".hybridddl").innerHTML,i.hdd.options[e].querySelector("input").checked=!0,i.hdd.options[e].classList.add("active")}),i.empty()&&i.reset(),i.isDS||i.updateOriginal(),i.hdd.selected.innerHTML=i.opt.selectedLabel(i.value),t&&i.emit("change")},i.removeValue=function(e,t=!1){let i,d=this;e.forEach(e=>{d.hdd.options[e].classList.remove("active"),d.hdd.options[e].querySelector("input").checked=!1,(i=d.sindex.indexOf(e))>=0&&(d.sindex.splice(i,1),delete d.value[e])}),0==d.sindex.length&&(d.hdd.options[""]?(d.sindex=[""],d.value={"":d.hdd.options[""].querySelector(".hybridddl").innerHTML},d.hdd.options[""].querySelector("input").checked=!0,d.hdd.options[""].classList.add("active")):d.value={"":d.opt.defaultText}),d.empty()&&d.reset(),d.isDS||d.updateOriginal(),d.hdd.selected.innerHTML=d.opt.selectedLabel(d.value),t&&d.emit("change")},i.updateOriginal=function(){let e=this;Object.keys(e.value).forEach(t=>{e.el.selectedIndex=-1,e.el.querySelector('option[value="'+t+'"]').selected=!0})},i.optionHover=function(){let e,t,i=this,d=arguments[0];if(d.shiftKey){if(t=d.target.classList.contains("hybriddd-option")?d.target:d.target.closest(".hybriddd-option")){switch(e=t.querySelector("input").value,d.type){case"mouseleave":i.event(i.hdd.ddlist,"remove",{mouseleave:i.hover}),e&&(i.hindex=[e]);break;case"mouseenter":i.hindex.includes(e)?(e=i.hindex[i.hindex.length-1],i.hindex=i.hindex.slice(0,-1)):e&&i.hindex.push(e),i.opt.limitSelection>0&&(i.hindex=i.hindex.slice(0,i.opt.limitSelection))}e&&!i.sindex.includes(e)&&i.hdd.options[e].classList.add("hover")}}else i.hindex.length>0&&(i.clearClass("hover"),i.hindex=[])},i.clearClass=function(e){let t=this;"string"==typeof e&&e.length>0&&t.hdd.querySelectorAll("."+e).forEach(t=>{t.classList.remove(e)})},i.openSelect=function(){let e=this,t=arguments[0];if(!e.el.disabled){if(t&&t.target){if(t.target.classList.contains("hybriddd-options")||t.target.closest(".hybriddd-options"))return;t.stopPropagation(),e.emit("hybrid-dd-"+t.type)}e.hdd.classList.contains("active")?e.closeSelect():(e.hdd.classList.add("active"),e.hdd.ddlist.style["min-width"]||(e.hdd.ddlist.style["min-width"]=e.hdd.ddlist.offsetWidth+12+"px"),e.event(document,"add",{click:e.close}),e.event(e.hdd,"add",{keyup:e.keyNav}),e.event(document,"add",{"hybrid-dd-click":e.close}))}},i.closeSelect=function(e){let t=this,i=arguments[0],d=arguments[1];if(!(i&&i.target&&i.target.classList.contains("hybriddd-option"))){if(d){if(d.target.isSameNode(t.el))return;if(t.multi&&(d.ctrlKey||d.shiftKey))return;if(d.target.parentNode.classList&&d.target.parentNode.classList.contains("hybriddd-group"))return;if(d.target.closest(".hybriddd-option"))return}t.hdd.classList.remove("active"),t.clearClass("hover"),t.hindex=[],t.event(document,"remove",{click:t.close}),t.event(document,"remove",{"hybrid-dd-click":t.close}),t.event(t.hdd,"remove",{keyup:t.keyNav}),t.listenForBulk&&(t.event(t.hdd.ddlist,"remove",{mouseenter:t.hover}),t.listenForBulk=!1),e&&t.blur()}},t});