Skip to content

Commit 70136b7

Browse files
Update ColReorder to v1.1.2 to handle new state object
1 parent 55b7f9f commit 70136b7

File tree

1 file changed

+75
-21
lines changed

1 file changed

+75
-21
lines changed

app/assets/javascripts/dataTables/extras/dataTables.colReorder.js

Lines changed: 75 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
/*! ColReorder 1.1.1
1+
/*! ColReorder 1.1.2
22
* ©2010-2014 SpryMedia Ltd - datatables.net/license
33
*/
44

55
/**
66
* @summary ColReorder
77
* @description Provide the ability to reorder columns in a DataTable
8-
* @version 1.1.1
8+
* @version 1.1.2
99
* @file dataTables.colReorder.js
1010
* @author SpryMedia Ltd (www.sprymedia.co.uk)
1111
* @contact www.sprymedia.co.uk/contact
@@ -111,6 +111,20 @@ $.fn.dataTableExt.oApi.fnColReorder = function ( oSettings, iFrom, iTo )
111111
{
112112
var v110 = $.fn.dataTable.Api ? true : false;
113113
var i, iLen, j, jLen, iCols=oSettings.aoColumns.length, nTrs, oCol;
114+
var attrMap = function ( obj, prop, mapping ) {
115+
if ( ! obj[ prop ] ) {
116+
return;
117+
}
118+
119+
var a = obj[ prop ].split('.');
120+
var num = a.shift();
121+
122+
if ( isNaN( num*1 ) ) {
123+
return;
124+
}
125+
126+
obj[ prop ] = mapping[ num*1 ]+'.'+a.join('.');
127+
};
114128

115129
/* Sanity check in the input */
116130
if ( iFrom == iTo )
@@ -187,9 +201,20 @@ $.fn.dataTableExt.oApi.fnColReorder = function ( oSettings, iFrom, iTo )
187201
for ( i=0, iLen=iCols ; i<iLen ; i++ )
188202
{
189203
oCol = oSettings.aoColumns[i];
204+
190205
if ( typeof oCol.mData == 'number' ) {
191206
oCol.mData = aiInvertMapping[ oCol.mData ];
192207

208+
// regenerate the get / set functions
209+
oSettings.oApi._fnColumnOptions( oSettings, i, {} );
210+
}
211+
else if ( $.isPlainObject( oCol.mData ) ) {
212+
// HTML5 data sourced
213+
attrMap( oCol.mData, '_', aiInvertMapping );
214+
attrMap( oCol.mData, 'filter', aiInvertMapping );
215+
attrMap( oCol.mData, 'sort', aiInvertMapping );
216+
attrMap( oCol.mData, 'type', aiInvertMapping );
217+
193218
// regenerate the get / set functions
194219
oSettings.oApi._fnColumnOptions( oSettings, i, {} );
195220
}
@@ -366,8 +391,10 @@ var ColReorder = function( dt, opts )
366391
}
367392

368393
// Convert from camelCase to Hungarian, just as DataTables does
369-
if ( $.fn.dataTable.camelToHungarian ) {
370-
$.fn.dataTable.camelToHungarian( ColReorder.defaults, opts || {} );
394+
var camelToHungarian = $.fn.dataTable.camelToHungarian;
395+
if ( camelToHungarian ) {
396+
camelToHungarian( ColReorder.defaults, ColReorder.defaults, true );
397+
camelToHungarian( ColReorder.defaults, opts || {} );
371398
}
372399

373400

@@ -732,28 +759,51 @@ ColReorder.prototype = {
732759
{
733760
var i, iLen, aCopy, iOrigColumn;
734761
var oSettings = this.s.dt;
762+
var columns = oSettings.aoColumns;
763+
764+
oState.ColReorder = [];
735765

736766
/* Sorting */
737-
for ( i=0 ; i<oState.aaSorting.length ; i++ )
738-
{
739-
oState.aaSorting[i][0] = oSettings.aoColumns[ oState.aaSorting[i][0] ]._ColReorder_iOrigCol;
740-
}
767+
if ( oState.aaSorting ) {
768+
// 1.10.0-
769+
for ( i=0 ; i<oState.aaSorting.length ; i++ ) {
770+
oState.aaSorting[i][0] = columns[ oState.aaSorting[i][0] ]._ColReorder_iOrigCol;
771+
}
741772

742-
var aSearchCopy = $.extend( true, [], oState.aoSearchCols );
743-
oState.ColReorder = [];
773+
var aSearchCopy = $.extend( true, [], oState.aoSearchCols );
744774

745-
for ( i=0, iLen=oSettings.aoColumns.length ; i<iLen ; i++ )
746-
{
747-
iOrigColumn = oSettings.aoColumns[i]._ColReorder_iOrigCol;
775+
for ( i=0, iLen=columns.length ; i<iLen ; i++ )
776+
{
777+
iOrigColumn = columns[i]._ColReorder_iOrigCol;
748778

749-
/* Column filter */
750-
oState.aoSearchCols[ iOrigColumn ] = aSearchCopy[i];
779+
/* Column filter */
780+
oState.aoSearchCols[ iOrigColumn ] = aSearchCopy[i];
751781

752-
/* Visibility */
753-
oState.abVisCols[ iOrigColumn ] = oSettings.aoColumns[i].bVisible;
782+
/* Visibility */
783+
oState.abVisCols[ iOrigColumn ] = columns[i].bVisible;
754784

755-
/* Column reordering */
756-
oState.ColReorder.push( iOrigColumn );
785+
/* Column reordering */
786+
oState.ColReorder.push( iOrigColumn );
787+
}
788+
}
789+
else if ( oState.order ) {
790+
// 1.10.1+
791+
for ( i=0 ; i<oState.order.length ; i++ ) {
792+
oState.order[i][0] = columns[ oState.order[i][0] ]._ColReorder_iOrigCol;
793+
}
794+
795+
var stateColumnsCopy = $.extend( true, [], oState.columns );
796+
797+
for ( i=0, iLen=columns.length ; i<iLen ; i++ )
798+
{
799+
iOrigColumn = columns[i]._ColReorder_iOrigCol;
800+
801+
/* Columns */
802+
oState.columns[ iOrigColumn ] = stateColumnsCopy[i];
803+
804+
/* Column reordering */
805+
oState.ColReorder.push( iOrigColumn );
806+
}
757807
}
758808
},
759809

@@ -1236,7 +1286,7 @@ ColReorder.defaults = {
12361286
* @type String
12371287
* @default As code
12381288
*/
1239-
ColReorder.version = "1.1.1";
1289+
ColReorder.version = "1.1.2";
12401290

12411291

12421292

@@ -1306,7 +1356,11 @@ return ColReorder;
13061356

13071357
// Define as an AMD module if possible
13081358
if ( typeof define === 'function' && define.amd ) {
1309-
define( 'datatables-colreorder', ['jquery', 'datatables'], factory );
1359+
define( ['jquery', 'datatables'], factory );
1360+
}
1361+
else if ( typeof exports === 'object' ) {
1362+
// Node/CommonJS
1363+
factory( require('jquery'), require('datatables') );
13101364
}
13111365
else if ( jQuery && !jQuery.fn.dataTable.ColReorder ) {
13121366
// Otherwise simply initialise as normal, stopping multiple evaluation

0 commit comments

Comments
 (0)