-
Notifications
You must be signed in to change notification settings - Fork 27
/
jquery.dataTables.colResize.js
646 lines (591 loc) · 26.3 KB
/
jquery.dataTables.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
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
/**
* @summary ColResize
* @description Provide the ability to resize columns in a DataTable
* @version 1.6.1
* @file jquery.dataTables.colResize.js
* @author Daniel Hobi, Lado Tadic, Daniel Petras
* Language: Javascript
* License: MIT
*/
(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, $).$;
}
return factory($, root, root.document);
};
}
else {
// Browser
factory(jQuery, window, document);
}
}(function ($, window, document) {
'use strict';
function settingsFallback(userSetting, fallBackSetting) {
let resultObject = {};
for (let prop in fallBackSetting) {
if (!fallBackSetting.hasOwnProperty(prop)) {
continue;
}
if (userSetting.hasOwnProperty(prop)) {
let userObject = userSetting[prop];
if (typeof userObject === 'object') {
resultObject[prop] = settingsFallback(userObject, fallBackSetting[prop]);
} else {
resultObject[prop] = userObject;
}
} else {
resultObject[prop] = fallBackSetting[prop];
}
}
return resultObject;
}
let DataTable = $.fn.dataTable;
/**
* ColResize provides column resizable control for DataTables
* @class ColResize
* @constructor
* @param {object} dt DataTables settings object
* @param {object} opts ColResize options
*/
let ColResize = function (dt, opts) {
opts = settingsFallback(opts || {}, ColResize.defaults);
let settings = new $.fn.dataTable.Api(dt).settings()[0];
dt = settings;
// Ensure that we can't initialise on the same table twice
if (settings._colResize) {
return settings._colResize;
}
// Convert from camelCase to Hungarian, just as DataTables does
let camelToHungarian = $.fn.dataTable.camelToHungarian;
if (camelToHungarian) {
camelToHungarian(ColResize.defaults, ColResize.defaults, true);
camelToHungarian(ColResize.defaults, opts || {});
}
this.s = {
dt: dt,
state: {
isDragging: false,
startX: 0,
originalTableWidth: 0,
originalWidth: [],
minWidth: 0,
maxWidth: 0,
$element: null,
column: null,
minBoundAllowClass: true,
maxBoundAllowClass: true,
isLastColumnDragging: false,
maxTableWidth: 0,
},
opts: opts
};
this.s.dt._colResize = this;
if (this.s.opts.isEnabled) {
this._fnConstruct();
}
return this;
};
$.extend(ColResize.prototype, {
fnEnable: function () {
if (this.isEnabled) {
this.s.dt.oInstance.oApi._fnLog(this.dt, 1, "ColResize: attempted to enable again. Ignoring.");
return;
}
this._fnConstruct();
},
fnReset: function () {
let self = this;
this._fnGetAllColumns().forEach(function (column) {
if (column.sWidth == null) {
return;
}
let widthResult = column.sWidth.match(/(\d+)/i),
oldWidth = widthResult != null ? parseInt(widthResult[0]) : 0,
newWidthResult = column._sResizableWidth.match(/(\d+)/i),
newWidth = newWidthResult != null ? parseInt(newWidthResult[0]) : 0,
$node = $(column.nTh);
self.s.state.originalWidth[$node.index()] = oldWidth;
column.width = column._sResizableWidth;
column.sWidth = column._sResizableWidth;
self._fnApplyWidth(newWidth - oldWidth, $node, column);
});
this.s.opts.onResizeEnd(null, this._fnGetAllColumns().map(this._fnMapColumn));
},
fnRestoreState: function () {
let self = this,
sizeMap = this.s.opts.stateLoadCallback(this.s.opts),
cols = this._fnGetAllColumns();
if (sizeMap == null) return;
if (sizeMap.length !== cols.length) {
this.s.dt.oInstance.oApi._fnLog(this.dt, 1, "ColResize: Array size doesn't match number of columns.");
return;
}
self.s.state.maxTableWidth = self._fnGetBodyScroll().length > 0 ? 0 : this._fnGetTable().width();
self.s.state.originalTableWidth = this._fnGetTable().width();
cols.forEach(function (column) {
if (column.sWidth == null) {
return;
}
let widthResult = column.sWidth.match(/(\d+)/i),
oldWidth = widthResult != null ? parseInt(widthResult[0]) : 0,
newWidth = sizeMap[column.idx],
$node = $(column.nTh);
self.s.state.originalWidth[$node.index()] = oldWidth;
column.width = newWidth + 'px';
column.sWidth = newWidth + 'px';
self._fnApplyWidth(newWidth - oldWidth, $node, column);
});
this.s.opts.onResizeEnd(null, this._fnGetAllColumns().map(this._fnMapColumn));
},
fnSaveState: function () {
let sizeMap = [];
this._fnGetAllColumns().forEach(function (column) {
let oldWidth = column.nTh.offsetWidth;
sizeMap[column.idx] = oldWidth;
});
this.s.opts.stateSaveCallback(this.s.opts, sizeMap);
},
fnDisable: function () {
if (!this.isEnabled) {
this.s.dt.oInstance.oApi._fnLog(this.dt, 1, "ColResize: attempted to disable again. Ignoring.");
return;
}
$(document).off('.ColResize');
this._fnGetAllColumns().forEach(function (column) {
let $columnNode = $(column.nTh);
$columnNode.off('.ColResize');
$columnNode.removeAttr('data-is-resizable');
});
this.isEnabled = false;
},
/**
* Constructor logic
* @method _fnConstruct
* @returns void
* @private
*/
_fnConstruct: function () {
let self = this;
// register document events
$(document).on('mousemove.ColResize touchmove.ColResize', function (e) {
if (self.s.state.isDragging) {
let changedWidth = self._fnGetXCoords(e) - self.s.state.startX;
self._fnApplyWidth(changedWidth, self.s.state.$element, self.s.state.column);
self.s.opts.onResize(self._fnMapColumn(self.s.state.column));
//scroll if the last element gets resized
if (self.s.state.isLastColumnDragging) {
let $scrollBody = self._fnGetBodyScroll();
if ($scrollBody.length > 0) {
$scrollBody[0].scrollLeft = $scrollBody[0].scrollWidth;
}
}
// do not outgrow table if not scrollable
if (self.s.state.maxTableWidth > 0) {
let currentTableWidth = self.s.state.$element.closest('table').width();
if (currentTableWidth > self.s.state.maxTableWidth) {
self._fnApplyWidth(changedWidth + (self.s.state.maxTableWidth - currentTableWidth, self.s.state.$element, self.s.state.column));
self._fnShowMaxBoundReached();
}
}
}
});
$(document).on('mouseup.ColResize touchend.ColResize', function () {
if (self.s.state.isDragging) {
// workaround to prevent sorting on column click
setTimeout(function () {
//disable sorting
self._fnGetAllColumns().forEach(function (column) {
column.bSortable = column._bSortableTempHolder;
});
}, 100);
// callback
let mappedColumns = self._fnGetAllColumns().map(self._fnMapColumn);
self.s.opts.onResizeEnd(self._fnMapColumn(self.s.state.column), mappedColumns);
if (self.s.opts.saveState) {
self.fnSaveState();
}
}
self._fnGetAllColumns().forEach(function (column) {
$(column.nTh).removeClass(self.s.opts.hoverClass);
});
self.s.state.isDragging = false;
});
//register column events
this._fnGetAllColumns().forEach(function (column) {
let $columnNode = $(column.nTh);
let isResizable = self._fnIsColumnResizable(column);
$columnNode.attr('data-is-resizable', isResizable.toString());
//save the original value (server) somewhere, we want the size of all of them.
column._sResizableWidth = column.sWidth;
if (isResizable) {
$columnNode.on('mousemove.ColResize touchmove.ColResize', function (e) {
let $node = $(e.currentTarget);
if (self._fnIsInDragArea($node, e)) {
$node.addClass(self.s.opts.hoverClass);
} else {
if (!self.s.state.isDragging) {
$node.removeClass(self.s.opts.hoverClass);
}
}
});
$columnNode.on('mouseout.ColResize', function (e) {
if (!self.s.state.isDragging) {
let $node = $(e.currentTarget);
$node.removeClass(self.s.opts.hoverClass);
}
});
$columnNode.on('mousedown.ColResize touchstart.ColResize', function (e) {
let $node = $(e.currentTarget);
if (self._fnIsInDragArea($node, e)) {
//disable sorting
self._fnGetAllColumns().forEach(function (column) {
column._bSortableTempHolder = column.bSortable;
column.bSortable = false;
self._fnRemovePercentWidths(column, $(column.nTh));
});
self.s.state.isDragging = true;
self.s.state.startX = self._fnGetXCoords(e);
self.s.state.maxTableWidth = self._fnGetBodyScroll().length > 0 ? 0 : $node.closest('table').width();
self.s.state.originalTableWidth = $node.closest('table').width();
self.s.state.originalWidth[$node.index()] = self._fnGetCurrentWidth($node);
self.s.state.minWidth = self._fnGetMinWidthOf($node);
self.s.state.maxWidth = self._fnGetMaxWidthOf($node);
self.s.state.minBoundAllowClass = true;
self.s.state.maxBoundAllowClass = true;
self.s.state.$element = $node;
self.s.state.column = column;
self.s.state.isLastColumnDragging = self._fnIsLastResizableColumnDragging(column);
self.s.opts.onResizeStart(null, self._fnGetAllColumns().map(self._fnMapColumn));
}
});
}
});
this.isEnabled = true;
if (this.s.opts.saveState) {
this.fnRestoreState();
}
},
_fnGetAllColumns: function () {
return this.s.dt.aoColumns;
},
_fnGetBodyScroll: function () {
return $(this.s.dt.nScrollBody);
},
_fnGetTable: function () {
return $(this.s.dt.nTable);
},
_fnRemovePercentWidths: function (column, $node) {
if ($node.attr('style') && $node.attr('style').indexOf('%') !== -1) {
this.s.dt.oInstance.oApi._fnLog(this.dt, 1, "ColResize: column styles in percentages is not supported, trying to convert to px on the fly.");
let width = $node.width();
$node.removeAttr('style');
column.sWidth = width + 'px';
column.width = width + 'px';
$node.width(width);
} else {
$node.width($node.width());
}
},
_fnIsInDragArea: function ($th, e) {
let rightSide = $th.offset().left + $th.outerWidth();
let xCoord = this._fnGetXCoords(e);
return (rightSide + 10) > xCoord && (rightSide - 10) < xCoord;
},
_fnGetXCoords: function (e) {
return e.type.indexOf('touch') !== -1 ? e.originalEvent.touches[0].pageX : e.pageX;
},
_fnApplyWidth: function (changedWidth, element, column) {
let self = this;
//keep inside bounds by manipulating changedWidth if any
changedWidth = this.s.opts.hasBoundCheck ? this._fnBoundCheck(changedWidth, element) : changedWidth;
//apply widths
let thWidth = this.s.state.originalWidth[element.index()] + changedWidth;
this._fnApplyWidthColumn(column, thWidth);
//change table size
let $table = element.closest('table');
let shouldChangeTableWidth = element.closest('.dataTables_scroll').length > 0 &&
($table.width() + changedWidth) > element.closest('.dataTables_scroll').width();
if (shouldChangeTableWidth) {
$table.width(self.s.state.originalTableWidth + changedWidth);
}
// possible body table
let scrollBodyTh = element.closest('.dataTables_scroll').find('.dataTables_scrollBody table th:nth-child(' + (element.index() + 1) + ')');
scrollBodyTh.outerWidth((thWidth) + 'px');
let $bodyTable = scrollBodyTh.closest('table');
$bodyTable.width($table.width());
// possible footer table
let scrollFooterTh = element.closest('.dataTables_scroll').find('.dataTables_scrollFoot table th:nth-child(' + (element.index() + 1) + ')');
scrollFooterTh.outerWidth((thWidth) + 'px');
let $footerTable = scrollFooterTh.closest('table');
$footerTable.width($table.width());
// HTML table can force columns to be wider than max-width and smaller than min-width. Overwrite style properties to look the same as the header
if (element.closest('.dataTables_scroll').length > 0) {
let additionalStylesForHiddenThRows = ';padding-top: 0px;padding-bottom: 0px;border-top-width: 0px;border-bottom-width: 0px;height: 0px;';
this._fnGetAllColumns().forEach(function (column) {
let $hbTh = $(column.nTh);
let currentIndex = $hbTh.index();
let currentStyles = $hbTh.attr('style') + additionalStylesForHiddenThRows;
//body table
let $sbTh = element.closest('.dataTables_scroll').find('.dataTables_scrollBody table th:nth-child(' + (currentIndex + 1) + ')');
$sbTh.attr('style', currentStyles);
//footer table
let $sfTh = element.closest('.dataTables_scroll').find('.dataTables_scrollFoot table th:nth-child(' + (currentIndex + 1) + ')');
$sfTh.attr('style', currentStyles);
});
}
},
_fnApplyWidthColumn: function (column, width) {
$(column.nTh).outerWidth(width + 'px');
column.sWidth = width + 'px';
},
_fnGetCurrentWidth: function ($node) {
let possibleWidths = $node.attr('style').split(';').map(function (cssPart) {
return cssPart.trim();
})
.filter(function (cssPart) {
return cssPart !== '';
})
.map(function (cssPart) {
let widthResult = cssPart.match(/^width: (\d+)px/i);
return widthResult != null ? parseInt(widthResult[1]) : 0;
})
.filter(function (possibleWidth) {
return !isNaN(possibleWidth) && possibleWidth > 0;
});
if (possibleWidths.length > 0) {
return possibleWidths[0];
}
return $node.width();
},
_fnGetMinWidthOf: function ($node) {
if (this.s.opts.getMinWidthOf != null) {
return this.s.opts.getMinWidthOf($node);
}
let minWidthFromCss = this._fnGetWidthOfValue($node.css('min-width'));
if (!isNaN(minWidthFromCss) && minWidthFromCss > 0) {
return minWidthFromCss;
}
//try to guess
let $hiddenElement = $node.clone().css({
left: -10000,
top: -10000,
position: 'absolute',
display: 'inline',
visibility: 'visible',
width: 'auto',
fontFamily: $node.css('font-family'),
fontSize: $node.css('font-size'),
padding: $node.css('padding')
}).appendTo('body');
let minWidth = parseInt($hiddenElement.width());
$hiddenElement.remove();
if (!$node.hasClass('sorting_disabled')) {
minWidth += 20; //sortable column needs a bit more space for the icon
}
return minWidth < 30 ? 30 : minWidth;
},
_fnGetMaxWidthOf: function ($node) {
return this._fnGetWidthOfValue($node.css('max-width'));
},
_fnGetWidthOfValue: function (widthStr) {
if (widthStr === 'none') {
return -1;
}
return parseInt(widthStr.match(/(\d+)px/ig));
},
_fnBoundCheck: function (changedWidth, element) {
let thWishWidth = (typeof this.s.state.originalWidth[element.index()] != 'undefined' ? this.s.state.originalWidth[element.index()] : this._fnGetCurrentWidth(element)) + changedWidth;
//min bound
if (this.s.state.minWidth !== -1 && thWishWidth < this.s.state.minWidth) {
let addBackToMinWidth = this.s.state.minWidth - thWishWidth;
changedWidth += addBackToMinWidth;
this._fnShowMinBoundReached();
} else {
this.s.state.minBoundAllowClass = true;
}
//max bound
if (this.s.state.maxWidth !== -1 && thWishWidth > this.s.state.maxWidth) {
let substractFromMaxWidth = thWishWidth - this.s.state.maxWidth;
changedWidth -= substractFromMaxWidth;
this._fnShowMaxBoundReached();
} else {
this.s.state.maxBoundAllowClass = true;
}
return changedWidth;
},
_fnShowMinBoundReached: function () {
let self = this;
if (this.s.state.minBoundAllowClass && this.s.state.$element) {
this.s.state.$element.addClass(this.s.opts.minBoundClass);
let $currentElement = this.s.state.$element;
setTimeout(function () {
$currentElement.removeClass(self.s.opts.minBoundClass);
}, 500);
this.s.state.minBoundAllowClass = false;
}
},
_fnShowMaxBoundReached: function () {
let self = this;
if (this.s.state.maxBoundAllowClass && this.s.state.$element) {
this.s.state.$element.addClass(this.s.opts.maxBoundClass);
let $currentElement = this.s.state.$element;
setTimeout(function () {
$currentElement.removeClass(self.s.opts.maxBoundClass);
}, 500);
this.s.state.maxBoundAllowClass = false;
}
},
_fnMapColumn: function (column) {
return { idx: column.idx, width: column.sWidth };
},
_fnIsLastResizableColumnDragging: function (draggingColumn) {
let visibleColumns = this._fnGetAllColumns().filter(function (column) {
return $(column.nTh).is(':visible');
});
let indexOfColumn = visibleColumns.indexOf(draggingColumn);
if (indexOfColumn === visibleColumns.length - 1) {
return true;
}
for (let counter = indexOfColumn + 1; counter < visibleColumns.length; counter++) {
let column = visibleColumns[counter];
if (this._fnIsColumnResizable(column)) {
return false;
}
}
return true;
},
_fnIsColumnResizable: function (column) {
return this.s.opts.isResizable(column);
}
});
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Static parameters
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/**
* ColResize default settings for initialisation
* @namespace
* @static
*/
ColResize.defaults = {
isEnabled: true,
hoverClass: 'dt-colresizable-hover',
hasBoundCheck: true,
minBoundClass: 'dt-colresizable-bound-min',
maxBoundClass: 'dt-colresizable-bound-max',
saveState: false,
isResizable: function (column) {
if (typeof column.isResizable === 'undefined') {
return true;
}
return column.isResizable;
},
onResizeStart: function (column, columns) {
},
onResize: function (column) {
},
onResizeEnd: function (column, columns) {
},
stateSaveCallback: function (settings, data) {
let stateStorageName = window.location.pathname + "/colResizeStateData";
localStorage.setItem(stateStorageName, JSON.stringify(data));
},
stateLoadCallback: function (settings) {
let stateStorageName = window.location.pathname + "/colResizeStateData",
data = localStorage.getItem(stateStorageName);
return data != null ? JSON.parse(data) : null;
},
getMinWidthOf: null
};
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Constants
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/**
* ColResize version
* @constant version
* @type String
* @default As code
*/
ColResize.version = "1.7.0";
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* DataTables interfaces
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// Expose
$.fn.dataTable.ColResize = ColResize;
$.fn.DataTable.ColResize = ColResize;
// Register a new feature with DataTables
if (typeof $.fn.dataTable == "function" &&
typeof $.fn.dataTableExt.fnVersionCheck == "function" &&
$.fn.dataTableExt.fnVersionCheck('1.10.8')) {
$.fn.dataTableExt.aoFeatures.push({
"fnInit": function (settings) {
let table = settings.oInstance;
if (!settings._colResize) {
let init = settings.oInit.colResize;
let opts = $.extend({}, init, DataTable.defaults.colResize);
new ColResize(settings, opts);
}
else {
table.oApi._fnLog(settings, 1, "ColResize: attempted to initialise twice. Ignoring second");
}
return null; /* No node for DataTables to insert */
},
"sFeature": "ColResize"
});
}
else {
alert("Warning: ColResize requires DataTables 1.10.8 or greater - www.datatables.net/download");
}
// Attach a listener to the document which listens for DataTables initialisation
// events so we can automatically initialise
$(document).on('preInit.dt.colResize', function (e, settings) {
if (e.namespace !== 'dt') {
return;
}
let init = settings.oInit.colResize;
let defaults = DataTable.defaults.colResize;
if (init || defaults) {
let opts = $.extend({}, init, defaults);
if (init !== false) {
new ColResize(settings, opts);
}
}
});
// API augmentation
$.fn.dataTable.Api.register('colResize.enable()', function () {
return this.iterator('table', function (ctx) {
ctx._colResize.fnEnable();
});
});
$.fn.dataTable.Api.register('colResize.disable()', function () {
return this.iterator('table', function (ctx) {
ctx._colResize.fnDisable();
});
});
$.fn.dataTable.Api.register('colResize.reset()', function () {
return this.iterator('table', function (ctx) {
ctx._colResize.fnReset();
});
});
$.fn.dataTable.Api.register('colResize.save()', function () {
return this.iterator('table', function (ctx) {
ctx._colResize.fnSaveState();
});
});
$.fn.dataTable.Api.register('colResize.restore()', function () {
return this.iterator('table', function (ctx) {
ctx._colResize.fnRestoreState();
});
});
}));