forked from florianfesti/boxes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
self.js
303 lines (276 loc) · 8.3 KB
/
self.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
function showThumbnail(img_link) {
const img = document.getElementById("sample-preview");
img.src = img_link;
img.style.height = "auto";
img.style.display = "block";
}
function showThumbnailEvt(evt) {
const url = evt.target.getAttribute("data-thumbnail");
showThumbnail(url);
}
function hideThumbnail() {
const img = document.getElementById("sample-preview");
img.style.display = "none";
}
function expandId(id) {
const e = document.getElementById(id);
const h = document.getElementById("h-" + id);
e.style.display = "block";
h.classList.add("open");
h.setAttribute("aria-expanded", "true");
}
function collapseId(id) {
const e = document.getElementById(id);
const h = document.getElementById("h-" + id);
e.style.display = "none";
h.classList.remove("open");
h.setAttribute("aria-expanded", "false");
}
function toggleId(id) {
const e = document.getElementById(id);
const h = document.getElementById("h-" + id);
if (e.style.display == null || e.style.display === "none") {
expandId(id);
} else {
collapseId(id);
}
}
function toggleEl(el) {
const id = el.getAttribute("data-id");
toggleId(id);
}
function toggleEvt(evt) {
const id = evt.target.getAttribute("data-id");
// https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles/button_role#examples
if (evt instanceof MouseEvent) {
toggleId(id);
}
if (evt instanceof KeyboardEvent && (evt.key === "Enter" || evt.key === " ")) {
evt.preventDefault();
toggleId(id);
}
}
function initToggle(el, hide = false) {
// Add event handler.
el.addEventListener("click", toggleEvt);
el.addEventListener("keydown", toggleEvt);
// Hide.
if (hide) {
toggleEl(el);
}
}
function initThumbnail(el) {
// Add event handler.
el.addEventListener("mouseenter", showThumbnailEvt);
el.addEventListener("mouseleave", hideThumbnail);
}
function initPage(num_hide = null) {
const h = document.getElementsByClassName("toggle");
let i = 0;
for (let el of h) {
if (num_hide === null || i < num_hide) {
initToggle(el, true);
} else {
initToggle(el, false);
}
i++;
}
const t = document.getElementsByClassName("thumbnail");
for (let el of t) initThumbnail(el);
}
function GridfinityTrayLayout_GenerateLayout(x, y, nx, ny, countx, county) {
// x = width in mm
// y = height in mm
// nx # of gridfinity grids in X
// ny # of gridfinity grids in Y
// countx split x into this many
// county split y into this many
layout = '';
if (countx == 0)
countx = nx;
if (county == 0)
county = ny
stepx = x / countx;
stepy = y / county;
for (i = 0; i < countx; i++) {
line = ' |'.repeat(i) + ` ,> ${stepx}mm\n`;
layout += line;
}
for (i = 0; i < county; i++) {
layout += "+-".repeat(countx) + "+\n";
layout += "| ".repeat(countx) + `| ${stepy}mm\n`;
}
layout += "+-".repeat(countx) + "+\n";
return layout
}
function GridfinityTrayUpdateLayout(event) {
console.log("update");
if (window.layoutUpdated == true) {
// Don't do the update if the layout has been manually touched.
if (confirm("You have manually updated the Layout. Do you wish to regenerate it?")) {
window.layoutUpdated = false;
} else {
return;
}
}
console.log("updating");
nx = document.getElementById('nx').value;
ny = document.getElementById('ny').value;
countx = document.getElementById('countx').value;
county = document.getElementById('county').value;
margin = document.getElementById('margin').value;
x = nx*42 - margin
y = ny*42 - margin
layout_id = document.getElementById('layout');
layout_id.value = GridfinityTrayLayout_GenerateLayout(x, y, nx, ny, countx, county);
}
function setUpdated() {
console.log("this was updated");
window.layoutUpdated=true;
}
function GridfinityTrayLayoutInit() {
console.log("update init");
ids = ['nx', 'ny', 'countx', 'county', 'margin'];
window.layoutUpdated=false;
for (id_string of ids) {
id = document.getElementById(id_string);
id.addEventListener('input', GridfinityTrayUpdateLayout);
}
layout_id = document.getElementById('layout');
layout_id.addEventListener('change', setUpdated);
layout_id.addEventListener('input', setUpdated);
GridfinityTrayUpdateLayout();
layout_id = document.getElementById('layout');
layout_id.rows = 20;
layout_id.cols = 24;
}
function ParseSections(s) {
var sections = [];
for (var section of s.split(":")) {
var operands = section.split("/");
if (operands.length > 2) return sections;
if (operands.length == 2) {
for (var i=0; i<operands[1]; i++) {
sections.push(Number(operands[0])/Number(operands[1]));
}
continue;
}
operands = section.split("*");
if (operands.length > 2) return sections;
if (operands.length == 2) {
for (var i=0; i<operands[1]; i++) {
sections.push(Number(operands[0]));
}
continue;
}
sections.push(Number(section));
}
return sections;
}
function TrayLayout_GenerateLayout(sx, sy) {
sx = ParseSections(sx);
sy = ParseSections(sy);
nx = sx.length
ny = sy.length
layout = '';
if (nx <= 0)
nx = 1;
if (ny <= 0)
ny = 1;
for (i = 0; i < nx; i++) {
line = ' |'.repeat(i) + ` ,> ${sx[i].toFixed(2)}mm\n`;
layout += line;
}
for (i = 0; i < ny; i++) {
layout += "+-".repeat(nx) + "+\n";
layout += "| ".repeat(nx) + `| ${sy[i].toFixed(2)}mm\n`;
}
layout += "+-".repeat(nx) + "+\n";
return layout
}
function TrayUpdateLayout(event) {
if (window.layoutUpdated == true) {
// Don't do the update if the layout has been manually touched.
if (confirm("You have manually updated the Layout. Do you wish to regenerate it?")) {
window.layoutUpdated = false;
} else {
return;
}
}
sx = document.getElementById('sx').value;
sy = document.getElementById('sy').value;
layout_id = document.getElementById('layout');
layout_id.value = TrayLayout_GenerateLayout(sx, sy);
}
function TrayLayoutInit() {
ids = ['sx', 'sy'];
window.layoutUpdated=false;
for (id_string of ids) {
id = document.getElementById(id_string);
id.addEventListener('input', TrayUpdateLayout);
}
TrayUpdateLayout();
layout_id = document.getElementById('layout');
layout_id.addEventListener('change', setUpdated);
layout_id.addEventListener('input', setUpdated);
layout_id.rows = 20;
layout_id.cols = 24;
}
function addCallbacks() {
page_callbacks = {
"TrayLayout": TrayLayoutInit,
"GridfinityTrayLayout": GridfinityTrayLayoutInit,
};
loc = new URL(window.location.href);
pathname = loc.pathname;
page = pathname.substr(pathname.lastIndexOf('/')+1);
if (page in page_callbacks) {
callback = page_callbacks[page];
callback();
}
}
document.addEventListener('DOMContentLoaded', function() {
addCallbacks();
}, false);
function collapseAll() {
const h = document.getElementsByClassName("toggle");
for (let el of h) {
id = el.getAttribute("data-id")
collapseId(id);
}
}
function expandAll() {
const h = document.getElementsByClassName("toggle");
for (let el of h) {
id = el.getAttribute("data-id")
expandId(id);
}
}
function showAll(str) {
let matching_ids = document.querySelectorAll('[id^="search_id_"]')
for (let id of matching_ids) {
id.style.display = "inline-block";
}
}
function showOnly(str) {
str = str.toLowerCase();
let matching_ids = document.querySelectorAll('[id^="search_id_"]')
for (let id of matching_ids) {
name = id.id.replace("search_id_", "").toLowerCase();
if (name.includes(str) || id.textContent.toLowerCase().includes(str)) {
id.style.display = "inline-block";
} else {
id.style.display = "none";
}
}
}
function filterSearchItems() {
const search = document.getElementById("search")
if (search.value.length == 0) {
collapseAll();
showAll()
} else {
expandAll();
showOnly(search.value)
}
}