forked from ckprod/table-drag-sort-resize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table-drag-sort-resize.js
923 lines (785 loc) · 32.5 KB
/
table-drag-sort-resize.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
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
;(function () {
"use strict";
// helper functions
function eventPageX(event) {
var pageX = event.pageX;
if (typeof pageX == 'undefined') {
var body = document.body;
var docElem = document.documentElement;
pageX = event.clientX + (docElem && docElem.scrollLeft || body && body.scrollLeft || 0) - (docElem && docElem.clientLeft || body && body.clientLeft || 0);
}
return pageX;
}
function elementStyleProperty(element, prop) {
if (window.getComputedStyle) {
return window.getComputedStyle(element, "").getPropertyValue(prop);
} else { // http://stackoverflow.com/questions/21797258/getcomputedstyle-like-javascript-function-for-ie8
var re = /(\-([a-z]){1})/g;
if (prop == 'float') prop = 'styleFloat';
if (re.test(prop)) {
prop = prop.replace(re, function () {
return arguments[2].toUpperCase();
});
}
return element.currentStyle[prop]
}
}
function numericProperty(prop) {
return (typeof prop == 'undefined' || prop == '' || prop == null) ? 0 : parseInt(prop);
}
function eventTarget (event) {
return event.target || event.srcElement;
}
// Natural Sort algorithm for Javascript - Version 0.7 - Released under MIT license
// Author: Jim Palmer (based on chunking idea from Dave Koelle)
// http://www.overset.com/2008/09/01/javascript-natural-sort-algorithm-with-unicode-support/
function naturalSort(a, b) {
var re = /(^-?[0-9]+(\.?[0-9]*)[df]?e?[0-9]?$|^0x[0-9a-f]+$|[0-9]+)/gi,
sre = /(^[ ]*|[ ]*$)/g,
dre = /(^([\w ]+,?[\w ]+)?[\w ]+,?[\w ]+\d+:\d+(:\d+)?[\w ]?|^\d{1,4}[\/\-]\d{1,4}[\/\-]\d{1,4}|^\w+, \w+ \d+, \d{4})/,
hre = /^0x[0-9a-f]+$/i,
ore = /^0/,
i = function (s) { return naturalSort.insensitive && ('' + s).toLowerCase() || '' + s; },
// convert all to strings strip whitespace
x = i(a).replace(sre, '') || '',
y = i(b).replace(sre, '') || '',
// chunk/tokenize
xN = x.replace(re, '\0$1\0').replace(/\0$/, '').replace(/^\0/, '').split('\0'),
yN = y.replace(re, '\0$1\0').replace(/\0$/, '').replace(/^\0/, '').split('\0'),
// numeric, hex or date detection
xD = parseInt(x.match(hre)) || (xN.length != 1 && x.match(dre) && Date.parse(x)),
yD = parseInt(y.match(hre)) || xD && y.match(dre) && Date.parse(y) || null,
oFxNcL, oFyNcL;
// first try and sort Hex codes or Dates
if (yD)
if (xD < yD) return -1;
else if (xD > yD) return 1;
// natural sorting through split numeric strings and default strings
for (var cLoc = 0, numS = Math.max(xN.length, yN.length) ; cLoc < numS; cLoc++) {
// find floats not starting with '0', string or 0 if not defined (Clint Priest)
oFxNcL = !(xN[cLoc] || '').match(ore) && parseFloat(xN[cLoc]) || xN[cLoc] || 0;
oFyNcL = !(yN[cLoc] || '').match(ore) && parseFloat(yN[cLoc]) || yN[cLoc] || 0;
// handle numeric vs string comparison - number < string - (Kyle Adams)
if (isNaN(oFxNcL) !== isNaN(oFyNcL)) { return (isNaN(oFxNcL)) ? 1 : -1; }
// rely on string comparison if different types - i.e. '02' < 2 != '02' < '2'
else if (typeof oFxNcL !== typeof oFyNcL) {
oFxNcL += '';
oFyNcL += '';
}
if (oFxNcL < oFyNcL) return -1;
if (oFxNcL > oFyNcL) return 1;
}
return 0;
}
function sort(cell, table) {
// store rows for sorting
var sortRows = [];
for (var i = 1; i < table.rows.length; i++) {
sortRows.push(table.rows[i]);
}
// sort
sortRows.sort(function (a, b) {
var x = a.cells[cell.cellIndex].textContent,
y = b.cells[cell.cellIndex].textContent;
return naturalSort(x, y);
});
if (hasClass(cell, 'sort-down')) {
cell.className = cell.className.replace(/ sort-down/, '');
cell.className += ' sort-up';
} else {
cell.className = cell.className.replace(/ sort-up/, '');
cell.className += ' sort-down';
}
// before we append should we reverse the new array or not?
if (hasClass(cell, 'sort-down')) {
sortRows.reverse();
}
for (i = 0; i < sortRows.length; i++) {
// appendChild(x) moves x if already present somewhere else in the DOM
table.tBodies[0].appendChild(sortRows[i]);
}
}
// https://github.com/tristen/tablesort/blob/gh-pages/src/tablesort.js
// line 280 - 282
var hasClass = function (el, c) {
return (' ' + el.className + ' ').indexOf(' ' + c + ' ') > -1;
}
// storage functions
// load state and returns the array
function loadState(key) {
var state = localStorage.getItem(key);
if (state != null) {
try {
state = JSON.parse(state);
} catch (e) {
state = new Array();
}
} else {
state = new Array();
}
return state;
}
function findIndex(state, searchId) {
//find element
for (var i = 0; i < state.length; i++) {
var id = state[i].id;
if (id == searchId) {
return i;
}
}
return -1;
}
function saveState(key, table /* name, prop*/) {
// ie in offline mode can't use localStorage,
// use alternative storage like
// https://github.com/andris9/simpleStorage
// or many more alternatives on
// https://github.com/Modernizr/Modernizr/wiki/HTML5-Cross-browser-Polyfills
if (!localStorage) {
console.log('localStorage not supported or not usable (i.e. ie in offline mode).');
return;
}
var state = loadState(key),
id = table.getAttribute('id'),
element = {id: id},
index = findIndex(state, id);
for (var i = 2; i < arguments.length; i+=2) {
element[arguments[i]] = arguments[i+1];
}
// place element
if (index < 0) {
state.push(element);
} else {
state.splice(index, 1, element);
}
localStorage.setItem(key, JSON.stringify(state));
}
function restoreState(key, table, name) {
var nc = table.rows[0].cells.length,
pm = new Array(nc);
for (var i = 0; i < nc; i++) {
pm[i] = i;
}
// ie in offline mode can't use localStorage,
// use alternative storage like
// https://github.com/andris9/simpleStorage
// or many more alternatives on
// https://github.com/Modernizr/Modernizr/wiki/HTML5-Cross-browser-Polyfills
if (!localStorage) {
console.log('localStorage not supported or not usable (i.e. ie in offline mode).');
return pm;
}
var state = loadState(key),
id = table.getAttribute('id'),
index = findIndex(state, id);
if (index >= 0) {
var element = state[index],
memory = element[name];
//check length
if (name == 'drag' || name == 'resize') {
var length = memory.length,
nc = table.rows[0].cells.length;
if (nc == length) {
if (name == 'drag') {
for (var i = 0; i < length; i++) {
var start = memory[i],
end = i;
pm.move(start, end);
if (pm[i] != start) moveTableColumn(table, start, end);
}
pm = memory;
} else if (name == 'resize') {
for (var i = 0; i < nc; i++) {
var cell = table.rows[0].cells[i];
cell.style.maxWidth = cell.style.width = memory[i];
}
}
}
} else if (name == 'sort') {
var cell = table.rows[0].cells[memory.index];
cell.className += ' ' + memory.order;
sort(cell, table);
}
}
return pm;
}
// Dragging (drag'n'drop) columns of html tables.
// https://github.com/irhc/mouse-handler
function MouseHandler () {
//this._mouseDownEvent
//this._mouseStarted
//this._mouseMoveDelegate
//this._mouseUpDelegate
}
MouseHandler.prototype = (function () {
// helper functions
// Cross browser event data based on
// jquery implementation
function getEvent(event) {
return event || window.event;
}
function eventWhich(event) {
return event.which || event.button;
}
function eventPageY(event) {
var pageY = event.pageY;
if (typeof pageY == 'undefined') {
var body = document.body;
var docElem = document.documentElement;
pageY = event.clientY + (docElem && docElem.scrollTop || body && body.scrollTop || 0) - (docElem && docElem.clientTop || body && body.clientTop || 0);
}
return pageY;
}
// prototype functions
function _mouseDown(event) {
// ie8 support
event = getEvent(event);
// we may have missed mouseup (out of window) - clean start, reset all
(this._mouseStarted && this._mouseUp(event));
// to compute the first (and the following) mouse move correctly
this._mouseDownEvent = event;
// the above line only works for ie>8, because _mouseDownEvent is a reference to the event
// so in ie8 you have two references (_mouseDownEvent and event) which points to the same object, the window.event
// to overcome this, you need a copy of the event e.g.
if (!event.which) { // detect ie8
var copy = {};
for (var attr in event) {
copy[attr] = event[attr];
}
this._mouseDownEvent = copy;
}
// only left mouse button down is of interest
// ie8 support
if (eventWhich(event) !== 1) {
return true;
}
// lets start and check distance first
if (this.options.distance == 0) {
this._mouseStarted = this._mousePrepareDrag(event) !== false;
if (!this._mouseStarted) {
// ie8 support
(event.preventDefault ? event.preventDefault() : (event.returnValue=false));
(event.stopPropagation ? event.stopPropagation() : (event.cancelBubble=true));
return true;
}
} else {
this._mousePrepareClick(event);
}
// to keep context
var _this = this;
this._mouseMoveDelegate = function (event) {
return _this._mouseMove(event);
};
this._mouseUpDelegate = function (event) {
return _this._mouseUp(event);
};
addEvent(document.body, 'mousemove', this._mouseMoveDelegate);
addEvent(document.body, 'mouseup', this._mouseUpDelegate);
// ie8 support
(event.preventDefault ? event.preventDefault() : (event.returnValue=false));
(event.stopPropagation ? event.stopPropagation() : (event.cancelBubble=true));
return true;
}
function _mouseMove(event) {
// ie8 support
event = getEvent(event);
// Iframe mouseup check - mouseup occurred in another document
if (!eventWhich(event)) {
return this._mouseUp(event);
}
// drag functionality
if (this._mouseStarted) {
this._mouseDrag(event);
// ie8 support
(event.preventDefault ? event.preventDefault() : (event.returnValue=false));
(event.stopPropagation ? event.stopPropagation() : (event.cancelBubble=true));
return false;
}
// check distance (no action circle)
if (this._mouseDistanceMet(event, this._mouseDownEvent)) {
// lets start
this._mouseStarted = (this._mousePrepareDrag(this._mouseDownEvent, event) !== false);
// and move or stop
(this._mouseStarted ? this._mouseDrag(event) : this._mouseUp(event));
}
// ie8 support
(event.preventDefault ? event.preventDefault() : (event.returnValue=false));
(event.stopPropagation ? event.stopPropagation() : (event.cancelBubble=true));
return !this.mouseStarted;
}
function _mouseUp(event) {
// ie8 support
event = getEvent(event);
removeEvent(document.body, 'mousemove', this._mouseMoveDelegate);
removeEvent(document.body, 'mouseup', this._mouseUpDelegate);
if (this._mouseStarted) {
this._mouseStarted = false;
this._mouseStopDrag(event);
} else {
this._mouseExecuteClick(event);
}
// ie8 support
(event.preventDefault ? event.preventDefault() : (event.returnValue=false));
(event.stopPropagation ? event.stopPropagation() : (event.cancelBubble=true));
return false;
}
function _mouseDistanceMet(newEvent, lastEvent) {
var x = Math.abs(eventPageX(lastEvent) - eventPageX(newEvent)),
y = Math.abs(eventPageY(lastEvent) - eventPageY(newEvent));
return (Math.sqrt(x*x + y*y)) >= this.options.distance;
}
// These are placeholder methods, to be overriden by extentions
function _mousePrepareClick() {}
function _mousePrepareDrag() {}
function _mouseDrag(event) {}
function _mouseExecuteClick() {}
function _mouseStopDrag() {}
return {
constructor: MouseHandler,
options: {
distance: 0
},
_mouseDown: _mouseDown,
_mouseMove: _mouseMove,
_mouseUp: _mouseUp,
_mouseDistanceMet: _mouseDistanceMet,
_mousePrepareClick: _mousePrepareClick,
_mousePrepareDrag: _mousePrepareDrag,
_mouseDrag: _mouseDrag,
_mouseExecuteClick: _mouseExecuteClick,
_mouseStopDrag: _mouseStopDrag
};
})();
// This simple and small javascript solution for dragging html tables
// is roughly based on
// http://akottr.github.io/dragtable/
// and
// http://www.danvk.org/wp/dragtable/
function DragSortHandler(table, options) {
//set default options
this.options.distance = 10;
this.options.restoreState = true;
// set options
var newOptions = {};
for (var opt in this.options)
newOptions[opt] = (typeof options[opt] == 'undefined') ? this.options[opt] : options[opt];
this.options = newOptions;
// table
this.table = table;
// header row
this.hr = table.rows[0];
// number of columns
this.nc = this.hr.cells.length;
// number of rows
this.nr = table.rows.length;
this._init();
}
(function () {
DragSortHandler.prototype = new MouseHandler();
DragSortHandler.prototype.constructor = DragSortHandler;
// helper functions
function tridentDetection() {
return (navigator.userAgent.indexOf("Trident") != -1) ? true : false;
};
function borderCollapseDetection(table) {
return elementStyleProperty(table, 'border-collapse') == 'collapse' ? true : false;
}
function getTableColumn(table, pageX, defaultColumn) {
var cells = table.rows[0].cells;
for (var i = 0; i < cells.length; i++) {
var tx = getOffsetRect(cells[i]).left;
if (tx <= pageX && pageX <= tx + cells[i].offsetWidth) {
return i;
}
}
return (typeof defaultColumn == 'undefined' ? -1 : defaultColumn);
}
function copyStyles(el) {
var cs = window.getComputedStyle ? window.getComputedStyle(el, null) : el.currentStyle,
css = '';
for (var i = 0; i < cs.length; i++) {
var style = cs[i];
css += style + ': ' + cs.getPropertyValue(style) + ';';
}
return css;
}
// public functions
DragSortHandler.prototype.refresh = function () {
if (typeof this.cell != 'undefined')
sort(this.cell, this.table);
};
// private functions
DragSortHandler.prototype._init = function () {
this.pm = new Array(this.nc);
for (var i = 0; i < this.nc; i++) {
this.pm[i] = i;
}
if (this.options.restoreState)
this.pm = restoreState('table-drag', this.table, 'drag');
};
// the overriden placeholder methods
DragSortHandler.prototype._mousePrepareDrag = function (event) {
var trident = tridentDetection(),
table = this.table,
borderCollapse = borderCollapseDetection(table),
tablePosition = getOffsetRect(table),
row = table.rows[0],
rowPosition = getOffsetRect(row),
rowOffsetHeight = row.offsetHeight,
tableClientLeft = trident ? (rowPosition.left - tablePosition.left) : table.clientLeft,
tableClientTop = trident ? (rowPosition.top - tablePosition.top) : table.clientTop,
backLeft = borderCollapse ? tablePosition.left : (tablePosition.left + tableClientLeft),
backTop = borderCollapse ? tablePosition.top : (rowPosition.top),
backWidth = borderCollapse ? table.offsetWidth : table.offsetWidth - 2 * tableClientLeft,
backHeight = table.rows[0].offsetHeight,
zIndex = numericProperty(table.style.zIndex),
zIndex = zIndex ? zIndex + 1 : 1,
initialColumn = eventTarget(event).parentNode.parentNode.cellIndex,
backgroundColor = elementStyleProperty(table, 'background-color');
// last column, initial column
this.lc = this.ic = initialColumn;
// overlay - back
var back = document.createElement("div");
back.style.position = 'absolute';
back.style.left = backLeft + 'px';
back.style.top = backTop + 'px';
back.style.width = backWidth + 'px';
back.style.height = backHeight + 'px';
back.style.backgroundColor = backgroundColor;
back.style.zIndex = zIndex;
// DEBUGGING
//back.style.opacity = 0.4;
//back.style.backgroundColor = 'green';
// overlay - front
for (var i = 0; i < this.nc; i++) {
var cell = row.cells[i],
cellPosition = getOffsetRect(cell),
offsetWidth = cell.offsetWidth,
offsetHeight = cell.offsetHeight,
clientWidth = cell.clientWidth,
clientHeight = cell.clientHeight,
clientLeft = cell.clientLeft,
clientTop = cell.clientTop,
clientRight = offsetWidth - clientWidth - clientLeft,
clientBottom = offsetHeight - clientHeight - clientTop,
paddingTop = numericProperty(elementStyleProperty(cell, 'padding-top')),
paddingBottom = numericProperty(elementStyleProperty(cell, 'padding-bottom')),
temp = cell.getBoundingClientRect(),
computedCellHeight = temp.bottom - temp.top - clientTop - clientBottom - paddingTop - paddingBottom,
borderLeftWidth = borderCollapse ? (clientRight + clientLeft) : clientLeft,
borderTopWidth = borderCollapse ? (clientTop + clientBottom) : clientTop,
borderRightWidth = borderCollapse ? (clientRight + clientLeft) : clientRight,
borderBottomWidth = borderCollapse ? (clientTop + clientBottom) : clientBottom,
elementBaseLeft = borderCollapse ? (cellPosition.left - backLeft - tableClientLeft) : cellPosition.left - backLeft,
elementBaseTop = borderCollapse ? (cellPosition.top - backTop - tableClientTop) : cellPosition.top - backTop,
elementBaseWidth = clientWidth + borderLeftWidth + borderRightWidth,
elementBaseHeight = rowOffsetHeight;
var element = document.createElement("div");
element.style.cssText = copyStyles(cell);
element.style.position = 'absolute';
element.style.left = 0;
element.style.top = 0;
element.style.height = computedCellHeight + 'px';
element.style.borderLeftWidth = borderLeftWidth + 'px';
element.style.borderTopWidth = borderTopWidth + 'px';
element.style.borderRightWidth = borderRightWidth + 'px';
element.style.borderBottomWidth = borderBottomWidth + 'px';
element.innerHTML = cell.innerHTML;
element.style.zIndex = zIndex + 3;
if (i == initialColumn) element.style.left = elementBaseLeft + 'px';
if (i == initialColumn) element.style.top = elementBaseTop + 'px';
var elementBase = document.createElement("div");
elementBase.style.position = 'absolute';
elementBase.style.left = elementBaseLeft + 'px';
elementBase.style.top = elementBaseTop + 'px';
elementBase.style.height = elementBaseHeight + 'px';
elementBase.style.width = elementBaseWidth + 'px';
elementBase.style.backgroundColor = 'white';
elementBase.style.zIndex = zIndex + 2;
if (i == initialColumn) elementBase.style.zIndex = zIndex + 1;
// DEBUGGING
//element.style.top = 50 + 'px';
//if (i == initialColumn) element.style.top = elementBaseTop + 75 + i*10 + 'px';
//elementBase.style.backgroundColor = 'green';
//elementBase.style.top = elementBaseTop + 75 + i*10 + 'px';
// drag element
if (i == initialColumn) this.de = element;
if (i != initialColumn) elementBase.appendChild(element);
back.appendChild(elementBase);
}
back.appendChild(this.de);
document.body.appendChild(back);
this.overlay = back;
// replace current document cursor
this.cur = document.body.style.cursor;
document.body.style.cursor = 'move';
return true;
};
DragSortHandler.prototype._mouseDrag = function (event) {
var distance = eventPageX(event) - eventPageX(this._mouseDownEvent),
table = this.table,
lastColumn = this.lc,
eventColumn = getTableColumn(table, eventPageX(event), lastColumn);
this.de.style.left = numericProperty(this.de.style.left) + distance + 'px';
if (eventColumn != lastColumn) { // bubble
var trident = tridentDetection(),
borderCollapse = borderCollapseDetection(table),
borderSpacing = borderCollapse ? 0 : numericProperty(elementStyleProperty(table, 'border-spacing')),
direction = sign(eventColumn - lastColumn);
for (var i = lastColumn; i != eventColumn; i += direction) {
var start = i,
end = start + direction,
shift = 0,
shift = (direction < 0 && start > this.ic) ? 1 : ((direction > 0 && start < this.ic) ? -1 : 0),
layerOne = this.overlay.childNodes[direction < 0 ? this.ic : (end + shift)],
layerTwo = this.overlay.childNodes[direction > 0 ? this.ic : (end + shift)],
borderLeftWidth = numericProperty(elementStyleProperty(direction < 0 ? layerTwo.childNodes[0] : this.de, 'border-left-width')),
borderLeftWidth = borderCollapse ? borderLeftWidth : 0,
left = numericProperty(layerTwo.style.left),
width = numericProperty(layerOne.style.width);
layerOne.style.left = left + 'px';
layerTwo.style.left = left + width + borderSpacing - borderLeftWidth + 'px';
// shift
this.pm.move(start, end);
// set new column
this.lc = end;
}
if (this.options.restoreState)
saveState('table-drag-sort-resize', this.table, 'drag', this.pm);
}
this._mouseDownEvent = event;
if (!event.which) { // detect ie8
var copy = {};
for (var attr in event) {
copy[attr] = event[attr];
}
this._mouseDownEvent = copy;
}
}
DragSortHandler.prototype._mouseExecuteClick = function (event) {
var index = 0,
cell = eventTarget(event).parentNode.parentNode;
for (var j = 0; j < this.nc; j++) {
var c = this.hr.cells[j];
if (c !== cell) {
if (hasClass(c, 'sort-up') || hasClass(c, 'sort-down')) {
c.className = c.className.replace(' sort-down', '')
.replace(' sort-up', '');
}
} else {
index = j;
}
}
this.cell = cell;
sort(cell, this.table);
if (this.options.restoreState)
saveState('table-drag-sort-resize', this.table, 'sort', {index: index, order: ((hasClass(cell, 'sort-down')) ? 'sort-up' : 'sort-down')});
}
DragSortHandler.prototype._mouseStopDrag = function (event) {
// remove overlay
document.body.removeChild(this.overlay);
// move column if neccessary
var table = this.table,
col = getTableColumn(table, eventPageX(event), this.lc);
if (col != this.ic)
moveTableColumn(table, this.ic, col);
// restore mouse cursor
document.body.style.cursor = this.cur;
};
})();
function ResizeHandler(table, options) {
//set default options
this.options.minWidth = 30;
this.options.restoreState = true;
this.options.fixed = false;
// set options
var newOptions = {};
for (var opt in this.options) {
newOptions[opt] = (typeof options[opt] == 'undefined') ? this.options[opt] : options[opt];
}
this.options = newOptions;
// table
this.table = table;
// header row
this.hr = table.rows[0];
// number of columns
this.nc = this.hr.cells.length;
// number of rows
this.nr = table.rows.length;
this._init();
}
(function () {
ResizeHandler.prototype = new MouseHandler();
ResizeHandler.prototype.constructor = ResizeHandler;
// private functions
ResizeHandler.prototype._init = function () {
for (var i = 0; i < this.nc; i++) {
var cell = this.hr.cells[i],
width = elementStyleProperty(cell, 'width'),
width = width == 'auto'?(cell.clientWidth-numericProperty(elementStyleProperty(cell, 'paddingLeft'))-numericProperty(elementStyleProperty(cell, 'paddingRight')))+'px':width; // ie8 support
cell.style.width = width;
}
if (this.options.restoreState)
restoreState('table-drag-sort-resize', this.table, 'resize');
};
// the overriden placeholder methods
ResizeHandler.prototype._mousePrepareDrag = function (event) {
// initial column
this.ic = eventTarget(event).parentNode.parentNode.cellIndex;
var initialColumn = this.ic,
fixed = this.options.fixed,
cell = [],
width = [];
for (var i = 0; i < 2; i++) {
cell[i] = this.hr.cells[initialColumn+(i?fixed:i)];
width[i] = numericProperty(cell[i].style.width);
}
for (var i = 0; i < this.nr; i++) {
for (var j = 0; j <= fixed; j++) {
cell = this.table.rows[i].cells[initialColumn+j];
cell.style.maxWidth = cell.style.width = width[j] + 'px';
}
}
// replace current document cursor
this.cur = document.body.style.cursor;
document.body.style.cursor = 'col-resize';
return true;
};
ResizeHandler.prototype._mouseDrag = function (event) {
var dist = eventPageX(event) - eventPageX(this._mouseDownEvent),
initialColumn = this.ic,
fixed = this.options.fixed,
cell = [],
width = [];
for (var i = 0; i < 2; i++) {
cell[i] = this.hr.cells[initialColumn+(i?fixed:i)];
width[i] = numericProperty(cell[i].style.width);
}
if (width[0] <= -dist || width[1] <= dist) {
this._mouseStopDrag(event);
} else {
var newWidth = [width[0] + dist, width[1] - dist];
if (newWidth[0] > this.options.minWidth && newWidth[1] > this.options.minWidth) {
for (var i = 0; i < this.nr; i++) {
for (var j = 0; j <= fixed; j++) {
cell = this.table.rows[i].cells[initialColumn+j];
cell.style.maxWidth = cell.style.width = newWidth[j] + 'px';
}
}
this._mouseDownEvent = event;
if (!event.which) { // detect ie8
var copy = {};
for (var attr in event) {
copy[attr] = event[attr];
}
this._mouseDownEvent = copy;
}
}
}
}
ResizeHandler.prototype._mouseStopDrag = function () {
var temp = new Array(this.nc);
for (var i = 0; i < this.nc; i++) {
var cell = this.hr.cells[i];
temp[i] = cell.style.width;
}
if (this.options.restoreState)
saveState('table-drag-sort-resize', this.table, 'resize', temp);
// restore mouse cursor
document.body.style.cursor = this.cur;
};
})();
function TableDragSortResize(table, options) {
// check input
if (table && table.tagName !== 'TABLE') {
console.log('ERROR: DOM element/input is not a table!');
return;
}
// check empty table
if (!(table && table.rows && table.rows.length > 0)) {
console.log('WARNING: Empty table.');
return;
}
options = options || {};
var dragSortHandler = new DragSortHandler(table, options);
var resizeHandler = new ResizeHandler(table, options);
// attach handlers to each cell of the header row.
for (var i = 0; i < ((options.fixed)?(dragSortHandler.nc-1):dragSortHandler.nc); i++) {
var cell = dragSortHandler.hr.cells[i];
// check and set space for sort order image
var paddingTop = numericProperty(elementStyleProperty(cell, 'padding-top'));
cell.style.paddingTop=(paddingTop>6?paddingTop:6)+'px';
cell.className += ' sort-header';
// add default cursor
cell.style.cursor = 'pointer';
addEvent(cell, 'mousedown', function (event) {
dragSortHandler._mouseDown(event);
});
cell.innerHTML = '<div class=\"resize-base\"><div class=\"resize-elem\"></div><div class=\"resize-text\">' + cell.innerHTML + '</div></div>';
addEvent(cell.childNodes[0].childNodes[0], 'mousedown', function (event) {
resizeHandler._mouseDown(event);
});
}
}
// export
if (typeof module !== 'undefined' && module.exports) {
module.exports = TableDragSortResize;
} else {
window.TableDragSortResize = TableDragSortResize;
}
// polyfills and public code snippets
// http://ejohn.org/apps/jselect/event.html
function addEvent(obj, type, fn) {
if (obj.attachEvent) {
obj['e' + type + fn] = fn;
obj[type + fn] = function () {
obj['e' + type + fn](window.event);
};
obj.attachEvent('on' + type, obj[type + fn]);
} else
obj.addEventListener(type, fn, false);
}
function removeEvent(obj, type, fn) {
if (obj.detachEvent) {
obj.detachEvent('on' + type, obj[type + fn]);
obj[type + fn] = null;
} else
obj.removeEventListener(type, fn, false);
}
// http://javascript.info/tutorial/coordinates
function getOffsetRect(elem) {
// (1)
var box = elem.getBoundingClientRect();
var body = document.body;
var docElem = document.documentElement;
// (2)
var scrollTop = window.pageYOffset || docElem.scrollTop || body.scrollTop;
var scrollLeft = window.pageXOffset || docElem.scrollLeft || body.scrollLeft;
// (3)
var clientTop = docElem.clientTop || body.clientTop || 0;
var clientLeft = docElem.clientLeft || body.clientLeft || 0;
// (4)
var top = box.top + scrollTop - clientTop;
var left = box.left + scrollLeft - clientLeft;
return { top: Math.round(top), left: Math.round(left) };
}
// based on
// https://groups.google.com/forum/#!msg/comp.lang.javascript/durZ17iSD0I/rnH2FqrvkooJ
function moveTableColumn(table, start, end) {
var row,
i = table.rows.length;
while (i--) {
row = table.rows[i];
var x = row.removeChild(row.cells[start]);
row.insertBefore(x, row.cells[end]);
}
}
// http://stackoverflow.com/questions/7624920/number-sign-in-javascript
function sign(x) {
return typeof x == 'number' ? x ? x < 0 ? -1 : 1 : x === x ? 0 : NaN : NaN;
}
// http://stackoverflow.com/questions/2440700/reordering-arrays
Array.prototype.move = function (from, to) {
this.splice(to, 0, this.splice(from, 1)[0]);
};
})();