This repository has been archived by the owner on Sep 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
columnDragMixin.js
116 lines (107 loc) · 4.31 KB
/
columnDragMixin.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
/*jslint browser: true, plusplus: true, unparam: true, vars: true */
"use strict";
/**
* Column Drag-and-Drop
*/
var columnDragMixin = {
dragColumn: null,
handleTHDragStart: function (e) {
if (!this.disableColumnMove && !(this.rowTemplate || this.rowEditorTemplate)) {
this.dragColumn = {
th: e.target,
column: e.target.templateInstance.model.column,
entered: null,
index: this.columns.indexOf(e.target.templateInstance.model.column)
};
e.target.classList.add('column-in-move');
e.dataTransfer.effectAllowed = 'move';
e.dataTransfer.setData('application/sortable-table-column-name', this.dragColumn.index);
}
},
handleTHDragOver: function (e) {
if (e.preventDefault) { e.preventDefault(); }// Necessary. Allows us to drop.
e.dataTransfer.dropEffect = 'move';
this.handleTHDragEnter(e);
return false;
},
findTHDropSide: function (e, self) {
var th = e.target;
var index = self.columns.indexOf(th.templateInstance.model.column);
if ((index < self.dragColumn.index - 1) || (index > self.dragColumn.index + 1)) {
if (th.getBoundingClientRect) {
var center = th.clientWidth / 2 + th.getBoundingClientRect().left;
return (center >= e.clientX) ? -1 : 1;
}
return 0;
}
if (index === self.dragColumn.index - 1) {
return -1;
}
if (index === self.dragColumn.index + 1) {
return 1;
}
},
handleTHDragEnter: function (e) {
if (this.dragColumn) {
var th = e.target;
this.dragColumn.entered = null;
while (th.tagName !== 'TH' && th.parentElement) {
th = th.parentElement;
if (th.tagName === 'TH') { this.dragColumn.entered = th; }
}
var dragSide = this.findTHDropSide(e, this);
if (dragSide < 0) {
th.classList.remove('column-move-right');
th.classList.add('column-move-left');
} else if (dragSide > 0) {
th.classList.remove('column-move-left');
th.classList.add('column-move-right');
}
[].forEach.call(this.shadowRoot.querySelectorAll('th'), function (element) {
if (element !== th) {
element.classList.remove('column-move-left');
element.classList.remove('column-move-right');
}
});
}
},
handleTHDragLeave: function (e) {
if (this.dragColumn) {
var th = e.target;
if (this.dragColumn.entered === th) {
this.dragColumn.entered = null;
} else if (th.classList) {
if (th.classList.contains('column-move-left')) { th.classList.remove('column-move-left'); }
if (th.classList.contains('column-move-right')) { th.classList.remove('column-move-right'); }
}
}
},
handleTHDrop: function (e) {
if (e.stopPropagation) { e.stopPropagation(); } // stops the browser from redirecting.
if (this.dragColumn && this.dragColumn.th.parentNode === e.target.parentNode) {
var newIndex = this.columns.indexOf(e.target.templateInstance.model.column);
var oldIndex = +e.dataTransfer.getData('application/sortable-table-column-name');
var dragSide = this.findTHDropSide(e, this);
if (newIndex < oldIndex && dragSide === 1) {
newIndex++;
} else if (newIndex > oldIndex && dragSide === -1) {
newIndex--;
}
if (newIndex !== oldIndex) {
var b = this.columns.splice(oldIndex, 1);
this.columns.splice(newIndex, 0, b[0]);
}
}
return false;
},
handleTHDragEnd: function () {
if (this.dragColumn) {
this.dragColumn.th.classList.remove('column-in-move');
[].forEach.call(this.shadowRoot.querySelectorAll('th'), function (element) {
element.classList.remove('column-move-left');
element.classList.remove('column-move-right');
});
}
this.dragColumn = null;
}
};