|
|
|
!function(n){"function"==typeof define&&define.amd?define(["jquery","datatables.net"],function(e){return n(e,window,document)}):"object"==typeof exports?module.exports=function(e,t){return e=e||window,t&&t.fn.dataTable||(t=require("datatables.net")(e,t).$),n(t,0,e.document)}:n(jQuery,window,document)}(function(f,e,t,n){"use strict";var m=f.fn.dataTable;function i(e,t){var n=this,e=new m.Api(e);this.classes=f.extend(!0,{},i.classes),this.dom={container:f("<div/>").addClass(this.classes.container)},this.c=f.extend(!0,{},i.defaults,t),this.s={dt:e},e.settings()[0].searchPane=this,e.columns(this.c.columns).eq(0).each(function(e){n._pane(e)}),f(this.dom.container).on("click","li",function(){n._toggle(this)}).on("click","button."+this.classes.clear,function(){n._clear(f(this).closest("div."+n.classes.pane.container))}),this._attach()}f.extend(i.prototype,{rebuild:function(){var t=this;this.dom.container.empty(),this.s.dt.columns(this.c.columns).eq(0).each(function(e){t._pane(e)})},_attach:function(){var e=this.c.container,e="function"==typeof e?e(this.s.dt):e;"prepend"===this.c.insert?f(this.dom.container).prependTo(e):f(this.dom.container).appendTo(e)},_binData:function(e){for(var t={},n=0,a=e.length;n<a;n++){var s=e[n];s&&(t[s]?t[s]++:t[s]=1)}return t},_clear:function(e){var t=this.classes,n=t.item.selected;e.find("li."+n).removeClass(n),e.removeClass(t.pane.active),this.s.dt.column(e.data("column")).search("").draw()},_pane:function(e){var t=this.classes,n=t.item,t=t.pane,a=this.s.dt,s=a.column(e),i=this._getOptions(e),c=f("<ul/>"),a="function"==typeof i.options?i.options(a,e):i.options?new m.Api(null,i.options):s.data(),o=this._binData(a.flatten());if(!(this._variance(o)<this.c.threshold)){for(var r,l,d=(d=s.search())?d.substr(1,d.length-2).split("|"):[],u=a.unique().sort().toArray(),h=0,p=u.length;h<p;h++)u[h]&&(r=f("<li/>").html('<span class="'+n.label+'">'+u[h]+"</span>").data("filter",u[h]).append(f("<span/>").addClass(n.count).html(o[u[h]])),d.length&&(l=u[h].replace?f.fn.dataTable.util.escapeRegex(u[h]):u[h],-1!==f.inArray(l,d))&&r.addClass(n.selected),c.append(r));i=f("<div/>").data("column",e).addClass(t.container).addClass(d.length?t.active:"").append(f('<button type="button">×</button>').addClass(this.classes.clear)).append(f("<div/>").addClass(t.title).html(f(s.header()).text())).append(f("<div/>").addClass(t.scroller).append(c)),a=this.dom.container,s=a.children().map(function(){if(f(this).data("column")==e)return this});s.length?s.replaceWith(i):f(a).append(i)}},_getOptions:function(e){return this.s.dt.settings()[0].aoColumns[e].searchPane||{}},_toggle:function(e){var t=this.classes,n=t.item.selected,a=this.s.dt,s=(e=f(e)).closest("div."+t.pane.container),i=s.data("column"),c=this._getOptions(i),e=(e.toggleClass(n,!e.hasClass(n)),s.find("li."+n));(0===e.length?(s.removeClass(t.pane.active),a.column(i).search("")):"any"===c.match?(s.addClass(t.pane.active),a.column(i).search("("+f.map(e,function(e){e=f(e).data("filter").toString(),e=f("<div/>").html(e).text();return f.fn.dataTable.util.escapeRegex(e)}).join("|")+")",!0,!1)):(s.addClass(t.pane.active),a.column(i).search("^("+f.map(e,function(e){e=f(e).data("filter").toString(),e=f("<div/>").html(e).text();return f.fn.dataTable.util.escapeRegex(e)}).join("|")+")$",!0,!1))).draw()},_variance:function(e){for(var t=f.map(e,function(e,t){return e}),e=t.length,n=0,a=0,s=e;a<s;a++)n+=t[a];for(var i=n/e,c=0,a=0,s=e;a<s;a++)c+=Math.pow(i-t[a],2);return c/(e-1)}}),i.classes={container:"dt-searchPanes",clear:"clear",pane:{active:"filtering",container:"pane",title:"title",scroller:"scroller"},item:{selected:"selected",label:"label",count:"count"}},i.defaults={container:function(e){return e.table().container()},columns:n,insert:"prepend",threshold:.5},i.version="0.0.2",f.fn.dataTable.SearchPanes=i,f.fn.DataTable.SearchPanes=i,m.Api.register("searchPanes.rebuild()",function(){return this.iterator("table",function(e){e.searchPane&&e.searchPane.rebuild()})}),m.Api.register("column().paneOptions()",function(n){return this.iterator("column",function(e,t){t=e.aoColumns[t];t
|