-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsplitterbar.js
133 lines (100 loc) · 4.57 KB
/
splitterbar.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
const SplitterBar = function(container, leftContent, rightContent) {
// We want two divs that we're dividing
const leftSide = document.createElement('div');
const rightSide = document.createElement('div');
const splitter = document.createElement('div');
leftSide.classList.add('leftSide');
rightSide.classList.add('rightSide');
splitter.classList.add('splitter');
if (leftContent !== null) {
leftSide.appendChild(leftContent);
}
if (rightContent !== null) {
rightSide.appendChild(rightContent);
}
container.appendChild(splitter);
splitter.style.width = '10px';
// splitter.style.left = splitter.parentElement.offsetWidth / 2 - (splitter.offsetWidth / 2) + 'px';
splitter.style.left = '50%';
splitter.style.transform = 'translateX(-50%)';
// leftSide.style.background = 'red';
// rightSide.style.background = 'blue';
splitter.style.background = 'black';
leftSide.style.left = 0;
leftSide.style.top = 0;
// leftSide.style.width = splitter.style.left;
leftSide.style.width = splitter.offsetLeft - splitter.offsetWidth / 2 + 'px';
console.log('splitter offset left plus width is ', splitter.offsetLeft + splitter.offsetWidth);
rightSide.style.left = (splitter.offsetLeft + splitter.offsetWidth / 2) + 'px';
rightSide.style.top = 0;
rightSide.style.width = container.offsetWidth - splitter.offsetLeft - 10 + 'px';
container.appendChild(leftSide);
container.appendChild(rightSide);
console.log('left of right pane is ', rightSide.offsetLeft)
let mouseIsDown = false;
let startX = null;
let globalXCoordinate = null;
// Will not touch
splitter.addEventListener('mousedown', function(evt) {
evt.preventDefault();
mouseIsDown = true;
startX = evt.offsetX;
startY = evt.offsetY;
});
leftSide.addEventListener('mousemove', function(evt) {
evt.preventDefault();
let left = this.offsetLeft;
globalXCoordinate = left + evt.offsetX - startX;
});
rightSide.addEventListener('mousemove', function(evt) {
evt.preventDefault();
let left = this.offsetLeft;
globalXCoordinate = left + evt.offsetX - startX;
});
splitter.addEventListener('mousemove', function(evt) {
evt.preventDefault();
let left = this.offsetLeft;
globalXCoordinate = left + evt.offsetX - startX;
});
document.body.addEventListener('mouseup', function(evt) {
mouseIsDown = false;
});
document.addEventListener('mouseup', function(evt) {
mouseIsDown = false;
});
document.addEventListener('mousemove', function(evt) {
evt.preventDefault();
evt.stopPropagation();
let containerWidth = container.getBoundingClientRect().width;
let hoveringOnDocument = evt.target.nodeName == 'HTML' || evt.target.nodeName == 'BODY';
let docX = evt.offsetX - container.getBoundingClientRect().x - startX;
if (mouseIsDown) {
console.log(splitter.offsetWidth);
// When dragging what do we need to do to take care of inner splitter areas?
if (hoveringOnDocument) {
if (docX < 0) {
docX = 0;
}
if (docX + splitter.offsetWidth > container.offsetWidth) {
docX = containerWidth - splitter.offsetWidth;
}
splitter.style.left = docX + 'px';
leftSide.style.width = splitter.offsetLeft - splitter.offsetWidth / 2 + 'px';
rightSide.style.width = (container.offsetWidth - leftSide.offsetWidth - splitter.offsetWidth) + 'px';
rightSide.style.left = splitter.offsetLeft + (splitter.offsetWidth / 2) + 'px';
} else {
if (globalXCoordinate + splitter.offsetWidth > containerWidth) {
globalXCoordinate = containerWidth - splitter.offsetWidth;
}
if (globalXCoordinate < 0) {
globalXCoordinate = 0;
}
splitter.style.left = globalXCoordinate + 'px';
leftSide.style.width = splitter.offsetLeft - splitter.offsetWidth / 2 + 'px';
rightSide.style.width = (container.offsetWidth - leftSide.offsetWidth - splitter.offsetWidth) + 'px';
rightSide.style.left = splitter.offsetLeft + splitter.offsetWidth / 2 + 'px';
}
}
});
};
// dragging seems off if done over the right side. Fix.