|
|
|
@ -1,5 +1,4 @@
|
|
|
|
|
// TODO
|
|
|
|
|
// - Tidy up class names
|
|
|
|
|
// - Option to have vertical layout class
|
|
|
|
|
// - Number of horizontal panels class options
|
|
|
|
|
// - Threshold option - require that there is duplicate information in a column before it is used
|
|
|
|
@ -44,14 +43,17 @@ function SearchPanes ( settings, opts ) {
|
|
|
|
|
var that = this;
|
|
|
|
|
var table = new DataTable.Api( settings );
|
|
|
|
|
|
|
|
|
|
this.s = {
|
|
|
|
|
dt: table
|
|
|
|
|
};
|
|
|
|
|
this.classes = $.extend( true, {}, SearchPanes.classes );
|
|
|
|
|
|
|
|
|
|
this.dom = {
|
|
|
|
|
container: $('<div class="dt-searchPane"/>')
|
|
|
|
|
container: $('<div/>')
|
|
|
|
|
.addClass( this.classes.container )
|
|
|
|
|
.appendTo( opts.container )
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.s = {
|
|
|
|
|
dt: table
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
table.columns(opts.columns).eq(0).each( function ( idx ) {
|
|
|
|
|
that._pane( idx );
|
|
|
|
@ -61,37 +63,26 @@ function SearchPanes ( settings, opts ) {
|
|
|
|
|
.on( 'click', 'li', function () {
|
|
|
|
|
that._toggle( this );
|
|
|
|
|
} )
|
|
|
|
|
.on( 'click', 'button.close', function () {
|
|
|
|
|
that._clear( $(this).closest('div.pane') );
|
|
|
|
|
.on( 'click', 'button.'+this.classes.clear, function () {
|
|
|
|
|
that._clear( $(this).closest('div.'+that.classes.pane.container) );
|
|
|
|
|
} );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$.extend( SearchPanes.prototype, {
|
|
|
|
|
_binData: function ( data, tags ) {
|
|
|
|
|
_binData: function ( data ) {
|
|
|
|
|
var out = {};
|
|
|
|
|
var add = function ( d ) {
|
|
|
|
|
if ( ! out[ d ] ) {
|
|
|
|
|
out[ d ] = 1;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
out[ d ]++;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
data.each( function (d) {
|
|
|
|
|
if ( ! d ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( tags ) {
|
|
|
|
|
var a = d.split(',');
|
|
|
|
|
for ( var i=0, ien=a.length ; i<ien ; i++ ) {
|
|
|
|
|
add( a[i] );
|
|
|
|
|
}
|
|
|
|
|
if ( ! out[ d ] ) {
|
|
|
|
|
out[ d ] = 1;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
add( d );
|
|
|
|
|
out[ d ]++;
|
|
|
|
|
}
|
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
@ -99,8 +90,11 @@ $.extend( SearchPanes.prototype, {
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
_clear: function ( pane ) {
|
|
|
|
|
pane.find( 'li.selected' ).removeClass( 'selected' );
|
|
|
|
|
pane.removeClass('filtering');
|
|
|
|
|
var classes = this.classes;
|
|
|
|
|
var itemSelected = classes.item.selected;
|
|
|
|
|
|
|
|
|
|
pane.find( 'li.'+itemSelected ).removeClass( itemSelected );
|
|
|
|
|
pane.removeClass( classes.pane.active );
|
|
|
|
|
|
|
|
|
|
this.s.dt
|
|
|
|
|
.column( pane.data('column') )
|
|
|
|
@ -109,11 +103,13 @@ $.extend( SearchPanes.prototype, {
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
_pane: function ( idx ) {
|
|
|
|
|
var classes = this.classes;
|
|
|
|
|
var itemClasses = classes.item;
|
|
|
|
|
var paneClasses = classes.pane;
|
|
|
|
|
var table = this.s.dt;
|
|
|
|
|
var column = table.column( idx );
|
|
|
|
|
var list = $('<ul/>');
|
|
|
|
|
var tags = $( column.header() ).hasClass('tags');
|
|
|
|
|
var bins = this._binData( column.data().flatten(), tags );
|
|
|
|
|
var bins = this._binData( column.data().flatten() );
|
|
|
|
|
|
|
|
|
|
// On initialisation, do we need to set a filtering value from a
|
|
|
|
|
// saved state or init option?
|
|
|
|
@ -122,19 +118,17 @@ $.extend( SearchPanes.prototype, {
|
|
|
|
|
search[0].substr( 1, search[0].length-2 ).split('|') :
|
|
|
|
|
[];
|
|
|
|
|
|
|
|
|
|
var data = tags ?
|
|
|
|
|
table.ajax.json().tags :
|
|
|
|
|
column.data().unique().sort().toArray();
|
|
|
|
|
var data = column.data().unique().sort().toArray();
|
|
|
|
|
|
|
|
|
|
for ( var i=0, ien=data.length ; i<ien ; i++ ) {
|
|
|
|
|
if ( data[i] ) {
|
|
|
|
|
var li = $('<li/>')
|
|
|
|
|
.html( '<span class="label">'+data[i]+'</span>' )
|
|
|
|
|
.html( '<span class="'+itemClasses.label+'">'+data[i]+'</span>' )
|
|
|
|
|
.data( 'filter', data[i] )
|
|
|
|
|
.append( $('<span class="count" />').html( bins[ data[i] ] ) );
|
|
|
|
|
.append( $('<span/>').addClass( itemClasses.count ).html( bins[ data[i] ] ) );
|
|
|
|
|
|
|
|
|
|
if ( $.inArray( data[i], search ) !== -1 ) {
|
|
|
|
|
li.addClass('selected');
|
|
|
|
|
li.addClass(itemClasses.selected);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
list.append( li );
|
|
|
|
@ -142,33 +136,36 @@ $.extend( SearchPanes.prototype, {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$(this.dom.container).append(
|
|
|
|
|
$('<div class="pane"/>')
|
|
|
|
|
$('<div/>')
|
|
|
|
|
.data( 'column', idx )
|
|
|
|
|
.addClass( search.length ? 'filtering' : '' )
|
|
|
|
|
.append( '<button type="button" class="close">×</button>' )
|
|
|
|
|
.append( $('<div class="title"/>').html( $(column.header()).text() ) )
|
|
|
|
|
.append( $('<div class="scroller"/>').append( list ) )
|
|
|
|
|
.addClass( paneClasses.container )
|
|
|
|
|
.addClass( search.length ? paneClasses.active : '' )
|
|
|
|
|
.append( $('<button type="button">×</button>').addClass(this.classes.clear) )
|
|
|
|
|
.append( $('<div/>').addClass(paneClasses.title).html( $(column.header()).text() ) )
|
|
|
|
|
.append( $('<div/>').addClass(paneClasses.scroller).append( list ) )
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
_toggle: function ( li ) {
|
|
|
|
|
var classes = this.classes;
|
|
|
|
|
var itemSelected = classes.item.selected;
|
|
|
|
|
var table = this.s.dt;
|
|
|
|
|
var li = $(li);
|
|
|
|
|
var pane = li.closest('div.pane');
|
|
|
|
|
var pane = li.closest('div.'+classes.pane.container);
|
|
|
|
|
|
|
|
|
|
li.toggleClass( 'selected', ! li.hasClass( 'selected' ) );
|
|
|
|
|
li.toggleClass( itemSelected, ! li.hasClass( itemSelected ) );
|
|
|
|
|
|
|
|
|
|
var filters = pane.find( 'li.selected' );
|
|
|
|
|
var filters = pane.find( 'li.'+itemSelected );
|
|
|
|
|
|
|
|
|
|
if ( filters.length === 0 ) {
|
|
|
|
|
pane.removeClass('filtering');
|
|
|
|
|
pane.removeClass( classes.pane.active );
|
|
|
|
|
table
|
|
|
|
|
.column( pane.data('column') )
|
|
|
|
|
.search('')
|
|
|
|
|
.draw();
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
pane.addClass('filtering');
|
|
|
|
|
pane.addClass( classes.pane.active );
|
|
|
|
|
table
|
|
|
|
|
.column( pane.data('column') )
|
|
|
|
|
.search( $.map( filters, function (filter) {
|
|
|
|
@ -179,6 +176,22 @@ $.extend( SearchPanes.prototype, {
|
|
|
|
|
}
|
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
SearchPanes.classes = {
|
|
|
|
|
container: 'dt-searchPanes',
|
|
|
|
|
clear: 'clear',
|
|
|
|
|
pane: {
|
|
|
|
|
active: 'filtering',
|
|
|
|
|
container: 'pane',
|
|
|
|
|
title: 'title',
|
|
|
|
|
scroller: 'scroller'
|
|
|
|
|
},
|
|
|
|
|
item: {
|
|
|
|
|
selected: 'selected',
|
|
|
|
|
label: 'label',
|
|
|
|
|
count: 'count'
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$(document).on( 'init.dt', function (e, settings, json) {
|
|
|
|
|