Feature - searchPanes: Code style with Prettier

pull/355/head
Allan Jardine 7 years ago
parent 797c3e6f90
commit e9c4e4a8ea

@ -8,175 +8,198 @@
// - State saving integration // - State saving integration
// - Fix regex characters - (CEO) for example // - Fix regex characters - (CEO) for example
(function(factory) {
(function( factory ){ if (typeof define === 'function' && define.amd) {
if ( typeof define === 'function' && define.amd ) {
// AMD // AMD
define( ['jquery', 'datatables.net'], function ( $ ) { define(['jquery', 'datatables.net'], function($) {
return factory( $, window, document ); return factory($, window, document);
} ); });
} } else if (typeof exports === 'object') {
else if ( typeof exports === 'object' ) {
// CommonJS // CommonJS
module.exports = function (root, $) { module.exports = function(root, $) {
if ( ! root ) { if (!root) {
root = window; root = window;
} }
if ( ! $ || ! $.fn.dataTable ) { if (!$ || !$.fn.dataTable) {
$ = require('datatables.net')(root, $).$; $ = require('datatables.net')(root, $).$;
} }
return factory( $, root, root.document ); return factory($, root, root.document);
}; };
} } else {
else {
// Browser // Browser
factory( jQuery, window, document ); factory(jQuery, window, document);
} }
}(function( $, window, document, undefined ) { })(function($, window, document, undefined) {
'use strict'; 'use strict';
var DataTable = $.fn.dataTable; var DataTable = $.fn.dataTable;
function SearchPanes(settings, opts) {
function SearchPanes ( settings, opts ) {
var that = this; var that = this;
var table = new DataTable.Api( settings ); var table = new DataTable.Api(settings);
this.classes = $.extend( true, {}, SearchPanes.classes ); this.classes = $.extend(true, {}, SearchPanes.classes);
this.dom = { this.dom = {
container: $('<div/>') container: $('<div/>')
.addClass( this.classes.container ) .addClass(this.classes.container)
.appendTo( opts.container ) .appendTo(opts.container)
}; };
this.s = { this.s = {
dt: table dt: table
}; };
table.columns(opts.columns).eq(0).each( function ( idx ) { table
that._pane( idx ); .columns(opts.columns)
} ); .eq(0)
.each(function(idx) {
that._pane(idx);
});
$(this.dom.container) $(this.dom.container)
.on( 'click', 'li', function () { .on('click', 'li', function() {
that._toggle( this ); that._toggle(this);
} ) })
.on( 'click', 'button.'+this.classes.clear, function () { .on('click', 'button.' + this.classes.clear, function() {
that._clear( $(this).closest('div.'+that.classes.pane.container) ); that._clear($(this).closest('div.' + that.classes.pane.container));
} ); });
} }
$.extend( SearchPanes.prototype, { $.extend(SearchPanes.prototype, {
_binData: function ( data ) { _binData: function(data) {
var out = {}; var out = {};
data.each( function (d) { data.each(function(d) {
if ( ! d ) { if (!d) {
return; return;
} }
if ( ! out[ d ] ) { if (!out[d]) {
out[ d ] = 1; out[d] = 1;
} } else {
else { out[d]++;
out[ d ]++;
} }
} ); });
return out; return out;
}, },
_clear: function ( pane ) { _clear: function(pane) {
var classes = this.classes; var classes = this.classes;
var itemSelected = classes.item.selected; var itemSelected = classes.item.selected;
pane.find( 'li.'+itemSelected ).removeClass( itemSelected ); pane.find('li.' + itemSelected).removeClass(itemSelected);
pane.removeClass( classes.pane.active ); pane.removeClass(classes.pane.active);
this.s.dt this.s.dt
.column( pane.data('column') ) .column(pane.data('column'))
.search('') .search('')
.draw(); .draw();
}, },
_pane: function ( idx ) { _pane: function(idx) {
var classes = this.classes; var classes = this.classes;
var itemClasses = classes.item; var itemClasses = classes.item;
var paneClasses = classes.pane; var paneClasses = classes.pane;
var table = this.s.dt; var table = this.s.dt;
var column = table.column( idx ); var column = table.column(idx);
var list = $('<ul/>'); var list = $('<ul/>');
var bins = this._binData( column.data().flatten() ); var bins = this._binData(column.data().flatten());
// On initialisation, do we need to set a filtering value from a // On initialisation, do we need to set a filtering value from a
// saved state or init option? // saved state or init option?
var search = column.search(); var search = column.search();
search = search.length && search[0] ? search =
search[0].substr( 1, search[0].length-2 ).split('|') : search.length && search[0]
[]; ? search[0].substr(1, search[0].length - 2).split('|')
: [];
var data = column.data().unique().sort().toArray();
var data = column
for ( var i=0, ien=data.length ; i<ien ; i++ ) { .data()
if ( data[i] ) { .unique()
.sort()
.toArray();
for (var i = 0, ien = data.length; i < ien; i++) {
if (data[i]) {
var li = $('<li/>') var li = $('<li/>')
.html( '<span class="'+itemClasses.label+'">'+data[i]+'</span>' ) .html(
.data( 'filter', data[i] ) '<span class="' + itemClasses.label + '">' + data[i] + '</span>'
.append( $('<span/>').addClass( itemClasses.count ).html( bins[ data[i] ] ) ); )
.data('filter', data[i])
.append(
$('<span/>')
.addClass(itemClasses.count)
.html(bins[data[i]])
);
if ( $.inArray( data[i], search ) !== -1 ) { if ($.inArray(data[i], search) !== -1) {
li.addClass(itemClasses.selected); li.addClass(itemClasses.selected);
} }
list.append( li ); list.append(li);
} }
} }
$(this.dom.container).append( $(this.dom.container).append(
$('<div/>') $('<div/>')
.data( 'column', idx ) .data('column', idx)
.addClass( paneClasses.container ) .addClass(paneClasses.container)
.addClass( search.length ? paneClasses.active : '' ) .addClass(search.length ? paneClasses.active : '')
.append( $('<button type="button">&times;</button>').addClass(this.classes.clear) ) .append(
.append( $('<div/>').addClass(paneClasses.title).html( $(column.header()).text() ) ) $('<button type="button">&times;</button>').addClass(
.append( $('<div/>').addClass(paneClasses.scroller).append( list ) ) this.classes.clear
)
)
.append(
$('<div/>')
.addClass(paneClasses.title)
.html($(column.header()).text())
)
.append(
$('<div/>')
.addClass(paneClasses.scroller)
.append(list)
)
); );
}, },
_toggle: function ( li ) { _toggle: function(li) {
var classes = this.classes; var classes = this.classes;
var itemSelected = classes.item.selected; var itemSelected = classes.item.selected;
var table = this.s.dt; var table = this.s.dt;
var li = $(li); var li = $(li);
var pane = li.closest('div.'+classes.pane.container); var pane = li.closest('div.' + classes.pane.container);
li.toggleClass( itemSelected, ! li.hasClass( itemSelected ) ); li.toggleClass(itemSelected, !li.hasClass(itemSelected));
var filters = pane.find( 'li.'+itemSelected ); var filters = pane.find('li.' + itemSelected);
if ( filters.length === 0 ) { if (filters.length === 0) {
pane.removeClass( classes.pane.active ); pane.removeClass(classes.pane.active);
table table
.column( pane.data('column') ) .column(pane.data('column'))
.search('') .search('')
.draw(); .draw();
} } else {
else { pane.addClass(classes.pane.active);
pane.addClass( classes.pane.active );
table table
.column( pane.data('column') ) .column(pane.data('column'))
.search( $.map( filters, function (filter) { .search(
$.map(filters, function(filter) {
return $(filter).data('filter'); return $(filter).data('filter');
} ).join('|'), true, false ) }).join('|'),
true,
false
)
.draw(); .draw();
} }
} }
} ); });
SearchPanes.classes = { SearchPanes.classes = {
container: 'dt-searchPanes', container: 'dt-searchPanes',
clear: 'clear', clear: 'clear',
pane: { pane: {
@ -190,26 +213,22 @@ SearchPanes.classes = {
label: 'label', label: 'label',
count: 'count' count: 'count'
} }
}; };
$(document).on( 'init.dt', function (e, settings, json) { $(document).on('init.dt', function(e, settings, json) {
if ( e.namespace !== 'dt' ) { if (e.namespace !== 'dt') {
return; return;
} }
var init = settings.oInit.searchPane; var init = settings.oInit.searchPane;
var defaults = DataTable.defaults.searchPane; var defaults = DataTable.defaults.searchPane;
if ( init || defaults ) { if (init || defaults) {
var opts = $.extend( {}, init, defaults ); var opts = $.extend({}, init, defaults);
if ( init !== false ) { if (init !== false) {
new SearchPanes( settings, opts ); new SearchPanes(settings, opts);
} }
} }
} ); });
});
}));

Loading…
Cancel
Save