dataTables.colReorder.js
35.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
/*! ColReorder 1.1.3
* ©2010-2014 SpryMedia Ltd - datatables.net/license
*/
/**
* @summary ColReorder
* @description Provide the ability to reorder columns in a DataTable
* @version 1.1.3
* @file dataTables.colReorder.js
* @author SpryMedia Ltd (www.sprymedia.co.uk)
* @contact www.sprymedia.co.uk/contact
* @copyright Copyright 2010-2014 SpryMedia Ltd.
*
* This source file is free software, available under the following license:
* MIT license - http://datatables.net/license/mit
*
* This source file is distributed in the hope that it will be useful, but
* 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(window, document, undefined) {
/**
* Switch the key value pairing of an index array to be value key (i.e. the old value is now the
* key). For example consider [ 2, 0, 1 ] this would be returned as [ 1, 2, 0 ].
* @method fnInvertKeyValues
* @param array aIn Array to switch around
* @returns array
*/
function fnInvertKeyValues( aIn )
{
var aRet=[];
for ( var i=0, iLen=aIn.length ; i<iLen ; i++ )
{
aRet[ aIn[i] ] = i;
}
return aRet;
}
/**
* Modify an array by switching the position of two elements
* @method fnArraySwitch
* @param array aArray Array to consider, will be modified by reference (i.e. no return)
* @param int iFrom From point
* @param int iTo Insert point
* @returns void
*/
function fnArraySwitch( aArray, iFrom, iTo )
{
var mStore = aArray.splice( iFrom, 1 )[0];
aArray.splice( iTo, 0, mStore );
}
/**
* Switch the positions of nodes in a parent node (note this is specifically designed for
* table rows). Note this function considers all element nodes under the parent!
* @method fnDomSwitch
* @param string sTag Tag to consider
* @param int iFrom Element to move
* @param int Point to element the element to (before this point), can be null for append
* @returns void
*/
function fnDomSwitch( nParent, iFrom, iTo )
{
var anTags = [];
for ( var i=0, iLen=nParent.childNodes.length ; i<iLen ; i++ )
{
if ( nParent.childNodes[i].nodeType == 1 )
{
anTags.push( nParent.childNodes[i] );
}
}
var nStore = anTags[ iFrom ];
if ( iTo !== null )
{
nParent.insertBefore( nStore, anTags[iTo] );
}
else
{
nParent.appendChild( nStore );
}
}
var factory = function( $, DataTable ) {
"use strict";
/**
* Plug-in for DataTables which will reorder the internal column structure by taking the column
* from one position (iFrom) and insert it into a given point (iTo).
* @method $.fn.dataTableExt.oApi.fnColReorder
* @param object oSettings DataTables settings object - automatically added by DataTables!
* @param int iFrom Take the column to be repositioned from this point
* @param int iTo and insert it into this point
* @returns void
*/
$.fn.dataTableExt.oApi.fnColReorder = function ( oSettings, iFrom, iTo )
{
var v110 = $.fn.dataTable.Api ? true : false;
var i, iLen, j, jLen, iCols=oSettings.aoColumns.length, nTrs, oCol;
var attrMap = function ( obj, prop, mapping ) {
if ( ! obj[ prop ] ) {
return;
}
var a = obj[ prop ].split('.');
var num = a.shift();
if ( isNaN( num*1 ) ) {
return;
}
obj[ prop ] = mapping[ num*1 ]+'.'+a.join('.');
};
/* Sanity check in the input */
if ( iFrom == iTo )
{
/* Pointless reorder */
return;
}
if ( iFrom < 0 || iFrom >= iCols )
{
this.oApi._fnLog( oSettings, 1, "ColReorder 'from' index is out of bounds: "+iFrom );
return;
}
if ( iTo < 0 || iTo >= iCols )
{
this.oApi._fnLog( oSettings, 1, "ColReorder 'to' index is out of bounds: "+iTo );
return;
}
/*
* Calculate the new column array index, so we have a mapping between the old and new
*/
var aiMapping = [];
for ( i=0, iLen=iCols ; i<iLen ; i++ )
{
aiMapping[i] = i;
}
fnArraySwitch( aiMapping, iFrom, iTo );
var aiInvertMapping = fnInvertKeyValues( aiMapping );
/*
* Convert all internal indexing to the new column order indexes
*/
/* Sorting */
for ( i=0, iLen=oSettings.aaSorting.length ; i<iLen ; i++ )
{
oSettings.aaSorting[i][0] = aiInvertMapping[ oSettings.aaSorting[i][0] ];
}
/* Fixed sorting */
if ( oSettings.aaSortingFixed !== null )
{
for ( i=0, iLen=oSettings.aaSortingFixed.length ; i<iLen ; i++ )
{
oSettings.aaSortingFixed[i][0] = aiInvertMapping[ oSettings.aaSortingFixed[i][0] ];
}
}
/* Data column sorting (the column which the sort for a given column should take place on) */
for ( i=0, iLen=iCols ; i<iLen ; i++ )
{
oCol = oSettings.aoColumns[i];
for ( j=0, jLen=oCol.aDataSort.length ; j<jLen ; j++ )
{
oCol.aDataSort[j] = aiInvertMapping[ oCol.aDataSort[j] ];
}
// Update the column indexes
if ( v110 ) {
oCol.idx = aiInvertMapping[ oCol.idx ];
}
}
if ( v110 ) {
// Update 1.10 optimised sort class removal variable
$.each( oSettings.aLastSort, function (i, val) {
oSettings.aLastSort[i].src = aiInvertMapping[ val.src ];
} );
}
/* Update the Get and Set functions for each column */
for ( i=0, iLen=iCols ; i<iLen ; i++ )
{
oCol = oSettings.aoColumns[i];
if ( typeof oCol.mData == 'number' ) {
oCol.mData = aiInvertMapping[ oCol.mData ];
// regenerate the get / set functions
oSettings.oApi._fnColumnOptions( oSettings, i, {} );
}
else if ( $.isPlainObject( oCol.mData ) ) {
// HTML5 data sourced
attrMap( oCol.mData, '_', aiInvertMapping );
attrMap( oCol.mData, 'filter', aiInvertMapping );
attrMap( oCol.mData, 'sort', aiInvertMapping );
attrMap( oCol.mData, 'type', aiInvertMapping );
// regenerate the get / set functions
oSettings.oApi._fnColumnOptions( oSettings, i, {} );
}
}
/*
* Move the DOM elements
*/
if ( oSettings.aoColumns[iFrom].bVisible )
{
/* Calculate the current visible index and the point to insert the node before. The insert
* before needs to take into account that there might not be an element to insert before,
* in which case it will be null, and an appendChild should be used
*/
var iVisibleIndex = this.oApi._fnColumnIndexToVisible( oSettings, iFrom );
var iInsertBeforeIndex = null;
i = iTo < iFrom ? iTo : iTo + 1;
while ( iInsertBeforeIndex === null && i < iCols )
{
iInsertBeforeIndex = this.oApi._fnColumnIndexToVisible( oSettings, i );
i++;
}
/* Header */
nTrs = oSettings.nTHead.getElementsByTagName('tr');
for ( i=0, iLen=nTrs.length ; i<iLen ; i++ )
{
fnDomSwitch( nTrs[i], iVisibleIndex, iInsertBeforeIndex );
}
/* Footer */
if ( oSettings.nTFoot !== null )
{
nTrs = oSettings.nTFoot.getElementsByTagName('tr');
for ( i=0, iLen=nTrs.length ; i<iLen ; i++ )
{
fnDomSwitch( nTrs[i], iVisibleIndex, iInsertBeforeIndex );
}
}
/* Body */
for ( i=0, iLen=oSettings.aoData.length ; i<iLen ; i++ )
{
if ( oSettings.aoData[i].nTr !== null )
{
fnDomSwitch( oSettings.aoData[i].nTr, iVisibleIndex, iInsertBeforeIndex );
}
}
}
/*
* Move the internal array elements
*/
/* Columns */
fnArraySwitch( oSettings.aoColumns, iFrom, iTo );
/* Search columns */
fnArraySwitch( oSettings.aoPreSearchCols, iFrom, iTo );
/* Array array - internal data anodes cache */
for ( i=0, iLen=oSettings.aoData.length ; i<iLen ; i++ )
{
var data = oSettings.aoData[i];
if ( v110 ) {
// DataTables 1.10+
if ( data.anCells ) {
fnArraySwitch( data.anCells, iFrom, iTo );
}
// For DOM sourced data, the invalidate will reread the cell into
// the data array, but for data sources as an array, they need to
// be flipped
if ( data.src !== 'dom' && $.isArray( data._aData ) ) {
fnArraySwitch( data._aData, iFrom, iTo );
}
}
else {
// DataTables 1.9-
if ( $.isArray( data._aData ) ) {
fnArraySwitch( data._aData, iFrom, iTo );
}
fnArraySwitch( data._anHidden, iFrom, iTo );
}
}
/* Reposition the header elements in the header layout array */
for ( i=0, iLen=oSettings.aoHeader.length ; i<iLen ; i++ )
{
fnArraySwitch( oSettings.aoHeader[i], iFrom, iTo );
}
if ( oSettings.aoFooter !== null )
{
for ( i=0, iLen=oSettings.aoFooter.length ; i<iLen ; i++ )
{
fnArraySwitch( oSettings.aoFooter[i], iFrom, iTo );
}
}
// In 1.10 we need to invalidate row cached data for sorting, filtering etc
if ( v110 ) {
var api = new $.fn.dataTable.Api( oSettings );
api.rows().invalidate();
}
/*
* Update DataTables' event handlers
*/
/* Sort listener */
for ( i=0, iLen=iCols ; i<iLen ; i++ )
{
$(oSettings.aoColumns[i].nTh).off('click.DT');
this.oApi._fnSortAttachListener( oSettings, oSettings.aoColumns[i].nTh, i );
}
/* Fire an event so other plug-ins can update */
$(oSettings.oInstance).trigger( 'column-reorder', [ oSettings, {
"iFrom": iFrom,
"iTo": iTo,
"aiInvertMapping": aiInvertMapping
} ] );
};
/**
* ColReorder provides column visibility control for DataTables
* @class ColReorder
* @constructor
* @param {object} dt DataTables settings object
* @param {object} opts ColReorder options
*/
var ColReorder = function( dt, opts )
{
var oDTSettings;
if ( $.fn.dataTable.Api ) {
oDTSettings = new $.fn.dataTable.Api( dt ).settings()[0];
}
// 1.9 compatibility
else if ( dt.fnSettings ) {
// DataTables object, convert to the settings object
oDTSettings = dt.fnSettings();
}
else if ( typeof dt === 'string' ) {
// jQuery selector
if ( $.fn.dataTable.fnIsDataTable( $(dt)[0] ) ) {
oDTSettings = $(dt).eq(0).dataTable().fnSettings();
}
}
else if ( dt.nodeName && dt.nodeName.toLowerCase() === 'table' ) {
// Table node
if ( $.fn.dataTable.fnIsDataTable( dt.nodeName ) ) {
oDTSettings = $(dt.nodeName).dataTable().fnSettings();
}
}
else if ( dt instanceof jQuery ) {
// jQuery object
if ( $.fn.dataTable.fnIsDataTable( dt[0] ) ) {
oDTSettings = dt.eq(0).dataTable().fnSettings();
}
}
else {
// DataTables settings object
oDTSettings = dt;
}
// Ensure that we can't initialise on the same table twice
if ( oDTSettings._colReorder ) {
throw "ColReorder already initialised on table #"+oDTSettings.nTable.id;
}
// Convert from camelCase to Hungarian, just as DataTables does
var camelToHungarian = $.fn.dataTable.camelToHungarian;
if ( camelToHungarian ) {
camelToHungarian( ColReorder.defaults, ColReorder.defaults, true );
camelToHungarian( ColReorder.defaults, opts || {} );
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Public class variables
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/**
* @namespace Settings object which contains customisable information for ColReorder instance
*/
this.s = {
/**
* DataTables settings object
* @property dt
* @type Object
* @default null
*/
"dt": null,
/**
* Initialisation object used for this instance
* @property init
* @type object
* @default {}
*/
"init": $.extend( true, {}, ColReorder.defaults, opts ),
/**
* Number of columns to fix (not allow to be reordered)
* @property fixed
* @type int
* @default 0
*/
"fixed": 0,
/**
* Number of columns to fix counting from right (not allow to be reordered)
* @property fixedRight
* @type int
* @default 0
*/
"fixedRight": 0,
/**
* Callback function for once the reorder has been done
* @property reorderCallback
* @type function
* @default null
*/
"reorderCallback": null,
/**
* @namespace Information used for the mouse drag
*/
"mouse": {
"startX": -1,
"startY": -1,
"offsetX": -1,
"offsetY": -1,
"target": -1,
"targetIndex": -1,
"fromIndex": -1
},
/**
* Information which is used for positioning the insert cusor and knowing where to do the
* insert. Array of objects with the properties:
* x: x-axis position
* to: insert point
* @property aoTargets
* @type array
* @default []
*/
"aoTargets": []
};
/**
* @namespace Common and useful DOM elements for the class instance
*/
this.dom = {
/**
* Dragging element (the one the mouse is moving)
* @property drag
* @type element
* @default null
*/
"drag": null,
/**
* The insert cursor
* @property pointer
* @type element
* @default null
*/
"pointer": null
};
/* Constructor logic */
this.s.dt = oDTSettings;
this.s.dt._colReorder = this;
this._fnConstruct();
/* Add destroy callback */
oDTSettings.oApi._fnCallbackReg(oDTSettings, 'aoDestroyCallback', $.proxy(this._fnDestroy, this), 'ColReorder');
return this;
};
ColReorder.prototype = {
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Public methods
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/**
* Reset the column ordering to the original ordering that was detected on
* start up.
* @return {this} Returns `this` for chaining.
*
* @example
* // DataTables initialisation with ColReorder
* var table = $('#example').dataTable( {
* "sDom": 'Rlfrtip'
* } );
*
* // Add click event to a button to reset the ordering
* $('#resetOrdering').click( function (e) {
* e.preventDefault();
* $.fn.dataTable.ColReorder( table ).fnReset();
* } );
*/
"fnReset": function ()
{
var a = [];
for ( var i=0, iLen=this.s.dt.aoColumns.length ; i<iLen ; i++ )
{
a.push( this.s.dt.aoColumns[i]._ColReorder_iOrigCol );
}
this._fnOrderColumns( a );
return this;
},
/**
* `Deprecated` - Get the current order of the columns, as an array.
* @return {array} Array of column identifiers
* @deprecated `fnOrder` should be used in preference to this method.
* `fnOrder` acts as a getter/setter.
*/
"fnGetCurrentOrder": function ()
{
return this.fnOrder();
},
/**
* Get the current order of the columns, as an array. Note that the values
* given in the array are unique identifiers for each column. Currently
* these are the original ordering of the columns that was detected on
* start up, but this could potentially change in future.
* @return {array} Array of column identifiers
*
* @example
* // Get column ordering for the table
* var order = $.fn.dataTable.ColReorder( dataTable ).fnOrder();
*//**
* Set the order of the columns, from the positions identified in the
* ordering array given. Note that ColReorder takes a brute force approach
* to reordering, so it is possible multiple reordering events will occur
* before the final order is settled upon.
* @param {array} [set] Array of column identifiers in the new order. Note
* that every column must be included, uniquely, in this array.
* @return {this} Returns `this` for chaining.
*
* @example
* // Swap the first and second columns
* $.fn.dataTable.ColReorder( dataTable ).fnOrder( [1, 0, 2, 3, 4] );
*
* @example
* // Move the first column to the end for the table `#example`
* var curr = $.fn.dataTable.ColReorder( '#example' ).fnOrder();
* var first = curr.shift();
* curr.push( first );
* $.fn.dataTable.ColReorder( '#example' ).fnOrder( curr );
*
* @example
* // Reverse the table's order
* $.fn.dataTable.ColReorder( '#example' ).fnOrder(
* $.fn.dataTable.ColReorder( '#example' ).fnOrder().reverse()
* );
*/
"fnOrder": function ( set )
{
if ( set === undefined )
{
var a = [];
for ( var i=0, iLen=this.s.dt.aoColumns.length ; i<iLen ; i++ )
{
a.push( this.s.dt.aoColumns[i]._ColReorder_iOrigCol );
}
return a;
}
this._fnOrderColumns( fnInvertKeyValues( set ) );
return this;
},
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Private methods (they are of course public in JS, but recommended as private)
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/**
* Constructor logic
* @method _fnConstruct
* @returns void
* @private
*/
"_fnConstruct": function ()
{
var that = this;
var iLen = this.s.dt.aoColumns.length;
var i;
/* Columns discounted from reordering - counting left to right */
if ( this.s.init.iFixedColumns )
{
this.s.fixed = this.s.init.iFixedColumns;
}
/* Columns discounted from reordering - counting right to left */
this.s.fixedRight = this.s.init.iFixedColumnsRight ?
this.s.init.iFixedColumnsRight :
0;
/* Drop callback initialisation option */
if ( this.s.init.fnReorderCallback )
{
this.s.reorderCallback = this.s.init.fnReorderCallback;
}
/* Add event handlers for the drag and drop, and also mark the original column order */
for ( i = 0; i < iLen; i++ )
{
if ( i > this.s.fixed-1 && i < iLen - this.s.fixedRight )
{
this._fnMouseListener( i, this.s.dt.aoColumns[i].nTh );
}
/* Mark the original column order for later reference */
this.s.dt.aoColumns[i]._ColReorder_iOrigCol = i;
}
/* State saving */
this.s.dt.oApi._fnCallbackReg( this.s.dt, 'aoStateSaveParams', function (oS, oData) {
that._fnStateSave.call( that, oData );
}, "ColReorder_State" );
/* An initial column order has been specified */
var aiOrder = null;
if ( this.s.init.aiOrder )
{
aiOrder = this.s.init.aiOrder.slice();
}
/* State loading, overrides the column order given */
if ( this.s.dt.oLoadedState && typeof this.s.dt.oLoadedState.ColReorder != 'undefined' &&
this.s.dt.oLoadedState.ColReorder.length == this.s.dt.aoColumns.length )
{
aiOrder = this.s.dt.oLoadedState.ColReorder;
}
/* If we have an order to apply - do so */
if ( aiOrder )
{
/* We might be called during or after the DataTables initialisation. If before, then we need
* to wait until the draw is done, if after, then do what we need to do right away
*/
if ( !that.s.dt._bInitComplete )
{
var bDone = false;
this.s.dt.aoDrawCallback.push( {
"fn": function () {
if ( !that.s.dt._bInitComplete && !bDone )
{
bDone = true;
var resort = fnInvertKeyValues( aiOrder );
that._fnOrderColumns.call( that, resort );
}
},
"sName": "ColReorder_Pre"
} );
}
else
{
var resort = fnInvertKeyValues( aiOrder );
that._fnOrderColumns.call( that, resort );
}
}
else {
this._fnSetColumnIndexes();
}
},
/**
* Set the column order from an array
* @method _fnOrderColumns
* @param array a An array of integers which dictate the column order that should be applied
* @returns void
* @private
*/
"_fnOrderColumns": function ( a )
{
if ( a.length != this.s.dt.aoColumns.length )
{
this.s.dt.oInstance.oApi._fnLog( this.s.dt, 1, "ColReorder - array reorder does not "+
"match known number of columns. Skipping." );
return;
}
for ( var i=0, iLen=a.length ; i<iLen ; i++ )
{
var currIndex = $.inArray( i, a );
if ( i != currIndex )
{
/* Reorder our switching array */
fnArraySwitch( a, currIndex, i );
/* Do the column reorder in the table */
this.s.dt.oInstance.fnColReorder( currIndex, i );
}
}
/* When scrolling we need to recalculate the column sizes to allow for the shift */
if ( this.s.dt.oScroll.sX !== "" || this.s.dt.oScroll.sY !== "" )
{
this.s.dt.oInstance.fnAdjustColumnSizing( false );
}
/* Save the state */
this.s.dt.oInstance.oApi._fnSaveState( this.s.dt );
this._fnSetColumnIndexes();
if ( this.s.reorderCallback !== null )
{
this.s.reorderCallback.call( this );
}
},
/**
* Because we change the indexes of columns in the table, relative to their starting point
* we need to reorder the state columns to what they are at the starting point so we can
* then rearrange them again on state load!
* @method _fnStateSave
* @param object oState DataTables state
* @returns string JSON encoded cookie string for DataTables
* @private
*/
"_fnStateSave": function ( oState )
{
var i, iLen, aCopy, iOrigColumn;
var oSettings = this.s.dt;
var columns = oSettings.aoColumns;
oState.ColReorder = [];
/* Sorting */
if ( oState.aaSorting ) {
// 1.10.0-
for ( i=0 ; i<oState.aaSorting.length ; i++ ) {
oState.aaSorting[i][0] = columns[ oState.aaSorting[i][0] ]._ColReorder_iOrigCol;
}
var aSearchCopy = $.extend( true, [], oState.aoSearchCols );
for ( i=0, iLen=columns.length ; i<iLen ; i++ )
{
iOrigColumn = columns[i]._ColReorder_iOrigCol;
/* Column filter */
oState.aoSearchCols[ iOrigColumn ] = aSearchCopy[i];
/* Visibility */
oState.abVisCols[ iOrigColumn ] = columns[i].bVisible;
/* Column reordering */
oState.ColReorder.push( iOrigColumn );
}
}
else if ( oState.order ) {
// 1.10.1+
for ( i=0 ; i<oState.order.length ; i++ ) {
oState.order[i][0] = columns[ oState.order[i][0] ]._ColReorder_iOrigCol;
}
var stateColumnsCopy = $.extend( true, [], oState.columns );
for ( i=0, iLen=columns.length ; i<iLen ; i++ )
{
iOrigColumn = columns[i]._ColReorder_iOrigCol;
/* Columns */
oState.columns[ iOrigColumn ] = stateColumnsCopy[i];
/* Column reordering */
oState.ColReorder.push( iOrigColumn );
}
}
},
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Mouse drop and drag
*/
/**
* Add a mouse down listener to a particluar TH element
* @method _fnMouseListener
* @param int i Column index
* @param element nTh TH element clicked on
* @returns void
* @private
*/
"_fnMouseListener": function ( i, nTh )
{
var that = this;
$(nTh).on( 'mousedown.ColReorder', function (e) {
e.preventDefault();
that._fnMouseDown.call( that, e, nTh );
} );
},
/**
* Mouse down on a TH element in the table header
* @method _fnMouseDown
* @param event e Mouse event
* @param element nTh TH element to be dragged
* @returns void
* @private
*/
"_fnMouseDown": function ( e, nTh )
{
var that = this;
/* Store information about the mouse position */
var target = $(e.target).closest('th, td');
var offset = target.offset();
var idx = parseInt( $(nTh).attr('data-column-index'), 10 );
if ( idx === undefined ) {
return;
}
this.s.mouse.startX = e.pageX;
this.s.mouse.startY = e.pageY;
this.s.mouse.offsetX = e.pageX - offset.left;
this.s.mouse.offsetY = e.pageY - offset.top;
this.s.mouse.target = this.s.dt.aoColumns[ idx ].nTh;//target[0];
this.s.mouse.targetIndex = idx;
this.s.mouse.fromIndex = idx;
this._fnRegions();
/* Add event handlers to the document */
$(document)
.on( 'mousemove.ColReorder', function (e) {
that._fnMouseMove.call( that, e );
} )
.on( 'mouseup.ColReorder', function (e) {
that._fnMouseUp.call( that, e );
} );
},
/**
* Deal with a mouse move event while dragging a node
* @method _fnMouseMove
* @param event e Mouse event
* @returns void
* @private
*/
"_fnMouseMove": function ( e )
{
var that = this;
if ( this.dom.drag === null )
{
/* Only create the drag element if the mouse has moved a specific distance from the start
* point - this allows the user to make small mouse movements when sorting and not have a
* possibly confusing drag element showing up
*/
if ( Math.pow(
Math.pow(e.pageX - this.s.mouse.startX, 2) +
Math.pow(e.pageY - this.s.mouse.startY, 2), 0.5 ) < 5 )
{
return;
}
this._fnCreateDragNode();
}
/* Position the element - we respect where in the element the click occured */
this.dom.drag.css( {
left: e.pageX - this.s.mouse.offsetX,
top: e.pageY - this.s.mouse.offsetY
} );
/* Based on the current mouse position, calculate where the insert should go */
var bSet = false;
var lastToIndex = this.s.mouse.toIndex;
for ( var i=1, iLen=this.s.aoTargets.length ; i<iLen ; i++ )
{
if ( e.pageX < this.s.aoTargets[i-1].x + ((this.s.aoTargets[i].x-this.s.aoTargets[i-1].x)/2) )
{
this.dom.pointer.css( 'left', this.s.aoTargets[i-1].x );
this.s.mouse.toIndex = this.s.aoTargets[i-1].to;
bSet = true;
break;
}
}
// The insert element wasn't positioned in the array (less than
// operator), so we put it at the end
if ( !bSet )
{
this.dom.pointer.css( 'left', this.s.aoTargets[this.s.aoTargets.length-1].x );
this.s.mouse.toIndex = this.s.aoTargets[this.s.aoTargets.length-1].to;
}
// Perform reordering if realtime updating is on and the column has moved
if ( this.s.init.bRealtime && lastToIndex !== this.s.mouse.toIndex ) {
this.s.dt.oInstance.fnColReorder( this.s.mouse.fromIndex, this.s.mouse.toIndex );
this.s.mouse.fromIndex = this.s.mouse.toIndex;
this._fnRegions();
}
},
/**
* Finish off the mouse drag and insert the column where needed
* @method _fnMouseUp
* @param event e Mouse event
* @returns void
* @private
*/
"_fnMouseUp": function ( e )
{
var that = this;
$(document).off( 'mousemove.ColReorder mouseup.ColReorder' );
if ( this.dom.drag !== null )
{
/* Remove the guide elements */
this.dom.drag.remove();
this.dom.pointer.remove();
this.dom.drag = null;
this.dom.pointer = null;
/* Actually do the reorder */
this.s.dt.oInstance.fnColReorder( this.s.mouse.fromIndex, this.s.mouse.toIndex );
this._fnSetColumnIndexes();
/* When scrolling we need to recalculate the column sizes to allow for the shift */
if ( this.s.dt.oScroll.sX !== "" || this.s.dt.oScroll.sY !== "" )
{
this.s.dt.oInstance.fnAdjustColumnSizing( false );
}
/* Save the state */
this.s.dt.oInstance.oApi._fnSaveState( this.s.dt );
if ( this.s.reorderCallback !== null )
{
this.s.reorderCallback.call( this );
}
}
},
/**
* Calculate a cached array with the points of the column inserts, and the
* 'to' points
* @method _fnRegions
* @returns void
* @private
*/
"_fnRegions": function ()
{
var aoColumns = this.s.dt.aoColumns;
this.s.aoTargets.splice( 0, this.s.aoTargets.length );
this.s.aoTargets.push( {
"x": $(this.s.dt.nTable).offset().left,
"to": 0
} );
var iToPoint = 0;
for ( var i=0, iLen=aoColumns.length ; i<iLen ; i++ )
{
/* For the column / header in question, we want it's position to remain the same if the
* position is just to it's immediate left or right, so we only incremement the counter for
* other columns
*/
if ( i != this.s.mouse.fromIndex )
{
iToPoint++;
}
if ( aoColumns[i].bVisible )
{
this.s.aoTargets.push( {
"x": $(aoColumns[i].nTh).offset().left + $(aoColumns[i].nTh).outerWidth(),
"to": iToPoint
} );
}
}
/* Disallow columns for being reordered by drag and drop, counting right to left */
if ( this.s.fixedRight !== 0 )
{
this.s.aoTargets.splice( this.s.aoTargets.length - this.s.fixedRight );
}
/* Disallow columns for being reordered by drag and drop, counting left to right */
if ( this.s.fixed !== 0 )
{
this.s.aoTargets.splice( 0, this.s.fixed );
}
},
/**
* Copy the TH element that is being drags so the user has the idea that they are actually
* moving it around the page.
* @method _fnCreateDragNode
* @returns void
* @private
*/
"_fnCreateDragNode": function ()
{
var scrolling = this.s.dt.oScroll.sX !== "" || this.s.dt.oScroll.sY !== "";
var origCell = this.s.dt.aoColumns[ this.s.mouse.targetIndex ].nTh;
var origTr = origCell.parentNode;
var origThead = origTr.parentNode;
var origTable = origThead.parentNode;
var cloneCell = $(origCell).clone();
// This is a slightly odd combination of jQuery and DOM, but it is the
// fastest and least resource intensive way I could think of cloning
// the table with just a single header cell in it.
this.dom.drag = $(origTable.cloneNode(false))
.addClass( 'DTCR_clonedTable' )
.append(
$(origThead.cloneNode(false)).append(
$(origTr.cloneNode(false)).append(
cloneCell[0]
)
)
)
.css( {
position: 'absolute',
top: 0,
left: 0,
width: $(origCell).outerWidth(),
height: $(origCell).outerHeight()
} )
.appendTo( 'body' );
this.dom.pointer = $('<div></div>')
.addClass( 'DTCR_pointer' )
.css( {
position: 'absolute',
top: scrolling ?
$('div.dataTables_scroll', this.s.dt.nTableWrapper).offset().top :
$(this.s.dt.nTable).offset().top,
height : scrolling ?
$('div.dataTables_scroll', this.s.dt.nTableWrapper).height() :
$(this.s.dt.nTable).height()
} )
.appendTo( 'body' );
},
/**
* Clean up ColReorder memory references and event handlers
* @method _fnDestroy
* @returns void
* @private
*/
"_fnDestroy": function ()
{
var i, iLen;
for ( i=0, iLen=this.s.dt.aoDrawCallback.length ; i<iLen ; i++ )
{
if ( this.s.dt.aoDrawCallback[i].sName === 'ColReorder_Pre' )
{
this.s.dt.aoDrawCallback.splice( i, 1 );
break;
}
}
$(this.s.dt.nTHead).find( '*' ).off( '.ColReorder' );
$.each( this.s.dt.aoColumns, function (i, column) {
$(column.nTh).removeAttr('data-column-index');
} );
this.s.dt._colReorder = null;
this.s = null;
},
/**
* Add a data attribute to the column headers, so we know the index of
* the row to be reordered. This allows fast detection of the index, and
* for this plug-in to work with FixedHeader which clones the nodes.
* @private
*/
"_fnSetColumnIndexes": function ()
{
$.each( this.s.dt.aoColumns, function (i, column) {
$(column.nTh).attr('data-column-index', i);
} );
}
};
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Static parameters
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/**
* ColReorder default settings for initialisation
* @namespace
* @static
*/
ColReorder.defaults = {
/**
* Predefined ordering for the columns that will be applied automatically
* on initialisation. If not specified then the order that the columns are
* found to be in the HTML is the order used.
* @type array
* @default null
* @static
* @example
* // Using the `oColReorder` option in the DataTables options object
* $('#example').dataTable( {
* "sDom": 'Rlfrtip',
* "oColReorder": {
* "aiOrder": [ 4, 3, 2, 1, 0 ]
* }
* } );
*
* @example
* // Using `new` constructor
* $('#example').dataTable()
*
* new $.fn.dataTable.ColReorder( '#example', {
* "aiOrder": [ 4, 3, 2, 1, 0 ]
* } );
*/
aiOrder: null,
/**
* Redraw the table's column ordering as the end user draws the column
* (`true`) or wait until the mouse is released (`false` - default). Note
* that this will perform a redraw on each reordering, which involves an
* Ajax request each time if you are using server-side processing in
* DataTables.
* @type boolean
* @default false
* @static
* @example
* // Using the `oColReorder` option in the DataTables options object
* $('#example').dataTable( {
* "sDom": 'Rlfrtip',
* "oColReorder": {
* "bRealtime": true
* }
* } );
*
* @example
* // Using `new` constructor
* $('#example').dataTable()
*
* new $.fn.dataTable.ColReorder( '#example', {
* "bRealtime": true
* } );
*/
bRealtime: false,
/**
* Indicate how many columns should be fixed in position (counting from the
* left). This will typically be 1 if used, but can be as high as you like.
* @type int
* @default 0
* @static
* @example
* // Using the `oColReorder` option in the DataTables options object
* $('#example').dataTable( {
* "sDom": 'Rlfrtip',
* "oColReorder": {
* "iFixedColumns": 1
* }
* } );
*
* @example
* // Using `new` constructor
* $('#example').dataTable()
*
* new $.fn.dataTable.ColReorder( '#example', {
* "iFixedColumns": 1
* } );
*/
iFixedColumns: 0,
/**
* As `iFixedColumnsRight` but counting from the right.
* @type int
* @default 0
* @static
* @example
* // Using the `oColReorder` option in the DataTables options object
* $('#example').dataTable( {
* "sDom": 'Rlfrtip',
* "oColReorder": {
* "iFixedColumnsRight": 1
* }
* } );
*
* @example
* // Using `new` constructor
* $('#example').dataTable()
*
* new $.fn.dataTable.ColReorder( '#example', {
* "iFixedColumnsRight": 1
* } );
*/
iFixedColumnsRight: 0,
/**
* Callback function that is fired when columns are reordered
* @type function():void
* @default null
* @static
* @example
* // Using the `oColReorder` option in the DataTables options object
* $('#example').dataTable( {
* "sDom": 'Rlfrtip',
* "oColReorder": {
* "fnReorderCallback": function () {
* alert( 'Columns reordered' );
* }
* }
* } );
*
* @example
* // Using `new` constructor
* $('#example').dataTable()
*
* new $.fn.dataTable.ColReorder( '#example', {
* "fnReorderCallback": function () {
* alert( 'Columns reordered' );
* }
* } );
*/
fnReorderCallback: null
};
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Constants
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/**
* ColReorder version
* @constant version
* @type String
* @default As code
*/
ColReorder.version = "1.1.3";
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* DataTables interfaces
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// Expose
$.fn.dataTable.ColReorder = ColReorder;
$.fn.DataTable.ColReorder = ColReorder;
// Register a new feature with DataTables
if ( typeof $.fn.dataTable == "function" &&
typeof $.fn.dataTableExt.fnVersionCheck == "function" &&
$.fn.dataTableExt.fnVersionCheck('1.9.3') )
{
$.fn.dataTableExt.aoFeatures.push( {
"fnInit": function( settings ) {
var table = settings.oInstance;
if ( ! settings._colReorder ) {
var dtInit = settings.oInit;
var opts = dtInit.colReorder || dtInit.oColReorder || {};
new ColReorder( settings, opts );
}
else {
table.oApi._fnLog( settings, 1, "ColReorder attempted to initialise twice. Ignoring second" );
}
return null; /* No node for DataTables to insert */
},
"cFeature": "R",
"sFeature": "ColReorder"
} );
}
else {
alert( "Warning: ColReorder requires DataTables 1.9.3 or greater - www.datatables.net/download");
}
// API augmentation
if ( $.fn.dataTable.Api ) {
$.fn.dataTable.Api.register( 'colReorder.reset()', function () {
return this.iterator( 'table', function ( ctx ) {
ctx._colReorder.fnReset();
} );
} );
$.fn.dataTable.Api.register( 'colReorder.order()', function ( set ) {
if ( set ) {
return this.iterator( 'table', function ( ctx ) {
ctx._colReorder.fnOrder( set );
} );
}
return this.context.length ?
this.context[0]._colReorder.fnOrder() :
null;
} );
}
return ColReorder;
}; // /factory
// 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.ColReorder ) {
// Otherwise simply initialise as normal, stopping multiple evaluation
factory( jQuery, jQuery.fn.dataTable );
}
})(window, document);