Facets.js
4.06 KB
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
define(function(require) {
'use strict';
var _ = require('underscore');
var Backbone = require('backbone');
var NestedModels = require('backbone-nested-models');
var Facet = require('./Facet');
var Sort = require('./Sort');
var Util = require('./Util');
var Facets = NestedModels.mixin(Backbone.Model.extend({
initialize: function(data, options) {
this.url = function() {
var url = options.search.url;
return _.isFunction(url) ? url.call(options.search) : url;
};
_.each(this.keys(), _.bind(function(facetName) {
var facet = this.get(facetName);
_.each({
'item-change': 'item-change',
'change:query': 'child-query',
'change:queryMin': 'item-change',
'change:queryMax': 'item-change',
}, function(eventValue, eventKey) {
facet.on(eventKey, function(model, value, options) {
console.log('trigger', facet.get('formName'), eventValue);
this.trigger(eventValue, facet, null, options);
}, this);
}, this);
}, this));
this._doSuggestions = _.debounce(_.bind(function(childFacet) {
if (childFacet.get('query')) {
this.fetch({
data: this.toJSON({childFacet: childFacet, facetSuggestions: true}),
childFacet: childFacet,
facetSuggestions: true,
merge: true,
traditional: true,
});
} else {
childFacet.set('suggestions', []);
}
}, this), 250);
this.on('child-query', this._doSuggestions, this);
return Facets.__super__.initialize.apply(this, arguments);
},
resetSearch: function(options) {
_.invoke(this.values(), 'resetSearch', options);
},
resetParameters: function(options) {
_.invoke(this.values(), 'resetParameters', options);
},
applyFacetResults: function(data, options) {
options = options || {};
_.each(this.keys(), _.bind(function(facetName) {
var facet = this.get(facetName);
facet.applyFacetResults(facetName, data, options);
}, this));
},
getFacetFormData: function(options) {
options = options || {};
var result = {
facet: true,
stats: true,
'facet.missing': true,
'facet.field': [],
'facet.range': [],
'facet.interval': [],
'facet.query': [],
'stats.field': [],
fq: [],
};
_.each(this.keys(), _.bind(function(facetName) {
var facet = this.get(facetName);
var facetResult = facet.getFacetFormData(facetName, options);
_.each([
'facet.field',
'facet.range',
'facet.interval',
'facet.query',
'stats.field',
'fq',
], function(key) {
result[key] = result[key].concat(facetResult[key]);
});
}, this));
return result;
},
toJSON: function toJSON(options) {
if (!!options.facetSuggestions) {
return _.extend({
rows: 0,
facet: true,
wt: 'json',
q: '*:*',
}, this.getFacetFormData(options));
}
},
parse: function parse(data, options) {
if (!!options.facetSuggestions) {
this.applyFacetResults(data, options);
}
return null;
},
}));
Facets.Sort = Sort;
Facets.Facet = Facet;
return Facets;
});