-
Notifications
You must be signed in to change notification settings - Fork 14
/
jquery.indextank.facetsrenderer.js
145 lines (117 loc) · 5.73 KB
/
jquery.indextank.facetsrenderer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
(function($){
if(!$.Indextank){
$.Indextank = new Object();
};
$.Indextank.FacetsRenderer = function(el, options){
// To avoid scope issues, use 'base' instead of 'this'
// to reference this class from internal events and functions.
var base = this;
// Access to jQuery and DOM versions of element
base.$el = $(el);
base.el = el;
// Add a reverse reference to the DOM object
base.$el.data("Indextank.FacetsRenderer", base);
base.init = function(){
base.options = $.extend({},$.Indextank.FacetsRenderer.defaultOptions, options);
base.$el.bind( "Indextank.AjaxSearch.success", function (event, data) {
base.$el.show();
base.$el.html("");
var queriedFacets = data.query.categoryFilters || {};
var $selectedFacetsContainer = $("<ul/>").attr("id", "indextank-selected-facets");
var $availableFacetsContainer = $("<div/>").attr("id", "indextank-available-facets");
$.each( data.facets, function (catName, values){
if (catName in queriedFacets) {
var $selectedFacet = base.renderSelectedFacet(queriedFacets, catName, data);
$selectedFacetsContainer.append($selectedFacet);
} else {
var $availableFacet = base.renderAvailableFacet(queriedFacets, catName, values, data);
$availableFacetsContainer.append($availableFacet);
}
});
var $facetsContent = $("<div/>").append($selectedFacetsContainer, $availableFacetsContainer);
var $facetsTitle = $("<h3/>").text("Filters");
base.$el.append($facetsTitle, $facetsContent);
});
};
base.renderSelectedFacet = function(queriedFacets, categoryName, data) {
// Render selected facet as a <li/> and return it
$item = $("<li/>");
$selectedCategory = $("<span/>").append(
$("<a/>").attr("href","#")
.append($("<span/>").text(categoryName + " - " + queriedFacets[categoryName]))
.click(function(){
// ensure query data has something on it
var query = data.query.clone();
// remove the selected category from the query
query.withoutCategories([categoryName]);
// start over!
query.withStart(0);
data.searcher.trigger("Indextank.AjaxSearch.runQuery", [query]);
})
);
$item.append($selectedCategory);
return $item;
}
base.renderAvailableFacet = function(queriedFacets, categoryName, categoryValues, data) {
// Render available facet as a <dl> (definition list) and return it
$facetContainer = $("<div/>");
$availableFacet = $("<dl/>");
$facetContainer.append($availableFacet);
$availableFacet.append($("<dt/>").text(categoryName));
// find out if we should collapse facets, or not
var sorted = [];
var categoriesCount = 0;
$.each(categoryValues, function( categoryValue, count) { categoriesCount += 1; sorted.push([categoryValue, count])});
sorted.sort(function(a,b){return b[1]-a[1];});
$extraValues = $();
if (categoriesCount > base.options.showableFacets) {
$more = $("<div/>").addClass("indextank-more-facets").hide();
$btn = $("<a/>").attr("href", "#").text("more " + categoryName + " options");
$extraValues = $("<dl/>");
$more.append($extraValues);
$btn.click(function(event) {
// need to call parents here, as 'button' messes up objects.
$(event.target).parents().children(".indextank-more-facets").toggle();
} );
$facetContainer.append($btn);
$facetContainer.append($more);
}
// for this category, render all the controls
$.each(sorted, function (idx, categoryCount) {
var categoryValue = categoryCount[0];
var count = categoryCount[1];
var dd = $("<dd/>").append(
$("<a/>")
.attr("href", "#")
.text(categoryValue + " (" + count + ")")
.click(function(){
// ensure query data has something on it
var query = data.query.clone();
filter = {};
filter[categoryName] = categoryValue;
query.withCategoryFilters(filter);
// start over!
query.withStart(0);
data.searcher.trigger("Indextank.AjaxSearch.runQuery", [query]);
})
);
if (idx < base.options.showableFacets) {
$availableFacet.append(dd);
} else {
$extraValues.append(dd);
}
});
return $facetContainer;
}
// Run initializer
base.init();
};
$.Indextank.FacetsRenderer.defaultOptions = {
showableFacets: 4,
};
$.fn.indextank_FacetsRenderer = function(options){
return this.each(function(){
(new $.Indextank.FacetsRenderer(this, options));
});
};
})(jQuery);