forked from koosvanderkolk/DataTables.columnResizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColResize.js
524 lines (433 loc) · 14.6 KB
/
ColResize.js
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
/*
* File: ColResize.js
* Version: 0.5.1
* CVS: $Id$
* Description: Column resizing in DataTables
* Author: Koos van der Kolk, based on work of Allan Jardine (www.sprymedia.co.uk)
* Created: Fri 10 Feb 2012
* Language: Javascript
* License: GPL v2 or BSD 3 point style
* Project: DataTables
* Contact: koosvdkolk@gmail.com
*
* This source file is free software, under either the GPL v2 license or a
* BSD style license, available at:
* http://datatables.net/license_gpl2
* http://datatables.net/license_bsd
*
*/
(function($, window, document) {
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* DataTables plug-in API functions
*
* This are required by ColResize in order to perform the tasks required, and also keep this
* code portable, to be used for other column reordering projects with DataTables, if needed.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/**
* 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.fnColResize
* @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.fnColResize = function ( oSettings, iFrom, iTo )
{
var i, iLen, j, jLen, iCols=oSettings.aoColumns.length, nTrs, oCol;
/* Sanity check in the input */
if ( iFrom == iTo )
{
/* Pointless reorder */
return;
}
if ( iFrom < 0 || iFrom >= iCols )
{
this.oApi._fnLog( oSettings, 1, "ColResize 'from' index is out of bounds: "+iFrom );
return;
}
if ( iTo < 0 || iTo >= iCols )
{
this.oApi._fnLog( oSettings, 1, "ColResize 'to' index is out of bounds: "+iTo );
return;
}
};
/**
* ColResize provides column visiblity control for DataTables
* @class ColResize
* @constructor
* @param {object} DataTables object
* @param {object} ColResize options
*/
ColResize = function( oTable, oOpts )
{
this.oTable = oTable;
/* Santiy check that we are a new instance */
if ( !this.CLASS || this.CLASS != "ColResize" )
{
alert( "Warning: ColResize must be initialised with the keyword 'new'" );
}
if ( typeof oOpts == 'undefined' )
{
oOpts = {};
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Public class variables
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/**
* @namespace Settings object which contains customisable information for ColResize 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": oOpts,
/**
* Number of columns to fix (not allow to be reordered)
* @property fixed
* @type int
* @default 0
*/
"fixed": 0,
/**
* @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,
/**
* Resizing a column
* @property drag
* @type element
* @default null
*/
"resize": null,
/**
* The insert cursor
* @property pointer
* @type element
* @default null
*/
"pointer": null
};
/* Constructor logic */
this.s.dt = oTable.fnSettings();
this._fnConstruct();
/* Store the instance for later use */
ColResize.aoInstances.push( this );
return this;
};
ColResize.prototype = {
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Public methods
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
"fnReset": function ()
{
var a = [];
for ( var i=0, iLen=this.s.dt.aoColumns.length ; i<iLen ; i++ )
{
a.push( this.s.dt.aoColumns[i]._ColResize_iOrigCol );
}
this._fnOrderColumns( a );
},
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* 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 i, iLen;
/* Columns discounted from reordering - counting left to right */
if ( typeof this.s.init.iFixedColumns != 'undefined' )
{
this.s.fixed = this.s.init.iFixedColumns;
}
/* Add event handlers for the resize, and also mark the original column order */
for ( i=0, iLen=this.s.dt.aoColumns.length ; i<iLen ; i++ )
{
if ( i > this.s.fixed-1 )
{
this._fnMouseListener( i, this.s.dt.aoColumns[i].nTh );
}
}
/* State saving */
this.s.dt.aoStateSave.push( {
"fn": function (oS, sVal) {
return that._fnStateSave.call( that, sVal );
},
"sName": "ColResize_State"
} );
},
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* 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).bind( 'mousemove.ColResize', function (e) {
$(nTh).unbind('mousedown.ColResize');
if (that.dom.resize === null)
{
/* Store information about the mouse position */
var nThTarget = e.target.nodeName == "TH" ? e.target : $(e.target).parents('TH')[0];
var offset = $(nThTarget).offset();
var nInnerWidth = $(nThTarget).innerWidth();
var nOuterWidth = $(nThTarget).outerWidth();
/* are we on the col border (if so, resize col) */
if (e.pageX >= Math.round(offset.left + nInnerWidth) && e.pageX <= Math.round(offset.left + nOuterWidth))
{
that._fnSetElementCursor(nThTarget, true);
$(nTh).bind( 'mousedown.ColResize', function (e) {
/* call handler */
that._fnMouseDown.call( that, e, nTh );
return false;
} );
}else{
that._fnSetElementCursor(nThTarget, false);
}
}
else
{
that._fnSetElementCursor(nThTarget, false);
}
} );
},
/**
* Assigns the css cursor property to an element
* @param boolean bForceResizeMode If set to true, the element gets the cursor:col-resize value, if false: pointer
**/
"_fnSetElementCursor": function(element, bForceResizeMode){
if (!element) return;
var cursorStyle = (bForceResizeMode === undefined || bForceResizeMode === false) ? "pointer" : "col-resize";
$(element).css({
'cursor': cursorStyle
});
},
/**
* 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,
oTable = this.oTable,
aoColumns = this.s.dt.aoColumns;
nTh = $(nTh);
/* temporarily disable sorting, because click event trigger sorting */
oTable.fnSortOnOff( '_all', false );
/* store width of last column */
that.nLastTh = $(nTh).parent().children('th:last');
that.nLastThOriginalWidth = that.nLastTh.width();
/* store some variables */
this.s.mouse.startX = e.pageX;
this.s.mouse.startWidth = nTh.width();
this.s.mouse.resizeElem = nTh;
var nThNext = nTh.next();
this.s.mouse.nextStartWidth = nThNext.width();
that.dom.resize = true;
$(document).unbind( 'mousemove.ColResize');
$(document).unbind( 'mouseup.ColResize');
/* Add event handlers to the document */
$(document).bind( 'mousemove.ColResize', function (e) {
that._fnMouseMove.call( that, e );
} );
$(document).bind( 'mouseup.ColResize', function (e) {
/* enable sorting on click, which will be fired after mouseup */
$(document).one( 'click.ColResize', function (e) {
oTable.fnSortOnOff( '_all', true );
} );
/* set the column width in the table */
that._fnSetColumnWidth(nTh);
if (nTh.index()!== that.nLastTh.index()) {
that._fnSetColumnWidth(that.nLastTh);
}
/* prevent bubbling etc */
that._fnMouseUp.call( that, e );
e.preventDefault();
e.cancelBubble = true;
e.stopImmediatePropagation();
/* set cursor css */
that._fnSetElementCursor(nTh, false);
/* unbind handlers */
$(document).unbind( 'mousemove.ColResize');
$(document).unbind( 'mouseup.ColResize');
return false;
} );
},
"_fnSetColumnWidth" : function(nTh){
var oTable = this.oTable;
var aoColumns = this.s.dt.aoColumns;
var iColumnIndex = $(nTh).index();
/* set it in datatable settings and redraw table */
aoColumns[iColumnIndex].sWidth = nTh.width() + 'px';
$(oTable).dataTable().fnDraw();
},
/**
* 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;
/* are we resizing a column ? */
if (this.dom.resize) {
var nTh = this.s.mouse.resizeElem;
var nThNext = $(nTh).next();
var moveLength = e.pageX-this.s.mouse.startX;
if (moveLength != 0)
/* resize the column header */
$(nTh).width(this.s.mouse.startWidth + moveLength);
/* also resize the last column header if the column width is decreased */
if (moveLength<0 && that.nLastTh.index() !== jQuery(nTh).index() ) {
that.nLastTh.width(that.nLastThOriginalWidth+Math.abs(moveLength));
}
}
},
/**
* 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;
this.dom.resize = null;
return false;
}
};
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Static parameters
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/**
* Array of all ColResize instances for later reference
* @property ColResize.aoInstances
* @type array
* @default []
* @static
*/
ColResize.aoInstances = [];
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Constants
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/**
* Name of this class
* @constant CLASS
* @type String
* @default ColResize
*/
ColResize.prototype.CLASS = "ColResize";
/**
* ColResize version
* @constant VERSION
* @type String
* @default As code
*/
ColResize.VERSION = "0.5.1";
ColResize.prototype.VERSION = ColResize.VERSION;
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Initialisation
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*
* Register a new feature with DataTables
*/
if ( typeof $.fn.dataTable == "function" &&
typeof $.fn.dataTableExt.fnVersionCheck == "function" &&
$.fn.dataTableExt.fnVersionCheck('1.8.0') )
{
/*
* Turn On/Off Sorting capability
*
* @param {object} oSettings DataTables settings object
* @param {array} | {string} aiColumns Array of columns or string == '_all'
* @param {boolean} bOn True to enable, false to disable
*/
$.fn.dataTableExt.oApi.fnSortOnOff = function ( oSettings, aiColumns, bOn )
{
var cols = typeof aiColumns == 'string' && aiColumns == '_all' ? oSettings.aoColumns : aiColumns;
for ( var i = 0, len = cols.length; i < len; i++ ) {
oSettings.aoColumns[ i ].bSortable = bOn;
}
}
$.fn.dataTableExt.aoFeatures.push( {
"fnInit": function( oDTSettings ) {
var oTable = oDTSettings.oInstance;
if ( typeof oTable._oPluginColResize == 'undefined' ) {
var opts = typeof oDTSettings.oInit.oColResize != 'undefined' ?
oDTSettings.oInit.oColResize : {};
oTable._oPluginColResize = new ColResize( oDTSettings.oInstance, opts );
} else {
oTable.oApi._fnLog( oDTSettings, 1, "ColResize attempted to initialise twice. Ignoring second" );
}
return null; /* No node to insert */
},
"cFeature": "z",
"sFeature": "ColResize"
} );
}
else
{
alert( "Warning: ColResize requires DataTables 1.8.0 or greater - www.datatables.net/download");
}
})(jQuery, window, document);