app/assets/javascripts/dataTables/extras/dataTables.fixedColumns.js in jquery-datatables-rails-3.3.0 vs app/assets/javascripts/dataTables/extras/dataTables.fixedColumns.js in jquery-datatables-rails-3.4.0
- old
+ new
@@ -1,13 +1,13 @@
-/*! FixedColumns 3.0.4
+/*! FixedColumns 3.2.0
* ©2010-2014 SpryMedia Ltd - datatables.net/license
*/
/**
* @summary FixedColumns
* @description Freeze columns in place on a scrolling DataTable
- * @version 3.0.4
+ * @version 3.2.0
* @file dataTables.fixedColumns.js
* @author SpryMedia Ltd (www.sprymedia.co.uk)
* @contact www.sprymedia.co.uk/contact
* @copyright Copyright 2010-2014 SpryMedia Ltd.
*
@@ -18,18 +18,40 @@
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the license files for details.
*
* For details please refer to: http://www.datatables.net
*/
+(function( factory ){
+ if ( typeof define === 'function' && define.amd ) {
+ // AMD
+ define( ['jquery', 'datatables.net'], function ( $ ) {
+ return factory( $, window, document );
+ } );
+ }
+ else if ( typeof exports === 'object' ) {
+ // CommonJS
+ module.exports = function (root, $) {
+ if ( ! root ) {
+ root = window;
+ }
+ if ( ! $ || ! $.fn.dataTable ) {
+ $ = require('datatables.net')(root, $).$;
+ }
-(function(window, document, undefined) {
+ return factory( $, root, root.document );
+ };
+ }
+ else {
+ // Browser
+ factory( jQuery, window, document );
+ }
+}(function( $, window, document, undefined ) {
+'use strict';
+var DataTable = $.fn.dataTable;
-var factory = function( $, DataTable ) {
-"use strict";
-
/**
* When making use of DataTables' x-axis scrolling feature, you may wish to
* fix the left most column in place. This plug-in for DataTables provides
* exactly this option (note for non-scrolling tables, please use the
* FixedHeader plug-in, which can fix headers, footers and columns). Key
@@ -60,18 +82,16 @@
*/
var FixedColumns = function ( dt, init ) {
var that = this;
/* Sanity check - you just know it will happen */
- if ( ! ( this instanceof FixedColumns ) )
- {
+ if ( ! ( this instanceof FixedColumns ) ) {
alert( "FixedColumns warning: FixedColumns must be initialised with the 'new' keyword." );
return;
}
- if ( typeof init == 'undefined' )
- {
+ if ( init === undefined || init === true ) {
init = {};
}
// Use the DataTables Hungarian notation mapping method, if it exists to
// provide forwards compatibility for camel case variables
@@ -80,13 +100,11 @@
camelToHungarian( FixedColumns.defaults, FixedColumns.defaults, true );
camelToHungarian( FixedColumns.defaults, init );
}
// v1.10 allows the settings object to be got form a number of sources
- var dtSettings = $.fn.dataTable.Api ?
- new $.fn.dataTable.Api( dt ).settings()[0] :
- dt.fnSettings();
+ var dtSettings = new $.fn.dataTable.Api( dt ).settings()[0];
/**
* Settings object which contains customisable information for FixedColumns instance
* @namespace
* @extends FixedColumns.defaults
@@ -263,10 +281,14 @@
"footer": null
}
}
};
+ if ( dtSettings._oFixedColumns ) {
+ throw 'FixedColumns already initialised on this table';
+ }
+
/* Attach the instance to the DataTables instance so it can be accessed easily */
dtSettings._oFixedColumns = this;
/* Let's do it */
if ( ! dtSettings._bInitComplete )
@@ -281,11 +303,11 @@
}
};
-FixedColumns.prototype = /** @lends FixedColumns.prototype */{
+$.extend( FixedColumns.prototype , {
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Public methods
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/**
@@ -472,11 +494,15 @@
// When the body is scrolled - scroll the left and right columns
$(this.dom.scroller)
.on( 'mouseover.DTFC touchstart.DTFC', function () {
mouseController = 'main';
} )
- .on( 'scroll.DTFC', function () {
+ .on( 'scroll.DTFC', function (e) {
+ if ( ! mouseController && e.originalEvent ) {
+ mouseController = 'main';
+ }
+
if ( mouseController === 'main' ) {
if ( that.s.iLeftColumns > 0 ) {
that.dom.grid.left.liner.scrollTop = that.dom.scroller.scrollTop;
}
if ( that.s.iRightColumns > 0 ) {
@@ -493,19 +519,23 @@
// When scrolling the left column, scroll the body and right column
$(that.dom.grid.left.liner)
.on( 'mouseover.DTFC touchstart.DTFC', function () {
mouseController = 'left';
} )
- .on( 'scroll.DTFC', function () {
+ .on( 'scroll.DTFC', function ( e ) {
+ if ( ! mouseController && e.originalEvent ) {
+ mouseController = 'left';
+ }
+
if ( mouseController === 'left' ) {
that.dom.scroller.scrollTop = that.dom.grid.left.liner.scrollTop;
if ( that.s.iRightColumns > 0 ) {
that.dom.grid.right.liner.scrollTop = that.dom.grid.left.liner.scrollTop;
}
}
} )
- .on( wheelType, function(e) { // xxx update the destroy as well
+ .on( wheelType, function(e) {
// Pass horizontal scrolling through
var xDelta = e.type === 'wheel' ?
-e.originalEvent.deltaX :
e.originalEvent.wheelDeltaX;
that.dom.scroller.scrollLeft -= xDelta;
@@ -516,11 +546,15 @@
// When scrolling the right column, scroll the body and the left column
$(that.dom.grid.right.liner)
.on( 'mouseover.DTFC touchstart.DTFC', function () {
mouseController = 'right';
} )
- .on( 'scroll.DTFC', function () {
+ .on( 'scroll.DTFC', function ( e ) {
+ if ( ! mouseController && e.originalEvent ) {
+ mouseController = 'right';
+ }
+
if ( mouseController === 'right' ) {
that.dom.scroller.scrollTop = that.dom.grid.right.liner.scrollTop;
if ( that.s.iLeftColumns > 0 ) {
that.dom.grid.left.liner.scrollTop = that.dom.grid.right.liner.scrollTop;
}
@@ -549,25 +583,27 @@
} )
.on( 'column-sizing.dt.DTFC', function () {
that._fnColCalc();
that._fnGridLayout( that );
} )
- .on( 'column-visibility.dt.DTFC', function () {
- that._fnColCalc();
- that._fnGridLayout( that );
- that._fnDraw( true );
+ .on( 'column-visibility.dt.DTFC', function ( e, settings, column, vis, recalc ) {
+ if ( recalc === undefined || recalc ) {
+ that._fnColCalc();
+ that._fnGridLayout( that );
+ that._fnDraw( true );
+ }
} )
.on( 'destroy.dt.DTFC', function () {
- jqTable.off( 'column-sizing.dt.DTFC destroy.dt.DTFC draw.dt.DTFC' );
+ jqTable.off( 'column-sizing.dt.DTFC column-visibility.dt.DTFC destroy.dt.DTFC draw.dt.DTFC' );
- $(that.dom.scroller).off( 'scroll.DTFC mouseover.DTFC' );
+ $(that.dom.scroller).off( 'mouseover.DTFC touchstart.DTFC scroll.DTFC' );
$(window).off( 'resize.DTFC' );
- $(that.dom.grid.left.liner).off( 'scroll.DTFC mouseover.DTFC '+wheelType );
+ $(that.dom.grid.left.liner).off( 'mouseover.DTFC touchstart.DTFC scroll.DTFC '+wheelType );
$(that.dom.grid.left.wrapper).remove();
- $(that.dom.grid.right.liner).off( 'scroll.DTFC mouseover.DTFC '+wheelType );
+ $(that.dom.grid.right.liner).off( 'mouseover.DTFC touchstart.DTFC scroll.DTFC '+wheelType );
$(that.dom.grid.right.wrapper).remove();
} );
/* Get things right to start with - note that due to adjusting the columns, there must be
* another redraw of the main table. It doesn't need to be a full redraw however.
@@ -920,22 +956,23 @@
* Make a copy of the layout object for a header or footer element from DataTables. Note that
* this method will clone the nodes in the layout object.
* @returns {Array} Copy of the layout array
* @param {Object} aoOriginal Layout array from DataTables (aoHeader or aoFooter)
* @param {Object} aiColumns Columns to copy
+ * @param {boolean} events Copy cell events or not
* @private
*/
- "_fnCopyLayout": function ( aoOriginal, aiColumns )
+ "_fnCopyLayout": function ( aoOriginal, aiColumns, events )
{
var aReturn = [];
var aClones = [];
var aCloned = [];
for ( var i=0, iLen=aoOriginal.length ; i<iLen ; i++ )
{
var aRow = [];
- aRow.nTr = $(aoOriginal[i].nTr).clone(true, true)[0];
+ aRow.nTr = $(aoOriginal[i].nTr).clone(events, false)[0];
for ( var j=0, jLen=this.s.iTableColumns ; j<jLen ; j++ )
{
if ( $.inArray( j, aiColumns ) === -1 )
{
@@ -943,11 +980,11 @@
}
var iCloned = $.inArray( aoOriginal[i][j].cell, aCloned );
if ( iCloned === -1 )
{
- var nClone = $(aoOriginal[i][j].cell).clone(true, true)[0];
+ var nClone = $(aoOriginal[i][j].cell).clone(events, false)[0];
aClones.push( nClone );
aCloned.push( aoOriginal[i][j].cell );
aRow.push( {
"cell": nClone,
@@ -990,21 +1027,19 @@
/*
* Header
*/
if ( bAll )
{
- if ( oClone.header !== null )
- {
- oClone.header.parentNode.removeChild( oClone.header );
- }
- oClone.header = $(this.dom.header).clone(true, true)[0];
+ $(oClone.header).remove();
+
+ oClone.header = $(this.dom.header).clone(true, false)[0];
oClone.header.className += " DTFC_Cloned";
oClone.header.style.width = "100%";
oGrid.head.appendChild( oClone.header );
/* Copy the DataTables layout cache for the header for our floating column */
- aoCloneLayout = this._fnCopyLayout( dt.aoHeader, aiColumns );
+ aoCloneLayout = this._fnCopyLayout( dt.aoHeader, aiColumns, true );
jqCloneThead = $('>thead', oClone.header);
jqCloneThead.empty();
/* Add the created cloned TR elements to the table */
for ( i=0, iLen=aoCloneLayout.length ; i<iLen ; i++ )
@@ -1022,11 +1057,11 @@
/* To ensure that we copy cell classes exactly, regardless of colspan, multiple rows
* etc, we make a copy of the header from the DataTable again, but don't insert the
* cloned cells, just copy the classes across. To get the matching layout for the
* fixed component, we use the DataTables _fnDetectHeader method, allowing 1:1 mapping
*/
- aoCloneLayout = this._fnCopyLayout( dt.aoHeader, aiColumns );
+ aoCloneLayout = this._fnCopyLayout( dt.aoHeader, aiColumns, false );
aoFixedHeader=[];
dt.oApi._fnDetectHeader( aoFixedHeader, $('>thead', oClone.header)[0] );
for ( i=0, iLen=aoCloneLayout.length ; i<iLen ; i++ )
@@ -1053,11 +1088,11 @@
$('>tbody>tr', that.dom.body).css('height', 'auto');
}
if ( oClone.body !== null )
{
- oClone.body.parentNode.removeChild( oClone.body );
+ $(oClone.body).remove();
oClone.body = null;
}
oClone.body = $(this.dom.body).clone(true)[0];
oClone.body.className += " DTFC_Cloned";
@@ -1167,11 +1202,11 @@
oClone.footer.className += " DTFC_Cloned";
oClone.footer.style.width = "100%";
oGrid.foot.appendChild( oClone.footer );
/* Copy the footer just like we do for the header */
- aoCloneLayout = this._fnCopyLayout( dt.aoFooter, aiColumns );
+ aoCloneLayout = this._fnCopyLayout( dt.aoFooter, aiColumns, true );
var jqCloneTfoot = $('>tfoot', oClone.footer);
jqCloneTfoot.empty();
for ( i=0, iLen=aoCloneLayout.length ; i<iLen ; i++ )
{
@@ -1179,11 +1214,11 @@
}
dt.oApi._fnDrawHead( dt, aoCloneLayout, true );
}
else
{
- aoCloneLayout = this._fnCopyLayout( dt.aoFooter, aiColumns );
+ aoCloneLayout = this._fnCopyLayout( dt.aoFooter, aiColumns, false );
var aoCurrFooter=[];
dt.oApi._fnDetectHeader( aoCurrFooter, $('>tfoot', oClone.footer)[0] );
for ( i=0, iLen=aoCloneLayout.length ; i<iLen ; i++ )
@@ -1278,11 +1313,11 @@
{
anClone[i].style.height = heights[i]+"px";
anOriginal[i].style.height = heights[i]+"px";
}
}
-};
+} );
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Statics
@@ -1374,50 +1409,110 @@
* @name FixedColumns.version
* @type String
* @default See code
* @static
*/
-FixedColumns.version = "3.0.4";
+FixedColumns.version = "3.2.0";
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
- * Fired events (for documentation)
+ * DataTables API integration
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
+DataTable.Api.register( 'fixedColumns()', function () {
+ return this;
+} );
-/**
- * Event fired whenever FixedColumns redraws the fixed columns (i.e. clones the table elements from the main DataTable). This will occur whenever the DataTable that the FixedColumns instance is attached does its own draw.
- * @name FixedColumns#draw.dtfc
- * @event
- * @param {event} e jQuery event object
- * @param {object} o Event parameters from FixedColumns
- * @param {object} o.leftClone Instance's object dom.clone.left for easy reference. This object contains references to the left fixed clumn column's nodes
- * @param {object} o.rightClone Instance's object dom.clone.right for easy reference. This object contains references to the right fixed clumn column's nodes
- */
+DataTable.Api.register( 'fixedColumns().update()', function () {
+ return this.iterator( 'table', function ( ctx ) {
+ if ( ctx._oFixedColumns ) {
+ ctx._oFixedColumns.fnUpdate();
+ }
+ } );
+} );
+DataTable.Api.register( 'fixedColumns().relayout()', function () {
+ return this.iterator( 'table', function ( ctx ) {
+ if ( ctx._oFixedColumns ) {
+ ctx._oFixedColumns.fnRedrawLayout();
+ }
+ } );
+} );
-// Make FixedColumns accessible from the DataTables instance
-$.fn.dataTable.FixedColumns = FixedColumns;
-$.fn.DataTable.FixedColumns = FixedColumns;
+DataTable.Api.register( 'rows().recalcHeight()', function () {
+ return this.iterator( 'row', function ( ctx, idx ) {
+ if ( ctx._oFixedColumns ) {
+ ctx._oFixedColumns.fnRecalculateHeight( this.row(idx).node() );
+ }
+ } );
+} );
+DataTable.Api.register( 'fixedColumns().rowIndex()', function ( row ) {
+ row = $(row);
-return FixedColumns;
-}; // /factory
+ return row.parents('.DTFC_Cloned').length ?
+ this.rows( { page: 'current' } ).indexes()[ row.index() ] :
+ this.row( row ).index();
+} );
+DataTable.Api.register( 'fixedColumns().cellIndex()', function ( cell ) {
+ cell = $(cell);
-// Define as an AMD module if possible
-if ( typeof define === 'function' && define.amd ) {
- define( ['jquery', 'datatables'], factory );
-}
-else if ( typeof exports === 'object' ) {
- // Node/CommonJS
- factory( require('jquery'), require('datatables') );
-}
-else if ( jQuery && !jQuery.fn.dataTable.FixedColumns ) {
- // Otherwise simply initialise as normal, stopping multiple evaluation
- factory( jQuery, jQuery.fn.dataTable );
-}
+ if ( cell.parents('.DTFC_Cloned').length ) {
+ var rowClonedIdx = cell.parent().index();
+ var rowIdx = this.rows( { page: 'current' } ).indexes()[ rowClonedIdx ];
+ var columnIdx;
+ if ( cell.parents('.DTFC_LeftWrapper').length ) {
+ columnIdx = cell.index();
+ }
+ else {
+ var columns = this.columns().flatten().length;
+ columnIdx = columns - this.context[0]._oFixedColumns.s.iRightColumns + cell.index();
+ }
-})(window, document);
+ return {
+ row: rowIdx,
+ column: this.column.index( 'toData', columnIdx ),
+ columnVisible: columnIdx
+ };
+ }
+ else {
+ return this.cell( cell ).index();
+ }
+} );
+
+
+
+/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
+ * Initialisation
+ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
+
+// Attach a listener to the document which listens for DataTables initialisation
+// events so we can automatically initialise
+$(document).on( 'init.dt.fixedColumns', function (e, settings) {
+ if ( e.namespace !== 'dt' ) {
+ return;
+ }
+
+ var init = settings.oInit.fixedColumns;
+ var defaults = DataTable.defaults.fixedColumns;
+
+ if ( init || defaults ) {
+ var opts = $.extend( {}, init, defaults );
+
+ if ( init !== false ) {
+ new FixedColumns( settings, opts );
+ }
+ }
+} );
+
+
+
+// Make FixedColumns accessible from the DataTables instance
+$.fn.dataTable.FixedColumns = FixedColumns;
+$.fn.DataTable.FixedColumns = FixedColumns;
+
+return FixedColumns;
+}));
\ No newline at end of file