-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathresize_table.js
More file actions
60 lines (47 loc) · 1.86 KB
/
resize_table.js
File metadata and controls
60 lines (47 loc) · 1.86 KB
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
const table = document.getElementById('resize_table_row');
const cols = table.querySelectorAll('div.col-lg');
const createResizableColumn = function (col, resizer) {
// Track the current position of mouse
let x = 0;
let w = 0;
const mouseDownHandler = function (e) {
// Get the current mouse position
x = e.clientX;
// Calculate the current width of column
const styles = window.getComputedStyle(col);
w = parseInt(styles.width, 10);
// Attach listeners for document's events
document.addEventListener('mousemove', mouseMoveHandler);
document.addEventListener('mouseup', mouseUpHandler);
resizer.classList.add('resizing');
};
const mouseMoveHandler = function (e) {
// Determine how far the mouse has been moved
const dx = e.clientX - x;
// Update the width of column
col.style.width = `${w + dx}px`;
};
// When user releases the mouse, remove the existing event listeners
const mouseUpHandler = function () {
document.removeEventListener('mousemove', mouseMoveHandler);
document.removeEventListener('mouseup', mouseUpHandler);
resizer.classList.remove('resizing');
};
resizer.addEventListener('mousedown', mouseDownHandler);
};
const cols_len = cols.length;
[].forEach.call([].slice.call(cols,0,-1), function (col) {
col.style.width = `${100/cols_len}%`;
});
[].forEach.call([].slice.call(cols,0,-1), function (col) {
// Create a resizer element
const resizer = document.createElement('div');
resizer.classList.add('resizer');
// Set the height
//resizer.style.height = `${table.offsetHeight/2}px`;
resizer.style.height = "100%";
// Add a resizer element to the column
col.appendChild(resizer);
// Will be implemented in the next section
createResizableColumn(col, resizer);
});