-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
184 lines (152 loc) · 5.92 KB
/
Copy pathapp.js
File metadata and controls
184 lines (152 loc) · 5.92 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
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
/*
UTILS
====
*/
const getLevel = function (levelDOM, p) {
const levelContainers = Array.from(levelDOM.children)
.filter(elem => elem.getAttribute('class') != "resizer")
const firstContainer = levelContainers[0]
const direction = firstContainer.getAttribute("class") === "container_horizontal" ? "h" : "v"
const unit = direction === "h" ? "width" : "height"
const level = {
"p": p,
"d": direction,
"l": []
}
let percentageTotal = 0
const finalIdx = levelContainers.length - 1
levelContainers.map((container, idx) => {
const child = container.children[0]
const type = child.getAttribute("class")
const rect = container.getBoundingClientRect()
const parentRect = container.parentNode.getBoundingClientRect()
let percent = 0;
if (idx === finalIdx) {
// To ensure level totals 100%
percent = 100 - percentageTotal;
} else {
percent = Math.ceil((rect[unit] * 100) / parentRect[unit]);
percentageTotal += percent;
}
let row = {}
if (type === "blob") {
const id = parseInt(child.id);
const blob = blobs.get(id);
row = {
"p": percent,
}
if (blob) {
row["c"] = blob["c"]
row["s"] = blob["s"] ? 1 : 0
row["r"] = blob["r"]
}
} else {
row = Object.assign({"p": percent}, getLevel(container, percent))
}
level["l"].push(row)
})
return level
}
const getData = function () {
const app = document.getElementById("app");
return getLevel(app, 100)
};
/*
CLICKABLES
====
*/
const resizable = function (resizerDOM) {
const direction = resizerDOM.getAttribute('data-direction') || 'horizontal';
let prevSibling = resizerDOM.previousElementSibling;
let nextSibling = resizerDOM.nextElementSibling;
let level = resizerDOM.parentNode;
let x = 0;
let y = 0;
let prevSiblingHeight = 0;
let prevSiblingWidth = 0;
let nextSiblingHeight = 0;
let nextSiblingWidth = 0;
const pointerDownHandler = function (e) {
e.preventDefault();
prevSibling = resizerDOM.previousElementSibling;
nextSibling = resizerDOM.nextElementSibling;
level = resizerDOM.parentNode;
// Determine if it's a touch event or mouse event
const clientX = e.touches ? e.touches[0].clientX : e.clientX;
const clientY = e.touches ? e.touches[0].clientY : e.clientY;
x = clientX;
y = clientY;
const prevRect = prevSibling.getBoundingClientRect();
prevSiblingHeight = prevRect.height;
prevSiblingWidth = prevRect.width;
const nextRect = nextSibling.getBoundingClientRect();
nextSiblingHeight = nextRect.height;
nextSiblingWidth = nextRect.width;
document.addEventListener('mousemove', pointerMoveHandler, { passive: false });
document.addEventListener('touchmove', pointerMoveHandler, { passive: false });
document.addEventListener('mouseup', pointerUpHandler);
document.addEventListener('touchend', pointerUpHandler);
};
const pointerMoveHandler = function (e) {
e.preventDefault();
const clientX = e.touches ? e.touches[0].clientX : e.clientX;
const clientY = e.touches ? e.touches[0].clientY : e.clientY;
const dx = clientX - x;
const dy = clientY - y;
switch (direction) {
case 'vertical':
const parentHeight = level.getBoundingClientRect().height;
const prevHeight = ((prevSiblingHeight + dy) * 100) / parentHeight;
const nextHeight = ((nextSiblingHeight - dy) * 100) / parentHeight;
prevSibling.style.height = prevHeight + '%';
nextSibling.style.height = nextHeight + '%';
break;
case 'horizontal':
default:
const parentWidth = level.getBoundingClientRect().width;
const prevWidth = ((prevSiblingWidth + dx) * 100) / parentWidth;
const nextWidth = ((nextSiblingWidth - dx) * 100) / parentWidth;
prevSibling.style.width = prevWidth + '%';
nextSibling.style.width = nextWidth + '%';
break;
}
};
const pointerUpHandler = function () {
document.removeEventListener('mousemove', pointerMoveHandler, { passive: false });
document.removeEventListener('touchmove', pointerMoveHandler, { passive: false });
document.removeEventListener('mouseup', pointerUpHandler);
document.removeEventListener('touchend', pointerUpHandler);
};
resizerDOM.removeEventListener('mousedown', pointerDownHandler);
resizerDOM.removeEventListener('touchstart', pointerDownHandler);
resizerDOM.addEventListener('mousedown', pointerDownHandler);
resizerDOM.addEventListener('touchstart', pointerDownHandler);
};
const paintable = function (paintDOM) {
const divider = paintDOM.getAttribute("divider") === "true"
const pointerDownHandler = function (e) {
e.preventDefault();
const painting = btoa(JSON.stringify(getData()))
const url = `paint.html?${divider ? "divider&" : ""}artwork=${painting}`
window.open(url).focus();
};
paintDOM.addEventListener('mousedown', pointerDownHandler, { passive: false });
paintDOM.addEventListener('touchstart', pointerDownHandler, { passive: false });
};
/*
RENDER
====
*/
const loadResizables = function () {
document.querySelectorAll('.resizer').forEach(function (elem) {
resizable(elem);
});
};
const renderApp = function () {
const appDOM = document.getElementById("app");
renderLevel(appDOM, startData, true)
loadResizables()
const paintDOM = document.getElementById("action-paint");
paintable(paintDOM)
};
document.addEventListener('DOMContentLoaded', renderApp);