-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
652 lines (624 loc) · 25.8 KB
/
index.html
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
<!DOCTYPE html>
<!-- https://lukelowrey.com/css-variable-theme-switcher/ -->
<html data-theme="light">
<head>
<meta charset="UTF-8">
<title>OpenAFPM CAD</title>
<style>
@font-face {
font-family: "Roboto";
src: url("font/Roboto-Light.ttf");
font-weight: 300;
}
@font-face {
font-family: "Roboto";
src: url("font/Roboto-Regular.ttf");
font-weight: 400;
}
@font-face {
font-family: "Roboto";
src: url("font/Roboto-Italic.ttf");
font-weight: 400;
font-style: italic;
}
@font-face {
font-family: "Roboto";
src: url("font/Roboto-Medium.ttf");
font-weight: 500;
}
@font-face {
font-family: "Roboto";
src: url("font/Roboto-Bold.ttf");
font-weight: 700;
}
:root {
--spacing: 8px;
--transition-duration-standard: 250ms;
--font-family: "Roboto";
--root-font-size: "Roboto";
/**
* Typography based on modular scale factor of 1.2,
* also known as the Minor Third.
* @see {@link https://www.modularscale.com/?16&px&1.2}
*/
--h1: 2.074rem;
--h2: 1.728rem;
--h3: 1.44rem;
--h4: 1.2rem;
--h5: 1rem;
--h6: 0.833rem;
--navigation-rail-width: 80px;
}
html[data-theme='light'] {
--primary-color-main: #43A047; /* Green 600 - https://www.materialui.co/colors */
--primary-color-dark: #388E3C; /* Green 700 - https://www.materialui.co/colors */
--error-color-main: #E53935; /* Red 600 - https://www.materialui.co/colors */
--error-color-dark: #D32F2F; /* Red 700 - https://www.materialui.co/colors */
--validation-color-main: #D32F2F; /* Red 700 - https://www.materialui.co/colors */
--background-default: #FFFFFF;
--background-navigation: #FFFFFF;
--background-paper: #DCDCDC; /* gainsboro - https://www.materialui.co/htmlcolors */
--background-disabled: rgba(0, 0, 0, 0.12);
--background-dropzone: #E8F5E9;
--text-color: rgba(0, 0, 0, 0.87);
--text-disabled: rgba(0, 0, 0, 0.38);
--divider: rgba(0, 0, 0, 0.12);
/* Generated from material theme builder figma plugin with #43A047 source color */
/* https://www.figma.com/community/plugin/1034969338659738588/material-theme-builder */
--md-sys-color-primary: rgb(59 105 57);
--md-sys-color-surface-tint: rgb(59 105 57);
--md-sys-color-on-primary: rgb(255 255 255);
--md-sys-color-primary-container: rgb(188 240 180);
--md-sys-color-on-primary-container: rgb(0 34 4);
--md-sys-color-secondary: rgb(82 99 79);
--md-sys-color-on-secondary: rgb(255 255 255);
--md-sys-color-secondary-container: rgb(213 232 207);
--md-sys-color-on-secondary-container: rgb(17 31 15);
--md-sys-color-tertiary: rgb(56 101 106);
--md-sys-color-on-tertiary: rgb(255 255 255);
--md-sys-color-tertiary-container: rgb(188 235 240);
--md-sys-color-on-tertiary-container: rgb(0 32 35);
--md-sys-color-error: rgb(186 26 26);
--md-sys-color-on-error: rgb(255 255 255);
--md-sys-color-error-container: rgb(255 218 214);
--md-sys-color-on-error-container: rgb(65 0 2);
--md-sys-color-background: rgb(247 251 241);
--md-sys-color-on-background: rgb(25 29 23);
--md-sys-color-surface: rgb(247 251 241);
--md-sys-color-on-surface: rgb(25 29 23);
--md-sys-color-surface-variant: rgb(222 229 216);
--md-sys-color-on-surface-variant: rgb(66 73 64);
--md-sys-color-outline: rgb(114 121 111);
--md-sys-color-outline-variant: rgb(194 201 189);
--md-sys-color-shadow: rgb(0 0 0);
--md-sys-color-scrim: rgb(0 0 0);
--md-sys-color-inverse-surface: rgb(45 50 44);
--md-sys-color-inverse-on-surface: rgb(239 242 233);
--md-sys-color-inverse-primary: rgb(161 211 154);
--md-sys-color-primary-fixed: rgb(188 240 180);
--md-sys-color-on-primary-fixed: rgb(0 34 4);
--md-sys-color-primary-fixed-dim: rgb(161 211 154);
--md-sys-color-on-primary-fixed-variant: rgb(35 80 36);
--md-sys-color-secondary-fixed: rgb(213 232 207);
--md-sys-color-on-secondary-fixed: rgb(17 31 15);
--md-sys-color-secondary-fixed-dim: rgb(186 204 179);
--md-sys-color-on-secondary-fixed-variant: rgb(59 75 56);
--md-sys-color-tertiary-fixed: rgb(188 235 240);
--md-sys-color-on-tertiary-fixed: rgb(0 32 35);
--md-sys-color-tertiary-fixed-dim: rgb(160 207 212);
--md-sys-color-on-tertiary-fixed-variant: rgb(31 77 82);
--md-sys-color-surface-dim: rgb(216 219 210);
--md-sys-color-surface-bright: rgb(247 251 241);
--md-sys-color-surface-container-lowest: rgb(255 255 255);
--md-sys-color-surface-container-low: rgb(241 245 236);
--md-sys-color-surface-container: rgb(236 239 230);
--md-sys-color-surface-container-high: rgb(230 233 224);
--md-sys-color-surface-container-highest: rgb(224 228 219);
}
html[data-theme='dark'] {
--primary-color-main: #A5D6A7; /* Green 200 - https://www.materialui.co/colors */
--primary-color-dark: #81C784; /* Green 300 - https://www.materialui.co/colors */
--error-color-main: #E53935; /* Red 600 - https://www.materialui.co/colors */
--error-color-dark: #D32F2F; /* Red 700 - https://www.materialui.co/colors */
--validation-color-main: #FF5252; /* Red A200 - https://www.materialui.co/colors */
--background-default: #212121;
--background-navigation: #303030;
--background-paper: #424242;
--background-disabled: rgba(255, 255, 255, 0.12);
--background-dropzone: #111;
--text-color: #FFFFFF;
--text-disabled: rgba(255, 255, 255, 0.5);
--divider: rgba(255, 255, 255, 0.12);
/* Generated from material theme builder figma plugin with #43A047 source color */
/* https://www.figma.com/community/plugin/1034969338659738588/material-theme-builder */
--md-sys-color-primary: rgb(161 211 154);
--md-sys-color-surface-tint: rgb(161 211 154);
--md-sys-color-on-primary: rgb(10 57 15);
--md-sys-color-primary-container: rgb(35 80 36);
--md-sys-color-on-primary-container: rgb(188 240 180);
--md-sys-color-secondary: rgb(186 204 179);
--md-sys-color-on-secondary: rgb(37 52 35);
--md-sys-color-secondary-container: rgb(59 75 56);
--md-sys-color-on-secondary-container: rgb(213 232 207);
--md-sys-color-tertiary: rgb(160 207 212);
--md-sys-color-on-tertiary: rgb(0 54 59);
--md-sys-color-tertiary-container: rgb(31 77 82);
--md-sys-color-on-tertiary-container: rgb(188 235 240);
--md-sys-color-error: rgb(255 180 171);
--md-sys-color-on-error: rgb(105 0 5);
--md-sys-color-error-container: rgb(147 0 10);
--md-sys-color-on-error-container: rgb(255 218 214);
--md-sys-color-background: rgb(16 20 15);
--md-sys-color-on-background: rgb(224 228 219);
--md-sys-color-surface: rgb(16 20 15);
--md-sys-color-on-surface: rgb(224 228 219);
--md-sys-color-surface-variant: rgb(66 73 64);
--md-sys-color-on-surface-variant: rgb(194 201 189);
--md-sys-color-outline: rgb(140 147 136);
--md-sys-color-outline-variant: rgb(66 73 64);
--md-sys-color-shadow: rgb(0 0 0);
--md-sys-color-scrim: rgb(0 0 0);
--md-sys-color-inverse-surface: rgb(224 228 219);
--md-sys-color-inverse-on-surface: rgb(45 50 44);
--md-sys-color-inverse-primary: rgb(59 105 57);
--md-sys-color-primary-fixed: rgb(188 240 180);
--md-sys-color-on-primary-fixed: rgb(0 34 4);
--md-sys-color-primary-fixed-dim: rgb(161 211 154);
--md-sys-color-on-primary-fixed-variant: rgb(35 80 36);
--md-sys-color-secondary-fixed: rgb(213 232 207);
--md-sys-color-on-secondary-fixed: rgb(17 31 15);
--md-sys-color-secondary-fixed-dim: rgb(186 204 179);
--md-sys-color-on-secondary-fixed-variant: rgb(59 75 56);
--md-sys-color-tertiary-fixed: rgb(188 235 240);
--md-sys-color-on-tertiary-fixed: rgb(0 32 35);
--md-sys-color-tertiary-fixed-dim: rgb(160 207 212);
--md-sys-color-on-tertiary-fixed-variant: rgb(31 77 82);
--md-sys-color-surface-dim: rgb(16 20 15);
--md-sys-color-surface-bright: rgb(54 58 52);
--md-sys-color-surface-container-lowest: rgb(11 15 10);
--md-sys-color-surface-container-low: rgb(25 29 23);
--md-sys-color-surface-container: rgb(29 33 27);
--md-sys-color-surface-container-high: rgb(39 43 37);
--md-sys-color-surface-container-highest: rgb(50 54 48);
}
html {
font-size: var(--root-font-size);
overflow-y: scroll;
}
body {
color: var(--text-color);
background: var(--background-default);
transition: var(--transition-duration-standard) ease-in-out;
margin: 0;
font-family: var(--font-family);
display: grid;
grid-template-rows: min-content 1fr;
}
html, body {
height: 100%;
}
header {
display: flex;
justify-content: space-between;
align-items: center;
background-color: var(--primary-color-main);
color: var(--background-default);
font-size: 1rem;
font-weight: bold;
padding: calc(var(--spacing) * 2);
position: relative;
box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.5);
transition: var(--transition-duration-standard) ease-in-out;
}
.container {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: 1.2rem;
}
#loading-spinner {
display: inline-block;
width: 35px;
height: 35px;
border: 3px solid var(--primary-color-main);
border-radius: 50%;
border-right-color: transparent;
animation: spin 1s ease infinite;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
@-webkit-keyframes spin {
to { transform: rotate(360deg); }
}
.dropzone {
background: var(--background-dropzone);
}
</style>
</head>
<body>
<header>
OpenAFPM CAD
<x-theme-toggle></x-theme-toggle>
</header>
<x-app>
<div id="visualization-root" style="width: 100%; height: 100%">
<x-empty-state>
<div class="container">
<div id="loading-spinner"></div>
<p>Loading</p>
</div>
</x-empty-state>
</div>
</x-app>
<script type="importmap">
{
"imports": {
"lit": "./node_modules/lit/index.js",
"lit/": "./node_modules/lit/",
"lit-html": "./node_modules/lit-html/lit-html.js",
"lit-html/": "./node_modules/lit-html/",
"lit-element/": "./node_modules/lit-element/",
"@lit/reactive-element": "./node_modules/@lit/reactive-element/reactive-element.js",
"@lit/reactive-element/": "./node_modules/@lit/reactive-element/",
"@material/web/": "./node_modules/@material/web/",
"tslib": "./node_modules/tslib/tslib.es6.mjs",
"openafpm-cad-visualization": "./node_modules/openafpm-cad-visualization/public/openafpm-cad-visualization.js"
}
}
</script>
<script type="module">
import {styles as typescaleStyles} from "@material/web/typography/md-typescale-styles.js";
document.adoptedStyleSheets.push(typescaleStyles.styleSheet);
</script>
<script type="module" src="./frontend/themeToggle.js"></script>
<script type="module" src="./frontend/app.js"></script>
<script>
// Setup theme toggle.
const themeToggle = document.querySelector("x-theme-toggle");
const visualizationRoot = document.getElementById("visualization-root");
const setTheme = theme => {
themeToggle.setAttribute("theme", theme);
document.documentElement.setAttribute("data-theme", theme);
visualizationRoot.setAttribute("data-theme", theme);
};
const preferredTheme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
const presetTheme = localStorage.getItem("theme") || preferredTheme;
if (presetTheme) setTheme(presetTheme);
themeToggle.addEventListener("click", () => {
const currentTheme = document.documentElement.getAttribute("data-theme");
const targetTheme = currentTheme === "light" ? "dark" : "light";
setTheme(targetTheme);
localStorage.setItem("theme", targetTheme);
});
// Setup app.
window.addEventListener("load", handleLoad);
function handleLoad() {
document.getElementById("loading-spinner").remove();
const openAfpmCadVisualizationPromise = import(
"./node_modules/openafpm-cad-visualization/public/openafpm-cad-visualization.js"
);
const enumsPromise = import("./frontend/enums.js");
const groupParametersPromise = import("./frontend/groupParameters.js");
Promise.all([openAfpmCadVisualizationPromise, enumsPromise, groupParametersPromise])
.then(setup);
}
function setup([{default: OpenAfpmCadVisualization}, enums, {default: groupParameters}]) {
const { Tab, Preset, Assembly } = enums;
const app = document.querySelector("x-app");
const buildRequestBody = (form, parameters) => JSON.stringify(groupParameters(form, parameters));
const initialState = {
preset: Preset.T_SHAPE,
assembly: Assembly.WIND_TURBINE,
tab: Tab.INPUTS,
parametersByPreset: null,
parametersSchemaByPreset: null,
form: null,
loading: false,
errorMessage: '',
archiveLoading: false,
archiveErrorMessage: '',
cncOverviewSvgLoading: false,
cncOverviewSvgErrorMessage: '',
cncOverviewSvg: '',
dxfArchiveLoading: false,
dxfArchiveErrorMessage: '',
dimensionTablesLoading: false,
dimensionTablesErrorMessage: '',
dimensionTables: null
};
Object.assign(app, initialState);
const setState = (nextState) => {
Object.assign(app, nextState);
};
function fetchJson(...args) {
return fetch(...args)
.then(response => response.json())
.then(response => {
if (response.error) {
throw new Error(response.error);
}
return response;
});
}
const defaultParametersPromise = fetchJson('/api/defaultparameters');
const parametersSchemaPromise = fetchJson('/api/parametersschema');
const promise = Promise.all([defaultParametersPromise, parametersSchemaPromise]);
function setForm() {
if (!app.parametersByPreset) return;
const parameters = flatten(app.parametersByPreset[app.preset]);
const form = mapValues(parameters, value => ({value, validationMessage: ''}));
setState({ form });
}
promise.then(([parametersByPreset, parametersSchemaByPreset]) => {
const parameters = flatten(parametersByPreset[initialState.preset]);
const form = mapValues(parameters, value => ({value, validationMessage: ''}));
setState({
parametersByPreset,
parametersSchemaByPreset
});
setForm();
})
setPromiseToState(promise, 'loading', 'errorMessage');
function setPromiseToState(promise, loadingProperty, errorMessageProperty) {
setState({ [loadingProperty]: true });
promise.then(() => {
setState({ [loadingProperty]: false });
})
.catch(error => {
if (error.name !== 'AbortError') {
setState({
[errorMessageProperty]: error.message,
[loadingProperty]: false
});
}
});
}
app.addEventListener('close-error', () => {
setState({ errorMessage: '' });
});
app.addEventListener('select-preset', (event) => {
const { selectedPreset } = event.detail;
setState({ preset: selectedPreset });
setForm();
});
app.addEventListener('input-change', event => {
const { name, value, validationMessage } = event.detail;
setState({
form: {
...app.form,
[name]: {
value,
validationMessage
}
}
});
});
function calculateWidth() {
// Get the width of document (without the scrollbar) minus navigation rail width.
const navigationRailWidth = parseInt(getComputedStyle(document.documentElement)
.getPropertyValue('--navigation-rail-width'));
return document.documentElement.clientWidth - navigationRailWidth;
}
function calculateHeight() {
const headerHeight = document.querySelector('header').offsetHeight;
const tabsHeight = app.renderRoot.querySelector('md-tabs').offsetHeight;
const offsetHeight = headerHeight + tabsHeight;
return window.innerHeight - offsetHeight;
}
let cleanUp = null;
let abortController = null;
let previousBody = null;
const openAfpmCadVisualization = new OpenAfpmCadVisualization({
rootDomElement: visualizationRoot,
width: calculateWidth(),
height: calculateHeight()
});
const handleResize = () => {
if (app.tab === Tab.VISUALIZE) {
const width = calculateWidth();
const height = calculateHeight();
visualizationRoot.style.height = height + 'px';
openAfpmCadVisualization.resize(width, height);
}
};
window.addEventListener('resize', handleResize, false);
function handleVisualize() {
if (cleanUp) cleanUp();
if (app.loading) abortController.abort();
abortController = new AbortController();
const body = buildRequestBody(app.form, app.parametersByPreset[app.preset]);
const visualizePromise = fetchJson(`/api/visualize/${app.assembly}`, {
body,
headers: {
'Content-Type': 'application/json'
},
method: 'POST',
signal: abortController.signal
});
if (previousBody !== body) {
const options = {
body,
headers: {
'Content-Type': 'application/json'
},
method: 'POST'
};
const cncOverviewPromise = fetchJson(`/api/getcncoverview`, options);
setState({ cncOverviewSvg: '', cncOverviewSvgErrorMessage: '' })
cncOverviewPromise.then(result => setState({ cncOverviewSvg: result.svg }));
setPromiseToState(cncOverviewPromise, 'cncOverviewSvgLoading', 'cncOverviewSvgErrorMessage');
const dimensionTablesPromise = fetchJson(`/api/getdimensiontables`, options);
setState({ dimensionTables: null, dimensionTablesErrorMessage: '' })
dimensionTablesPromise.then(result => setState({ dimensionTables: result.tables }));
setPromiseToState(dimensionTablesPromise, 'dimensionTablesLoading', 'dimensionTablesErrorMessage');
}
setPromiseToState(visualizePromise, 'loading', 'errorMessage');
visualizationRoot.innerHTML = '';
const loadObjText = () => visualizePromise.then(({objText}) => objText);
const furlTransformPromise = visualizePromise.then(({furlTransform}) => furlTransform);
furlTransformPromise.catch(error => error.name !== 'AbortError' && console.error(error));
openAfpmCadVisualization.visualize(loadObjText, app.assembly, furlTransformPromise);
handleResize();
const handleMouseMove = (event) => openAfpmCadVisualization.handleMouseMove(event);
window.addEventListener('mousemove', handleMouseMove, false);
previousBody = body;
cleanUp = () => {
window.removeEventListener('mousemove', handleMouseMove);
};
}
const debouncedVisualizeHandler = debounce(handleVisualize, 200);
app.addEventListener('visualize', debouncedVisualizeHandler);
app.addEventListener('select-assembly', (event) => {
const { selectedAssembly } = event.detail;
if (app.assembly !== selectedAssembly) {
setState({ assembly: selectedAssembly });
const hasClickedVisualizeButtonOnce = Boolean(cleanUp);
if (hasClickedVisualizeButtonOnce) debouncedVisualizeHandler();
}
});
app.addEventListener('select-tab', (event) => {
const { selectedTab } = event.detail;
setState({ tab: selectedTab });
// resize as a macro-task to ensure DAT GUI
// is visible before setting its top position.
setTimeout(handleResize);
});
/**
* Import
* --------------------------------------------------------------------------------------------------------------*/
const handleDragOver = event => {
event.preventDefault();
if (!document.body.classList.contains('dropzone')) {
document.body.classList.add('dropzone');
}
};
const handleRemoveDropzoneClass = event => {
if (document.body.classList.contains('dropzone')) {
document.body.classList.remove('dropzone');
}
};
const handleFileUpload = file => {
if (file.type === 'application/json') {
const reader = new FileReader();
reader.onload = (loadEvent) => {
try {
// TODO: We should validate this user input instead of trusting it.
const {preset, ...parametersByGroup} = JSON.parse(loadEvent.target.result);
const parameters = flatten(parametersByGroup);
const form = mapValues(parameters, value => ({value, validationMessage: ''}));
setState({ form });
if (preset) setState({ preset });
} catch (error) {
console.error(error);
}
};
reader.readAsText(file);
} else if (file.path.endsWith('.mat')) {
const loadmatPromise = fetchJson('/api/loadmat', {
body: JSON.stringify({path: file.path}),
headers: {
'Content-Type': 'application/json'
},
method: 'POST',
});
loadmatPromise.then(({preset, ...parametersByGroup}) => {
const parameters = flatten(parametersByGroup);
const form = mapValues(parameters, value => ({value, validationMessage: ''}));
setState({ form: {...app.form, ...form} });
if (preset) setState({ preset });
});
loadmatPromise.catch(console.error);
} else {
console.warn(`Type of '${file.name}' must be 'application/json' instead of '${file.type}' or name must end with '.mat' extension.`);
}
};
const handleDrop = event => {
event.preventDefault();
handleRemoveDropzoneClass(event);
const firstFile = event.dataTransfer.items?.[0];
handleFileUpload(firstFile);
}
document.body.addEventListener('dragover', handleDragOver);
document.body.addEventListener('dragleave', handleRemoveDropzoneClass);
document.body.addEventListener('drop', handleDrop);
app.addEventListener('file-upload', (event) => {
handleFileUpload(event.detail.file);
});
/** ------------------------------------------------------------------------------------------------------------ */
function createArchiveDownloadHandler(requestPath, archiveName, loadingProperty, errorMessageProperty) {
return () => {
if (!app.form || app[loadingProperty]) return;
setState({ [loadingProperty]: true, [errorMessageProperty]: '' });
fetch(requestPath, {
body: buildRequestBody(app.form, app.parametersByPreset[app.preset]),
headers: {
'Content-Type': 'application/json'
},
method: 'POST'
})
.then(response => {
if (!response.ok) {
return response.json();
}
return response;
})
.then(response => {
if (response.error) {
throw new Error(response.error);
}
return response;
})
.then(response => response.blob())
.then(blob => {
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = archiveName;
link.click();
})
.catch(error => {
if (error.name !== 'AbortError') {
setState({ [errorMessageProperty]: error.message });
}
})
.finally(() => {
setState({ [loadingProperty]: false });
});
};
}
const handleDownloadArchive = createArchiveDownloadHandler(
'/api/archive', 'WindTurbine.zip', 'archiveLoading', 'archiveErrorMessage'
);
app.addEventListener('download-archive', handleDownloadArchive);
const handleDownloadDxfArchive = createArchiveDownloadHandler(
'/api/dxfarchive', 'WindTurbineDXF.zip', 'dxfArchiveLoading', 'dxfArchiveErrorMessage'
);
app.addEventListener('download-dxf-archive', handleDownloadDxfArchive);
}
function flatten(object) {
return Object.values(object)
.reduce((acc, obj) => ({...acc, ...obj}), {});
}
function mapEntries(obj, entryMapper) {
return Object.fromEntries(Object.entries(obj).map(entryMapper));
}
function mapValues(obj, valueMapper) {
return mapEntries(obj, ([key, value]) => (
[key, valueMapper(value)]
));
}
function debounce(callback, wait) {
let timeoutId = null;
return (...args) => {
window.clearTimeout(timeoutId);
timeoutId = window.setTimeout(() => {
callback.apply(null, args);
}, wait);
};
}
</script>
</body>
</html>