/
choicefilter.min.js
1 lines (1 loc) · 11.6 KB
/
choicefilter.min.js
1
var ChoiceFilter=function(t,e,i){this.settings={},this.attributes={elemId:"data-chf-id",elemFilters:"data-chf-filters",elemWrapper:"data-chf-wrapper",elemDependsOn:"data-chf-dpnds2",elemIndependent:"data-chf-indpndt",elemPresentOnEveryParent:"data-chf-prst-prt",elemValueHolder:"data-chf-val",elemListValueHolder:"data-chf-vlhld",elemValueDelimiter:"data-chf-dlmtr",elemSelectAfterFilter:"data-chf-pst-filt",elemHolderControls:"data-chf-cntrls",elemIgnoreValue:"data-chf-ingr-vl"},this.defaults={resetPrevious:!0,autoFilterOnInit:!0,valueDelimiter:",",wrapperSelector:null,dataOnly:!1,independentChoices:[],showAllChildChoicesWhen:[],autoSelectedChoicesAfterFilter:[],presentOnEveryParent:[],forceResetAllChain:!1,parentControlsVisibilityOf:null,ignoreParentValues:[]},this.settings=this.extend(this.settings,this.defaults),this.settings=this.extend(this.settings,i),this.data=this.mapData(this.settings.data),this.settings.independentChoices=this.parseValueToArray(this.settings.independentChoices),this.settings.presentOnEveryParent=this.parseValueToArray(this.settings.presentOnEveryParent),this.settings.autoSelectedChoicesAfterFilter=this.parseValueToArray(this.settings.autoSelectedChoicesAfterFilter),this.settings.showAllChildChoicesWhen=this.parseValueToArray(this.settings.showAllChildChoicesWhen),this.settings.ignoreParentValues=this.parseValueToArray(this.settings.ignoreParentValues),this.parent=t instanceof HTMLElement?[t]:t,this.child=e instanceof HTMLElement?[e]:e,this.parentType=this.parent[0].type,this.childType=this.child[0].type};ChoiceFilter.prototype.init=function(){switch(this.parentType){case"select-one":case"select-multiple":this.initParentSelect();break;case"radio":case"checkbox":this.initParentInput()}switch(this.childType){case"select-one":case"select-multiple":this.initChildSelect();break;case"radio":case"checkbox":this.initChildInputItems()}if(this.settings.autoFilterOnInit){var t=document.createEvent("Event");t.initEvent("change",!1,!0),this.parent[0].dispatchEvent(t)}},Object.prototype.filtchoices=function(t,e){return new ChoiceFilter(this,t,e).init(),t},ChoiceFilter.prototype.initParentSelect=function(){var t=this.parent[0];t.hasAttribute(this.attributes.elemId)||(t.setAttribute(this.attributes.elemId,this.generateId()),this.addChangeListener(t));for(var e=t.querySelectorAll("option"),i=0;i<e.length;i++)this.settings.resetPrevious&&this.dropParentItemProps(e[i]),this.setParentItemProps(e[i])},ChoiceFilter.prototype.initChildSelect=function(){var t,e=this.child[0];e.hasAttribute(this.attributes.elemId)?(t=e.getAttribute(this.attributes.elemId),this.disconnectPreviousParent(t)):(t=this.generateId(),e.setAttribute(this.attributes.elemId,t),this.addChangeListener(e));for(var i=e.querySelectorAll("option"),r=0;r<i.length;r++)this.settings.resetPrevious&&this.dropChildItemProps(i[r]),this.setChildItemProps(i[r]);for(r=0;r<this.parent.length;r++)this.parent[r].setAttribute(this.attributes.elemFilters,t);this.setHolderAttributes(e)},ChoiceFilter.prototype.disconnectPreviousParent=function(t){for(var e=document.querySelectorAll(this.getAttrEq(this.attributes.elemFilters,t)),i=0;i<e.length;i++)e[i].removeAttribute(this.attributes.elemFilters)},ChoiceFilter.prototype.initParentInput=function(){var t,e=this.parent[0];t=e.hasAttribute(this.attributes.elemId)?e.getAttribute(this.attributes.elemId):this.generateId();for(var i=0;i<this.parent.length;i++)this.parent[i].hasAttribute(this.attributes.elemFilters)||this.addChangeListener(this.parent[i]),this.parent[i].setAttribute(this.attributes.elemId,t),this.settings.resetPrevious&&this.dropParentItemProps(this.parent[i]),this.setParentItemProps(this.parent[i]);this.generateElemListValueHolder(e,t)},ChoiceFilter.prototype.initChildInputItems=function(){var t,e=this.child[0];e.hasAttribute(this.attributes.elemId)?(t=e.getAttribute(this.attributes.elemId),this.disconnectPreviousParent(t)):t=this.generateId();for(var i=0;i<this.child.length;i++)this.child[i].setAttribute(this.attributes.elemId,t),this.setChildItemProps(this.child[i]);for(i=0;i<this.parent.length;i++)this.parent[i].setAttribute(this.attributes.elemFilters,t);var r=this.generateElemListValueHolder(this.child[0],t);this.setHolderAttributes(r[0])},ChoiceFilter.prototype.setParentItemProps=function(t){var e=t.getAttribute("value");this.setBinaryAttr(t,this.settings.ignoreParentValues,e,this.attributes.elemIgnoreValue)},ChoiceFilter.prototype.setChildItemProps=function(t){var e=t.getAttribute("value");this.setBinaryAttr(t,this.settings.independentChoices,e,this.attributes.elemIndependent),this.setBinaryAttr(t,this.settings.presentOnEveryParent,e,this.attributes.elemPresentOnEveryParent),this.setBinaryAttr(t,this.settings.autoSelectedChoicesAfterFilter,e,this.attributes.elemSelectAfterFilter),this.data.hasOwnProperty(e)?this.data[e]=this.data[e].concat(this.settings.showAllChildChoicesWhen):this.data[e]=this.settings.showAllChildChoicesWhen,this.data.hasOwnProperty(e)&&this.data[e].length>0&&t.setAttribute(this.attributes.elemDependsOn,this.data[e])},ChoiceFilter.prototype.dropParentItemProps=function(t){t.removeAttribute(this.attributes.elemIgnoreValue)},ChoiceFilter.prototype.dropChildItemProps=function(t){t.removeAttribute(this.attributes.elemDependsOn),t.removeAttribute(this.attributes.elemIndependent),t.removeAttribute(this.attributes.elemPresentOnEveryParent),t.removeAttribute(this.attributes.elemSelectAfterFilter)},ChoiceFilter.prototype.setHolderAttributes=function(t){!0!==this.settings.dataOnly&&(t.removeAttribute(this.attributes.elemWrapper),this.settings.wrapperSelector!=this.defaults.wrapperSelector&&t.setAttribute(this.attributes.elemWrapper,this.settings.wrapperSelector),t.removeAttribute(this.attributes.elemValueDelimiter),this.settings.valueDelimiter!=this.defaults.valueDelimiter&&t.setAttribute(this.attributes.elemValueDelimiter,this.settings.valueDelimiter),t.removeAttribute(this.attributes.elemHolderControls),this.settings.parentControlsVisibilityOf!=this.defaults.parentControlsVisibilityOf&&t.setAttribute(this.attributes.elemHolderControls,this.settings.parentControlsVisibilityOf))},ChoiceFilter.prototype.mapData=function(t){var e=[];for(parent_val in t)if(t.hasOwnProperty(parent_val))for(var i=0;i<t[parent_val].length;i++)e.hasOwnProperty(t[parent_val][i])?e[t[parent_val][i]].push(parent_val):e[t[parent_val][i]]=[parent_val];return e},ChoiceFilter.prototype.generateId=function(){return void 0===ChoiceFilter.chfid&&(ChoiceFilter.chfid=0),ChoiceFilter.chfid++,ChoiceFilter.chfid},ChoiceFilter.prototype.generateElemListValueHolder=function(t,e){var i=t.parentNode.querySelectorAll("input"+this.getAttrEq(this.attributes.elemListValueHolder,e));return 0===i.length&&((i=document.createElement("input")).style.display="none",i.setAttribute("type","text"),i.setAttribute("disabled","disabled"),i.setAttribute(this.attributes.elemListValueHolder,e),t.parentNode.insertBefore(i,t)),i instanceof HTMLElement?[i]:i},ChoiceFilter.prototype.addChangeListener=function(t){var e=this;t.addEventListener("change",function(t){e.valueHandler(this),e.filter(this),t.preventDefault()},!1)},ChoiceFilter.prototype.filter=function(t){if(this.hasDependent(t)){var e=this.getDependent(t);switch(e.type){case"select-one":case"select-multiple":this.filterSelectItems(e,t);break;case"radio":case"checkbox":this.filterInputItems(e,t)}this.filter(e)}},ChoiceFilter.prototype.filterSelectItems=function(t,e){var i=this.getValueType(e),r=this.getHolderProps(t);this.controledElems(r,i);for(var s=t.querySelectorAll("option"),l="",a=!0,n=0;n<s.length;n++){var h=this.getStyle(s[n],i,r.delimiter);s[n].style.display=h.style,s[n].disabled=h.disabled,r.hasWrapper&&(this.closest(s[n],r.wrapper).style.display=h.style),h.isNotActive?s[n].selected=!1:t.value==s[n].value&&(a=!1),""==l&&""==h.style&&this.attrEnabled(s[n],this.attributes.elemSelectAfterFilter)&&(l=s[n].getAttribute("value"))}a&&(t.value=l)},ChoiceFilter.prototype.filterInputItems=function(t,e){var i=this.getValueType(e),r=t.getAttribute(this.attributes.elemId),s=document.querySelectorAll("input"+this.getAttrEq(this.attributes.elemId,r)),l=s[0].parentNode.querySelectorAll("input"+this.getAttrEq(this.attributes.elemListValueHolder,r)),a=this.getHolderProps(l[0]);this.controledElems(a,i);for(var n=0;n<s.length;n++){var h=this.getStyle(s[n],i,a.delimiter);if(s[n].hasAttribute("id")){var o=document.querySelectorAll('label[for="'+s[n].getAttribute("id")+'"]');o.length>0&&(o[0].style.display=h.style)}s[n].style.display=h.style,h.isNotActive?s[n].checked=!1:this.attrEnabled(s[n],this.attributes.elemSelectAfterFilter)&&(s[n].checked=!0),a.hasWrapper&&(this.closest(s[n],a.wrapper).style.display=h.style)}l[0].setAttribute(this.attributes.elemValueHolder,"")},ChoiceFilter.prototype.getHolderProps=function(t){return{hasWrapper:t.hasAttribute(this.attributes.elemWrapper),wrapper:t.getAttribute(this.attributes.elemWrapper),delimiter:t.hasAttribute(this.attributes.elemValueDelimiter)?t.getAttribute(this.attributes.elemValueDelimiter):this.defaults.valueDelimiter,controlElem:t.hasAttribute(this.attributes.elemHolderControls)?t.getAttribute(this.attributes.elemHolderControls):this.defaults.parentControlsVisibilityOf}},ChoiceFilter.prototype.hasDependent=function(t){return t.hasAttribute(this.attributes.elemFilters)},ChoiceFilter.prototype.getDependent=function(t){var e=t.getAttribute(this.attributes.elemFilters);return document.querySelectorAll(this.getAttrEq(this.attributes.elemId,e))[0]},ChoiceFilter.prototype.valueHandler=function(t){switch(t.type){case"radio":case"checkbox":for(var e=[],i=t.getAttribute(this.attributes.elemId),r=document.querySelectorAll("input"+this.getAttrEq(this.attributes.elemId,i)),s=0;s<r.length;s++)r[s].checked&&e.push(r[s].getAttribute("value"));r[0].parentNode.querySelectorAll("input"+this.getAttrEq(this.attributes.elemListValueHolder,i))[0].setAttribute(this.attributes.elemValueHolder,e.join())}},ChoiceFilter.prototype.getValueType=function(t){var e=[];switch(t.type){case"select-one":case"select-multiple":for(var i,r=t&&t.options,s=0,l=r.length;s<l;s++)(i=r[s]).selected&&!this.attrEnabled(i,this.attributes.elemIgnoreValue)&&e.push(i.value);case"radio":case"checkbox":var a=t.getAttribute(this.attributes.elemId),n=document.querySelectorAll(this.getAttrEq(this.attributes.elemId,a));for(s=0;s<n.length;s++)n[s].checked&&!this.attrEnabled(n[s],this.attributes.elemIgnoreValue)&&e.push(n[s].getAttribute("value"))}return e},ChoiceFilter.prototype.getStyle=function(t,e,i){var r="",s=!0,l=!1;if(this.attrEnabled(t,this.attributes.elemIndependent)||e.length>0&&this.attrEnabled(t,this.attributes.elemPresentOnEveryParent))return{style:r,isNotActive:l};if(r="none",l=!0,t.hasAttribute(this.attributes.elemDependsOn)){var a=t.getAttribute(this.attributes.elemDependsOn);a=a.split(i);for(var n=0;n<a.length;n++)if(e.indexOf(a[n])>-1){r="",l=!1,s=!1;break}}return{style:r,isNotActive:l,disabled:s}},ChoiceFilter.prototype.extend=function(t,e){for(var i in e)e.hasOwnProperty(i)&&(t[i]=e[i]);return t},ChoiceFilter.prototype.attrEnabled=function(t,e){return 1===parseInt(t.getAttribute(e))},ChoiceFilter.prototype.setBinaryAttr=function(t,e,i,r){this.settings.dataOnly&&0===e.length||(e.indexOf(i)>-1?t.setAttribute(r,1):t.setAttribute(r,0))},ChoiceFilter.prototype.parseValueToArray=function(t){return t instanceof Array||(t=[t]),t.length>0&&(t=t.join().split(",")),t},ChoiceFilter.prototype.controledElems=function(t,e){if(t.controlElem!==this.defaults.parentControlsVisibilityOf)for(var i=0===e.length?"none":"",r=document.querySelectorAll(t.controlElem),s=0;s<r.length;s++)r[s].style.display=i},ChoiceFilter.prototype.getAttrEq=function(t,e){return"["+t+"='"+e+"']"},ChoiceFilter.prototype.closest=function(t,e){var i=Element.prototype;for(i.matches=i.matches||i.webkitMatchesSelector||i.msMatchesSelector||i.mozMatchesSelector;t!==document.body;)if((t=t.parentElement).matches(e))return t};