PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/src/extensions/filter/backgrid-filter.js

https://github.com/antonyraj/backgrid
JavaScript | 394 lines | 188 code | 53 blank | 153 comment | 39 complexity | 48e802d6e7a9dac1ddc861f17e8986c5 MD5 | raw file
Possible License(s): MIT
  1. /*
  2. backgrid-filter
  3. http://github.com/wyuenho/backgrid
  4. Copyright (c) 2013 Jimmy Yuen Ho Wong and contributors
  5. Licensed under the MIT @license.
  6. */
  7. (function (root) {
  8. "use strict";
  9. var Backbone = root.Backbone, Backgrid = root.Backgrid, lunr = root.lunr;
  10. /**
  11. ServerSideFilter is a search form widget that submits a query to the server
  12. for filtering the current collection.
  13. @class Backgrid.Extension.ServerSideFilter
  14. */
  15. var ServerSideFilter = Backgrid.Extension.ServerSideFilter = Backbone.View.extend({
  16. /** @property */
  17. tagName: "form",
  18. /** @property */
  19. className: "backgrid-filter form-search",
  20. /** @property {function(Object, ?Object=): string} template */
  21. template: _.template('<div class="input-prepend input-append"><span class="add-on"><i class="icon-search"></i></span><input type="text" <% if (placeholder) { %> placeholder="<%- placeholder %>" <% } %> name="<%- name %>" /><span class="add-on"><a class="close" href="#">&times;</a></span></div>'),
  22. /** @property */
  23. events: {
  24. "click .close": "clear",
  25. "submit": "search"
  26. },
  27. /** @property {string} [name='q'] Query key */
  28. name: "q",
  29. /**
  30. @property {string} [placeholder] The HTML5 placeholder to appear beneath
  31. the search box.
  32. */
  33. placeholder: null,
  34. /**
  35. @param {Object} options
  36. @param {Backbone.Collection} options.collection
  37. @param {string} [options.name]
  38. @param {string} [options.placeholder]
  39. */
  40. initialize: function (options) {
  41. Backgrid.requireOptions(options, ["collection"]);
  42. Backbone.View.prototype.initialize.apply(this, arguments);
  43. this.name = options.name || this.name;
  44. this.placeholder = options.placeholder || this.placeholder;
  45. // Persist the query on pagination
  46. var collection = this.collection, self = this;
  47. if (Backbone.PageableCollection &&
  48. collection instanceof Backbone.PageableCollection &&
  49. collection.mode == "server") {
  50. collection.queryParams[this.name] = function () {
  51. return self.searchBox().val() || null;
  52. };
  53. }
  54. },
  55. searchBox: function () {
  56. return this.$el.find("input[type=text]");
  57. },
  58. /**
  59. Upon search form submission, this event handler constructs a query
  60. parameter object and pass it to Collection#fetch for server-side
  61. filtering.
  62. */
  63. search: function (e) {
  64. if (e) e.preventDefault();
  65. var data = {};
  66. // go back to the first page on search
  67. var collection = this.collection;
  68. if (Backbone.PageableCollection &&
  69. collection instanceof Backbone.PageableCollection &&
  70. collection.mode == "server") {
  71. collection.state.currentPage = 1;
  72. }
  73. else {
  74. var query = this.searchBox().val();
  75. if (query) data[this.name] = query;
  76. }
  77. collection.fetch({data: data, reset: true});
  78. },
  79. /**
  80. Event handler for the close button. Clears the search box and refetch the
  81. collection.
  82. */
  83. clear: function (e) {
  84. if (e) e.preventDefault();
  85. this.searchBox().val(null);
  86. this.collection.fetch({reset: true});
  87. },
  88. /**
  89. Renders a search form with a text box, optionally with a placeholder and
  90. a preset value if supplied during initialization.
  91. */
  92. render: function () {
  93. this.$el.empty().append(this.template({
  94. name: this.name,
  95. placeholder: this.placeholder,
  96. value: this.value
  97. }));
  98. this.delegateEvents();
  99. return this;
  100. }
  101. });
  102. /**
  103. ClientSideFilter is a search form widget that searches a collection for
  104. model matches against a query on the client side. The exact matching
  105. algorithm can be overriden by subclasses.
  106. @class Backgrid.Extension.ClientSideFilter
  107. @extends Backgrid.Extension.ServerSideFilter
  108. */
  109. var ClientSideFilter = Backgrid.Extension.ClientSideFilter = ServerSideFilter.extend({
  110. /** @property */
  111. events: {
  112. "click .close": function (e) {
  113. e.preventDefault();
  114. this.clear();
  115. },
  116. "keydown input[type=text]": "search",
  117. "submit": function (e) {
  118. e.preventDefault();
  119. this.search();
  120. }
  121. },
  122. /**
  123. @property {?Array.<string>} [fields] A list of model field names to
  124. search for matches. If null, all of the fields will be searched.
  125. */
  126. fields: null,
  127. /**
  128. @property [wait=149] The time in milliseconds to wait since for since the
  129. last change to the search box's value before searching. This value can be
  130. adjusted depending on how often the search box is used and how large the
  131. search index is.
  132. */
  133. wait: 149,
  134. /**
  135. Debounces the #search and #clear methods and makes a copy of the given
  136. collection for searching.
  137. @param {Object} options
  138. @param {Backbone.Collection} options.collection
  139. @param {string} [options.placeholder]
  140. @param {string} [options.fields]
  141. @param {string} [options.wait=149]
  142. */
  143. initialize: function (options) {
  144. ServerSideFilter.prototype.initialize.apply(this, arguments);
  145. this.fields = options.fields || this.fields;
  146. this.wait = options.wait || this.wait;
  147. this._debounceMethods(["search", "clear"]);
  148. var collection = this.collection = this.collection.fullCollection || this.collection;
  149. var shadowCollection = this.shadowCollection = collection.clone();
  150. this.listenTo(collection, "add", function (model, collection, options) {
  151. shadowCollection.add(model, options);
  152. });
  153. this.listenTo(collection, "remove", function (model, collection, options) {
  154. shadowCollection.remove(model, options);
  155. });
  156. this.listenTo(collection, "sort", function (col) {
  157. if (!this.searchBox().val()) shadowCollection.reset(col.models);
  158. });
  159. this.listenTo(collection, "reset", function (col, options) {
  160. options = _.extend({reindex: true}, options || {});
  161. if (options.reindex && col === collection &&
  162. options.from == null && options.to == null) {
  163. shadowCollection.reset(col.models);
  164. }
  165. });
  166. },
  167. _debounceMethods: function (methodNames) {
  168. if (_.isString(methodNames)) methodNames = [methodNames];
  169. this.undelegateEvents();
  170. for (var i = 0, l = methodNames.length; i < l; i++) {
  171. var methodName = methodNames[i];
  172. var method = this[methodName];
  173. this[methodName] = _.debounce(method, this.wait);
  174. }
  175. this.delegateEvents();
  176. },
  177. /**
  178. This default implementation takes a query string and returns a matcher
  179. function that looks for matches in the model's #fields or all of its
  180. fields if #fields is null, for any of the words in the query
  181. case-insensitively.
  182. Subclasses overriding this method must take care to conform to the
  183. signature of the matcher function. In addition, when the matcher function
  184. is called, its context will be bound to this ClientSideFilter object so
  185. it has access to the filter's attributes and methods.
  186. @param {string} query The search query in the search box.
  187. @return {function(Backbone.Model):boolean} A matching function.
  188. */
  189. makeMatcher: function (query) {
  190. var regexp = new RegExp(query.trim().split(/\W/).join("|"), "i");
  191. return function (model) {
  192. var keys = this.fields || model.keys();
  193. for (var i = 0, l = keys.length; i < l; i++) {
  194. if (regexp.test(model.get(keys[i]) + "")) return true;
  195. }
  196. return false;
  197. };
  198. },
  199. /**
  200. Takes the query from the search box, constructs a matcher with it and
  201. loops through collection looking for matches. Reset the given collection
  202. when all the matches have been found.
  203. */
  204. search: function () {
  205. var matcher = _.bind(this.makeMatcher(this.searchBox().val()), this);
  206. var col = this.collection;
  207. if (col.pageableCollection) col.pageableCollection.getFirstPage({silent: true});
  208. this.collection.reset(this.shadowCollection.filter(matcher), {reindex: false});
  209. },
  210. /**
  211. Clears the search box and reset the collection to its original.
  212. */
  213. clear: function () {
  214. this.searchBox().val(null);
  215. this.collection.reset(this.shadowCollection.models, {reindex: false});
  216. }
  217. });
  218. /**
  219. LunrFilter is a ClientSideFilter that uses [lunrjs](http://lunrjs.com/) to
  220. index the text fields of each model for a collection, and performs
  221. full-text searching.
  222. @class Backgrid.Extension.LunrFilter
  223. @extends Backgrid.Extension.ClientSideFilter
  224. */
  225. Backgrid.Extension.LunrFilter = ClientSideFilter.extend({
  226. /**
  227. @property {string} [ref="id"]`lunrjs` document reference attribute name.
  228. */
  229. ref: "id",
  230. /**
  231. @property {Object} fields A hash of `lunrjs` index field names and boost
  232. value. Unlike ClientSideFilter#fields, LunrFilter#fields is _required_ to
  233. initialize the index.
  234. */
  235. fields: null,
  236. /**
  237. Indexes the underlying collection on construction. The index will refresh
  238. when the underlying collection is reset. If any model is added, removed
  239. or if any indexed fields of any models has changed, the index will be
  240. updated.
  241. @param {Object} options
  242. @param {Backbone.Collection} options.collection
  243. @param {string} [options.placeholder]
  244. @param {string} [options.ref] `lunrjs` document reference attribute name.
  245. @param {Object} [options.fields] A hash of `lunrjs` index field names and
  246. boost value.
  247. @param {number} [options.wait]
  248. */
  249. initialize: function (options) {
  250. ClientSideFilter.prototype.initialize.apply(this, arguments);
  251. this.ref = options.ref || this.ref;
  252. var collection = this.collection = this.collection.fullCollection || this.collection;
  253. this.listenTo(collection, "add", this.addToIndex);
  254. this.listenTo(collection, "remove", this.removeFromIndex);
  255. this.listenTo(collection, "reset", this.resetIndex);
  256. this.listenTo(collection, "change", this.updateIndex);
  257. this.resetIndex(collection);
  258. },
  259. /**
  260. Reindex the collection. If `options.reindex` is `false`, this method is a
  261. no-op.
  262. @param {Backbone.Collection} collection
  263. @param {Object} [options]
  264. @param {boolean} [options.reindex=true]
  265. */
  266. resetIndex: function (collection, options) {
  267. options = _.extend({reindex: true}, options || {});
  268. if (options.reindex) {
  269. var self = this;
  270. this.index = lunr(function () {
  271. _.each(self.fields, function (boost, fieldName) {
  272. this.field(fieldName, boost);
  273. this.ref(self.ref);
  274. }, this);
  275. });
  276. collection.each(function (model) {
  277. this.addToIndex(model);
  278. }, this);
  279. }
  280. },
  281. /**
  282. Adds the given model to the index.
  283. @param {Backbone.Model} model
  284. */
  285. addToIndex: function (model) {
  286. var index = this.index;
  287. var doc = model.toJSON();
  288. if (index.documentStore.has(doc[this.ref])) index.update(doc);
  289. else index.add(doc);
  290. },
  291. /**
  292. Removes the given model from the index.
  293. @param {Backbone.Model} model
  294. */
  295. removeFromIndex: function (model) {
  296. var index = this.index;
  297. var doc = model.toJSON();
  298. if (index.documentStore.has(doc[this.ref])) index.remove(doc);
  299. },
  300. /**
  301. Updates the index for the given model.
  302. @param {Backbone.Model} model
  303. */
  304. updateIndex: function (model) {
  305. var changed = model.changedAttributes();
  306. if (changed && !_.isEmpty(_.intersection(_.keys(this.fields),
  307. _.keys(changed)))) {
  308. this.index.update(model.toJSON());
  309. }
  310. },
  311. /**
  312. Takes the query from the search box and performs a full-text search on
  313. the client-side. The search result is returned by resetting the
  314. underlying collection to the models after interrogating the index for the
  315. query answer.
  316. */
  317. search: function () {
  318. var searchResults = this.index.search(this.searchBox().val());
  319. var models = [];
  320. for (var i = 0; i < searchResults.length; i++) {
  321. var result = searchResults[i];
  322. models.push(this.shadowCollection.get(result.ref));
  323. }
  324. var col = this.collection;
  325. if (col.pageableCollection) col.pageableCollection.getFirstPage({silent: true});
  326. col.reset(models, {reindex: false});
  327. }
  328. });
  329. }(this));