Repository navigation
Expand file tree
/
Copy pathbackgrid-filter.js
More file actions
380 lines (318 loc) · 11.9 KB
/
Copy pathbackgrid-filter.js
File metadata and controls
380 lines (318 loc) · 11.9 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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
backgrid-filter
http://github.com/wyuenho/backgrid
Copyright (c) 2013-present Cloudflare, Inc and contributors
Licensed under the MIT @license.
*/
(function (root, factory) {
// CommonJS
// This block must come first for Webpack to resolve the require call
if (typeof exports == "object") {
module.exports = factory(
require("underscore"),
require("backbone"),
require("backgrid")
);
}
// AMD. Register as an anonymous module.
else if (typeof define === 'function' && define.amd) {
define(function (require, exports, module) {
return factory(
require("underscore"),
require("backbone"),
require("backgrid")
);
});
}
// Browser
else {
factory(root._, root.Backbone, root.Backgrid);
}
}(this, function (_, Backbone, Backgrid) {
"use strict";
var exports = {};
/**
ServerSideFilter is a search form widget that submits a query to the server
for filtering the current collection.
@class Backgrid.Extension.ServerSideFilter
*/
var ServerSideFilter = exports.ServerSideFilter = Backgrid.Extension.ServerSideFilter = Backbone.View.extend({
/** @property */
tagName: "form",
/** @property */
className: "backgrid-filter form-search",
/** @property {function(Object, ?Object=): string} template */
template: function (data) {
return '<span class="search"> </span><input type="search" ' + (data.placeholder ? 'placeholder="' + data.placeholder + '"' : '') + ' name="' + data.name + '" ' + (data.value ? 'value="' + data.value + '"' : '') + '/><a class="clear" data-backgrid-action="clear" href="#">×</a>';
},
/** @property */
events: {
"keyup input[type=search]": "showClearButtonMaybe",
"click a[data-backgrid-action=clear]": "clear",
"submit": "search"
},
/** @property {string} [name='q'] Query key */
name: "q",
/** @property {string} [value] The search box value. */
value: null,
/**
@property {string} [placeholder] The HTML5 placeholder to appear beneath
the search box.
*/
placeholder: null,
/**
@param {Object} options
@param {Backbone.Collection} options.collection
@param {string} [options.name]
@param {string} [options.value]
@param {string} [options.placeholder]
@param {function(Object): string} [options.template]
*/
initialize: function (options) {
ServerSideFilter.__super__.initialize.apply(this, arguments);
this.name = options.name || this.name;
this.value = options.value || this.value;
this.placeholder = options.placeholder || this.placeholder;
this.template = options.template || this.template;
// Persist the query on pagination
var collection = this.collection, self = this;
if (Backbone.PageableCollection &&
collection instanceof Backbone.PageableCollection &&
collection.mode == "server") {
collection.queryParams[this.name] = function () {
return self.query() || null;
};
}
},
/**
Event handler. Clear the search box and reset the internal search value.
*/
clearSearchBox: function() {
this.value = null;
this.searchBox().val(null);
this.showClearButtonMaybe();
},
/**
Event handler. Show the clear button when the search box has text, hide
it otherwise.
*/
showClearButtonMaybe: function () {
var $clearButton = this.clearButton();
var searchTerms = this.query();
if (searchTerms) $clearButton.show();
else $clearButton.hide();
},
/**
Returns the search input box.
*/
searchBox: function () {
return this.$el.find("input[type=search]");
},
/**
Returns the clear button.
*/
clearButton: function () {
return this.$el.find("a[data-backgrid-action=clear]");
},
/**
Returns the current search query.
*/
query: function() {
this.value = this.searchBox().val();
return this.value;
},
/**
Upon search form submission, this event handler constructs a query
parameter object and pass it to Collection#fetch for server-side
filtering.
If the collection is a PageableCollection, searching will go back to the
first page.
*/
search: function (e) {
if (e) e.preventDefault();
var data = {};
var query = this.query();
if (query) data[this.name] = query;
var collection = this.collection;
// go back to the first page on search
if (Backbone.PageableCollection &&
collection instanceof Backbone.PageableCollection) {
collection.getFirstPage({data: data, reset: true, fetch: true});
}
else collection.fetch({data: data, reset: true});
},
/**
Event handler for the clear button. Clears the search box and refetch the
collection.
If the collection is a PageableCollection, clearing will go back to the
first page.
*/
clear: function (e) {
if (e) e.preventDefault();
this.clearSearchBox();
var collection = this.collection;
// go back to the first page on clear
if (Backbone.PageableCollection &&
collection instanceof Backbone.PageableCollection) {
collection.getFirstPage({reset: true, fetch: true});
}
else collection.fetch({reset: true});
},
/**
Renders a search form with a text box, optionally with a placeholder and
a preset value if supplied during initialization.
*/
render: function () {
this.$el.empty().append(this.template({
name: this.name,
placeholder: this.placeholder,
value: this.value
}));
this.showClearButtonMaybe();
this.delegateEvents();
return this;
}
});
/**
ClientSideFilter is a search form widget that searches a collection for
model matches against a query on the client side. The exact matching
algorithm can be overriden by subclasses.
@class Backgrid.Extension.ClientSideFilter
@extends Backgrid.Extension.ServerSideFilter
*/
var ClientSideFilter = exports.ClientSideFilter = Backgrid.Extension.ClientSideFilter = ServerSideFilter.extend({
/** @property */
events: _.extend({}, ServerSideFilter.prototype.events, {
"click a[data-backgrid-action=clear]": function (e) {
e.preventDefault();
this.clear();
},
"keydown input[type=search]": "search",
"submit": function (e) {
e.preventDefault();
this.search();
}
}),
/**
@property {?Array.<string>} [fields] A list of model field names to
search for matches. If null, all of the fields will be searched.
*/
fields: null,
/**
@property [wait=149] The time in milliseconds to wait since the last
change to the search box's value before searching. This value can be
adjusted depending on how often the search box is used and how large the
search index is.
*/
wait: 149,
/**
Debounces the #search and #clear methods and makes a copy of the given
collection for searching.
@param {Object} options
@param {Backbone.Collection} options.collection
@param {string} [options.placeholder]
@param {string} [options.fields]
@param {string} [options.wait=149]
*/
initialize: function (options) {
ClientSideFilter.__super__.initialize.apply(this, arguments);
this.fields = options.fields || this.fields;
this.wait = options.wait || this.wait;
this._debounceMethods(["search", "clear"]);
var collection = this.collection = this.collection.fullCollection || this.collection;
var shadowCollection = this.shadowCollection = collection.clone();
this.listenTo(collection, "add", function (model, collection, options) {
shadowCollection.add(model, options);
});
this.listenTo(collection, "remove", function (model, collection, options) {
shadowCollection.remove(model, options);
});
this.listenTo(collection, "sort", function (col) {
if (!this.query()) shadowCollection.reset(col.models);
});
this.listenTo(collection, "reset", function (col, options) {
options = _.extend({reindex: true}, options || {});
if (options.reindex && options.from == null && options.to == null) {
shadowCollection.reset(col.models);
}
});
},
_debounceMethods: function (methodNames) {
if (_.isString(methodNames)) methodNames = [methodNames];
this.undelegateEvents();
for (var i = 0, l = methodNames.length; i < l; i++) {
var methodName = methodNames[i];
var method = this[methodName];
this[methodName] = _.debounce(method, this.wait);
}
this.delegateEvents();
},
/**
Constructs a Javascript regular expression object for #makeMatcher.
This default implementation takes a query string and returns a Javascript
RegExp object that matches any of the words contained in the query string
case-insensitively. Override this method to return a different regular
expression matcher if this behavior is not desired.
@param {string} query The search query in the search box.
@return {RegExp} A RegExp object to match against model #fields.
*/
makeRegExp: function (query) {
return new RegExp(query.trim().split(/\s+/).join("|"), "i");
},
/**
This default implementation takes a query string and returns a matcher
function that looks for matches in the model's #fields or all of its
fields if #fields is null, for any of the words in the query
case-insensitively using the regular expression object returned from
#makeRegExp.
Most of time, you'd want to override the regular expression used for
matching. If so, please refer to the #makeRegExp documentation,
otherwise, you can override this method to return a custom matching
function.
Subclasses overriding this method must take care to conform to the
signature of the matcher function. The matcher function is a function
that takes a model as paramter and returns true if the model matches a
search, or false otherwise.
In addition, when the matcher function is called, its context will be
bound to this ClientSideFilter object so it has access to the filter's
attributes and methods.
@param {string} query The search query in the search box.
@return {function(Backbone.Model):boolean} A matching function.
*/
makeMatcher: function (query) {
var regexp = this.makeRegExp(query);
return function (model) {
var keys = this.fields || model.keys();
for (var i = 0, l = keys.length; i < l; i++) {
if (regexp.test(model.get(keys[i]) + "")) return true;
}
return false;
};
},
/**
Takes the query from the search box, constructs a matcher with it and
loops through collection looking for matches. Reset the given collection
when all the matches have been found.
If the collection is a PageableCollection, searching will go back to the
first page.
*/
search: function () {
var matcher = _.bind(this.makeMatcher(this.query()), this);
var col = this.collection;
if (col.pageableCollection) col.pageableCollection.getFirstPage({silent: true});
col.reset(this.shadowCollection.filter(matcher), {reindex: false});
},
/**
Clears the search box and reset the collection to its original.
If the collection is a PageableCollection, clearing will go back to the
first page.
*/
clear: function () {
this.clearSearchBox();
var col = this.collection;
if (col.pageableCollection) col.pageableCollection.getFirstPage({silent: true});
col.reset(this.shadowCollection.models, {reindex: false});
}
});
return exports;
}));