{"version":3,"file":"facetwp.min.js","sources":["../../../assets/js/facetwp.js"],"sourcesContent":["import closeSvg from '../../assets/svg/close.svg';\n\ndocument.addEventListener('DOMContentLoaded', function() {\n\tconst fwp = window.FWP;\n\n\tconst facetsNode = document.querySelector('.facets');\n\tlet facetsActiveNode = document.querySelector('.facets-active');\n\n\tif ( ! facetsActiveNode ) {\n\t\tfacetsActiveNode = document.createElement('div');\n\t\tfacetsActiveNode.className = 'facets-active';\n\t\tfacetsNode.insertAdjacentElement( 'afterend', facetsActiveNode );\n\t}\n\n\tif ( ! facetsNode ) {\n\t\treturn;\n\t}\n\n\tconst resetLabel = function( node ) {\n\t\tconst { settings } = fwp;\n\t\tconst name = node.dataset.name;\n\n\t\tif ( ! name ) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst label = settings.labels[ name ];\n\n\t\tif ( ! label ) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst labelNode = node.querySelector('.fs-label');\n\n\t\tif ( ! labelNode ) {\n\t\t\treturn;\n\t\t}\n\n\t\tlabelNode.innerHTML = label;\n\t}\n\n\tdocument.addEventListener('facetwp-refresh', () => {\n\t\tfacetsActiveNode.classList.add( 'facets-active--loading' );\n\t} );\n\n\tfwp.hooks.addAction( 'facetwp/loaded', (e) => {\n\t\tconst { facets, facet_type, settings } = fwp;\n\n\t\tdocument.querySelectorAll('.facetwp-facet[data-type=\"fselect\"]').forEach( resetLabel );\n\n\t\t// close the dropdowns after one has been selected\n\t\tfacetsNode.querySelectorAll('.fs-open').forEach( node => node.classList.remove('fs-open') );\n\t\tfacetsNode.querySelectorAll('.fs-dropdown').forEach( node => node.classList.add('hidden') );\n\n\t\tfacetsActiveNode.classList.remove( 'facets-active--loading' );\n\t\tfacetsActiveNode.innerHTML = '';\n\n\t\tconst fselects = Object.keys(facet_type).filter(facet => facet_type[facet] === 'fselect');\n\n\t\tconst activeFacetCount = fselects.reduce( ( carry, facet ) => carry + facets[ facet ].length, 0 );\n\n\n\t\tfselects.forEach( facet => {\n\t\t\tfacets[ facet ].forEach( filter => {\n\t\t\t\tconst removeButton = document.createElement('button');\n\t\t\t\tconst option = document.querySelector( `.facetwp-facet[data-name=\"${facet}\"] .fs-option[data-value=\"${filter}\"] .fs-option-label` );\n\t\t\t\tconst label = option ? option.innerText.trim().replace(/\\(\\d+\\)$/, '') : filter;\n\n\t\t\t\tremoveButton.type = 'button';\n\t\t\t\tremoveButton.className = 'facet-remove facet-remove--one';\n\t\t\t\tremoveButton.insertAdjacentHTML( 'afterbegin', `${label} ${closeSvg}` );\n\t\t\t\tremoveButton.addEventListener( 'click', () => {\n\t\t\t\t\tfacetsActiveNode.classList.add( 'facets-active--loading' );\n\n\t\t\t\t\twindow.FWP.facets[ facet ] = facets[ facet ].filter( f => f !== filter );\n\t\t\t\t\twindow.FWP.fetchData();\n\t\t\t\t\twindow.FWP.setHash();\n\n\t\t\t\t\tif ( window.FWP.last_active_facet ) {\n\t\t\t\t\t\tdelete window.FWP.last_active_facet;\n\t\t\t\t\t}\n\t\t\t\t} );\n\n\t\t\t\tfacetsActiveNode.appendChild( removeButton );\n\t\t\t} );\n\t\t} );\n\n\t\tif ( activeFacetCount > 1 ) {\n\t\t\tconst clearButton = document.createElement('button');\n\t\t\tclearButton.type = 'button';\n\t\t\tclearButton.className = 'facet-remove facet-remove--all';\n\t\t\tclearButton.insertAdjacentHTML( 'afterbegin', `Clear All ${closeSvg}` );\n\t\t\tclearButton.addEventListener( 'click', () => fwp.reset() );\n\n\t\t\tfacetsActiveNode.appendChild( clearButton );\n\t\t}\n\n\t\t// scroll the body back up to the facets if we're below (e.g. pagination)\n\t\tif ( facetsNode && facetsNode.getBoundingClientRect().top < 0 && window.wdg.scripts.header.headroom ) {\n\t\t\t// unpin and freeze the header so we don't cover up the facets when we scroll back up\n\t\t\tconst { headroom } = window.wdg.scripts.header;\n\t\t\theadroom.freeze();\n\t\t\theadroom.unpin();\n\n\t\t\tfacetsNode.scrollIntoView( { behavior: 'smooth' } );\n\n\t\t\t// there's no callback for scrollIntoView - the just wait an arbirtary time to unfreeze the header\n\t\t\tsetTimeout( () => headroom.unfreeze(), 1000 );\n\t\t}\n\t} );\n} );\n"],"names":["document","addEventListener","fwp","window","FWP","facetsNode","querySelector","facetsActiveNode","createElement","className","insertAdjacentElement","resetLabel","node","settings","name","dataset","label","labels","labelNode","innerHTML","classList","add","hooks","addAction","e","facets","facet_type","querySelectorAll","forEach","remove","fselects","Object","keys","filter","facet","activeFacetCount","reduce","carry","length","removeButton","option","innerText","trim","replace","type","insertAdjacentHTML","closeSvg","f","fetchData","setHash","last_active_facet","appendChild","clearButton","reset","getBoundingClientRect","top","wdg","scripts","header","headroom","freeze","unpin","scrollIntoView","behavior","setTimeout","unfreeze"],"mappings":"8RAEAA,SAASC,iBAAiB,oBAAoB,WAC7C,IAAMC,EAAMC,OAAOC,IAEbC,EAAaL,SAASM,cAAc,WACtCC,EAAmBP,SAASM,cAAc,kBAQzC,GANEC,KACNA,EAAmBP,SAASQ,cAAc,QACzBC,UAAY,gBAC7BJ,EAAWK,sBAAuB,WAAYH,IAGxCF,EAAF,CAIL,IAAMM,EAAa,SAAUC,GAC5B,IAAQC,EAAaX,EAAbW,SACFC,EAAOF,EAAKG,QAAQD,KAErB,GAAEA,EAAF,CAIL,IAAME,EAAQH,EAASI,OAAQH,GAE1B,GAAEE,EAAF,CAIL,IAAME,EAAYN,EAAKN,cAAc,aAE9BY,IAIPA,EAAUC,UAAYH,MAGvBhB,SAASC,iBAAiB,mBAAmB,WAC5CM,EAAiBa,UAAUC,IAAK,6BAGjCnB,EAAIoB,MAAMC,UAAW,kBAAkB,SAACC,GACvC,IAAQC,EAAiCvB,EAAjCuB,OAAQC,EAAyBxB,EAAzBwB,WAAyBxB,EAAbW,SAE5Bb,SAAS2B,iBAAiB,uCAAuCC,QAASjB,GAG1EN,EAAWsB,iBAAiB,YAAYC,SAAS,SAAAhB,GAAI,OAAIA,EAAKQ,UAAUS,OAAO,cAC/ExB,EAAWsB,iBAAiB,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