-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
197 lines (167 loc) · 6.71 KB
/
index.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
class tableResizable {
constructor (id, options) {
this._el = document.querySelector(`#${id}`);
// 实际使用中需要对dom结构进行判断,这里就不做了
this._tables = Array.from(this._el.querySelectorAll('table'));
setTimeout(() => this._resolveDom());
this.store = {
dragging: false, //是否拖动
draggingColumn: null, //拖动的对象
miniWidth: 30, //拖动的最小宽度
startMouseLeft: undefined, //鼠标点击时的clientX
startLeft: undefined, //th右离table的距离
startColumnLeft: undefined, //th左离table的距离
tableLeft: undefined, //table离页面左边的距离,
HColumns: [],
BColumns: [],
};
};
_saveCols(header, body) {
// cols
this.store.HColumns = Array.from(header.querySelectorAll('col')).map(v => ({
el: v,
isChange: false,
}));
this.store.BColumns = Array.from(body.querySelectorAll('col')).map(v => ({
el: v,
isChange: false,
}));
};
_resolveDom() {
const [ THeader ] = this._tables;
let TBody;
const Tr = THeader.tHead.rows[0];
const columns = Array.from(Tr.cells);
const Bcolgroup = document.createElement('colgroup');
const cols = columns.map((item, index) => {
const col = document.createElement('col');
item.dataset.index = index;
col.width = +item.offsetWidth;
return col;
});
cols.reduce((newDom, item) => {
newDom.appendChild(item);
return newDom;
}, Bcolgroup);
const HColgroup = Bcolgroup.cloneNode(true);
//不管是一个table还是两个,都把header合body提出来
if (this._tables.length === 1) {
const [ , tbody ] = Array.from(THeader.children);
tbody.remove();
var HFirstChild = THeader.firstChild;
THeader.insertBefore(HColgroup, HFirstChild);
TBody = THeader.cloneNode();
TBody.appendChild(Bcolgroup);
TBody.appendChild(tbody);
this._el.appendChild(TBody);
} else {
var HFirstChild = THeader.firstChild;
THeader.insertBefore(HColgroup, HFirstChild);
[ , TBody ] = this._tables;
var BFirstChild = TBody.firstChild;
TBody.insertBefore(Bcolgroup, BFirstChild);
}
//拖动时的占位线
const hold = document.createElement('div');
hold.classList.add('resizable-hold');
this._el.appendChild(hold);
// 把cols缓存起来
this._saveCols(THeader, TBody);
//处理事件
Tr.addEventListener('mousemove', this.handleMouseMove.bind(this));
Tr.addEventListener('mouseout', this.handleMouseOut.bind(this));
//处理拖动
const handleMouseDown = (evt) => {
if (this.store.draggingColumn) {
this.store.dragging = true;
let { target } = evt;
while (target && target.tagName !== 'TH') {
target = target.parentNode;
}
if (!target) return;
const tableEle = THeader;
const tableLeft = tableEle.getBoundingClientRect().left;
const columnRect = target.getBoundingClientRect();
const minLeft = columnRect.left - tableLeft + this.store.miniWidth;
target.classList.add('noclick');
this.store.startMouseLeft = evt.clientX;
this.store.startLeft = columnRect.right - tableLeft;
this.store.startColumnLeft = columnRect.left - tableLeft;
this.store.tableLeft = tableLeft;
document.onselectstart = () => false;
document.ondragstart = () => false;
hold.style.display = 'block';
hold.style.left = this.store.startLeft + 'px';
const handleOnMouseMove = (event) => {
const deltaLeft = event.clientX - this.store.startMouseLeft;
const proxyLeft = this.store.startLeft + deltaLeft;
hold.style.left = Math.max(minLeft, proxyLeft) + 'px';
};
// 宽度是这样分配的,举个🌰,如果a,b,c,d,他们每个都有个changed状态,默认false,拖过a,a.changed改为true,改变的宽度就由剩下的b,c,d平摊,如果都改变了,就让最后一个元素d背锅
const handleOnMouseUp = (event) => {
if (this.store.dragging) {
const { startColumnLeft } = this.store;
const finalLeft = parseInt(hold.style.left, 10);
const columnWidth = finalLeft - startColumnLeft;
const index = +target.dataset.index;
HColgroup.children[index].width = columnWidth;
if (index !== this.store.HColumns.length - 1) {
this.store.HColumns[index].isChange = true;
}
const deltaLeft = event.clientX - this.store.startMouseLeft;
const changeColumns = this.store.HColumns.filter((v, i) => i > index && !v.isChange && +v.el.width > 30);
changeColumns.forEach(item => {
item.el.width = +item.el.width - deltaLeft / changeColumns.length;
});
this.store.BColumns.forEach((item, i) => {
item.el.width = this.store.HColumns[i].el.width;
});
hold.style.display = 'none';
document.body.style.cursor = '';
this.store.dragging = false;
this.store.draggingColumn = null;
this.store.startMouseLeft = undefined;
this.store.startLeft = undefined;
this.store.startColumnLeft = undefined;
this.store.tableLeft = undefined;
}
document.removeEventListener('mousemove', handleOnMouseMove);
document.removeEventListener('mouseup', handleOnMouseUp);
document.onselectstart = null;
document.ondragstart = null;
setTimeout(() => {
target.classList.remove('noclick');
}, 0);
};
document.addEventListener('mouseup', handleOnMouseUp);
document.addEventListener('mousemove', handleOnMouseMove);
}
};
Tr.addEventListener('mousedown', handleMouseDown);
};
handleMouseMove(evt) {
let { target } = evt;
while (target && target.tagName !== 'TH') {
target = target.parentNode;
}
if (!target) return;
if (!this.store.dragging) {
const rect = target.getBoundingClientRect();
const bodyStyle = document.body.style;
if (rect.width > 12 && rect.right - evt.pageX < 8) {
bodyStyle.cursor = 'col-resize';
target.style.cursor = 'col-resize';
this.store.draggingColumn = target;
} else {
bodyStyle.cursor = '';
target.style.cursor = 'pointer';
this.store.draggingColumn = null;
}
}
};
handleMouseOut() {
document.body.style.cursor = '';
}
}
const table1 = new tableResizable('table-resizable1');
const table2 = new tableResizable('table-resizable2');