@metoceanapi/wxtiles-common 0.0.5 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,560 @@
1
+ import {
2
+ type WxColorStyleStrict,
3
+ type WxColorStyleWeak,
4
+ WxGetColorSchemes,
5
+ WxGetColorStyles,
6
+ } from '../utils/wxtools';
7
+
8
+ // // Leaflet
9
+ // const editor = new WxStyleEditorControl();
10
+ // map.addControl(new (L.Control.extend(editor.extender()))({ position: 'topleft' }));
11
+
12
+ // // MBox
13
+ // const editor = new WxStyleEditorControl();
14
+ // map.addControl(editor, 'top-left');
15
+
16
+ export class WxStyleEditorControl {
17
+ onchange?: (style: WxColorStyleWeak, nonnative?: boolean) => Promise<void>;
18
+
19
+ editorTextAreaEl: HTMLTextAreaElement;
20
+ editorDivEl: HTMLDivElement;
21
+
22
+ // inputs
23
+ parentInput: HTMLInputElement; // string
24
+
25
+ nameInput: HTMLInputElement; // string
26
+
27
+ fillSelect: HTMLSelectElement; // string
28
+
29
+ isolineColorSelect: HTMLSelectElement; // string
30
+ isolineColorInput: HTMLInputElement;
31
+
32
+ isolineTextInput: HTMLInputElement; // boolean
33
+
34
+ vectorTypeSelect: HTMLSelectElement; // string
35
+
36
+ vectorColorSelect: HTMLSelectElement; // string
37
+ vectorColorInput: HTMLInputElement; // string
38
+
39
+ vectorFactorInput: HTMLInputElement; // number
40
+
41
+ streamLineColorSelect: HTMLSelectElement; // string
42
+ streamLineColorInput: HTMLInputElement; // string
43
+
44
+ streamLineSpeedFactorInput: HTMLInputElement; // number
45
+
46
+ streamLineStaticInput: HTMLInputElement; // boolean
47
+
48
+ showBelowMinInput: HTMLInputElement; // boolean
49
+
50
+ showAboveMaxInput: HTMLInputElement; // boolean
51
+
52
+ colorSchemeSelect: HTMLSelectElement; // string
53
+
54
+ colorsInput: HTMLInputElement; // string[];
55
+
56
+ colorMapInput: HTMLInputElement; // [number, string][];
57
+
58
+ levelsInput: HTMLInputElement; // number[];
59
+
60
+ blurRadiusInput: HTMLInputElement; // number
61
+
62
+ addDegreesInput: HTMLInputElement; // number
63
+
64
+ unitsInput: HTMLInputElement; // string
65
+
66
+ extraUnitsInput: HTMLInputElement; // WxUnits as { [name: string]: [string, number, ?number] };
67
+
68
+ maskSelect: HTMLSelectElement; // string
69
+
70
+ animationSpeed: HTMLInputElement; // number
71
+ noiseTexturePow: HTMLInputElement; // number
72
+ vectorFieldFactor: HTMLInputElement; // number
73
+ animationIntensity: HTMLInputElement; // number
74
+ wavesCount: HTMLInputElement; // number
75
+
76
+ styleBase: WxColorStyleStrict;
77
+
78
+ // main container
79
+ parent: HTMLDivElement;
80
+
81
+ constructor() {
82
+ this.parent = document.createElement("div");
83
+ this.parent.className = "mapboxgl-ctrl leaflet-control"; // in case of MapBox or Leaflet
84
+
85
+ this.parent.style.borderStyle = "solid";
86
+ this.parent.style.borderColor = "#000";
87
+ this.parent.style.backgroundColor = "#aaaaaaaa";
88
+ this.parent.style.padding = "5px";
89
+
90
+ this.parent.onmousemove =
91
+ this.parent.ondblclick =
92
+ this.parent.onclick =
93
+ (e) => e.stopPropagation?.(); // in case of Leaflet
94
+
95
+ const { base } = WxGetColorStyles();
96
+ this.styleBase = base;
97
+
98
+ // helpers
99
+ // Top level container
100
+ const topmostDivEl = createEl(this.parent, "div", {
101
+ onclick: onwheel,
102
+ ondblclick: onwheel,
103
+ onwheel,
104
+ onmousedown,
105
+ onmouseup,
106
+ });
107
+ // Button to open/close editor
108
+ const customStyleButtonEl = createEl(topmostDivEl, "button", {
109
+ id: "customStyleButton",
110
+ style: "height: 1.5em; background-color: gray",
111
+ innerText: "Custom Style",
112
+ });
113
+ // Editor container
114
+ const customStyleHiddableDivEl = createEl(topmostDivEl, "div", {
115
+ id: "customStyleDiv",
116
+ style:
117
+ "display: none; border-color: black; border-style: solid; border-width: 2px; background-color: rgba(135, 206, 250, 0.8)",
118
+ });
119
+ customStyleButtonEl.addEventListener("click", () => {
120
+ if (customStyleHiddableDivEl.style.display !== "none") {
121
+ customStyleHiddableDivEl.style.display = "none";
122
+ customStyleButtonEl.innerHTML = "show Custom Style Editor";
123
+ } else {
124
+ customStyleHiddableDivEl.style.display = "flex";
125
+ customStyleButtonEl.innerHTML = "update Custom Style & Hide";
126
+ }
127
+ });
128
+
129
+ // Text area to edit custom style
130
+ this.editorTextAreaEl = createEl(customStyleHiddableDivEl, "textarea", {
131
+ id: "customStyleTextArea",
132
+ style: "width: 20vw;",
133
+ });
134
+ this.editorTextAreaEl.readOnly = true;
135
+ // this.editorTextAreaEl.addEventListener('change', () => this._onTextChange());
136
+ // Editor container
137
+ this.editorDivEl = createEl(customStyleHiddableDivEl, "div");
138
+ // Helpers
139
+ const addLabel = (id: string, br = true) => {
140
+ createEl(this.editorDivEl, "label", {
141
+ htmlFor: id,
142
+ id: `${id}Label`,
143
+ className: `${id}LabelClass`,
144
+ textContent: id.replace(/Input|Select$/i, ""),
145
+ });
146
+ br && createEl(this.editorDivEl, "br", {});
147
+ };
148
+
149
+ const addInput = ({
150
+ id,
151
+ type,
152
+ min,
153
+ max,
154
+ step,
155
+ onEvent = "change",
156
+ br = true,
157
+ }: {
158
+ id: string;
159
+ type: string;
160
+ onEvent?: string;
161
+ min?: string;
162
+ max?: string;
163
+ step?: string;
164
+ br?: boolean;
165
+ }): HTMLInputElement => {
166
+ const el = createEl(this.editorDivEl, "input", {
167
+ id,
168
+ className: `${id}Class`,
169
+ type,
170
+ min,
171
+ max,
172
+ step,
173
+ });
174
+ onEvent !== "none" &&
175
+ el.addEventListener(onEvent, () => this._onDivChange());
176
+ addLabel(id, br);
177
+ return el;
178
+ };
179
+
180
+ const addSelect = ({
181
+ id,
182
+ opts,
183
+ onEvent = "change",
184
+ br = true,
185
+ }: { id: string; opts: string[]; onEvent?: string; br?: boolean }) => {
186
+ const el = createEl(this.editorDivEl, "select", {
187
+ id,
188
+ className: `${id}Class`,
189
+ });
190
+ onEvent !== "none" &&
191
+ el.addEventListener(onEvent, () => this._onDivChange());
192
+ for (const name of opts) {
193
+ el.options.add(createEl(el, "option", { value: name, text: name }));
194
+ }
195
+ addLabel(id, br);
196
+ el.selectedIndex = 0;
197
+ return el;
198
+ };
199
+
200
+ const addSelectInputColor = (
201
+ id: string,
202
+ ): [HTMLSelectElement, HTMLInputElement] => {
203
+ const select = addSelect({
204
+ id: `${id}Select`,
205
+ br: false,
206
+ opts: ["inverted", "fill", "none", "custom"],
207
+ });
208
+ const input = addInput({
209
+ id: `${id}Input`,
210
+ type: "color",
211
+ onEvent: "none",
212
+ });
213
+ input.addEventListener("change", () => {
214
+ select.value = "custom";
215
+ this._onDivChange();
216
+ });
217
+ return [select, input];
218
+ };
219
+
220
+ this.parentInput = addInput({ id: "parentInput", type: "text" });
221
+ this.nameInput = addInput({ id: "nameInput", type: "text" });
222
+ this.fillSelect = addSelect({
223
+ id: "fillSelect",
224
+ opts: ["gradient", "solid", "none"],
225
+ });
226
+
227
+ [this.isolineColorSelect, this.isolineColorInput] =
228
+ addSelectInputColor("isolineColor");
229
+ this.isolineTextInput = addInput({
230
+ id: "isolineTextInput",
231
+ type: "checkbox",
232
+ });
233
+
234
+ this.vectorFactorInput = addInput({
235
+ id: "vectorFactorInput",
236
+ type: "number",
237
+ min: "0.1",
238
+ max: "10",
239
+ step: "0.1",
240
+ });
241
+ this.vectorTypeSelect = addSelect({
242
+ id: "vectorTypeSelect",
243
+ opts: ["arrows", "barbs", "none"],
244
+ });
245
+ [this.vectorColorSelect, this.vectorColorInput] =
246
+ addSelectInputColor("vectorColor");
247
+ [this.streamLineColorSelect, this.streamLineColorInput] =
248
+ addSelectInputColor("streamLineColor");
249
+
250
+ this.streamLineSpeedFactorInput = addInput({
251
+ id: "streamLineSpeedFactorInput",
252
+ type: "range",
253
+ onEvent: "input",
254
+ min: "0.1",
255
+ max: "10",
256
+ step: "0.1",
257
+ });
258
+ this.streamLineStaticInput = addInput({
259
+ id: "streamLineStaticInput",
260
+ type: "checkbox",
261
+ });
262
+
263
+ this.showBelowMinInput = addInput({
264
+ id: "showBelowMinInput",
265
+ type: "checkbox",
266
+ });
267
+ this.showAboveMaxInput = addInput({
268
+ id: "showAboveMaxInput",
269
+ type: "checkbox",
270
+ });
271
+
272
+ this.colorSchemeSelect = addSelect({
273
+ id: "colorSchemeSelect",
274
+ opts: Object.keys(WxGetColorSchemes()),
275
+ onEvent: "none",
276
+ });
277
+ this.colorSchemeSelect.addEventListener("change", () => {
278
+ const { colors, ...styleWithoutColors } = this.getStyle();
279
+ const style = styleWithoutColors;
280
+ this._setStyleToTextArea(style);
281
+ this.colorsInput.value = "";
282
+ this._onDivChange();
283
+ });
284
+
285
+ this.colorsInput = addInput({ id: "colorsInput", type: "text" });
286
+ this.colorMapInput = addInput({ id: "colorMapInput", type: "text" });
287
+ this.levelsInput = addInput({ id: "levelsInput", type: "text" }); // TODO:
288
+
289
+ this.blurRadiusInput = addInput({
290
+ id: "blurRadiusInput",
291
+ type: "range",
292
+ onEvent: "input",
293
+ min: "0",
294
+ max: "10",
295
+ step: "1",
296
+ });
297
+ this.addDegreesInput = addInput({
298
+ id: "addDegreesInput",
299
+ type: "number",
300
+ min: "0",
301
+ max: "360",
302
+ step: "1",
303
+ });
304
+ this.unitsInput = addInput({ id: "unitsInput", type: "text" });
305
+ this.extraUnitsInput = addInput({ id: "extraUnitsInput", type: "text" });
306
+ this.maskSelect = addSelect({
307
+ id: "maskSelect",
308
+ opts: ["none", "sea", "land"],
309
+ });
310
+
311
+ this.animationSpeed = addInput({
312
+ id: "animationSpeed",
313
+ type: "range",
314
+ onEvent: "input",
315
+ min: "0.1",
316
+ max: "10",
317
+ step: "0.1",
318
+ });
319
+ this.noiseTexturePow = addInput({
320
+ id: "noiseTexturePow",
321
+ type: "range",
322
+ onEvent: "input",
323
+ min: "5",
324
+ max: "8",
325
+ step: "1",
326
+ });
327
+ this.vectorFieldFactor = addInput({
328
+ id: "vectorFieldFactor",
329
+ type: "range",
330
+ onEvent: "input",
331
+ min: "0.1",
332
+ max: "3",
333
+ step: "0.1",
334
+ });
335
+ this.animationIntensity = addInput({
336
+ id: "animationIntensity",
337
+ type: "range",
338
+ onEvent: "input",
339
+ min: "0.1",
340
+ max: "10",
341
+ step: "0.1",
342
+ });
343
+ this.wavesCount = addInput({
344
+ id: "wavesCount",
345
+ type: "range",
346
+ onEvent: "input",
347
+ min: "2",
348
+ max: "10",
349
+ step: "1",
350
+ });
351
+ }
352
+
353
+ // for Leaflet
354
+ extender() {
355
+ return { onAdd: () => this.onAdd(), onRemove: () => this.onRemove() };
356
+ }
357
+
358
+ onAdd(/* map */) {
359
+ return this.parent;
360
+ }
361
+
362
+ onRemove() {
363
+ this.parent.parentNode?.removeChild(this.parent);
364
+ }
365
+
366
+ getStyle(): WxColorStyleWeak {
367
+ return this._getStyleFromDiv();
368
+ }
369
+
370
+ setStyle(style: WxColorStyleWeak): void {
371
+ if (style.colorMap) {
372
+ const { colors, levels, ...filteredStyle } = style;
373
+ this._setStyleToDiv(filteredStyle);
374
+ } else {
375
+ this._setStyleToDiv(style);
376
+ }
377
+ this._setStyleToTextArea(this._getStyleFromDiv());
378
+ }
379
+
380
+ protected _getStyleFromDiv(): WxColorStyleWeak {
381
+ const objFromValue = (field: string) => {
382
+ try {
383
+ return this[field].value ? JSON.parse(this[field].value) : undefined;
384
+ } catch (e) {
385
+ console.log(field, " : parsing error: ", e);
386
+ return undefined;
387
+ }
388
+ };
389
+
390
+ const colorFromSelectInput = (
391
+ select: HTMLSelectElement,
392
+ input: HTMLInputElement,
393
+ ): string | undefined => {
394
+ if (select.value === "custom") {
395
+ return input.value;
396
+ }
397
+ return select.value || undefined;
398
+ };
399
+
400
+ const style: WxColorStyleWeak = {
401
+ parent: this.parentInput.value || undefined, // string;
402
+ name: this.nameInput.value || undefined, //string;
403
+ // biome-ignore lint/suspicious/noExplicitAny: Suppressing for specific use case
404
+ fill: (this.fillSelect.value as any) || undefined, //string;
405
+ isolineColor: colorFromSelectInput(
406
+ this.isolineColorSelect,
407
+ this.isolineColorInput,
408
+ // biome-ignore lint/suspicious/noExplicitAny: Suppressing for specific use
409
+ ) as any, //string;
410
+ isolineText: this.isolineTextInput.checked, //boolean;
411
+ // biome-ignore lint/suspicious/noExplicitAny: Suppressing for specific use case
412
+ vectorType: (this.vectorTypeSelect.value as any) || undefined, //string;
413
+ vectorColor: colorFromSelectInput(
414
+ this.vectorColorSelect,
415
+ this.vectorColorInput,
416
+ // biome-ignore lint/suspicious/noExplicitAny: Suppressing for specific use case
417
+ ) as any,
418
+ vectorFactor: +this.vectorFactorInput.value, //number;
419
+
420
+ streamLineColor: colorFromSelectInput(
421
+ this.streamLineColorSelect,
422
+ this.streamLineColorInput,
423
+ // biome-ignore lint/suspicious/noExplicitAny: Suppressing for specific use case
424
+ ) as any,
425
+ streamLineSpeedFactor: +this.streamLineSpeedFactorInput.value, //number;
426
+ streamLineStatic: this.streamLineStaticInput.checked, //boolean;
427
+ showBelowMin: this.showBelowMinInput.checked, //boolean;
428
+ showAboveMax: this.showAboveMaxInput.checked, //boolean;
429
+ colorScheme: this.colorSchemeSelect.value || undefined, //string;
430
+ colors: objFromValue("colorsInput"), // string[];
431
+ colorMap: objFromValue("colorMapInput"), // [number, string][];
432
+ levels: objFromValue("levelsInput"), // number[];
433
+ blurRadius: +this.blurRadiusInput.value, //number;
434
+ addDegrees: +this.addDegreesInput.value, //number;
435
+ units: this.unitsInput.value || undefined, //string;
436
+ extraUnits: objFromValue("extraUnitsInput"), // WxUnits; //{ [name: string]: [string, number, ?number] };
437
+ // biome-ignore lint/suspicious/noExplicitAny: Suppressing for specific use case
438
+ mask: (this.maskSelect.value as any) || undefined, // string;
439
+ gl: {
440
+ animationSpeed: +this.animationSpeed.value || undefined,
441
+ noiseTexturePow: +this.noiseTexturePow.value || undefined,
442
+ vectorFieldFactor: +this.vectorFieldFactor.value || undefined,
443
+ animationIntensity: +this.animationIntensity.value || undefined,
444
+ wavesCount: +this.wavesCount.value || undefined,
445
+ },
446
+ };
447
+
448
+ return style;
449
+ }
450
+
451
+ protected _setStyleToTextArea(style: WxColorStyleWeak): void {
452
+ this.editorTextAreaEl.value = JSON.stringify(JSONsort(style), null, 2);
453
+ }
454
+
455
+ protected _setStyleToDiv(style: WxColorStyleWeak): void {
456
+ this.parentInput.value = style.parent || ""; // string;
457
+ this.nameInput.value = style.name || ""; //string;
458
+ this.fillSelect.value = style.fill || ""; //string;
459
+ this.isolineColorSelect.value =
460
+ style.isolineColor?.[0] === "#" ? "custom" : style.isolineColor || ""; //string;
461
+ this.isolineColorInput.value =
462
+ style.isolineColor?.[0] === "#" ? style.isolineColor : "#000000"; //string;
463
+ this.isolineTextInput.checked = style.isolineText || false; //boolean;
464
+ this.vectorTypeSelect.value = style.vectorType || ""; //string;
465
+ this.vectorColorSelect.value =
466
+ style.vectorColor?.[0] === "#" ? "custom" : style.vectorColor || ""; //string;
467
+ this.vectorColorInput.value =
468
+ style.vectorColor?.[0] === "#" ? style.vectorColor : "#000000"; //string;
469
+ this.vectorFactorInput.value = style.vectorFactor?.toString() || "1"; //number;
470
+ this.streamLineColorSelect.value =
471
+ style.streamLineColor?.[0] === "#"
472
+ ? "custom"
473
+ : style.streamLineColor || ""; //string;
474
+ this.streamLineColorInput.value =
475
+ style.streamLineColor?.[0] === "#" ? style.streamLineColor : "#000000"; //string;
476
+ this.streamLineSpeedFactorInput.value =
477
+ style.streamLineSpeedFactor?.toString() || "1"; //number;
478
+ this.streamLineStaticInput.checked = style.streamLineStatic || false; //boolean;
479
+ this.showBelowMinInput.checked = style.showBelowMin || false; //boolean;
480
+ this.showAboveMaxInput.checked = style.showAboveMax || false; //boolean;
481
+ this.colorSchemeSelect.value = style.colorScheme || ""; //string;
482
+ this.colorsInput.value = style.colors?.length
483
+ ? JSON.stringify(style.colors)
484
+ : ""; // string[];
485
+ this.colorMapInput.value = style.colorMap?.length
486
+ ? JSON.stringify(style.colorMap)
487
+ : ""; // [number, string][];
488
+ this.levelsInput.value = style.levels?.length
489
+ ? JSON.stringify(style.levels.map((s) => +s.toFixed(2)))
490
+ : ""; // number[];
491
+ this.blurRadiusInput.value = style.blurRadius?.toString() || ""; //number;
492
+ this.addDegreesInput.value = style.addDegrees?.toString() || ""; //number;
493
+ this.unitsInput.value = style.units || ""; //string;
494
+ this.extraUnitsInput.value = style.extraUnits
495
+ ? JSON.stringify(style.extraUnits)
496
+ : ""; // WxUnits; //{ [name: string]: [string, number, ?number] };
497
+ this.maskSelect.value = style.mask || ""; // string;
498
+ if (style.gl?.animationSpeed) {
499
+ this.animationSpeed.value = style.gl.animationSpeed.toString();
500
+ }
501
+ if (style.gl?.noiseTexturePow) {
502
+ this.noiseTexturePow.value = style.gl.noiseTexturePow.toString();
503
+ }
504
+ if (style.gl?.vectorFieldFactor) {
505
+ this.vectorFieldFactor.value = style.gl.vectorFieldFactor.toString();
506
+ }
507
+ if (style.gl?.animationIntensity) {
508
+ this.animationIntensity.value = style.gl.animationIntensity.toString();
509
+ }
510
+ if (style.gl?.wavesCount) {
511
+ this.wavesCount.value = style.gl.wavesCount.toString();
512
+ }
513
+ }
514
+
515
+ protected _onDivChange(): void {
516
+ const style = this._getStyleFromDiv();
517
+ for (const key of Object.keys(style)) {
518
+ if (style[key] === undefined || style[key] === "" || style[key].length === 0) {
519
+ delete style[key];
520
+ }
521
+ }
522
+ // update text area
523
+ this._setStyleToTextArea(style);
524
+ // call callback
525
+ this.onchange?.(this.getStyle());
526
+ }
527
+ }
528
+
529
+ function createEl<K extends keyof HTMLElementTagNameMap>(
530
+ container: HTMLElement,
531
+ tag: K,
532
+ // biome-ignore lint/suspicious/noExplicitAny: Suppressing for specific use case
533
+ params?: any,
534
+ ): HTMLElementTagNameMap[K] {
535
+ const el = document.createElement(tag);
536
+ Object.assign(el, params);
537
+ container?.appendChild(el);
538
+ return el;
539
+ }
540
+
541
+ // biome-ignore lint/suspicious/noExplicitAny: Suppressing for specific use case
542
+ function JSONsort(o: any) {
543
+ if (Array.isArray(o)) {
544
+ return o.map(JSONsort);
545
+ }
546
+ if (typeof o === "object" && o !== null) {
547
+ const keys = Object.keys(o)
548
+ // .map((a) => a.toUpperCase())
549
+ .sort((a, b) => {
550
+ const aa = a.toUpperCase();
551
+ const bb = b.toUpperCase();
552
+ return aa === bb ? 0 : aa > bb ? 1 : -1;
553
+ });
554
+ return keys.reduce((a, k) => {
555
+ a[k] = JSONsort(o[k]);
556
+ return a;
557
+ }, {});
558
+ }
559
+ return o;
560
+ }
@@ -0,0 +1,133 @@
1
+ //import type { WxTileSource } from "../../index";
2
+ //import type { WxTileSource } from "./WxInfoControl";
3
+ declare type WxDate = string | number | Date;
4
+ declare interface WxRequestInit {};
5
+
6
+ declare interface WxEventType {
7
+ changed: any;
8
+ };
9
+
10
+ declare type ListenerMethod = <T extends keyof WxEventType>(arg?: WxEventType[T],) => void;
11
+
12
+ declare interface WxTileSource {
13
+ getTime(): string;
14
+ setTime(time?: WxDate, requestInit?: WxRequestInit, redraw?: boolean, ): Promise<string>;
15
+ getAllTimes(): string[];
16
+ unsetCoarseLevel(): Promise<void>;
17
+ setCoarseLevel(number): Promise<void>;
18
+ on<T extends keyof WxEventType>(type: T, listener: ListenerMethod): void;
19
+ }
20
+
21
+
22
+ // // Leaflet
23
+ // const timeControl = new WxTimeControl();
24
+ // map.addControl(new (L.Control.extend(timeControl.extender()))({ position: 'topright' }));
25
+
26
+ // // MBox
27
+ // const timeControl = new WxTimeControl();
28
+ // map.addControl(timeControl, 'top-right');
29
+
30
+ export class WxTimeControl {
31
+ private readonly _div: HTMLDivElement;
32
+ private readonly buttonPlayStop: HTMLButtonElement;
33
+ private readonly timesEl: HTMLSelectElement;
34
+ onchange: (time: string) => void = () => {};
35
+ constructor(
36
+ public readonly delay: number,
37
+ private wxsource?: WxTileSource,
38
+ ) {
39
+ const div = document.createElement("div");
40
+ div.className = "mapboxgl-ctrl leaflet-control";
41
+ div.style.borderStyle = "solid";
42
+ div.style.borderColor = "#000";
43
+ div.style.backgroundColor = "#aaaaaaaa";
44
+ div.style.padding = "5px";
45
+ div.style.display = "flex";
46
+ div.style.flexDirection = "column";
47
+ this._div = div;
48
+ div.innerText = "Time animation";
49
+ this.buttonPlayStop = document.createElement("button");
50
+ div.appendChild(this.buttonPlayStop);
51
+ this.timesEl = document.createElement("select");
52
+ div.appendChild(this.timesEl);
53
+ this.timesEl.onchange = async () => {
54
+ if (this.timesEl.style.backgroundColor === "yellow") return;
55
+ this.timesEl.style.backgroundColor = "yellow";
56
+ this.timesEl.value =
57
+ (await this.wxsource?.setTime(this.timesEl.value)) || "";
58
+ this.onchange(this.timesEl.value);
59
+ this.timesEl.style.backgroundColor = "white";
60
+ };
61
+
62
+ this.wxsource && this.updateSource(this.wxsource);
63
+
64
+ this.buttonPlayStop.innerHTML = "Start";
65
+ let t = 0;
66
+ const holder = { abortController: new AbortController() };
67
+
68
+ this.buttonPlayStop.onclick = async () => {
69
+ if (this.buttonPlayStop.innerHTML === "Start") {
70
+ this.buttonPlayStop.innerHTML = "Stop"; // change button text
71
+ const nextTimeStep = async () => {
72
+ // recursive time steps renderer function
73
+ if (!this.wxsource) return;
74
+ this.timesEl.style.backgroundColor = "yellow";
75
+ if (this.buttonPlayStop.innerHTML === "Stop") {
76
+ const nextTimeIndex = t++ % this.wxsource.getAllTimes().length;
77
+ await this.wxsource.setTime(nextTimeIndex, holder.abortController);
78
+ setTimeout(nextTimeStep, this.delay);
79
+ } else {
80
+ await this.wxsource.unsetCoarseLevel();
81
+ }
82
+
83
+ this.timesEl.value = this.wxsource.getTime() || "";
84
+ this.onchange(this.timesEl.value);
85
+ this.timesEl.style.backgroundColor = "white";
86
+ };
87
+
88
+ await this.wxsource?.setCoarseLevel(2);
89
+ nextTimeStep();
90
+ } else {
91
+ holder.abortController.abort();
92
+ holder.abortController = new AbortController(); // recreate new abort controller
93
+ this.buttonPlayStop.innerHTML = "Start";
94
+ }
95
+ };
96
+ }
97
+
98
+ setTimes(times: string[]) {
99
+ this.timesEl.options.length = 0;
100
+ // fill this.timesEl with values from times
101
+ for (let i = 0; i < times.length; i++) {
102
+ const option = document.createElement("option");
103
+ option.value = times[i];
104
+ option.text = times[i];
105
+ this.timesEl.appendChild(option);
106
+ }
107
+
108
+ this.onchange(this.timesEl.value);
109
+ }
110
+
111
+ updateSource(wxsource?: WxTileSource) {
112
+ this.buttonPlayStop.innerHTML = "Start"; // stop time animation
113
+ this.wxsource = wxsource;
114
+ const times = this.wxsource?.getAllTimes() || [];
115
+ this.setTimes(times);
116
+ this.timesEl.value = this.wxsource?.getTime() || "";
117
+ this.onchange(this.timesEl.value);
118
+ wxsource?.on("changed", () => this.updateSource(wxsource));
119
+ }
120
+
121
+ onAdd(/* map */) {
122
+ return this._div;
123
+ }
124
+
125
+ onRemove() {
126
+ this._div.parentNode?.removeChild(this._div);
127
+ }
128
+
129
+ // for Leaflet
130
+ extender() {
131
+ return { onAdd: () => this.onAdd(), onRemove: () => this.onRemove() };
132
+ }
133
+ }