@metoceanapi/wxtiles-leaflet 1.0.14 → 1.1.1

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,326 @@
1
+ import { ColorStyleStrict } from '../src/wxtiles';
2
+ import { WxGetColorSchemes, WxGetColorStyles } from '../src/wxtiles';
3
+ import { ColorStyleWeak } from '../src/utils/wxtools';
4
+ import L from 'leaflet';
5
+
6
+ function createEl<K extends keyof HTMLElementTagNameMap>(container: HTMLElement, tag: K, params?: any): HTMLElementTagNameMap[K] {
7
+ const el = document.createElement(tag);
8
+ Object.assign(el, params);
9
+ container?.appendChild(el);
10
+ return el;
11
+ }
12
+
13
+ function JSONsort(o: any) {
14
+ if (Array.isArray(o)) {
15
+ return o.map(JSONsort);
16
+ } else if (typeof o === 'object' && o !== null) {
17
+ const keys = Object.keys(o)
18
+ // .map((a) => a.toUpperCase())
19
+ .sort((a, b) => {
20
+ const aa = a.toUpperCase();
21
+ const bb = b.toUpperCase();
22
+ return aa == bb ? 0 : aa > bb ? 1 : -1;
23
+ });
24
+ return keys.reduce((a, k) => {
25
+ a[k] = JSONsort(o[k]);
26
+ return a;
27
+ }, {});
28
+ }
29
+ return o;
30
+ }
31
+
32
+ export class Editor {
33
+ onchange?: (style: ColorStyleStrict) => void;
34
+
35
+ editorTextAreaEl: HTMLTextAreaElement;
36
+ editorDivEl: HTMLDivElement;
37
+
38
+ // inputs
39
+ parentInput: HTMLInputElement; // string
40
+
41
+ nameInput: HTMLInputElement; // string
42
+
43
+ fillSelect: HTMLSelectElement; // string
44
+
45
+ isolineColorSelect: HTMLSelectElement; // string
46
+ isolineColorInput: HTMLInputElement;
47
+
48
+ isolineTextInput: HTMLInputElement; // boolean
49
+
50
+ vectorTypeSelect: HTMLSelectElement; // string
51
+
52
+ vectorColorSelect: HTMLSelectElement; // string
53
+ vectorColorInput: HTMLInputElement; // string
54
+
55
+ vectorFactorInput: HTMLInputElement; // number
56
+
57
+ streamLineColorSelect: HTMLSelectElement; // string
58
+ streamLineColorInput: HTMLInputElement; // string
59
+
60
+ streamLineSpeedFactorInput: HTMLInputElement; // number
61
+
62
+ streamLineStaticInput: HTMLInputElement; // boolean
63
+
64
+ showBelowMinInput: HTMLInputElement; // boolean
65
+
66
+ showAboveMaxInput: HTMLInputElement; // boolean
67
+
68
+ colorSchemeSelect: HTMLSelectElement; // string
69
+
70
+ colorsInput: HTMLInputElement; // string[];
71
+
72
+ colorMapInput: HTMLInputElement; // [number, string][];
73
+
74
+ levelsInput: HTMLInputElement; // number[];
75
+
76
+ blurRadiusInput: HTMLInputElement; // number
77
+
78
+ addDegreesInput: HTMLInputElement; // number
79
+
80
+ unitsInput: HTMLInputElement; // string
81
+
82
+ extraUnitsInput: HTMLInputElement; // Units as { [name: string]: [string, number, ?number] };
83
+
84
+ maskSelect: HTMLSelectElement; // string
85
+
86
+ styleBase: ColorStyleStrict;
87
+
88
+ constructor(map: L.Map, parent: HTMLElement, selectStyleEl: HTMLSelectElement, opts: { id: string; className: string }) {
89
+ this.styleBase = WxGetColorStyles()['base'];
90
+
91
+ // helpers
92
+ const onwheel = L.DomEvent.stopPropagation;
93
+ const onmousedown = (e: MouseEvent) => map.dragging.disable() && onwheel(e);
94
+ const onmouseup = (e: MouseEvent) => map.dragging.enable() && onwheel(e);
95
+ // Top level container
96
+ const topmostDivEl = createEl(parent, 'div', { onclick: onwheel, ondblclick: onwheel, onwheel, onmousedown, onmouseup });
97
+ // Button to open/close editor
98
+ const customStyleButtonEl = createEl(topmostDivEl, 'button', {
99
+ id: 'customStyleButton',
100
+ style: 'height: 1.5em; background-color: gray',
101
+ innerText: 'Custom Style',
102
+ });
103
+ // Editor container
104
+ const customStyleHiddableDivEl = createEl(topmostDivEl, 'div', {
105
+ id: 'customStyleDiv',
106
+ style: 'display: none; border-color: black; border-style: solid; border-width: 2px; background-color: rgba(135, 206, 250, 0.8)',
107
+ });
108
+ customStyleButtonEl.addEventListener('click', () => {
109
+ if (customStyleHiddableDivEl.style.display !== 'none') {
110
+ customStyleHiddableDivEl.style.display = 'none';
111
+ customStyleButtonEl.innerHTML = 'show Custom Style Editor';
112
+ } else {
113
+ customStyleHiddableDivEl.style.display = 'flex';
114
+ customStyleButtonEl.innerHTML = 'update Custom Style & Hide';
115
+ selectStyleEl.value = 'custom';
116
+ }
117
+ });
118
+
119
+ // Text area to edit custom style
120
+ this.editorTextAreaEl = createEl(customStyleHiddableDivEl, 'textarea', { id: 'customStyleTextArea', style: 'width: 20vw; height: 70vh' });
121
+ this.editorTextAreaEl.addEventListener('change', () => this._onTextChange());
122
+ // Editor container
123
+ this.editorDivEl = createEl(customStyleHiddableDivEl, 'div', opts);
124
+ // Helpers
125
+ const addLabel = (id: string, br: boolean = true) => {
126
+ createEl(this.editorDivEl, 'label', { htmlFor: id, id: id + 'Label', className: id + 'LabelClass', textContent: id.replace(/Input|Select$/i, '') });
127
+ br && createEl(this.editorDivEl, 'br', {});
128
+ };
129
+
130
+ const addInput = ({
131
+ id,
132
+ type,
133
+ min,
134
+ max,
135
+ step,
136
+ onEvent = 'change',
137
+ br = true,
138
+ }: {
139
+ id: string;
140
+ type: string;
141
+ onEvent?: string;
142
+ min?: string;
143
+ max?: string;
144
+ step?: string;
145
+ br?: boolean;
146
+ }): HTMLInputElement => {
147
+ const el = createEl(this.editorDivEl, 'input', { id, className: id + 'Class', type, min, max, step });
148
+ onEvent !== 'none' && el.addEventListener(onEvent, () => this._onDivChange());
149
+ addLabel(id, br);
150
+ return el;
151
+ };
152
+
153
+ const addSelect = ({ id, opts, onEvent = 'change', br = true }: { id: string; opts: string[]; onEvent?: string; br?: boolean }) => {
154
+ const el = createEl(this.editorDivEl, 'select', { id, className: id + 'Class' });
155
+ onEvent !== 'none' && el.addEventListener(onEvent, () => this._onDivChange());
156
+ opts.forEach((name) => el.options.add(createEl(el, 'option', { value: name, text: name })));
157
+ addLabel(id, br);
158
+ el.selectedIndex = 0;
159
+ return el;
160
+ };
161
+
162
+ const addSelectInput = (id: string): [HTMLSelectElement, HTMLInputElement] => {
163
+ const select = addSelect({ id: id + 'Select', br: false, opts: ['', 'inverted', 'fill', 'none', 'custom'] });
164
+ const input = addInput({ id: id + 'Input', type: 'color', onEvent: 'none' });
165
+ input.addEventListener('change', () => {
166
+ select.value = 'custom';
167
+ this._onDivChange();
168
+ });
169
+ return [select, input];
170
+ };
171
+
172
+ this.parentInput = addInput({ id: 'parentInput', type: 'text' });
173
+ this.nameInput = addInput({ id: 'nameInput', type: 'text' });
174
+ this.fillSelect = addSelect({ id: 'fillSelect', opts: ['', 'gradient', 'solid', 'none'] });
175
+
176
+ [this.isolineColorSelect, this.isolineColorInput] = addSelectInput('isolineColor');
177
+ this.isolineTextInput = addInput({ id: 'isolineTextInput', type: 'checkbox' });
178
+
179
+ this.vectorFactorInput = addInput({ id: 'vectorFactorInput', type: 'number', min: '0.1', max: '10', step: '0.1' });
180
+ this.vectorTypeSelect = addSelect({ id: 'vectorTypeSelect', opts: ['', 'arrows', 'barbs', 'none'] });
181
+ [this.vectorColorSelect, this.vectorColorInput] = addSelectInput('vectorColor');
182
+ [this.streamLineColorSelect, this.streamLineColorInput] = addSelectInput('streamLineColor');
183
+
184
+ this.streamLineSpeedFactorInput = addInput({ id: 'streamLineSpeedFactorInput', type: 'number', min: '0.1', max: '10', step: '0.1' });
185
+ this.streamLineStaticInput = addInput({ id: 'streamLineStaticInput', type: 'checkbox' });
186
+
187
+ this.showBelowMinInput = addInput({ id: 'showBelowMinInput', type: 'checkbox' });
188
+ this.showAboveMaxInput = addInput({ id: 'showAboveMaxInput', type: 'checkbox' });
189
+
190
+ this.colorSchemeSelect = addSelect({ id: 'colorSchemeSelect', opts: Object.keys(WxGetColorSchemes()), onEvent: 'none' });
191
+ this.colorSchemeSelect.addEventListener('change', () => {
192
+ const style = this.getStyle();
193
+ delete style.colors;
194
+ this._setStyleToTextArea(style);
195
+ this.colorsInput.value = '';
196
+ this._onDivChange();
197
+ });
198
+
199
+ this.colorsInput = addInput({ id: 'colorsInput', type: 'text' }); // TODO:
200
+ this.colorMapInput = addInput({ id: 'colorMapInput', type: 'text' }); // TODO:
201
+ this.levelsInput = addInput({ id: 'levelsInput', type: 'text' }); // TODO:
202
+
203
+ this.blurRadiusInput = addInput({ id: 'blurRadiusInput', type: 'range', onEvent: 'input', min: '0', max: '10', step: '1' });
204
+ this.addDegreesInput = addInput({ id: 'addDegreesInput', type: 'number', min: '0', max: '360', step: '1' });
205
+ this.unitsInput = addInput({ id: 'unitsInput', type: 'text' });
206
+ this.extraUnitsInput = addInput({ id: 'extraUnitsInput', type: 'text' });
207
+ this.maskSelect = addSelect({ id: 'maskSelect', opts: ['', 'none', 'sea', 'land'] });
208
+ }
209
+
210
+ getStyle(): ColorStyleStrict {
211
+ // deep copy from _getStyleFromTextArea, _getStyleFromDiv not needed as affected the textarea already
212
+ return Object.assign({}, this.styleBase, this._getStyleFromTextArea() /*, this._getStyleFromDiv()*/);
213
+ }
214
+
215
+ setStyle(style: ColorStyleWeak): void {
216
+ this._setStyleToTextArea(style);
217
+ this._setStyleToDiv(style);
218
+ }
219
+
220
+ // this.editorTextAreaEl is always modifyed by StyleFromDiv at any changes in the div
221
+ protected _getStyleFromTextArea(): ColorStyleWeak {
222
+ try {
223
+ return JSON.parse(this.editorTextAreaEl.value);
224
+ } catch (e) {
225
+ return e;
226
+ }
227
+ }
228
+
229
+ protected _getStyleFromDiv(): ColorStyleWeak {
230
+ const objFromValue = (field: string) => {
231
+ try {
232
+ return this[field].value ? JSON.parse(this[field].value) : undefined;
233
+ } catch (e) {
234
+ console.log(field, ' : parsing error: ', e);
235
+ return undefined;
236
+ }
237
+ };
238
+
239
+ const colorFromSelectInput = (select: HTMLSelectElement, input: HTMLInputElement): string | undefined => {
240
+ if (select.value === 'custom') {
241
+ return input.value;
242
+ }
243
+ return select.value || undefined;
244
+ };
245
+
246
+ const style: ColorStyleWeak = {
247
+ parent: this.parentInput.value, // string;
248
+ name: this.nameInput.value || undefined, //string;
249
+ fill: this.fillSelect.value || undefined, //string;
250
+ isolineColor: colorFromSelectInput(this.isolineColorSelect, this.isolineColorInput), //string;
251
+ isolineText: this.isolineTextInput.checked, //boolean;
252
+ vectorType: this.vectorTypeSelect.value || undefined, //string;
253
+ vectorColor: colorFromSelectInput(this.vectorColorSelect, this.vectorColorInput), //string;
254
+ vectorFactor: +this.vectorFactorInput.value, //number;
255
+ streamLineColor: colorFromSelectInput(this.streamLineColorSelect, this.streamLineColorInput), //string;
256
+ streamLineSpeedFactor: +this.streamLineSpeedFactorInput.value, //number;
257
+ streamLineStatic: this.streamLineStaticInput.checked, //boolean;
258
+ showBelowMin: this.showBelowMinInput.checked, //boolean;
259
+ showAboveMax: this.showAboveMaxInput.checked, //boolean;
260
+ colorScheme: this.colorSchemeSelect.value || undefined, //string;
261
+ colors: objFromValue('colorsInput'), // string[];
262
+ colorMap: objFromValue('colorMapInput'), // [number, string][];
263
+ levels: objFromValue('levelsInput'), // number[];
264
+ blurRadius: +this.blurRadiusInput.value, //number;
265
+ addDegrees: +this.addDegreesInput.value, //number;
266
+ units: this.unitsInput.value || undefined, //string;
267
+ extraUnits: objFromValue('extraUnitsInput'), // Units; //{ [name: string]: [string, number, ?number] };
268
+ mask: this.maskSelect.value || undefined, // string;
269
+ };
270
+
271
+ return style;
272
+ }
273
+
274
+ protected _setStyleToTextArea(style: ColorStyleWeak): void {
275
+ this.editorTextAreaEl.value = JSON.stringify(JSONsort(style), null, 2);
276
+ }
277
+
278
+ protected _setStyleToDiv(style: ColorStyleWeak): void {
279
+ this.parentInput.value = style.parent || ''; // string;
280
+ this.nameInput.value = style.name || ''; //string;
281
+ this.fillSelect.value = style.fill || ''; //string;
282
+ this.isolineColorSelect.value = style.isolineColor?.[0] === '#' ? 'custom' : style.isolineColor || ''; //string;
283
+ this.isolineColorInput.value = style.isolineColor?.[0] === '#' ? style.isolineColor : ''; //string;
284
+ this.isolineTextInput.checked = style.isolineText || false; //boolean;
285
+ this.vectorTypeSelect.value = style.vectorType || ''; //string;
286
+ this.vectorColorSelect.value = style.vectorColor?.[0] === '#' ? 'custom' : style.vectorColor || ''; //string;
287
+ this.vectorColorInput.value = style.vectorColor?.[0] === '#' ? style.vectorColor : ''; //string;
288
+ this.vectorFactorInput.value = style.vectorFactor?.toString() || '1'; //number;
289
+ this.streamLineColorSelect.value = style.streamLineColor?.[0] === '#' ? 'custom' : style.streamLineColor || ''; //string;
290
+ this.streamLineColorInput.value = style.streamLineColor?.[0] === '#' ? style.streamLineColor : ''; //string;
291
+ this.streamLineSpeedFactorInput.value = style.streamLineSpeedFactor?.toString() || '1'; //number;
292
+ this.streamLineStaticInput.checked = style.streamLineStatic || false; //boolean;
293
+ this.showBelowMinInput.checked = style.showBelowMin || false; //boolean;
294
+ this.showAboveMaxInput.checked = style.showAboveMax || false; //boolean;
295
+ this.colorSchemeSelect.value = style.colorScheme || ''; //string;
296
+ this.colorsInput.value = style.colors?.length ? JSON.stringify(style.colors) : ''; // string[];
297
+ this.colorMapInput.value = style.colorMap?.length ? JSON.stringify(style.colorMap) : ''; // [number, string][];
298
+ this.levelsInput.value = style.levels?.length ? JSON.stringify(style.levels) : ''; // number[];
299
+ this.blurRadiusInput.value = style.blurRadius?.toString() || ''; //number;
300
+ this.addDegreesInput.value = style.addDegrees?.toString() || ''; //number;
301
+ this.unitsInput.value = style.units || ''; //string;
302
+ this.extraUnitsInput.value = style.extraUnits ? JSON.stringify(style.extraUnits) : ''; // Units; //{ [name: string]: [string, number, ?number] };
303
+ this.maskSelect.value = style.mask || ''; // string;
304
+ }
305
+
306
+ protected _onDivChange(): void {
307
+ const cleanUp = (obj: any) => {
308
+ Object.keys(obj).forEach((key) => {
309
+ if (obj[key] === undefined) delete obj[key];
310
+ });
311
+ return obj;
312
+ };
313
+
314
+ // update text area
315
+ const style = Object.assign(this._getStyleFromTextArea(), cleanUp(this._getStyleFromDiv()));
316
+ this.editorTextAreaEl.value = JSON.stringify(style, null, 2);
317
+ this.onchange?.(this.getStyle());
318
+ }
319
+
320
+ protected _onTextChange(): void {
321
+ // update div
322
+ const style = this._getStyleFromTextArea();
323
+ this._setStyleToDiv(style);
324
+ this.onchange?.(this.getStyle());
325
+ }
326
+ }