diff --git a/dist/stackonly/tablesaw.stackonly.css b/dist/stackonly/tablesaw.stackonly.css index a040dcf..363b3eb 100644 --- a/dist/stackonly/tablesaw.stackonly.css +++ b/dist/stackonly/tablesaw.stackonly.css @@ -1,4 +1,4 @@ -/*! Tablesaw - v3.0.1 - 2017-06-01 +/*! Tablesaw - v3.0.2 - 2017-07-07 * https://github.com/filamentgroup/tablesaw * Copyright (c) 2017 Filament Group; Licensed MIT */ diff --git a/dist/stackonly/tablesaw.stackonly.jquery.js b/dist/stackonly/tablesaw.stackonly.jquery.js index 3335c7c..250b8c8 100644 --- a/dist/stackonly/tablesaw.stackonly.jquery.js +++ b/dist/stackonly/tablesaw.stackonly.jquery.js @@ -1,4 +1,4 @@ -/*! Tablesaw - v3.0.1 - 2017-06-01 +/*! Tablesaw - v3.0.2 - 2017-07-07 * https://github.com/filamentgroup/tablesaw * Copyright (c) 2017 Filament Group; Licensed MIT */ // UMD module definition @@ -36,20 +36,39 @@ var Tablesaw = { i18n: { - modes: [ 'Stack', 'Swipe', 'Toggle' ], - columns: 'Columns', - columnBtnText: 'Columns', - columnsDialogError: 'No eligible columns.', - sort: 'Sort' + modeStack: "Stack", + modeSwipe: "Swipe", + modeToggle: "Toggle", + modeSwitchColumnsAbbreviated: "Cols", + modeSwitchColumns: "Columns", + columnToggleButton: "Columns", + columnToggleError: "No eligible columns.", + sort: "Sort", + swipePreviousColumn: "Previous column", + swipeNextColumn: "Next column" }, // cut the mustard - mustard: ( 'head' in document ) && // IE9+, Firefox 4+, Safari 5.1+, Mobile Safari 4.1+, Opera 11.5+, Android 2.3+ - ( !window.blackberry || window.WebKitPoint ) && // only WebKit Blackberry (OS 6+) + mustard: + "head" in document && // IE9+, Firefox 4+, Safari 5.1+, Mobile Safari 4.1+, Opera 11.5+, Android 2.3+ + (!window.blackberry || window.WebKitPoint) && // only WebKit Blackberry (OS 6+) !window.operamini }; -if( Tablesaw.mustard ) { - $( document.documentElement ).addClass( 'tablesaw-enhanced' ); +$(win.document).on("enhance.tablesaw", function() { + // Extend i18n config, if one exists. + if (typeof TablesawConfig !== "undefined" && TablesawConfig.i18n) { + Tablesaw.i18n = $.extend(Tablesaw.i18n, TablesawConfig.i18n || {}); + } + + Tablesaw.i18n.modes = [ + Tablesaw.i18n.modeStack, + Tablesaw.i18n.modeSwipe, + Tablesaw.i18n.modeToggle + ]; +}); + +if (Tablesaw.mustard) { + $(document.documentElement).addClass("tablesaw-enhanced"); } (function() { @@ -70,21 +89,21 @@ if( Tablesaw.mustard ) { Tablesaw.events = events; - var Table = function( element ) { - if( !element ) { - throw new Error( "Tablesaw requires an element." ); + var Table = function(element) { + if (!element) { + throw new Error("Tablesaw requires an element."); } this.table = element; - this.$table = $( element ); + this.$table = $(element); // only one and are allowed, per the specification - this.$thead = this.$table.children().filter( "thead" ).eq( 0 ); + this.$thead = this.$table.children().filter("thead").eq(0); // multiple are allowed, per the specification - this.$tbody = this.$table.children().filter( "tbody" ); + this.$tbody = this.$table.children().filter("tbody"); - this.mode = this.$table.attr( "data-tablesaw-mode" ) || defaultMode; + this.mode = this.$table.attr("data-tablesaw-mode") || defaultMode; this.$toolbar = null; @@ -93,36 +112,36 @@ if( Tablesaw.mustard ) { Table.prototype.init = function() { // assign an id if there is none - if ( !this.$table.attr( "id" ) ) { - this.$table.attr( "id", pluginName + "-" + Math.round( Math.random() * 10000 ) ); + if (!this.$table.attr("id")) { + this.$table.attr("id", pluginName + "-" + Math.round(Math.random() * 10000)); } this.createToolbar(); this._initCells(); - this.$table.data( pluginName, this ); + this.$table.data(pluginName, this); - this.$table.trigger( events.create, [ this ] ); + this.$table.trigger(events.create, [this]); }; - Table.prototype.getConfig = function( pluginSpecificConfig ) { + Table.prototype.getConfig = function(pluginSpecificConfig) { // shoestring extend doesn’t support arbitrary args - var configs = $.extend( defaultConfig, pluginSpecificConfig || {} ); - return $.extend( configs, typeof TablesawConfig !== "undefined" ? TablesawConfig : {} ); + var configs = $.extend(defaultConfig, pluginSpecificConfig || {}); + return $.extend(configs, typeof TablesawConfig !== "undefined" ? TablesawConfig : {}); }; Table.prototype._getPrimaryHeaderRow = function() { - return this._getHeaderRows().eq( 0 ); + return this._getHeaderRows().eq(0); }; Table.prototype._getHeaderRows = function() { - return this.$thead.children().filter( "tr" ).filter(function() { - return !$( this ).is( "[data-tablesaw-ignorerow]" ); + return this.$thead.children().filter("tr").filter(function() { + return !$(this).is("[data-tablesaw-ignorerow]"); }); }; - Table.prototype._getRowIndex = function( $row ) { + Table.prototype._getRowIndex = function($row) { return $row.prevAll().length; }; @@ -130,28 +149,28 @@ if( Tablesaw.mustard ) { var self = this; var indeces = []; this._getHeaderRows().each(function() { - indeces.push( self._getRowIndex( $( this ) ) ); + indeces.push(self._getRowIndex($(this))); }); return indeces; }; - Table.prototype._getPrimaryHeaderCells = function( $row ) { - return ( $row || this._getPrimaryHeaderRow() ).find( "th" ); + Table.prototype._getPrimaryHeaderCells = function($row) { + return ($row || this._getPrimaryHeaderRow()).find("th"); }; - Table.prototype._findPrimaryHeadersForCell = function( cell ) { + Table.prototype._findPrimaryHeadersForCell = function(cell) { var $headerRow = this._getPrimaryHeaderRow(); - var $headers = this._getPrimaryHeaderCells( $headerRow ); - var headerRowIndex = this._getRowIndex( $headerRow ); + var $headers = this._getPrimaryHeaderCells($headerRow); + var headerRowIndex = this._getRowIndex($headerRow); var results = []; - for( var rowNumber = 0; rowNumber < this.headerMapping.length; rowNumber++ ) { - if( rowNumber === headerRowIndex ) { + for (var rowNumber = 0; rowNumber < this.headerMapping.length; rowNumber++) { + if (rowNumber === headerRowIndex) { continue; } - for( var colNumber = 0; colNumber < this.headerMapping[ rowNumber ].length; colNumber++ ) { - if( this.headerMapping[ rowNumber ][ colNumber ] === cell ) { - results.push( $headers[ colNumber ] ); + for (var colNumber = 0; colNumber < this.headerMapping[rowNumber].length; colNumber++) { + if (this.headerMapping[rowNumber][colNumber] === cell) { + results.push($headers[colNumber]); } } } @@ -161,20 +180,20 @@ if( Tablesaw.mustard ) { // used by init cells Table.prototype.getRows = function() { var self = this; - return this.$table.find( "tr" ).filter(function() { - return $( this ).closest( "table" ).is( self.$table ); + return this.$table.find("tr").filter(function() { + return $(this).closest("table").is(self.$table); }); }; // used by sortable - Table.prototype.getBodyRows = function( tbody ) { - return ( tbody ? $( tbody ) : this.$tbody ).children().filter( "tr" ); + Table.prototype.getBodyRows = function(tbody) { + return (tbody ? $(tbody) : this.$tbody).children().filter("tr"); }; - Table.prototype.getHeaderCellIndex = function( cell ) { - var lookup = this.headerMapping[ 0 ]; - for( var colIndex = 0; colIndex < lookup.length; colIndex++ ) { - if( lookup[ colIndex ] === cell ) { + Table.prototype.getHeaderCellIndex = function(cell) { + var lookup = this.headerMapping[0]; + for (var colIndex = 0; colIndex < lookup.length; colIndex++) { + if (lookup[colIndex] === cell) { return colIndex; } } @@ -186,36 +205,36 @@ if( Tablesaw.mustard ) { var $rows = this.getRows(); var columnLookup = []; - $rows.each(function( rowNumber ) { - columnLookup[ rowNumber ] = []; + $rows.each(function(rowNumber) { + columnLookup[rowNumber] = []; }); - $rows.each(function( rowNumber ) { + $rows.each(function(rowNumber) { var coltally = 0; - var $t = $( this ); + var $t = $(this); var children = $t.children(); children.each(function() { - var colspan = parseInt( this.getAttribute( "colspan" ), 10 ); - var rowspan = parseInt( this.getAttribute( "rowspan" ), 10 ); + var colspan = parseInt(this.getAttribute("colspan"), 10); + var rowspan = parseInt(this.getAttribute("rowspan"), 10); // set in a previous rowspan - while( columnLookup[ rowNumber ][ coltally ] ) { + while (columnLookup[rowNumber][coltally]) { coltally++; } - columnLookup[ rowNumber ][ coltally ] = this; + columnLookup[rowNumber][coltally] = this; // TODO? both colspan and rowspan - if( colspan ) { - for( var k = 0; k < colspan - 1; k++ ){ + if (colspan) { + for (var k = 0; k < colspan - 1; k++) { coltally++; - columnLookup[ rowNumber ][ coltally ] = this; + columnLookup[rowNumber][coltally] = this; } } - if( rowspan ) { - for( var j = 1; j < rowspan; j++ ){ - columnLookup[ rowNumber + j ][ coltally ] = this; + if (rowspan) { + for (var j = 1; j < rowspan; j++) { + columnLookup[rowNumber + j][coltally] = this; } } @@ -224,22 +243,22 @@ if( Tablesaw.mustard ) { }); var headerRowIndeces = this._getHeaderRowIndeces(); - for( var colNumber = 0; colNumber < columnLookup[ 0 ].length; colNumber++ ) { - for( var headerIndex = 0, k = headerRowIndeces.length; headerIndex < k; headerIndex++ ) { - var headerCol = columnLookup[ headerRowIndeces[ headerIndex ] ][ colNumber ]; + for (var colNumber = 0; colNumber < columnLookup[0].length; colNumber++) { + for (var headerIndex = 0, k = headerRowIndeces.length; headerIndex < k; headerIndex++) { + var headerCol = columnLookup[headerRowIndeces[headerIndex]][colNumber]; - var rowNumber = headerRowIndeces[ headerIndex ]; + var rowNumber = headerRowIndeces[headerIndex]; var rowCell; - if( !headerCol.cells ) { + if (!headerCol.cells) { headerCol.cells = []; } - while( rowNumber < columnLookup.length ) { - rowCell = columnLookup[ rowNumber ][ colNumber ]; + while (rowNumber < columnLookup.length) { + rowCell = columnLookup[rowNumber][colNumber]; - if( headerCol !== rowCell ) { - headerCol.cells.push( rowCell ); + if (headerCol !== rowCell) { + headerCol.cells.push(rowCell); } rowNumber++; @@ -253,38 +272,36 @@ if( Tablesaw.mustard ) { Table.prototype.refresh = function() { this._initCells(); - this.$table.trigger( events.refresh, [ this ] ); + this.$table.trigger(events.refresh, [this]); }; Table.prototype._getToolbarAnchor = function() { var $parent = this.$table.parent(); - if( $parent.is( ".tablesaw-overflow" ) ) { + if ($parent.is(".tablesaw-overflow")) { return $parent; } return this.$table; }; - Table.prototype._getToolbar = function( $anchor ) { - if( !$anchor ) { + Table.prototype._getToolbar = function($anchor) { + if (!$anchor) { $anchor = this._getToolbarAnchor(); } - return $anchor.prev().filter( "." + classes.toolbar ); + return $anchor.prev().filter("." + classes.toolbar); }; Table.prototype.createToolbar = function() { // Insert the toolbar // TODO move this into a separate component var $anchor = this._getToolbarAnchor(); - var $toolbar = this._getToolbar( $anchor ); - if( !$toolbar.length ) { - $toolbar = $( '
' ) - .addClass( classes.toolbar ) - .insertBefore( $anchor ); + var $toolbar = this._getToolbar($anchor); + if (!$toolbar.length) { + $toolbar = $("
").addClass(classes.toolbar).insertBefore($anchor); } this.$toolbar = $toolbar; - if( this.mode ) { - this.$toolbar.addClass( 'tablesaw-mode-' + this.mode ); + if (this.mode) { + this.$toolbar.addClass("tablesaw-mode-" + this.mode); } }; @@ -292,172 +309,181 @@ if( Tablesaw.mustard ) { // Don’t remove the toolbar, just erase the classes on it. // Some of the table features are not yet destroy-friendly. this._getToolbar().each(function() { - this.className = this.className.replace( /\btablesaw-mode\-\w*\b/gi, '' ); + this.className = this.className.replace(/\btablesaw-mode\-\w*\b/gi, ""); }); - var tableId = this.$table.attr( 'id' ); - $( document ).off( "." + tableId ); - $( window ).off( "." + tableId ); + var tableId = this.$table.attr("id"); + $(document).off("." + tableId); + $(window).off("." + tableId); // other plugins - this.$table.trigger( events.destroy, [ this ] ); + this.$table.trigger(events.destroy, [this]); - this.$table.removeData( pluginName ); + this.$table.removeData(pluginName); }; // Collection method. - $.fn[ pluginName ] = function() { - return this.each( function() { - var $t = $( this ); + $.fn[pluginName] = function() { + return this.each(function() { + var $t = $(this); - if( $t.data( pluginName ) ){ + if ($t.data(pluginName)) { return; } - new Table( this ); + new Table(this); }); }; - var $doc = $( win.document ); - $doc.on( "enhance.tablesaw", function( e ) { + var $doc = $(win.document); + $doc.on("enhance.tablesaw", function(e) { // Cut the mustard - if( Tablesaw.mustard ) { - $( e.target ).find( initSelector ).filter( initFilterSelector )[ pluginName ](); + if (Tablesaw.mustard) { + $(e.target).find(initSelector).filter(initFilterSelector)[pluginName](); } }); // Avoid a resize during scroll: // Some Mobile devices trigger a resize during scroll (sometimes when - // doing elastic stretch at the end of the document or from the + // doing elastic stretch at the end of the document or from the // location bar hide) var isScrolling = false; var scrollTimeout; - $doc.on( "scroll.tablesaw", function() { + $doc.on("scroll.tablesaw", function() { isScrolling = true; - win.clearTimeout( scrollTimeout ); + win.clearTimeout(scrollTimeout); scrollTimeout = win.setTimeout(function() { isScrolling = false; - }, 300 ); // must be greater than the resize timeout below + }, 300); // must be greater than the resize timeout below }); var resizeTimeout; - $( win ).on( "resize", function() { - if( !isScrolling ) { - win.clearTimeout( resizeTimeout ); + $(win).on("resize", function() { + if (!isScrolling) { + win.clearTimeout(resizeTimeout); resizeTimeout = win.setTimeout(function() { - $doc.trigger( events.resize ); - }, 150 ); // must be less than the scrolling timeout above. + $doc.trigger(events.resize); + }, 150); // must be less than the scrolling timeout above. } }); +})(); -}()); - -(function(){ - +(function() { var classes = { - stackTable: 'tablesaw-stack', - cellLabels: 'tablesaw-cell-label', - cellContentLabels: 'tablesaw-cell-content' + stackTable: "tablesaw-stack", + cellLabels: "tablesaw-cell-label", + cellContentLabels: "tablesaw-cell-content" }; var data = { - key: 'tablesaw-stack' + key: "tablesaw-stack" }; var attrs = { - labelless: 'data-tablesaw-no-labels', - hideempty: 'data-tablesaw-hide-empty' + labelless: "data-tablesaw-no-labels", + hideempty: "data-tablesaw-hide-empty" }; - var Stack = function( element, tablesaw ) { - + var Stack = function(element, tablesaw) { this.tablesaw = tablesaw; - this.$table = $( element ); + this.$table = $(element); - this.labelless = this.$table.is( '[' + attrs.labelless + ']' ); - this.hideempty = this.$table.is( '[' + attrs.hideempty + ']' ); + this.labelless = this.$table.is("[" + attrs.labelless + "]"); + this.hideempty = this.$table.is("[" + attrs.hideempty + "]"); - this.$table.data( data.key, this ); + this.$table.data(data.key, this); }; Stack.prototype.init = function() { - this.$table.addClass( classes.stackTable ); + this.$table.addClass(classes.stackTable); - if( this.labelless ) { + if (this.labelless) { return; } var self = this; - this.$table.find( "th, td" ).filter(function() { - return !$( this ).closest( "thead" ).length; - }).filter(function() { - return !$( this ).closest( "tr" ).is( "[" + attrs.labelless + "]" ) && - ( !self.hideempty || !!$( this ).html() ); - }).each(function() { - var $newHeader = $( document.createElement( "b" ) ).addClass( classes.cellLabels ); - var $cell = $( this ); - - $( self.tablesaw._findPrimaryHeadersForCell( this ) ).each(function( index ) { - var $header = $( this.cloneNode( true ) ); - // TODO decouple from sortable better - // Changed from .text() in https://github.com/filamentgroup/tablesaw/commit/b9c12a8f893ec192830ec3ba2d75f062642f935b - // to preserve structural html in headers, like - var $sortableButton = $header.find( ".tablesaw-sortable-btn" ); - $header.find( ".tablesaw-sortable-arrow" ).remove(); - - // TODO decouple from checkall better - var $checkall = $header.find( "[data-tablesaw-checkall]" ); - $checkall.closest( "label" ).remove(); - if( $checkall.length ) { - $newHeader = $([]); - return; + this.$table + .find("th, td") + .filter(function() { + return !$(this).closest("thead").length; + }) + .filter(function() { + return ( + !$(this).closest("tr").is("[" + attrs.labelless + "]") && + (!self.hideempty || !!$(this).html()) + ); + }) + .each(function() { + var $newHeader = $(document.createElement("b")).addClass(classes.cellLabels); + var $cell = $(this); + + $(self.tablesaw._findPrimaryHeadersForCell(this)).each(function(index) { + var $header = $(this.cloneNode(true)); + // TODO decouple from sortable better + // Changed from .text() in https://github.com/filamentgroup/tablesaw/commit/b9c12a8f893ec192830ec3ba2d75f062642f935b + // to preserve structural html in headers, like + var $sortableButton = $header.find(".tablesaw-sortable-btn"); + $header.find(".tablesaw-sortable-arrow").remove(); + + // TODO decouple from checkall better + var $checkall = $header.find("[data-tablesaw-checkall]"); + $checkall.closest("label").remove(); + if ($checkall.length) { + $newHeader = $([]); + return; + } + + if (index > 0) { + $newHeader.append(document.createTextNode(", ")); + } + $newHeader.append( + $sortableButton.length ? $sortableButton[0].childNodes : $header[0].childNodes + ); + }); + + if ($newHeader.length && !$cell.find("." + classes.cellContentLabels).length) { + $cell.wrapInner(""); } - if( index > 0 ) { - $newHeader.append( document.createTextNode( ", " ) ); + // Update if already exists. + var $label = $cell.find("." + classes.cellLabels); + if (!$label.length) { + $cell.prepend($newHeader); + } else { + // only if changed + $label.replaceWith($newHeader); } - $newHeader.append( $sortableButton.length ? $sortableButton[ 0 ].childNodes : $header[ 0 ].childNodes ); }); - - if( $newHeader.length && !$cell.find( "." + classes.cellContentLabels ).length ) { - $cell.wrapInner( "" ); - } - - // Update if already exists. - var $label = $cell.find( "." + classes.cellLabels ); - if( !$label.length ) { - $cell.prepend( $newHeader ); - } else { // only if changed - $label.replaceWith( $newHeader ); - } - }); }; Stack.prototype.destroy = function() { - this.$table.removeClass( classes.stackTable ); - this.$table.find( '.' + classes.cellLabels ).remove(); - this.$table.find( '.' + classes.cellContentLabels ).each(function() { - $( this ).replaceWith( this.childNodes ); + this.$table.removeClass(classes.stackTable); + this.$table.find("." + classes.cellLabels).remove(); + this.$table.find("." + classes.cellContentLabels).each(function() { + $(this).replaceWith(this.childNodes); }); }; // on tablecreate, init - $( document ).on( Tablesaw.events.create, function( e, tablesaw ){ - if( tablesaw.mode === 'stack' ){ - var table = new Stack( tablesaw.table, tablesaw ); - table.init(); - } - }).on( Tablesaw.events.refresh, function( e, tablesaw ){ - if( tablesaw.mode === 'stack' ){ - $( tablesaw.table ).data( data.key ).init(); - } - }).on( Tablesaw.events.destroy, function( e, tablesaw ){ - if( tablesaw.mode === 'stack' ){ - $( tablesaw.table ).data( data.key ).destroy(); - } - }); + $(document) + .on(Tablesaw.events.create, function(e, tablesaw) { + if (tablesaw.mode === "stack") { + var table = new Stack(tablesaw.table, tablesaw); + table.init(); + } + }) + .on(Tablesaw.events.refresh, function(e, tablesaw) { + if (tablesaw.mode === "stack") { + $(tablesaw.table).data(data.key).init(); + } + }) + .on(Tablesaw.events.destroy, function(e, tablesaw) { + if (tablesaw.mode === "stack") { + $(tablesaw.table).data(data.key).destroy(); + } + }); +})(); -}()); })); diff --git a/dist/stackonly/tablesaw.stackonly.js b/dist/stackonly/tablesaw.stackonly.js index b6714d2..4ecbec7 100644 --- a/dist/stackonly/tablesaw.stackonly.js +++ b/dist/stackonly/tablesaw.stackonly.js @@ -1,4 +1,4 @@ -/*! Tablesaw - v3.0.1 - 2017-06-01 +/*! Tablesaw - v3.0.2 - 2017-07-07 * https://github.com/filamentgroup/tablesaw * Copyright (c) 2017 Filament Group; Licensed MIT */ /*! Shoestring - v2.0.0 - 2017-02-14 @@ -1743,20 +1743,39 @@ var Tablesaw = { i18n: { - modes: [ 'Stack', 'Swipe', 'Toggle' ], - columns: 'Columns', - columnBtnText: 'Columns', - columnsDialogError: 'No eligible columns.', - sort: 'Sort' + modeStack: "Stack", + modeSwipe: "Swipe", + modeToggle: "Toggle", + modeSwitchColumnsAbbreviated: "Cols", + modeSwitchColumns: "Columns", + columnToggleButton: "Columns", + columnToggleError: "No eligible columns.", + sort: "Sort", + swipePreviousColumn: "Previous column", + swipeNextColumn: "Next column" }, // cut the mustard - mustard: ( 'head' in document ) && // IE9+, Firefox 4+, Safari 5.1+, Mobile Safari 4.1+, Opera 11.5+, Android 2.3+ - ( !window.blackberry || window.WebKitPoint ) && // only WebKit Blackberry (OS 6+) + mustard: + "head" in document && // IE9+, Firefox 4+, Safari 5.1+, Mobile Safari 4.1+, Opera 11.5+, Android 2.3+ + (!window.blackberry || window.WebKitPoint) && // only WebKit Blackberry (OS 6+) !window.operamini }; -if( Tablesaw.mustard ) { - $( document.documentElement ).addClass( 'tablesaw-enhanced' ); +$(win.document).on("enhance.tablesaw", function() { + // Extend i18n config, if one exists. + if (typeof TablesawConfig !== "undefined" && TablesawConfig.i18n) { + Tablesaw.i18n = $.extend(Tablesaw.i18n, TablesawConfig.i18n || {}); + } + + Tablesaw.i18n.modes = [ + Tablesaw.i18n.modeStack, + Tablesaw.i18n.modeSwipe, + Tablesaw.i18n.modeToggle + ]; +}); + +if (Tablesaw.mustard) { + $(document.documentElement).addClass("tablesaw-enhanced"); } (function() { @@ -1777,21 +1796,21 @@ if( Tablesaw.mustard ) { Tablesaw.events = events; - var Table = function( element ) { - if( !element ) { - throw new Error( "Tablesaw requires an element." ); + var Table = function(element) { + if (!element) { + throw new Error("Tablesaw requires an element."); } this.table = element; - this.$table = $( element ); + this.$table = $(element); // only one and are allowed, per the specification - this.$thead = this.$table.children().filter( "thead" ).eq( 0 ); + this.$thead = this.$table.children().filter("thead").eq(0); // multiple are allowed, per the specification - this.$tbody = this.$table.children().filter( "tbody" ); + this.$tbody = this.$table.children().filter("tbody"); - this.mode = this.$table.attr( "data-tablesaw-mode" ) || defaultMode; + this.mode = this.$table.attr("data-tablesaw-mode") || defaultMode; this.$toolbar = null; @@ -1800,36 +1819,36 @@ if( Tablesaw.mustard ) { Table.prototype.init = function() { // assign an id if there is none - if ( !this.$table.attr( "id" ) ) { - this.$table.attr( "id", pluginName + "-" + Math.round( Math.random() * 10000 ) ); + if (!this.$table.attr("id")) { + this.$table.attr("id", pluginName + "-" + Math.round(Math.random() * 10000)); } this.createToolbar(); this._initCells(); - this.$table.data( pluginName, this ); + this.$table.data(pluginName, this); - this.$table.trigger( events.create, [ this ] ); + this.$table.trigger(events.create, [this]); }; - Table.prototype.getConfig = function( pluginSpecificConfig ) { + Table.prototype.getConfig = function(pluginSpecificConfig) { // shoestring extend doesn’t support arbitrary args - var configs = $.extend( defaultConfig, pluginSpecificConfig || {} ); - return $.extend( configs, typeof TablesawConfig !== "undefined" ? TablesawConfig : {} ); + var configs = $.extend(defaultConfig, pluginSpecificConfig || {}); + return $.extend(configs, typeof TablesawConfig !== "undefined" ? TablesawConfig : {}); }; Table.prototype._getPrimaryHeaderRow = function() { - return this._getHeaderRows().eq( 0 ); + return this._getHeaderRows().eq(0); }; Table.prototype._getHeaderRows = function() { - return this.$thead.children().filter( "tr" ).filter(function() { - return !$( this ).is( "[data-tablesaw-ignorerow]" ); + return this.$thead.children().filter("tr").filter(function() { + return !$(this).is("[data-tablesaw-ignorerow]"); }); }; - Table.prototype._getRowIndex = function( $row ) { + Table.prototype._getRowIndex = function($row) { return $row.prevAll().length; }; @@ -1837,28 +1856,28 @@ if( Tablesaw.mustard ) { var self = this; var indeces = []; this._getHeaderRows().each(function() { - indeces.push( self._getRowIndex( $( this ) ) ); + indeces.push(self._getRowIndex($(this))); }); return indeces; }; - Table.prototype._getPrimaryHeaderCells = function( $row ) { - return ( $row || this._getPrimaryHeaderRow() ).find( "th" ); + Table.prototype._getPrimaryHeaderCells = function($row) { + return ($row || this._getPrimaryHeaderRow()).find("th"); }; - Table.prototype._findPrimaryHeadersForCell = function( cell ) { + Table.prototype._findPrimaryHeadersForCell = function(cell) { var $headerRow = this._getPrimaryHeaderRow(); - var $headers = this._getPrimaryHeaderCells( $headerRow ); - var headerRowIndex = this._getRowIndex( $headerRow ); + var $headers = this._getPrimaryHeaderCells($headerRow); + var headerRowIndex = this._getRowIndex($headerRow); var results = []; - for( var rowNumber = 0; rowNumber < this.headerMapping.length; rowNumber++ ) { - if( rowNumber === headerRowIndex ) { + for (var rowNumber = 0; rowNumber < this.headerMapping.length; rowNumber++) { + if (rowNumber === headerRowIndex) { continue; } - for( var colNumber = 0; colNumber < this.headerMapping[ rowNumber ].length; colNumber++ ) { - if( this.headerMapping[ rowNumber ][ colNumber ] === cell ) { - results.push( $headers[ colNumber ] ); + for (var colNumber = 0; colNumber < this.headerMapping[rowNumber].length; colNumber++) { + if (this.headerMapping[rowNumber][colNumber] === cell) { + results.push($headers[colNumber]); } } } @@ -1868,20 +1887,20 @@ if( Tablesaw.mustard ) { // used by init cells Table.prototype.getRows = function() { var self = this; - return this.$table.find( "tr" ).filter(function() { - return $( this ).closest( "table" ).is( self.$table ); + return this.$table.find("tr").filter(function() { + return $(this).closest("table").is(self.$table); }); }; // used by sortable - Table.prototype.getBodyRows = function( tbody ) { - return ( tbody ? $( tbody ) : this.$tbody ).children().filter( "tr" ); + Table.prototype.getBodyRows = function(tbody) { + return (tbody ? $(tbody) : this.$tbody).children().filter("tr"); }; - Table.prototype.getHeaderCellIndex = function( cell ) { - var lookup = this.headerMapping[ 0 ]; - for( var colIndex = 0; colIndex < lookup.length; colIndex++ ) { - if( lookup[ colIndex ] === cell ) { + Table.prototype.getHeaderCellIndex = function(cell) { + var lookup = this.headerMapping[0]; + for (var colIndex = 0; colIndex < lookup.length; colIndex++) { + if (lookup[colIndex] === cell) { return colIndex; } } @@ -1893,36 +1912,36 @@ if( Tablesaw.mustard ) { var $rows = this.getRows(); var columnLookup = []; - $rows.each(function( rowNumber ) { - columnLookup[ rowNumber ] = []; + $rows.each(function(rowNumber) { + columnLookup[rowNumber] = []; }); - $rows.each(function( rowNumber ) { + $rows.each(function(rowNumber) { var coltally = 0; - var $t = $( this ); + var $t = $(this); var children = $t.children(); children.each(function() { - var colspan = parseInt( this.getAttribute( "colspan" ), 10 ); - var rowspan = parseInt( this.getAttribute( "rowspan" ), 10 ); + var colspan = parseInt(this.getAttribute("colspan"), 10); + var rowspan = parseInt(this.getAttribute("rowspan"), 10); // set in a previous rowspan - while( columnLookup[ rowNumber ][ coltally ] ) { + while (columnLookup[rowNumber][coltally]) { coltally++; } - columnLookup[ rowNumber ][ coltally ] = this; + columnLookup[rowNumber][coltally] = this; // TODO? both colspan and rowspan - if( colspan ) { - for( var k = 0; k < colspan - 1; k++ ){ + if (colspan) { + for (var k = 0; k < colspan - 1; k++) { coltally++; - columnLookup[ rowNumber ][ coltally ] = this; + columnLookup[rowNumber][coltally] = this; } } - if( rowspan ) { - for( var j = 1; j < rowspan; j++ ){ - columnLookup[ rowNumber + j ][ coltally ] = this; + if (rowspan) { + for (var j = 1; j < rowspan; j++) { + columnLookup[rowNumber + j][coltally] = this; } } @@ -1931,22 +1950,22 @@ if( Tablesaw.mustard ) { }); var headerRowIndeces = this._getHeaderRowIndeces(); - for( var colNumber = 0; colNumber < columnLookup[ 0 ].length; colNumber++ ) { - for( var headerIndex = 0, k = headerRowIndeces.length; headerIndex < k; headerIndex++ ) { - var headerCol = columnLookup[ headerRowIndeces[ headerIndex ] ][ colNumber ]; + for (var colNumber = 0; colNumber < columnLookup[0].length; colNumber++) { + for (var headerIndex = 0, k = headerRowIndeces.length; headerIndex < k; headerIndex++) { + var headerCol = columnLookup[headerRowIndeces[headerIndex]][colNumber]; - var rowNumber = headerRowIndeces[ headerIndex ]; + var rowNumber = headerRowIndeces[headerIndex]; var rowCell; - if( !headerCol.cells ) { + if (!headerCol.cells) { headerCol.cells = []; } - while( rowNumber < columnLookup.length ) { - rowCell = columnLookup[ rowNumber ][ colNumber ]; + while (rowNumber < columnLookup.length) { + rowCell = columnLookup[rowNumber][colNumber]; - if( headerCol !== rowCell ) { - headerCol.cells.push( rowCell ); + if (headerCol !== rowCell) { + headerCol.cells.push(rowCell); } rowNumber++; @@ -1960,38 +1979,36 @@ if( Tablesaw.mustard ) { Table.prototype.refresh = function() { this._initCells(); - this.$table.trigger( events.refresh, [ this ] ); + this.$table.trigger(events.refresh, [this]); }; Table.prototype._getToolbarAnchor = function() { var $parent = this.$table.parent(); - if( $parent.is( ".tablesaw-overflow" ) ) { + if ($parent.is(".tablesaw-overflow")) { return $parent; } return this.$table; }; - Table.prototype._getToolbar = function( $anchor ) { - if( !$anchor ) { + Table.prototype._getToolbar = function($anchor) { + if (!$anchor) { $anchor = this._getToolbarAnchor(); } - return $anchor.prev().filter( "." + classes.toolbar ); + return $anchor.prev().filter("." + classes.toolbar); }; Table.prototype.createToolbar = function() { // Insert the toolbar // TODO move this into a separate component var $anchor = this._getToolbarAnchor(); - var $toolbar = this._getToolbar( $anchor ); - if( !$toolbar.length ) { - $toolbar = $( '
' ) - .addClass( classes.toolbar ) - .insertBefore( $anchor ); + var $toolbar = this._getToolbar($anchor); + if (!$toolbar.length) { + $toolbar = $("
").addClass(classes.toolbar).insertBefore($anchor); } this.$toolbar = $toolbar; - if( this.mode ) { - this.$toolbar.addClass( 'tablesaw-mode-' + this.mode ); + if (this.mode) { + this.$toolbar.addClass("tablesaw-mode-" + this.mode); } }; @@ -1999,172 +2016,181 @@ if( Tablesaw.mustard ) { // Don’t remove the toolbar, just erase the classes on it. // Some of the table features are not yet destroy-friendly. this._getToolbar().each(function() { - this.className = this.className.replace( /\btablesaw-mode\-\w*\b/gi, '' ); + this.className = this.className.replace(/\btablesaw-mode\-\w*\b/gi, ""); }); - var tableId = this.$table.attr( 'id' ); - $( document ).off( "." + tableId ); - $( window ).off( "." + tableId ); + var tableId = this.$table.attr("id"); + $(document).off("." + tableId); + $(window).off("." + tableId); // other plugins - this.$table.trigger( events.destroy, [ this ] ); + this.$table.trigger(events.destroy, [this]); - this.$table.removeData( pluginName ); + this.$table.removeData(pluginName); }; // Collection method. - $.fn[ pluginName ] = function() { - return this.each( function() { - var $t = $( this ); + $.fn[pluginName] = function() { + return this.each(function() { + var $t = $(this); - if( $t.data( pluginName ) ){ + if ($t.data(pluginName)) { return; } - new Table( this ); + new Table(this); }); }; - var $doc = $( win.document ); - $doc.on( "enhance.tablesaw", function( e ) { + var $doc = $(win.document); + $doc.on("enhance.tablesaw", function(e) { // Cut the mustard - if( Tablesaw.mustard ) { - $( e.target ).find( initSelector ).filter( initFilterSelector )[ pluginName ](); + if (Tablesaw.mustard) { + $(e.target).find(initSelector).filter(initFilterSelector)[pluginName](); } }); // Avoid a resize during scroll: // Some Mobile devices trigger a resize during scroll (sometimes when - // doing elastic stretch at the end of the document or from the + // doing elastic stretch at the end of the document or from the // location bar hide) var isScrolling = false; var scrollTimeout; - $doc.on( "scroll.tablesaw", function() { + $doc.on("scroll.tablesaw", function() { isScrolling = true; - win.clearTimeout( scrollTimeout ); + win.clearTimeout(scrollTimeout); scrollTimeout = win.setTimeout(function() { isScrolling = false; - }, 300 ); // must be greater than the resize timeout below + }, 300); // must be greater than the resize timeout below }); var resizeTimeout; - $( win ).on( "resize", function() { - if( !isScrolling ) { - win.clearTimeout( resizeTimeout ); + $(win).on("resize", function() { + if (!isScrolling) { + win.clearTimeout(resizeTimeout); resizeTimeout = win.setTimeout(function() { - $doc.trigger( events.resize ); - }, 150 ); // must be less than the scrolling timeout above. + $doc.trigger(events.resize); + }, 150); // must be less than the scrolling timeout above. } }); +})(); -}()); - -(function(){ - +(function() { var classes = { - stackTable: 'tablesaw-stack', - cellLabels: 'tablesaw-cell-label', - cellContentLabels: 'tablesaw-cell-content' + stackTable: "tablesaw-stack", + cellLabels: "tablesaw-cell-label", + cellContentLabels: "tablesaw-cell-content" }; var data = { - key: 'tablesaw-stack' + key: "tablesaw-stack" }; var attrs = { - labelless: 'data-tablesaw-no-labels', - hideempty: 'data-tablesaw-hide-empty' + labelless: "data-tablesaw-no-labels", + hideempty: "data-tablesaw-hide-empty" }; - var Stack = function( element, tablesaw ) { - + var Stack = function(element, tablesaw) { this.tablesaw = tablesaw; - this.$table = $( element ); + this.$table = $(element); - this.labelless = this.$table.is( '[' + attrs.labelless + ']' ); - this.hideempty = this.$table.is( '[' + attrs.hideempty + ']' ); + this.labelless = this.$table.is("[" + attrs.labelless + "]"); + this.hideempty = this.$table.is("[" + attrs.hideempty + "]"); - this.$table.data( data.key, this ); + this.$table.data(data.key, this); }; Stack.prototype.init = function() { - this.$table.addClass( classes.stackTable ); + this.$table.addClass(classes.stackTable); - if( this.labelless ) { + if (this.labelless) { return; } var self = this; - this.$table.find( "th, td" ).filter(function() { - return !$( this ).closest( "thead" ).length; - }).filter(function() { - return !$( this ).closest( "tr" ).is( "[" + attrs.labelless + "]" ) && - ( !self.hideempty || !!$( this ).html() ); - }).each(function() { - var $newHeader = $( document.createElement( "b" ) ).addClass( classes.cellLabels ); - var $cell = $( this ); - - $( self.tablesaw._findPrimaryHeadersForCell( this ) ).each(function( index ) { - var $header = $( this.cloneNode( true ) ); - // TODO decouple from sortable better - // Changed from .text() in https://github.com/filamentgroup/tablesaw/commit/b9c12a8f893ec192830ec3ba2d75f062642f935b - // to preserve structural html in headers, like - var $sortableButton = $header.find( ".tablesaw-sortable-btn" ); - $header.find( ".tablesaw-sortable-arrow" ).remove(); - - // TODO decouple from checkall better - var $checkall = $header.find( "[data-tablesaw-checkall]" ); - $checkall.closest( "label" ).remove(); - if( $checkall.length ) { - $newHeader = $([]); - return; + this.$table + .find("th, td") + .filter(function() { + return !$(this).closest("thead").length; + }) + .filter(function() { + return ( + !$(this).closest("tr").is("[" + attrs.labelless + "]") && + (!self.hideempty || !!$(this).html()) + ); + }) + .each(function() { + var $newHeader = $(document.createElement("b")).addClass(classes.cellLabels); + var $cell = $(this); + + $(self.tablesaw._findPrimaryHeadersForCell(this)).each(function(index) { + var $header = $(this.cloneNode(true)); + // TODO decouple from sortable better + // Changed from .text() in https://github.com/filamentgroup/tablesaw/commit/b9c12a8f893ec192830ec3ba2d75f062642f935b + // to preserve structural html in headers, like + var $sortableButton = $header.find(".tablesaw-sortable-btn"); + $header.find(".tablesaw-sortable-arrow").remove(); + + // TODO decouple from checkall better + var $checkall = $header.find("[data-tablesaw-checkall]"); + $checkall.closest("label").remove(); + if ($checkall.length) { + $newHeader = $([]); + return; + } + + if (index > 0) { + $newHeader.append(document.createTextNode(", ")); + } + $newHeader.append( + $sortableButton.length ? $sortableButton[0].childNodes : $header[0].childNodes + ); + }); + + if ($newHeader.length && !$cell.find("." + classes.cellContentLabels).length) { + $cell.wrapInner(""); } - if( index > 0 ) { - $newHeader.append( document.createTextNode( ", " ) ); + // Update if already exists. + var $label = $cell.find("." + classes.cellLabels); + if (!$label.length) { + $cell.prepend($newHeader); + } else { + // only if changed + $label.replaceWith($newHeader); } - $newHeader.append( $sortableButton.length ? $sortableButton[ 0 ].childNodes : $header[ 0 ].childNodes ); }); - - if( $newHeader.length && !$cell.find( "." + classes.cellContentLabels ).length ) { - $cell.wrapInner( "" ); - } - - // Update if already exists. - var $label = $cell.find( "." + classes.cellLabels ); - if( !$label.length ) { - $cell.prepend( $newHeader ); - } else { // only if changed - $label.replaceWith( $newHeader ); - } - }); }; Stack.prototype.destroy = function() { - this.$table.removeClass( classes.stackTable ); - this.$table.find( '.' + classes.cellLabels ).remove(); - this.$table.find( '.' + classes.cellContentLabels ).each(function() { - $( this ).replaceWith( this.childNodes ); + this.$table.removeClass(classes.stackTable); + this.$table.find("." + classes.cellLabels).remove(); + this.$table.find("." + classes.cellContentLabels).each(function() { + $(this).replaceWith(this.childNodes); }); }; // on tablecreate, init - $( document ).on( Tablesaw.events.create, function( e, tablesaw ){ - if( tablesaw.mode === 'stack' ){ - var table = new Stack( tablesaw.table, tablesaw ); - table.init(); - } - }).on( Tablesaw.events.refresh, function( e, tablesaw ){ - if( tablesaw.mode === 'stack' ){ - $( tablesaw.table ).data( data.key ).init(); - } - }).on( Tablesaw.events.destroy, function( e, tablesaw ){ - if( tablesaw.mode === 'stack' ){ - $( tablesaw.table ).data( data.key ).destroy(); - } - }); + $(document) + .on(Tablesaw.events.create, function(e, tablesaw) { + if (tablesaw.mode === "stack") { + var table = new Stack(tablesaw.table, tablesaw); + table.init(); + } + }) + .on(Tablesaw.events.refresh, function(e, tablesaw) { + if (tablesaw.mode === "stack") { + $(tablesaw.table).data(data.key).init(); + } + }) + .on(Tablesaw.events.destroy, function(e, tablesaw) { + if (tablesaw.mode === "stack") { + $(tablesaw.table).data(data.key).destroy(); + } + }); +})(); -}()); })); diff --git a/dist/stackonly/tablesaw.stackonly.scss b/dist/stackonly/tablesaw.stackonly.scss index 019a567..facd1f4 100644 --- a/dist/stackonly/tablesaw.stackonly.scss +++ b/dist/stackonly/tablesaw.stackonly.scss @@ -1,7 +1,7 @@ -/*! Tablesaw - v3.0.1 - 2017-06-01 +/*! Tablesaw - v3.0.2 - 2017-07-07 * https://github.com/filamentgroup/tablesaw * Copyright (c) 2017 Filament Group; Licensed MIT */ -/*! Tablesaw - v3.0.1 - 2017-06-01 +/*! Tablesaw - v3.0.2 - 2017-07-07 * https://github.com/filamentgroup/tablesaw * Copyright (c) 2017 Filament Group; Licensed MIT */ diff --git a/dist/tablesaw-init.js b/dist/tablesaw-init.js index 1dab969..b466826 100644 --- a/dist/tablesaw-init.js +++ b/dist/tablesaw-init.js @@ -1,25 +1,23 @@ -/*! Tablesaw - v3.0.1 - 2017-06-01 +/*! Tablesaw - v3.0.2 - 2017-07-07 * https://github.com/filamentgroup/tablesaw * Copyright (c) 2017 Filament Group; Licensed MIT */ -(function( win ) { +(function(win) { "use strict"; - var $; - if( 'shoestring' in win ) { + if ("shoestring" in win) { $ = win.shoestring; - } else if( 'jQuery' in win ) { + } else if ("jQuery" in win) { $ = win.jQuery; } else { - throw new Error( "tablesaw: DOM library not found." ); + throw new Error("tablesaw: DOM library not found."); } // DOM-ready auto-init of plugins. // Many plugins bind to an "enhance" event to init themselves on dom ready, or when new markup is inserted into the DOM // Use raw DOMContentLoaded instead of shoestring (may have issues in Android 2.3, exhibited by stack table) - if( "addEventListener" in document ) { - document.addEventListener( "DOMContentLoaded", function() { - $( document ).trigger( "enhance.tablesaw" ); + if ("addEventListener" in document) { + document.addEventListener("DOMContentLoaded", function() { + $(document).trigger("enhance.tablesaw"); }); } - -})( typeof window !== "undefined" ? window : this ); \ No newline at end of file +})(typeof window !== "undefined" ? window : this); diff --git a/dist/tablesaw.css b/dist/tablesaw.css index 96e98cd..82f506e 100644 --- a/dist/tablesaw.css +++ b/dist/tablesaw.css @@ -1,4 +1,4 @@ -/*! Tablesaw - v3.0.1 - 2017-06-01 +/*! Tablesaw - v3.0.2 - 2017-07-07 * https://github.com/filamentgroup/tablesaw * Copyright (c) 2017 Filament Group; Licensed MIT */ @@ -289,21 +289,21 @@ a.tablesaw-btn { clear: both; } -.tablesaw-bar-section .a11y-sm { - clip: rect(0 0 0 0); - height: 1px; - overflow: hidden; - position: absolute; - width: 1px; +.tablesaw-bar-section .abbreviated { + display: inline; +} + +.tablesaw-bar-section .longform { + display: none; } @media (min-width: 24em) { - .tablesaw-bar-section .a11y-sm { - clip: none; - height: auto; - width: auto; - position: static; - overflow: visible; + .tablesaw-bar-section .abbreviated { + display: none; + } + + .tablesaw-bar-section .longform { + display: inline; } } @@ -314,6 +314,11 @@ a.tablesaw-btn { vertical-align: middle; } +.tablesaw-sortable-btn { + /* same as cell padding above */ + padding: .5em .7em; +} + .tablesaw thead th { text-align: left; } @@ -685,8 +690,6 @@ a.tablesaw-btn { color: inherit; background: transparent; border: 0; - /* same as cell padding in tables.css */ - padding: .5em .7em; text-align: inherit; font: inherit; text-transform: inherit; diff --git a/dist/tablesaw.jquery.js b/dist/tablesaw.jquery.js index 1cd9306..7cef1db 100644 --- a/dist/tablesaw.jquery.js +++ b/dist/tablesaw.jquery.js @@ -1,4 +1,4 @@ -/*! Tablesaw - v3.0.1 - 2017-06-01 +/*! Tablesaw - v3.0.2 - 2017-07-07 * https://github.com/filamentgroup/tablesaw * Copyright (c) 2017 Filament Group; Licensed MIT */ // UMD module definition @@ -36,20 +36,39 @@ var Tablesaw = { i18n: { - modes: [ 'Stack', 'Swipe', 'Toggle' ], - columns: 'Columns', - columnBtnText: 'Columns', - columnsDialogError: 'No eligible columns.', - sort: 'Sort' + modeStack: "Stack", + modeSwipe: "Swipe", + modeToggle: "Toggle", + modeSwitchColumnsAbbreviated: "Cols", + modeSwitchColumns: "Columns", + columnToggleButton: "Columns", + columnToggleError: "No eligible columns.", + sort: "Sort", + swipePreviousColumn: "Previous column", + swipeNextColumn: "Next column" }, // cut the mustard - mustard: ( 'head' in document ) && // IE9+, Firefox 4+, Safari 5.1+, Mobile Safari 4.1+, Opera 11.5+, Android 2.3+ - ( !window.blackberry || window.WebKitPoint ) && // only WebKit Blackberry (OS 6+) + mustard: + "head" in document && // IE9+, Firefox 4+, Safari 5.1+, Mobile Safari 4.1+, Opera 11.5+, Android 2.3+ + (!window.blackberry || window.WebKitPoint) && // only WebKit Blackberry (OS 6+) !window.operamini }; -if( Tablesaw.mustard ) { - $( document.documentElement ).addClass( 'tablesaw-enhanced' ); +$(win.document).on("enhance.tablesaw", function() { + // Extend i18n config, if one exists. + if (typeof TablesawConfig !== "undefined" && TablesawConfig.i18n) { + Tablesaw.i18n = $.extend(Tablesaw.i18n, TablesawConfig.i18n || {}); + } + + Tablesaw.i18n.modes = [ + Tablesaw.i18n.modeStack, + Tablesaw.i18n.modeSwipe, + Tablesaw.i18n.modeToggle + ]; +}); + +if (Tablesaw.mustard) { + $(document.documentElement).addClass("tablesaw-enhanced"); } (function() { @@ -70,21 +89,21 @@ if( Tablesaw.mustard ) { Tablesaw.events = events; - var Table = function( element ) { - if( !element ) { - throw new Error( "Tablesaw requires an element." ); + var Table = function(element) { + if (!element) { + throw new Error("Tablesaw requires an element."); } this.table = element; - this.$table = $( element ); + this.$table = $(element); // only one and are allowed, per the specification - this.$thead = this.$table.children().filter( "thead" ).eq( 0 ); + this.$thead = this.$table.children().filter("thead").eq(0); // multiple are allowed, per the specification - this.$tbody = this.$table.children().filter( "tbody" ); + this.$tbody = this.$table.children().filter("tbody"); - this.mode = this.$table.attr( "data-tablesaw-mode" ) || defaultMode; + this.mode = this.$table.attr("data-tablesaw-mode") || defaultMode; this.$toolbar = null; @@ -93,36 +112,36 @@ if( Tablesaw.mustard ) { Table.prototype.init = function() { // assign an id if there is none - if ( !this.$table.attr( "id" ) ) { - this.$table.attr( "id", pluginName + "-" + Math.round( Math.random() * 10000 ) ); + if (!this.$table.attr("id")) { + this.$table.attr("id", pluginName + "-" + Math.round(Math.random() * 10000)); } this.createToolbar(); this._initCells(); - this.$table.data( pluginName, this ); + this.$table.data(pluginName, this); - this.$table.trigger( events.create, [ this ] ); + this.$table.trigger(events.create, [this]); }; - Table.prototype.getConfig = function( pluginSpecificConfig ) { + Table.prototype.getConfig = function(pluginSpecificConfig) { // shoestring extend doesn’t support arbitrary args - var configs = $.extend( defaultConfig, pluginSpecificConfig || {} ); - return $.extend( configs, typeof TablesawConfig !== "undefined" ? TablesawConfig : {} ); + var configs = $.extend(defaultConfig, pluginSpecificConfig || {}); + return $.extend(configs, typeof TablesawConfig !== "undefined" ? TablesawConfig : {}); }; Table.prototype._getPrimaryHeaderRow = function() { - return this._getHeaderRows().eq( 0 ); + return this._getHeaderRows().eq(0); }; Table.prototype._getHeaderRows = function() { - return this.$thead.children().filter( "tr" ).filter(function() { - return !$( this ).is( "[data-tablesaw-ignorerow]" ); + return this.$thead.children().filter("tr").filter(function() { + return !$(this).is("[data-tablesaw-ignorerow]"); }); }; - Table.prototype._getRowIndex = function( $row ) { + Table.prototype._getRowIndex = function($row) { return $row.prevAll().length; }; @@ -130,28 +149,28 @@ if( Tablesaw.mustard ) { var self = this; var indeces = []; this._getHeaderRows().each(function() { - indeces.push( self._getRowIndex( $( this ) ) ); + indeces.push(self._getRowIndex($(this))); }); return indeces; }; - Table.prototype._getPrimaryHeaderCells = function( $row ) { - return ( $row || this._getPrimaryHeaderRow() ).find( "th" ); + Table.prototype._getPrimaryHeaderCells = function($row) { + return ($row || this._getPrimaryHeaderRow()).find("th"); }; - Table.prototype._findPrimaryHeadersForCell = function( cell ) { + Table.prototype._findPrimaryHeadersForCell = function(cell) { var $headerRow = this._getPrimaryHeaderRow(); - var $headers = this._getPrimaryHeaderCells( $headerRow ); - var headerRowIndex = this._getRowIndex( $headerRow ); + var $headers = this._getPrimaryHeaderCells($headerRow); + var headerRowIndex = this._getRowIndex($headerRow); var results = []; - for( var rowNumber = 0; rowNumber < this.headerMapping.length; rowNumber++ ) { - if( rowNumber === headerRowIndex ) { + for (var rowNumber = 0; rowNumber < this.headerMapping.length; rowNumber++) { + if (rowNumber === headerRowIndex) { continue; } - for( var colNumber = 0; colNumber < this.headerMapping[ rowNumber ].length; colNumber++ ) { - if( this.headerMapping[ rowNumber ][ colNumber ] === cell ) { - results.push( $headers[ colNumber ] ); + for (var colNumber = 0; colNumber < this.headerMapping[rowNumber].length; colNumber++) { + if (this.headerMapping[rowNumber][colNumber] === cell) { + results.push($headers[colNumber]); } } } @@ -161,20 +180,20 @@ if( Tablesaw.mustard ) { // used by init cells Table.prototype.getRows = function() { var self = this; - return this.$table.find( "tr" ).filter(function() { - return $( this ).closest( "table" ).is( self.$table ); + return this.$table.find("tr").filter(function() { + return $(this).closest("table").is(self.$table); }); }; // used by sortable - Table.prototype.getBodyRows = function( tbody ) { - return ( tbody ? $( tbody ) : this.$tbody ).children().filter( "tr" ); + Table.prototype.getBodyRows = function(tbody) { + return (tbody ? $(tbody) : this.$tbody).children().filter("tr"); }; - Table.prototype.getHeaderCellIndex = function( cell ) { - var lookup = this.headerMapping[ 0 ]; - for( var colIndex = 0; colIndex < lookup.length; colIndex++ ) { - if( lookup[ colIndex ] === cell ) { + Table.prototype.getHeaderCellIndex = function(cell) { + var lookup = this.headerMapping[0]; + for (var colIndex = 0; colIndex < lookup.length; colIndex++) { + if (lookup[colIndex] === cell) { return colIndex; } } @@ -186,36 +205,36 @@ if( Tablesaw.mustard ) { var $rows = this.getRows(); var columnLookup = []; - $rows.each(function( rowNumber ) { - columnLookup[ rowNumber ] = []; + $rows.each(function(rowNumber) { + columnLookup[rowNumber] = []; }); - $rows.each(function( rowNumber ) { + $rows.each(function(rowNumber) { var coltally = 0; - var $t = $( this ); + var $t = $(this); var children = $t.children(); children.each(function() { - var colspan = parseInt( this.getAttribute( "colspan" ), 10 ); - var rowspan = parseInt( this.getAttribute( "rowspan" ), 10 ); + var colspan = parseInt(this.getAttribute("colspan"), 10); + var rowspan = parseInt(this.getAttribute("rowspan"), 10); // set in a previous rowspan - while( columnLookup[ rowNumber ][ coltally ] ) { + while (columnLookup[rowNumber][coltally]) { coltally++; } - columnLookup[ rowNumber ][ coltally ] = this; + columnLookup[rowNumber][coltally] = this; // TODO? both colspan and rowspan - if( colspan ) { - for( var k = 0; k < colspan - 1; k++ ){ + if (colspan) { + for (var k = 0; k < colspan - 1; k++) { coltally++; - columnLookup[ rowNumber ][ coltally ] = this; + columnLookup[rowNumber][coltally] = this; } } - if( rowspan ) { - for( var j = 1; j < rowspan; j++ ){ - columnLookup[ rowNumber + j ][ coltally ] = this; + if (rowspan) { + for (var j = 1; j < rowspan; j++) { + columnLookup[rowNumber + j][coltally] = this; } } @@ -224,22 +243,22 @@ if( Tablesaw.mustard ) { }); var headerRowIndeces = this._getHeaderRowIndeces(); - for( var colNumber = 0; colNumber < columnLookup[ 0 ].length; colNumber++ ) { - for( var headerIndex = 0, k = headerRowIndeces.length; headerIndex < k; headerIndex++ ) { - var headerCol = columnLookup[ headerRowIndeces[ headerIndex ] ][ colNumber ]; + for (var colNumber = 0; colNumber < columnLookup[0].length; colNumber++) { + for (var headerIndex = 0, k = headerRowIndeces.length; headerIndex < k; headerIndex++) { + var headerCol = columnLookup[headerRowIndeces[headerIndex]][colNumber]; - var rowNumber = headerRowIndeces[ headerIndex ]; + var rowNumber = headerRowIndeces[headerIndex]; var rowCell; - if( !headerCol.cells ) { + if (!headerCol.cells) { headerCol.cells = []; } - while( rowNumber < columnLookup.length ) { - rowCell = columnLookup[ rowNumber ][ colNumber ]; + while (rowNumber < columnLookup.length) { + rowCell = columnLookup[rowNumber][colNumber]; - if( headerCol !== rowCell ) { - headerCol.cells.push( rowCell ); + if (headerCol !== rowCell) { + headerCol.cells.push(rowCell); } rowNumber++; @@ -253,38 +272,36 @@ if( Tablesaw.mustard ) { Table.prototype.refresh = function() { this._initCells(); - this.$table.trigger( events.refresh, [ this ] ); + this.$table.trigger(events.refresh, [this]); }; Table.prototype._getToolbarAnchor = function() { var $parent = this.$table.parent(); - if( $parent.is( ".tablesaw-overflow" ) ) { + if ($parent.is(".tablesaw-overflow")) { return $parent; } return this.$table; }; - Table.prototype._getToolbar = function( $anchor ) { - if( !$anchor ) { + Table.prototype._getToolbar = function($anchor) { + if (!$anchor) { $anchor = this._getToolbarAnchor(); } - return $anchor.prev().filter( "." + classes.toolbar ); + return $anchor.prev().filter("." + classes.toolbar); }; Table.prototype.createToolbar = function() { // Insert the toolbar // TODO move this into a separate component var $anchor = this._getToolbarAnchor(); - var $toolbar = this._getToolbar( $anchor ); - if( !$toolbar.length ) { - $toolbar = $( '
' ) - .addClass( classes.toolbar ) - .insertBefore( $anchor ); + var $toolbar = this._getToolbar($anchor); + if (!$toolbar.length) { + $toolbar = $("
").addClass(classes.toolbar).insertBefore($anchor); } this.$toolbar = $toolbar; - if( this.mode ) { - this.$toolbar.addClass( 'tablesaw-mode-' + this.mode ); + if (this.mode) { + this.$toolbar.addClass("tablesaw-mode-" + this.mode); } }; @@ -292,313 +309,320 @@ if( Tablesaw.mustard ) { // Don’t remove the toolbar, just erase the classes on it. // Some of the table features are not yet destroy-friendly. this._getToolbar().each(function() { - this.className = this.className.replace( /\btablesaw-mode\-\w*\b/gi, '' ); + this.className = this.className.replace(/\btablesaw-mode\-\w*\b/gi, ""); }); - var tableId = this.$table.attr( 'id' ); - $( document ).off( "." + tableId ); - $( window ).off( "." + tableId ); + var tableId = this.$table.attr("id"); + $(document).off("." + tableId); + $(window).off("." + tableId); // other plugins - this.$table.trigger( events.destroy, [ this ] ); + this.$table.trigger(events.destroy, [this]); - this.$table.removeData( pluginName ); + this.$table.removeData(pluginName); }; // Collection method. - $.fn[ pluginName ] = function() { - return this.each( function() { - var $t = $( this ); + $.fn[pluginName] = function() { + return this.each(function() { + var $t = $(this); - if( $t.data( pluginName ) ){ + if ($t.data(pluginName)) { return; } - new Table( this ); + new Table(this); }); }; - var $doc = $( win.document ); - $doc.on( "enhance.tablesaw", function( e ) { + var $doc = $(win.document); + $doc.on("enhance.tablesaw", function(e) { // Cut the mustard - if( Tablesaw.mustard ) { - $( e.target ).find( initSelector ).filter( initFilterSelector )[ pluginName ](); + if (Tablesaw.mustard) { + $(e.target).find(initSelector).filter(initFilterSelector)[pluginName](); } }); // Avoid a resize during scroll: // Some Mobile devices trigger a resize during scroll (sometimes when - // doing elastic stretch at the end of the document or from the + // doing elastic stretch at the end of the document or from the // location bar hide) var isScrolling = false; var scrollTimeout; - $doc.on( "scroll.tablesaw", function() { + $doc.on("scroll.tablesaw", function() { isScrolling = true; - win.clearTimeout( scrollTimeout ); + win.clearTimeout(scrollTimeout); scrollTimeout = win.setTimeout(function() { isScrolling = false; - }, 300 ); // must be greater than the resize timeout below + }, 300); // must be greater than the resize timeout below }); var resizeTimeout; - $( win ).on( "resize", function() { - if( !isScrolling ) { - win.clearTimeout( resizeTimeout ); + $(win).on("resize", function() { + if (!isScrolling) { + win.clearTimeout(resizeTimeout); resizeTimeout = win.setTimeout(function() { - $doc.trigger( events.resize ); - }, 150 ); // must be less than the scrolling timeout above. + $doc.trigger(events.resize); + }, 150); // must be less than the scrolling timeout above. } }); +})(); -}()); - -(function(){ - +(function() { var classes = { - stackTable: 'tablesaw-stack', - cellLabels: 'tablesaw-cell-label', - cellContentLabels: 'tablesaw-cell-content' + stackTable: "tablesaw-stack", + cellLabels: "tablesaw-cell-label", + cellContentLabels: "tablesaw-cell-content" }; var data = { - key: 'tablesaw-stack' + key: "tablesaw-stack" }; var attrs = { - labelless: 'data-tablesaw-no-labels', - hideempty: 'data-tablesaw-hide-empty' + labelless: "data-tablesaw-no-labels", + hideempty: "data-tablesaw-hide-empty" }; - var Stack = function( element, tablesaw ) { - + var Stack = function(element, tablesaw) { this.tablesaw = tablesaw; - this.$table = $( element ); + this.$table = $(element); - this.labelless = this.$table.is( '[' + attrs.labelless + ']' ); - this.hideempty = this.$table.is( '[' + attrs.hideempty + ']' ); + this.labelless = this.$table.is("[" + attrs.labelless + "]"); + this.hideempty = this.$table.is("[" + attrs.hideempty + "]"); - this.$table.data( data.key, this ); + this.$table.data(data.key, this); }; Stack.prototype.init = function() { - this.$table.addClass( classes.stackTable ); + this.$table.addClass(classes.stackTable); - if( this.labelless ) { + if (this.labelless) { return; } var self = this; - this.$table.find( "th, td" ).filter(function() { - return !$( this ).closest( "thead" ).length; - }).filter(function() { - return !$( this ).closest( "tr" ).is( "[" + attrs.labelless + "]" ) && - ( !self.hideempty || !!$( this ).html() ); - }).each(function() { - var $newHeader = $( document.createElement( "b" ) ).addClass( classes.cellLabels ); - var $cell = $( this ); - - $( self.tablesaw._findPrimaryHeadersForCell( this ) ).each(function( index ) { - var $header = $( this.cloneNode( true ) ); - // TODO decouple from sortable better - // Changed from .text() in https://github.com/filamentgroup/tablesaw/commit/b9c12a8f893ec192830ec3ba2d75f062642f935b - // to preserve structural html in headers, like - var $sortableButton = $header.find( ".tablesaw-sortable-btn" ); - $header.find( ".tablesaw-sortable-arrow" ).remove(); - - // TODO decouple from checkall better - var $checkall = $header.find( "[data-tablesaw-checkall]" ); - $checkall.closest( "label" ).remove(); - if( $checkall.length ) { - $newHeader = $([]); - return; + this.$table + .find("th, td") + .filter(function() { + return !$(this).closest("thead").length; + }) + .filter(function() { + return ( + !$(this).closest("tr").is("[" + attrs.labelless + "]") && + (!self.hideempty || !!$(this).html()) + ); + }) + .each(function() { + var $newHeader = $(document.createElement("b")).addClass(classes.cellLabels); + var $cell = $(this); + + $(self.tablesaw._findPrimaryHeadersForCell(this)).each(function(index) { + var $header = $(this.cloneNode(true)); + // TODO decouple from sortable better + // Changed from .text() in https://github.com/filamentgroup/tablesaw/commit/b9c12a8f893ec192830ec3ba2d75f062642f935b + // to preserve structural html in headers, like + var $sortableButton = $header.find(".tablesaw-sortable-btn"); + $header.find(".tablesaw-sortable-arrow").remove(); + + // TODO decouple from checkall better + var $checkall = $header.find("[data-tablesaw-checkall]"); + $checkall.closest("label").remove(); + if ($checkall.length) { + $newHeader = $([]); + return; + } + + if (index > 0) { + $newHeader.append(document.createTextNode(", ")); + } + $newHeader.append( + $sortableButton.length ? $sortableButton[0].childNodes : $header[0].childNodes + ); + }); + + if ($newHeader.length && !$cell.find("." + classes.cellContentLabels).length) { + $cell.wrapInner(""); } - if( index > 0 ) { - $newHeader.append( document.createTextNode( ", " ) ); + // Update if already exists. + var $label = $cell.find("." + classes.cellLabels); + if (!$label.length) { + $cell.prepend($newHeader); + } else { + // only if changed + $label.replaceWith($newHeader); } - $newHeader.append( $sortableButton.length ? $sortableButton[ 0 ].childNodes : $header[ 0 ].childNodes ); }); - - if( $newHeader.length && !$cell.find( "." + classes.cellContentLabels ).length ) { - $cell.wrapInner( "" ); - } - - // Update if already exists. - var $label = $cell.find( "." + classes.cellLabels ); - if( !$label.length ) { - $cell.prepend( $newHeader ); - } else { // only if changed - $label.replaceWith( $newHeader ); - } - }); }; Stack.prototype.destroy = function() { - this.$table.removeClass( classes.stackTable ); - this.$table.find( '.' + classes.cellLabels ).remove(); - this.$table.find( '.' + classes.cellContentLabels ).each(function() { - $( this ).replaceWith( this.childNodes ); + this.$table.removeClass(classes.stackTable); + this.$table.find("." + classes.cellLabels).remove(); + this.$table.find("." + classes.cellContentLabels).each(function() { + $(this).replaceWith(this.childNodes); }); }; // on tablecreate, init - $( document ).on( Tablesaw.events.create, function( e, tablesaw ){ - if( tablesaw.mode === 'stack' ){ - var table = new Stack( tablesaw.table, tablesaw ); - table.init(); - } - }).on( Tablesaw.events.refresh, function( e, tablesaw ){ - if( tablesaw.mode === 'stack' ){ - $( tablesaw.table ).data( data.key ).init(); - } - }).on( Tablesaw.events.destroy, function( e, tablesaw ){ - if( tablesaw.mode === 'stack' ){ - $( tablesaw.table ).data( data.key ).destroy(); - } - }); + $(document) + .on(Tablesaw.events.create, function(e, tablesaw) { + if (tablesaw.mode === "stack") { + var table = new Stack(tablesaw.table, tablesaw); + table.init(); + } + }) + .on(Tablesaw.events.refresh, function(e, tablesaw) { + if (tablesaw.mode === "stack") { + $(tablesaw.table).data(data.key).init(); + } + }) + .on(Tablesaw.events.destroy, function(e, tablesaw) { + if (tablesaw.mode === "stack") { + $(tablesaw.table).data(data.key).destroy(); + } + }); +})(); -}()); (function() { var pluginName = "tablesawbtn", methods = { - _create: function(){ - return $( this ).each(function() { - $( this ) - .trigger( "beforecreate." + pluginName )[ pluginName ]( "_init" ) - .trigger( "create." + pluginName ); + _create: function() { + return $(this).each(function() { + $(this) + .trigger("beforecreate." + pluginName) + [pluginName]("_init") + .trigger("create." + pluginName); }); }, - _init: function(){ - var oEl = $( this ), - sel = this.getElementsByTagName( "select" )[ 0 ]; + _init: function() { + var oEl = $(this), + sel = this.getElementsByTagName("select")[0]; - if( sel ) { + if (sel) { // TODO next major version: remove .btn-select - $( this ) - .addClass( "btn-select tablesaw-btn-select" )[ pluginName ]( "_select", sel ); + $(this).addClass("btn-select tablesaw-btn-select")[pluginName]("_select", sel); } return oEl; }, - _select: function( sel ) { - var update = function( oEl, sel ) { - var opts = $( sel ).find( "option" ); - var label = document.createElement( "span" ); + _select: function(sel) { + var update = function(oEl, sel) { + var opts = $(sel).find("option"); + var label = document.createElement("span"); var el; var children; var found = false; - label.setAttribute( "aria-hidden", "true" ); + label.setAttribute("aria-hidden", "true"); label.innerHTML = " "; opts.each(function() { var opt = this; - if( opt.selected ) { + if (opt.selected) { label.innerHTML = opt.text; } }); children = oEl.childNodes; - if( opts.length > 0 ){ - for( var i = 0, l = children.length; i < l; i++ ) { - el = children[ i ]; + if (opts.length > 0) { + for (var i = 0, l = children.length; i < l; i++) { + el = children[i]; - if( el && el.nodeName.toUpperCase() === "SPAN" ) { - oEl.replaceChild( label, el ); + if (el && el.nodeName.toUpperCase() === "SPAN") { + oEl.replaceChild(label, el); found = true; } } - if( !found ) { - oEl.insertBefore( label, oEl.firstChild ); + if (!found) { + oEl.insertBefore(label, oEl.firstChild); } } }; - update( this, sel ); - $( this ).on( "change refresh", function() { - update( this, sel ); + update(this, sel); + $(this).on("change refresh", function() { + update(this, sel); }); } }; // Collection method. - $.fn[ pluginName ] = function( arrg, a, b, c ) { + $.fn[pluginName] = function(arrg, a, b, c) { return this.each(function() { - // if it's a method - if( arrg && typeof( arrg ) === "string" ){ - return $.fn[ pluginName ].prototype[ arrg ].call( this, a, b, c ); + if (arrg && typeof arrg === "string") { + return $.fn[pluginName].prototype[arrg].call(this, a, b, c); } // don't re-init - if( $( this ).data( pluginName + "active" ) ){ - return $( this ); + if ($(this).data(pluginName + "active")) { + return $(this); } - $( this ).data( pluginName + "active", true ); + $(this).data(pluginName + "active", true); - $.fn[ pluginName ].prototype._create.call( this ); + $.fn[pluginName].prototype._create.call(this); }); }; // add methods - $.extend( $.fn[ pluginName ].prototype, methods ); - -}()); -(function(){ + $.extend($.fn[pluginName].prototype, methods); +})(); +(function() { var data = { key: "tablesaw-coltoggle" }; - var ColumnToggle = function( element ) { - - this.$table = $( element ); + var ColumnToggle = function(element) { + this.$table = $(element); - if( !this.$table.length ) { + if (!this.$table.length) { return; } - this.tablesaw = this.$table.data( "tablesaw" ); + this.tablesaw = this.$table.data("tablesaw"); this.attributes = { subrow: "data-tablesaw-subrow", ignorerow: "data-tablesaw-ignorerow", - btnTarget: 'data-tablesaw-columntoggle-btn-target', - set: 'data-tablesaw-columntoggle-set' + btnTarget: "data-tablesaw-columntoggle-btn-target", + set: "data-tablesaw-columntoggle-set" }; this.classes = { - columnToggleTable: 'tablesaw-columntoggle', - columnBtnContain: 'tablesaw-columntoggle-btnwrap tablesaw-advance', - columnBtn: 'tablesaw-columntoggle-btn tablesaw-nav-btn down', - popup: 'tablesaw-columntoggle-popup', - priorityPrefix: 'tablesaw-priority-' + columnToggleTable: "tablesaw-columntoggle", + columnBtnContain: "tablesaw-columntoggle-btnwrap tablesaw-advance", + columnBtn: "tablesaw-columntoggle-btn tablesaw-nav-btn down", + popup: "tablesaw-columntoggle-popup", + priorityPrefix: "tablesaw-priority-" }; this.set = []; this.$headers = this.tablesaw._getPrimaryHeaderCells(); - this.$table.data( data.key, this ); + this.$table.data(data.key, this); }; // Column Toggle Sets (one column chooser can control multiple tables) ColumnToggle.prototype.initSet = function() { - var set = this.$table.attr( this.attributes.set ); - if( set ) { + var set = this.$table.attr(this.attributes.set); + if (set) { // Should not include the current table - var table = this.$table[ 0 ]; - this.set = $( "table[" + this.attributes.set + "='" + set + "']" ).filter(function() { - return this !== table; - }).get(); + var table = this.$table[0]; + this.set = $("table[" + this.attributes.set + "='" + set + "']") + .filter(function() { + return this !== table; + }) + .get(); } }; ColumnToggle.prototype.init = function() { - - if( !this.$table.length ) { + if (!this.$table.length) { return; } @@ -611,146 +635,154 @@ if( Tablesaw.mustard ) { self = this; var cfg = this.tablesaw.getConfig({ - getColumnToggleLabelTemplate: function( text ) { + getColumnToggleLabelTemplate: function(text) { return ""; } }); - this.$table.addClass( this.classes.columnToggleTable ); + this.$table.addClass(this.classes.columnToggleTable); - tableId = this.$table.attr( "id" ); + tableId = this.$table.attr("id"); id = tableId + "-popup"; - $btnContain = $( "
" ); + $btnContain = $("
"); // TODO next major version: remove .btn - $menuButton = $( "
" + - "" + Tablesaw.i18n.columnBtnText + "" ); - $popup = $( "
" ); - $menu = $( "
" ); + $menuButton = $( + "" + + "" + + Tablesaw.i18n.columnToggleButton + + "" + ); + $popup = $("
"); + $menu = $("
"); this.$popup = $popup; var hasNonPersistentHeaders = false; - this.$headers.each( function() { - var $this = $( this ), + this.$headers.each(function() { + var $this = $(this), priority = $this.attr("data-tablesaw-priority"), - $cells = self.$getCells( this ); + $cells = self.$getCells(this); - if( priority && priority !== "persist" ) { - $cells.addClass( self.classes.priorityPrefix + priority ); + if (priority && priority !== "persist") { + $cells.addClass(self.classes.priorityPrefix + priority); - $( cfg.getColumnToggleLabelTemplate( $this.text() ) ) - .appendTo( $menu ) - .find( "input[type=\"checkbox\"]" ) - .data( "tablesaw-header", this ); + $(cfg.getColumnToggleLabelTemplate($this.text())) + .appendTo($menu) + .find('input[type="checkbox"]') + .data("tablesaw-header", this); hasNonPersistentHeaders = true; } }); - if( !hasNonPersistentHeaders ) { - $menu.append( '' ); + if (!hasNonPersistentHeaders) { + $menu.append(""); } - $menu.appendTo( $popup ); + $menu.appendTo($popup); - function onToggleCheckboxChange( checkbox ) { + function onToggleCheckboxChange(checkbox) { var checked = checkbox.checked; - var header = self.getHeaderFromCheckbox( checkbox ); - var $cells = self.$getCells( header ); + var header = self.getHeaderFromCheckbox(checkbox); + var $cells = self.$getCells(header); - $cells[ !checked ? "addClass" : "removeClass" ]( "tablesaw-cell-hidden" ); - $cells[ checked ? "addClass" : "removeClass" ]( "tablesaw-cell-visible" ); + $cells[!checked ? "addClass" : "removeClass"]("tablesaw-cell-hidden"); + $cells[checked ? "addClass" : "removeClass"]("tablesaw-cell-visible"); - self.updateColspanIgnoredRows( checked, $( header ).add( header.cells ) ); + self.updateColspanIgnoredRows(checked, $(header).add(header.cells)); - self.$table.trigger( 'tablesawcolumns' ); + self.$table.trigger("tablesawcolumns"); } // bind change event listeners to inputs - TODO: move to a private method? - $menu.find( 'input[type="checkbox"]' ).on( "change", function(e) { - onToggleCheckboxChange( e.target ); + $menu.find('input[type="checkbox"]').on("change", function(e) { + onToggleCheckboxChange(e.target); - if( self.set.length ) { + if (self.set.length) { var index; - $( self.$popup ).find( "input[type='checkbox']" ).each(function( j ) { - if( this === e.target ) { + $(self.$popup).find("input[type='checkbox']").each(function(j) { + if (this === e.target) { index = j; return false; } }); - $( self.set ).each(function() { - var checkbox = $( this ).data( data.key ).$popup.find( "input[type='checkbox']" ).get( index ); - if( checkbox ) { + $(self.set).each(function() { + var checkbox = $(this).data(data.key).$popup.find("input[type='checkbox']").get(index); + if (checkbox) { checkbox.checked = e.target.checked; - onToggleCheckboxChange( checkbox ); + onToggleCheckboxChange(checkbox); } }); } }); - $menuButton.appendTo( $btnContain ); + $menuButton.appendTo($btnContain); // Use a different target than the toolbar - var $btnTarget = $( this.$table.attr( this.attributes.btnTarget ) ); - $btnContain.appendTo( $btnTarget.length ? $btnTarget : this.tablesaw.$toolbar ); + var $btnTarget = $(this.$table.attr(this.attributes.btnTarget)); + $btnContain.appendTo($btnTarget.length ? $btnTarget : this.tablesaw.$toolbar); - function closePopup( event ) { + function closePopup(event) { // Click came from inside the popup, ignore. - if( event && $( event.target ).closest( "." + self.classes.popup ).length ) { + if (event && $(event.target).closest("." + self.classes.popup).length) { return; } - $( document ).off( 'click.' + tableId ); - $menuButton.removeClass( 'up' ).addClass( 'down' ); - $btnContain.removeClass( 'visible' ); + $(document).off("click." + tableId); + $menuButton.removeClass("up").addClass("down"); + $btnContain.removeClass("visible"); } var closeTimeout; function openPopup() { - $btnContain.addClass( 'visible' ); - $menuButton.removeClass( 'down' ).addClass( 'up' ); + $btnContain.addClass("visible"); + $menuButton.removeClass("down").addClass("up"); - $( document ).off( 'click.' + tableId, closePopup ); + $(document).off("click." + tableId, closePopup); - window.clearTimeout( closeTimeout ); + window.clearTimeout(closeTimeout); closeTimeout = window.setTimeout(function() { - $( document ).on( 'click.' + tableId, closePopup ); - }, 15 ); + $(document).on("click." + tableId, closePopup); + }, 15); } - $menuButton.on( "click.tablesaw", function( event ) { + $menuButton.on("click.tablesaw", function(event) { event.preventDefault(); - if( !$btnContain.is( ".visible" ) ) { + if (!$btnContain.is(".visible")) { openPopup(); } else { closePopup(); } }); - $popup.appendTo( $btnContain ); + $popup.appendTo($btnContain); this.$menu = $menu; // Fix for iOS not rendering shadows correctly when using `-webkit-overflow-scrolling` - var $overflow = this.$table.closest( ".tablesaw-overflow" ); - if( $overflow.css( "-webkit-overflow-scrolling" ) ) { + var $overflow = this.$table.closest(".tablesaw-overflow"); + if ($overflow.css("-webkit-overflow-scrolling")) { var timeout; - $overflow.on( "scroll", function() { - var $div = $( this ); - window.clearTimeout( timeout ); + $overflow.on("scroll", function() { + var $div = $(this); + window.clearTimeout(timeout); timeout = window.setTimeout(function() { - $div.css( "-webkit-overflow-scrolling", "auto" ); + $div.css("-webkit-overflow-scrolling", "auto"); window.setTimeout(function() { - $div.css( "-webkit-overflow-scrolling", "touch" ); - }, 0 ); - }, 100 ); + $div.css("-webkit-overflow-scrolling", "touch"); + }, 0); + }, 100); }); } - $(window).on( Tablesaw.events.resize + "." + tableId, function(){ + $(window).on(Tablesaw.events.resize + "." + tableId, function() { self.refreshToggle(); }); @@ -758,121 +790,130 @@ if( Tablesaw.mustard ) { this.refreshToggle(); }; - ColumnToggle.prototype.updateColspanIgnoredRows = function( invisibleColumnCount, $cells ) { - this.$table.find( "[" + this.attributes.subrow + "],[" + this.attributes.ignorerow + "]" ).each(function() { - var $t = $( this ); - var $td = $t.find( "td[colspan]" ).eq( 0 ); - var excludedInvisibleColumns; - - var colspan; - var originalColspan; - var modifier; + ColumnToggle.prototype.updateColspanIgnoredRows = function(invisibleColumnCount, $cells) { + this.$table + .find("[" + this.attributes.subrow + "],[" + this.attributes.ignorerow + "]") + .each(function() { + var $t = $(this); + var $td = $t.find("td[colspan]").eq(0); + var excludedInvisibleColumns; + + var colspan; + var originalColspan; + var modifier; + + // increment or decrementing only (from a user triggered column show/hide) + if (invisibleColumnCount === true || invisibleColumnCount === false) { + // unless the column being hidden is not included in the colspan + modifier = $cells.filter(function() { + return this === $td[0]; + }).length + ? invisibleColumnCount ? 1 : -1 + : 0; + + colspan = parseInt($td.attr("colspan"), 10) + modifier; + } else { + // triggered from a resize or init + originalColspan = $td.data("original-colspan"); - // increment or decrementing only (from a user triggered column show/hide) - if( invisibleColumnCount === true || invisibleColumnCount === false ) { - // unless the column being hidden is not included in the colspan - modifier = $cells.filter(function() { - return this === $td[ 0 ]; - }).length ? ( invisibleColumnCount ? 1 : -1 ) : 0; + if (originalColspan) { + colspan = originalColspan; + } else { + colspan = parseInt($td.attr("colspan"), 10); + $td.data("original-colspan", colspan); + } - colspan = parseInt( $td.attr( "colspan" ), 10 ) + modifier; - } else { - // triggered from a resize or init - originalColspan = $td.data( "original-colspan" ); + excludedInvisibleColumns = $t.find("td").filter(function() { + return this !== $td[0] && $(this).css("display") === "none"; + }).length; - if( originalColspan ) { - colspan = originalColspan; - } else { - colspan = parseInt( $td.attr( "colspan" ), 10 ); - $td.data( "original-colspan", colspan ); + colspan -= invisibleColumnCount - excludedInvisibleColumns; } - excludedInvisibleColumns = $t.find( "td" ).filter(function() { - return this !== $td[ 0 ] && $( this ).css( "display" ) === "none"; - }).length; - - colspan -= ( invisibleColumnCount - excludedInvisibleColumns ); - } - - // TODO add a colstart param so that this more appropriately selects colspan elements based on the column being hidden. - $td.attr( "colspan", colspan ); - }); + // TODO add a colstart param so that this more appropriately selects colspan elements based on the column being hidden. + $td.attr("colspan", colspan); + }); }; - ColumnToggle.prototype.$getCells = function( th ) { + ColumnToggle.prototype.$getCells = function(th) { var self = this; - return $( th ).add( th.cells ).filter(function() { - var $t = $( this ); + return $(th).add(th.cells).filter(function() { + var $t = $(this); var $row = $t.parent(); - var hasColspan = $t.is( "[colspan]" ); + var hasColspan = $t.is("[colspan]"); // no subrows or ignored rows (keep cells in ignored rows that do not have a colspan) - return !$row.is( "[" + self.attributes.subrow + "]" ) && - ( !$row.is( "[" + self.attributes.ignorerow + "]" ) || !hasColspan ); + return ( + !$row.is("[" + self.attributes.subrow + "]") && + (!$row.is("[" + self.attributes.ignorerow + "]") || !hasColspan) + ); }); }; - ColumnToggle.prototype.getHeaderFromCheckbox = function( checkbox ) { - return $( checkbox ).data( "tablesaw-header" ); + ColumnToggle.prototype.getHeaderFromCheckbox = function(checkbox) { + return $(checkbox).data("tablesaw-header"); }; ColumnToggle.prototype.refreshToggle = function() { var self = this; var invisibleColumns = 0; - this.$menu.find( "input" ).each( function() { - var header = self.getHeaderFromCheckbox( this ); - var isVisible = self.$getCells( header ).eq( 0 ).css( "display" ) === "table-cell"; + this.$menu.find("input").each(function() { + var header = self.getHeaderFromCheckbox(this); + var isVisible = self.$getCells(header).eq(0).css("display") === "table-cell"; this.checked = isVisible; - if( !isVisible ) { + if (!isVisible) { invisibleColumns++; } }); - this.updateColspanIgnoredRows( invisibleColumns ); + this.updateColspanIgnoredRows(invisibleColumns); }; ColumnToggle.prototype.destroy = function() { - this.$table.removeClass( this.classes.columnToggleTable ); - this.$table.find( 'th, td' ).each(function() { - var $cell = $( this ); - $cell.removeClass( 'tablesaw-cell-hidden' ) - .removeClass( 'tablesaw-cell-visible' ); + this.$table.removeClass(this.classes.columnToggleTable); + this.$table.find("th, td").each(function() { + var $cell = $(this); + $cell.removeClass("tablesaw-cell-hidden").removeClass("tablesaw-cell-visible"); - this.className = this.className.replace( /\bui\-table\-priority\-\d\b/g, '' ); + this.className = this.className.replace(/\bui\-table\-priority\-\d\b/g, ""); }); }; // on tablecreate, init - $( document ).on( Tablesaw.events.create, function( e, tablesaw ){ - - if( tablesaw.mode === 'columntoggle' ){ - var table = new ColumnToggle( tablesaw.table ); + $(document).on(Tablesaw.events.create, function(e, tablesaw) { + if (tablesaw.mode === "columntoggle") { + var table = new ColumnToggle(tablesaw.table); table.init(); } + }); - } ); + $(document).on(Tablesaw.events.destroy, function(e, tablesaw) { + if (tablesaw.mode === "columntoggle") { + $(tablesaw.table).data(data.key).destroy(); + } + }); - $( document ).on( Tablesaw.events.destroy, function( e, tablesaw ){ - if( tablesaw.mode === 'columntoggle' ){ - $( tablesaw.table ).data( data.key ).destroy(); + $(document).on(Tablesaw.events.refresh, function(e, tablesaw) { + if (tablesaw.mode === "columntoggle") { + $(tablesaw.table).data(data.key).refreshPriority(); } - } ); + }); +})(); -}()); -;(function() { - function getSortValue( cell ) { +(function() { + function getSortValue(cell) { var text = []; - $( cell.childNodes ).each(function() { - var $el = $( this ); - if( $el.is( 'input, select' ) ) { - text.push( $el.val() ); - } else if( $el.is( '.tablesaw-cell-label' ) ) { + $(cell.childNodes).each(function() { + var $el = $(this); + if ($el.is("input, select")) { + text.push($el.val()); + } else if ($el.is(".tablesaw-cell-label")) { } else { - text.push( ( $el.text() || '' ).replace(/^\s+|\s+$/g, '') ); + text.push(($el.text() || "").replace(/^\s+|\s+$/g, "")); } }); - return text.join( '' ); + return text.join(""); } var pluginName = "tablesaw-sortable", @@ -890,204 +931,230 @@ if( Tablesaw.mustard ) { ascend: pluginName + "-ascending", descend: pluginName + "-descending", switcher: pluginName + "-switch", - tableToolbar: 'tablesaw-bar-section', + tableToolbar: "tablesaw-bar-section", sortButton: pluginName + "-btn" }, methods = { - _create: function( o ){ - return $( this ).each(function() { - var init = $( this ).data( pluginName + "-init" ); - if( init ) { + _create: function(o) { + return $(this).each(function() { + var init = $(this).data(pluginName + "-init"); + if (init) { return false; } - $( this ) - .data( pluginName + "-init", true ) - .trigger( "beforecreate." + pluginName ) - [ pluginName ]( "_init" , o ) - .trigger( "create." + pluginName ); + $(this) + .data(pluginName + "-init", true) + .trigger("beforecreate." + pluginName) + [pluginName]("_init", o) + .trigger("create." + pluginName); }); }, - _init: function(){ - var el = $( this ); - var tblsaw = el.data( "tablesaw" ); + _init: function() { + var el = $(this); + var tblsaw = el.data("tablesaw"); var heads; var $switcher; - function addClassToHeads( h ){ - $.each( h , function( i , v ){ - $( v ).addClass( classes.head ); + function addClassToHeads(h) { + $.each(h, function(i, v) { + $(v).addClass(classes.head); }); } - function makeHeadsActionable( h , fn ){ - $.each( h , function( i , col ){ - var b = $( "