/
AbstractFacetWidget.js
292 lines (271 loc) · 8.51 KB
/
AbstractFacetWidget.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
(function (callback) {
if (typeof define === 'function' && define.amd) {
define(['core/AbstractWidget', 'core/Parameter'], callback);
}
else {
callback();
}
}(function () {
/**
* Baseclass for all facet widgets.
*
* @class AbstractFacetWidget
* @augments AjaxSolr.AbstractWidget
*/
AjaxSolr.AbstractFacetWidget = AjaxSolr.AbstractWidget.extend(
/** @lends AjaxSolr.AbstractFacetWidget.prototype */
{
/**
* @param {Object} attributes
* @param {String} attributes.field The field to facet on.
* @param {Number} [attributes.start] This widget will by default set the
* offset parameter to 0 on each request.
* @param {Boolean} [attributes.multivalue] Set to <tt>false</tt> to force a
* single "fq" parameter for this widget. Defaults to <tt>true</tt>.
*/
constructor: function (attributes) {
AjaxSolr.extend(this, {
start: 0,
field: null,
multivalue: true
}, attributes);
},
init: function () {
this.initStore();
},
/**
* Add facet parameters to the parameter store.
*/
initStore: function () {
/* http://wiki.apache.org/solr/SimpleFacetParameters */
var parameters = [
'facet.prefix',
'facet.sort',
'facet.limit',
'facet.offset',
'facet.mincount',
'facet.missing',
'facet.method',
'facet.enum.cache.minDf'
];
this.manager.store.addByValue('facet', true);
// Set facet.field, facet.date or facet.range to truthy values to add
// related per-field parameters to the parameter store.
if (this['facet.field'] !== undefined) {
this.manager.store.addByValue('facet.field', this.field);
}
else if (this['facet.date'] !== undefined) {
this.manager.store.addByValue('facet.date', this.field);
parameters = parameters.concat([
'facet.date.start',
'facet.date.end',
'facet.date.gap',
'facet.date.hardend',
'facet.date.other',
'facet.date.include'
]);
}
else if (this['facet.range'] !== undefined) {
this.manager.store.addByValue('facet.range', this.field);
parameters = parameters.concat([
'facet.range.start',
'facet.range.end',
'facet.range.gap',
'facet.range.hardend',
'facet.range.other',
'facet.range.include'
]);
}
for (var i = 0, l = parameters.length; i < l; i++) {
if (this[parameters[i]] !== undefined) {
this.manager.store.addByValue('f.' + this.field + '.' + parameters[i], this[parameters[i]]);
}
}
},
/**
* @returns {Boolean} Whether any filter queries have been set using this
* widget's facet field.
*/
isEmpty: function () {
return !this.manager.store.find('fq', new RegExp('^-?' + this.field + ':'));
},
/**
* Sets the filter query.
*
* @returns {Boolean} Whether the selection changed.
*/
set: function (value) {
return this.changeSelection(function () {
var a = this.manager.store.removeByValue('fq', new RegExp('^-?' + this.field + ':')),
b = this.manager.store.addByValue('fq', this.fq(value));
return a || b;
});
},
/**
* Adds a filter query.
*
* @returns {Boolean} Whether a filter query was added.
*/
add: function (value) {
return this.changeSelection(function () {
return this.manager.store.addByValue('fq', this.fq(value));
});
},
/**
* Removes a filter query.
*
* @returns {Boolean} Whether a filter query was removed.
*/
remove: function (value) {
return this.changeSelection(function () {
return this.manager.store.removeByValue('fq', this.fq(value));
});
},
/**
* Removes all filter queries using the widget's facet field.
*
* @returns {Boolean} Whether a filter query was removed.
*/
clear: function () {
return this.changeSelection(function () {
return this.manager.store.removeByValue('fq', new RegExp('^-?' + this.field + ':'));
});
},
/**
* Helper for selection functions.
*
* @param {Function} Selection function to call.
* @returns {Boolean} Whether the selection changed.
*/
changeSelection: function (func) {
changed = func.apply(this);
if (changed) {
this.afterChangeSelection();
}
return changed;
},
/**
* An abstract hook for child implementations.
*
* <p>This method is executed after the filter queries change.</p>
*/
afterChangeSelection: function () {},
/**
* One of "facet.field", "facet.date" or "facet.range" must be set on the
* widget in order to determine where the facet counts are stored.
*
* @returns {Array} An array of objects with the properties <tt>facet</tt> and
* <tt>count</tt>, e.g <tt>{ facet: 'facet', count: 1 }</tt>.
*/
getFacetCounts: function () {
var property;
if (this['facet.field'] !== undefined) {
property = 'facet_fields';
}
else if (this['facet.date'] !== undefined) {
property = 'facet_dates';
}
else if (this['facet.range'] !== undefined) {
property = 'facet_ranges';
}
if (property !== undefined) {
switch (this.manager.store.get('json.nl').val()) {
case 'map':
return this.getFacetCountsMap(property);
case 'arrarr':
return this.getFacetCountsArrarr(property);
default:
return this.getFacetCountsFlat(property);
}
}
throw 'Cannot get facet counts unless one of the following properties is set to "true" on widget "' + this.id + '": "facet.field", "facet.date", or "facet.range".';
},
/**
* Used if the facet counts are represented as a JSON object.
*
* @param {String} property "facet_fields", "facet_dates", or "facet_ranges".
* @returns {Array} An array of objects with the properties <tt>facet</tt> and
* <tt>count</tt>, e.g <tt>{ facet: 'facet', count: 1 }</tt>.
*/
getFacetCountsMap: function (property) {
var counts = [];
for (var facet in this.manager.response.facet_counts[property][this.field]) {
counts.push({
facet: facet,
count: parseInt(this.manager.response.facet_counts[property][this.field][facet])
});
}
return counts;
},
/**
* Used if the facet counts are represented as an array of two-element arrays.
*
* @param {String} property "facet_fields", "facet_dates", or "facet_ranges".
* @returns {Array} An array of objects with the properties <tt>facet</tt> and
* <tt>count</tt>, e.g <tt>{ facet: 'facet', count: 1 }</tt>.
*/
getFacetCountsArrarr: function (property) {
var counts = [];
for (var i = 0, l = this.manager.response.facet_counts[property][this.field].length; i < l; i++) {
counts.push({
facet: this.manager.response.facet_counts[property][this.field][i][0],
count: parseInt(this.manager.response.facet_counts[property][this.field][i][1])
});
}
return counts;
},
/**
* Used if the facet counts are represented as a flat array.
*
* @param {String} property "facet_fields", "facet_dates", or "facet_ranges".
* @returns {Array} An array of objects with the properties <tt>facet</tt> and
* <tt>count</tt>, e.g <tt>{ facet: 'facet', count: 1 }</tt>.
*/
getFacetCountsFlat: function (property) {
var counts = [];
for (var i = 0, l = this.manager.response.facet_counts[property][this.field].length; i < l; i += 2) {
counts.push({
facet: this.manager.response.facet_counts[property][this.field][i],
count: parseInt(this.manager.response.facet_counts[property][this.field][i+1])
});
}
return counts;
},
/**
* @param {String} value The value.
* @returns {Function} Sends a request to Solr if it successfully adds a
* filter query with the given value.
*/
clickHandler: function (value) {
var self = this, meth = this.multivalue ? 'add' : 'set';
return function () {
if (self[meth].call(self, value)) {
self.doRequest();
}
return false;
}
},
/**
* @param {String} value The value.
* @returns {Function} Sends a request to Solr if it successfully removes a
* filter query with the given value.
*/
unclickHandler: function (value) {
var self = this;
return function () {
if (self.remove(value)) {
self.doRequest();
}
return false;
}
},
/**
* @param {String} value The facet value.
* @param {Boolean} exclude Whether to exclude this fq parameter value.
* @returns {String} An fq parameter value.
*/
fq: function (value, exclude) {
return (exclude ? '-' : '') + this.field + ':' + AjaxSolr.Parameter.escapeValue(value);
}
});
}));