-
Notifications
You must be signed in to change notification settings - Fork 0
/
faceted-filtering.min.js
1 lines (1 loc) · 4.26 KB
/
faceted-filtering.min.js
1
var hidableElems,facetPlaceholderElems,facetButtonClass="btn-xs btn btn-default",hasFiltersInURL=!1;function initFacets(){hidableElems||(hidableElems=$(".hidable"),facetPlaceholderElems=$(".facet-placeholder"),initSelectionFromUrl(),updateFacets())}function reloadFacets(){hidableElems=$(".hidable"),facetPlaceholderElems=$(".facet-placeholder"),updateFacets()}function updateFacets(){facetPlaceholderElems.each(function(){var e=$(this),t=e.attr("data-selected-facets"),a=void 0!==t&&t.length>0,i=e.data("facet-type"),l="data-facet-"+i,r=facetRetrievalClasses(i),n=$(r+" ["+l+"]");if("*"!=r){var c=$(r).parents("["+l+"]").addBack("["+l+"]");n=$.merge(c,n)}var s=n.map(function(){return this.getAttribute(l+"-order")+"%%"+this.getAttribute(l)});if(valArraySorted=$(s.sort()).map(function(){return this.replace(/([^%]*%%)?(.*)/,"$2")}),a){n=$(t.split("%%%%")).map(function(){return this.replace(/%%/g,"")});$.merge(valArraySorted,n)}var o=$(unique(valArraySorted)).map(function(){if(this.length<1)return"";var e="%%"+this+"%%";return'<div class="'+(facetButtonClass+" facet-"+(a&&t.indexOf(e)>-1))+'">'+this+"</div>"}).get().join("");o.length<1&&(o='<span class="no-facets">Nothing to filter</span>'),$(this).html(o),$(this).children().click(toggleFacet)})}function facetRetrievalClasses(e){return"*"+facetPlaceholderElems.filter('[data-selected-facets*="%%"][data-facet-type!="'+e+'"]').map(function(){return".facet-selected-"+$(this).data("facet-type")}).get().join("")}function unique(e){return $.grep(e,function(t,a){return a===$.inArray(t,e)})}function initSelectionFromUrl(){var e=window.location.search;e.length&&(facetPlaceholderElems.each(function(){var t=$(this).data("facet-type"),a=encodeURIComponent(t),i=new RegExp("(&|\\?)"+a+"=([^&]*)");if(match=i.exec(e),null!=match){hasFiltersInURL=!0;var l="%%"+decodeURIComponent(match[2]).split("+").join("%%%%")+"%%";$(this).attr("data-selected-facets",searchStringUnEscape(l))}}),filterFacets())}function toggleFacet(e){var t=e.currentTarget.textContent;doToggleFacet($(e.currentTarget).parent(".facet-placeholder"),t)}function doToggleFacet(e,t){var a=e.data("facet-type"),i=e.attr("data-selected-facets"),l="%%"+t+"%%",r=isInitialFilter(a,t);if(i||(i=""),i.indexOf(l)>-1)r||e.attr("data-selected-facets",i.replace(l,""));else{var n=i+l;e.attr("data-selected-facets",n)}filterFacets(),updateFacets(),r&&removeInitialFilter(a,t)}function searchStringEscape(e){return e.replace("+","\\plus")}function searchStringUnEscape(e){return e.replace("\\plus,","+")}var initialFilters={};function filterKey(e,t){return"filtered-"+e+"-"+t}function addInitialFilter(e,t){hasFiltersInURL||(initialFilters[filterKey(e,t)]=!0,doToggleFacet(facetPlaceholderElems.filter("[data-facet-type="+e+"]").first(),t))}function removeInitialFilter(e,t){initialFilters[filterKey(e,t)]=!1}function isInitialFilter(e,t){return!0===initialFilters[filterKey(e,t)]}function filterFacets(){var e=document.location.search,t="*";if(facetPlaceholderElems.filter("[data-selected-facets]").each(function(){var a=$(this).data("facet-type"),i="data-facet-"+a,l=/%%([^%]+)%%/g,r=$(this).attr("data-selected-facets");match=l.exec(r);var n=[],c=[];if(r.length>0)for(;null!=match;)isInitialFilter(a,match[1])||c.push(searchStringEscape(match[1])),n.push("["+i+'="'+match[1].replace('"','\\"')+'"]'),match=l.exec(r);e=insertParam2(e,a,c.join("+"));var s="facet-selected-"+a;if(hidableElems.removeClass(s),n.length){var o=n.join(","),d=$(o);d.closest(".hidable").addClass(s),d.find(".hidable:not(:has(.hidable))").addClass(s),t+="."+s}}),e!=document.location.search){var a=window.location.protocol+"//"+window.location.host+window.location.pathname+e+window.location.hash;history.replaceState(null,document.title+" - filtered",a)}hidableElems.attr("data-is-visible",!1),"*"!=t?hidableElems.filter(t).attr("data-is-visible",!0).parents(".hidable").attr("data-is-visible",!0):hidableElems.filter(t).attr("data-is-visible",!0),hidableElems.filter("[data-is-visible!=true]").hide(),hidableElems.filter("[data-is-visible=true]").show()}function insertParam2(e,t,a){t=encodeURIComponent(t),a=encodeURIComponent(a);var i=e.length?e.substring(1,e.length):"",l=a.length?t+"="+a:"",r=new RegExp("(^|&)"+t+"=[^&]*");return i=l.length?i.replace(r,"$1"+l):i.replace(r,""),l.length&&i.indexOf(l)<0&&(i+=(i.length>0?"&":"")+l),"?"+i}$("document").ready(initFacets);