From e221ce9c3df00784f13693ffdc580560ccc5e8e6 Mon Sep 17 00:00:00 2001 From: Allan Jardine Date: Thu, 30 Nov 2017 11:10:26 +0000 Subject: [PATCH] Features - search pane: Cleaning up into functions --- features/searchPane/dataTables.searchPane.js | 135 ++++++++++-------- .../searchPane/dataTables.searchPane.scss | 2 +- features/searchPane/index.html | 9 +- 3 files changed, 81 insertions(+), 65 deletions(-) diff --git a/features/searchPane/dataTables.searchPane.js b/features/searchPane/dataTables.searchPane.js index fd5a464..f433440 100644 --- a/features/searchPane/dataTables.searchPane.js +++ b/features/searchPane/dataTables.searchPane.js @@ -1,6 +1,5 @@ // TODO // - Tidy up class names -// - Clean up code into functions // - 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 @@ -8,6 +7,7 @@ // - Styling for container / header // - Styling for clear option // - State saving integration +// - Fix regex characters - (CEO) for example (function( factory ){ @@ -43,15 +43,77 @@ var DataTable = $.fn.dataTable; function SearchPanes ( settings, opts ) { var that = this; var table = new DataTable.Api( settings ); - var container = $('
') - .appendTo( opts.container ); + + this.s = { + dt: table + }; + + this.dom = { + container: $('
') + .appendTo( opts.container ) + } table.columns(opts.columns).eq(0).each( function ( idx ) { - var column = table.column( idx ); + that._pane( idx ); + } ); + + $(this.dom.container) + .on( 'click', 'li', function () { + that._toggle( this ); + } ) + .on( 'click', 'button.close', function () { + that._clear( $(this).closest('div.pane') ); + } ); +} + + +$.extend( SearchPanes.prototype, { + _binData: function ( data, tags ) { + 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'); var tags = $( column.header() ).hasClass('tags'); - var bins = that._binData( column.data().flatten(), tags ); + var bins = this._binData( column.data().flatten(), tags ); // On initialisation, do we need to set a filtering value from a // saved state or init option? @@ -79,7 +141,7 @@ function SearchPanes ( settings, opts ) { } } - $(container).append( + $(this.dom.container).append( $('
') .data( 'column', idx ) .addClass( search.length ? 'filtering' : '' ) @@ -87,17 +149,15 @@ function SearchPanes ( settings, opts ) { .append( $('
').html( $(column.header()).text() ) ) .append( $('
').append( list ) ) ); - } ); + }, + + _toggle: function ( li ) { + var table = this.s.dt; + var li = $(li); + var pane = li.closest('div.pane'); - $(container).on( 'click', 'li', function () { - if ( $(this).hasClass( 'selected' ) ) { - $(this).removeClass( 'selected' ); - } - else { - $(this).addClass( 'selected' ); - } + li.toggleClass( 'selected', ! li.hasClass( 'selected' ) ); - var pane = $(this).closest('div.pane'); var filters = pane.find( 'li.selected' ); if ( filters.length === 0 ) { @@ -116,51 +176,6 @@ function SearchPanes ( settings, opts ) { } ).join('|'), true, false ) .draw(); } - } ); - - - $(container).on( 'click', 'button.close', function () { - var pane = $(this).closest('div.pane'); - pane.find( 'li.selected' ).removeClass( 'selected' ); - pane.removeClass('filtering'); - - table - .column( pane.data('column') ) - .search('') - .draw(); - } ); -} - - -$.extend( SearchPanes.prototype, { - _binData: function ( data, tags ) { - 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 ; iDataTables search pane example - + - - + +