/* backgrid-filter http://github.com/wyuenho/backgrid Copyright (c) 2013-present Cloudflare, Inc and contributors Licensed under the MIT @license. */ !function(a,b){ // CommonJS // This block must come first for Webpack to resolve the require call if("object"==typeof exports){var c;try{c=require("lunr")}catch(d){}module.exports=b(require("underscore"),require("backbone"),require("backgrid"),c)}else"function"==typeof define&&define.amd?define(function(a,c,d){var e;try{e=a("lunr.js")}catch(f){}return b(a("underscore"),a("backbone"),a("backgrid"),e)}):b(a._,a.Backbone,a.Backgrid,a.lunr)}(this,function(a,b,c,d){"use strict";var e={},f=e.ServerSideFilter=c.Extension.ServerSideFilter=b.View.extend({/** @property */ tagName:"form",/** @property */ className:"backgrid-filter form-search",/** @property {function(Object, ?Object=): string} template */ template:function(a){return' ×'},/** @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(a){f.__super__.initialize.apply(this,arguments),this.name=a.name||this.name,this.value=a.value||this.value,this.placeholder=a.placeholder||this.placeholder,this.template=a.template||this.template; // Persist the query on pagination var c=this.collection,d=this;b.PageableCollection&&c instanceof b.PageableCollection&&"server"==c.mode&&(c.queryParams[this.name]=function(){return d.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 a=this.clearButton(),b=this.query();b?a.show():a.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(){return this.value=this.searchBox().val(),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(a){a&&a.preventDefault();var c={},d=this.query();d&&(c[this.name]=d);var e=this.collection; // go back to the first page on search b.PageableCollection&&e instanceof b.PageableCollection?e.getFirstPage({data:c,reset:!0,fetch:!0}):e.fetch({data:c,reset:!0})},/** 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(a){a&&a.preventDefault(),this.clearSearchBox();var c=this.collection; // go back to the first page on clear b.PageableCollection&&c instanceof b.PageableCollection?c.getFirstPage({reset:!0,fetch:!0}):c.fetch({reset:!0})},/** Renders a search form with a text box, optionally with a placeholder and a preset value if supplied during initialization. */ render:function(){return this.$el.empty().append(this.template({name:this.name,placeholder:this.placeholder,value:this.value})),this.showClearButtonMaybe(),this.delegateEvents(),this}}),g=e.ClientSideFilter=c.Extension.ClientSideFilter=f.extend({/** @property */ events:a.extend({},f.prototype.events,{"click a[data-backgrid-action=clear]":function(a){a.preventDefault(),this.clear()},"keydown input[type=search]":"search",submit:function(a){a.preventDefault(),this.search()}}),/** @property {?Array.} [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(b){g.__super__.initialize.apply(this,arguments),this.fields=b.fields||this.fields,this.wait=b.wait||this.wait,this._debounceMethods(["search","clear"]);var c=this.collection=this.collection.fullCollection||this.collection,d=this.shadowCollection=c.clone();this.listenTo(c,"add",function(a,b,c){d.add(a,c)}),this.listenTo(c,"remove",function(a,b,c){d.remove(a,c)}),this.listenTo(c,"sort",function(a){this.query()||d.reset(a.models)}),this.listenTo(c,"reset",function(b,c){c=a.extend({reindex:!0},c||{}),c.reindex&&null==c.from&&null==c.to&&d.reset(b.models)})},_debounceMethods:function(b){a.isString(b)&&(b=[b]),this.undelegateEvents();for(var c=0,d=b.length;c