@metoceanapi/wxtiles-common 0.0.8 → 0.0.11-0

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.
Files changed (162) hide show
  1. package/.github/workflows/lint.yml +1 -1
  2. package/.github/workflows/validate-pull-request-title.yml +17 -0
  3. package/LICENSE.txt +1 -1
  4. package/biome.json +26 -1
  5. package/controls/WxAPIControl/WxAPIControl-busy.ts +16 -0
  6. package/controls/WxAPIControl/WxAPIControl-datasets.ts +102 -0
  7. package/controls/WxAPIControl/WxAPIControl-filter.ts +66 -0
  8. package/controls/WxAPIControl/WxAPIControl-variables.ts +73 -0
  9. package/controls/WxAPIControl.ts +87 -120
  10. package/controls/WxInfoControl.ts +87 -2
  11. package/controls/WxLegendControl.ts +28 -2
  12. package/controls/WxStyleEditorControl.ts +129 -7
  13. package/controls/WxTimeControl.ts +133 -5
  14. package/docs/docs/.nojekyll +1 -0
  15. package/docs/docs/assets/highlight.css +92 -0
  16. package/docs/docs/assets/icons.svg +1 -0
  17. package/docs/docs/assets/style.css +1610 -0
  18. package/docs/docs/classes/controls_WxAPIControl.WxAPIControl.html +28 -0
  19. package/docs/docs/classes/controls_WxInfoControl.WxInfoControl.html +20 -0
  20. package/docs/docs/classes/controls_WxLegendControl.WxLegendControl.html +19 -0
  21. package/docs/docs/classes/controls_WxStyleEditorControl.WxStyleEditorControl.html +111 -0
  22. package/docs/docs/classes/controls_WxTimeControl.WxTimeControl.html +28 -0
  23. package/docs/docs/classes/managers_BaseDatasetManager.BaseDatasetManager.html +104 -0
  24. package/docs/docs/classes/managers_WxAllDatasetsManager.WxAllDatasetsManager.html +24 -0
  25. package/docs/docs/classes/types.WxRasterDataCache.html +27 -0
  26. package/docs/docs/classes/utils_RawCLUT.RawCLUT.html +18 -0
  27. package/docs/docs/classes/utils_qtree.QTree.html +22 -0
  28. package/docs/docs/classes/utils_wxtools.DataIntegral.html +10 -0
  29. package/docs/docs/classes/wxapi.WxAPI.html +45 -0
  30. package/docs/docs/classes/wxlayer_loader.Loader.html +31 -0
  31. package/docs/docs/classes/wxlayer_painter.Painter.html +20 -0
  32. package/docs/docs/classes/wxlayer_wxlayer.WxLayer.html +57 -0
  33. package/docs/docs/enums/utils_qtree.TileType.html +5 -0
  34. package/docs/docs/functions/utils_RawCLUT.WxCreateLegend.html +5 -0
  35. package/docs/docs/functions/utils_mercator.LatLonToPixels.html +6 -0
  36. package/docs/docs/functions/utils_mercator.PixelsToLatLon.html +6 -0
  37. package/docs/docs/functions/utils_mercator.PixelsToLonLat.html +6 -0
  38. package/docs/docs/functions/utils_mercator.coordToPixel.html +5 -0
  39. package/docs/docs/functions/utils_tests_wxtools.tests.html +1 -0
  40. package/docs/docs/functions/utils_wxtools.HEXtoRGBA.html +3 -0
  41. package/docs/docs/functions/utils_wxtools.HashXYZ.html +4 -0
  42. package/docs/docs/functions/utils_wxtools.RGBAtoHEX.html +3 -0
  43. package/docs/docs/functions/utils_wxtools.RGBtoHEX.html +3 -0
  44. package/docs/docs/functions/utils_wxtools.WXLOG.html +2 -0
  45. package/docs/docs/functions/utils_wxtools.WxGetColorSchemes.html +2 -0
  46. package/docs/docs/functions/utils_wxtools.WxGetColorStyles.html +2 -0
  47. package/docs/docs/functions/utils_wxtools.WxTilesLibSetup.html +3 -0
  48. package/docs/docs/functions/utils_wxtools.WxTilesLogging.html +5 -0
  49. package/docs/docs/functions/utils_wxtools.blurData.html +7 -0
  50. package/docs/docs/functions/utils_wxtools.buildIntegralPare.html +6 -0
  51. package/docs/docs/functions/utils_wxtools.cacheUriPromise.html +2 -0
  52. package/docs/docs/functions/utils_wxtools.create2DContext.html +6 -0
  53. package/docs/docs/functions/utils_wxtools.createEl.html +5 -0
  54. package/docs/docs/functions/utils_wxtools.createLevels.html +6 -0
  55. package/docs/docs/functions/utils_wxtools.fetchJson.html +6 -0
  56. package/docs/docs/functions/utils_wxtools.getClosestTimeString.html +5 -0
  57. package/docs/docs/functions/utils_wxtools.loadDataIntegral.html +5 -0
  58. package/docs/docs/functions/utils_wxtools.loadImage.html +4 -0
  59. package/docs/docs/functions/utils_wxtools.loadImageData.html +5 -0
  60. package/docs/docs/functions/utils_wxtools.makeConverter.html +9 -0
  61. package/docs/docs/functions/utils_wxtools.mixColor.html +3 -0
  62. package/docs/docs/functions/utils_wxtools.refineColor.html +3 -0
  63. package/docs/docs/functions/utils_wxtools.uriXYZ.html +5 -0
  64. package/docs/docs/functions/wxlayer_WxImplementation.WxLayerBaseImplementationMixin.html +4 -0
  65. package/docs/docs/functions/wxlayer_loadertools.applyMask.html +10 -0
  66. package/docs/docs/functions/wxlayer_loadertools.makeBox.html +4 -0
  67. package/docs/docs/functions/wxlayer_loadertools.splitCoords.html +6 -0
  68. package/docs/docs/functions/wxlayer_loadertools.subData.html +5 -0
  69. package/docs/docs/functions/wxlayer_loadertools.subDataDegree.html +5 -0
  70. package/docs/docs/functions/wxlayer_loadertools.subMask.html +5 -0
  71. package/docs/docs/hierarchy.html +1 -0
  72. package/docs/docs/index.html +7 -0
  73. package/docs/docs/interfaces/types.IsoInfo.html +8 -0
  74. package/docs/docs/interfaces/types.SLinePoint.html +4 -0
  75. package/docs/docs/interfaces/types.WxAPIOptions.html +37 -0
  76. package/docs/docs/interfaces/types.WxAllBoundariesMeta.html +8 -0
  77. package/docs/docs/interfaces/types.WxAllDatasetsShortMetas.html +4 -0
  78. package/docs/docs/interfaces/types.WxBoundaryMeta.html +10 -0
  79. package/docs/docs/interfaces/types.WxData.html +10 -0
  80. package/docs/docs/interfaces/types.WxDataSetManagerOptions.html +14 -0
  81. package/docs/docs/interfaces/types.WxDatasetMeta.html +20 -0
  82. package/docs/docs/interfaces/types.WxDatasetShortMeta.html +9 -0
  83. package/docs/docs/interfaces/types.WxLayerAPI.html +24 -0
  84. package/docs/docs/interfaces/types.WxLayerBaseAPI.html +16 -0
  85. package/docs/docs/interfaces/types.WxLayerOptions.html +12 -0
  86. package/docs/docs/interfaces/types.WxLngLat.html +6 -0
  87. package/docs/docs/interfaces/types.WxRasterData.html +13 -0
  88. package/docs/docs/interfaces/types.WxRequestInit.html +3 -0
  89. package/docs/docs/interfaces/types.WxTileInfo.html +18 -0
  90. package/docs/docs/interfaces/types.WxVariableMeta.html +13 -0
  91. package/docs/docs/interfaces/types.WxVariablesMetas.html +4 -0
  92. package/docs/docs/interfaces/utils_RawCLUT.WxLegend.html +14 -0
  93. package/docs/docs/interfaces/utils_RawCLUT.WxTick.html +10 -0
  94. package/docs/docs/interfaces/utils_qtree.Tree.html +7 -0
  95. package/docs/docs/interfaces/utils_wxtools.CachedUriLoaderPromiseFunc.html +2 -0
  96. package/docs/docs/interfaces/utils_wxtools.ColorStylesIncomplete.html +2 -0
  97. package/docs/docs/interfaces/utils_wxtools.Converter.html +5 -0
  98. package/docs/docs/interfaces/utils_wxtools.DataPicture.html +6 -0
  99. package/docs/docs/interfaces/utils_wxtools.IntegralPare.html +6 -0
  100. package/docs/docs/interfaces/utils_wxtools.WxColorSchemes.html +3 -0
  101. package/docs/docs/interfaces/utils_wxtools.WxColorStyleStrict.html +95 -0
  102. package/docs/docs/interfaces/utils_wxtools.WxColorStyleWeak.html +95 -0
  103. package/docs/docs/interfaces/utils_wxtools.WxColorStylesStrict.html +2 -0
  104. package/docs/docs/interfaces/utils_wxtools.WxColorStylesWeakMixed.html +2 -0
  105. package/docs/docs/interfaces/utils_wxtools.WxTilesLibOptions.html +8 -0
  106. package/docs/docs/interfaces/utils_wxtools.WxUnits.html +2 -0
  107. package/docs/docs/interfaces/utils_wxtools.XYZ.html +5 -0
  108. package/docs/docs/modules/controls_WxAPIControl.html +1 -0
  109. package/docs/docs/modules/controls_WxInfoControl.html +1 -0
  110. package/docs/docs/modules/controls_WxLegendControl.html +1 -0
  111. package/docs/docs/modules/controls_WxStyleEditorControl.html +1 -0
  112. package/docs/docs/modules/controls_WxTimeControl.html +1 -0
  113. package/docs/docs/modules/defaults_colorschemes.html +1 -0
  114. package/docs/docs/modules/defaults_styles.html +1 -0
  115. package/docs/docs/modules/defaults_uconv.html +1 -0
  116. package/docs/docs/modules/managers_BaseDatasetManager.html +1 -0
  117. package/docs/docs/modules/managers_WxAllDatasetsManager.html +1 -0
  118. package/docs/docs/modules/types.html +1 -0
  119. package/docs/docs/modules/utils_RawCLUT.html +1 -0
  120. package/docs/docs/modules/utils_mercator.html +1 -0
  121. package/docs/docs/modules/utils_qtree.html +1 -0
  122. package/docs/docs/modules/utils_tests_wxtools.html +1 -0
  123. package/docs/docs/modules/utils_wxtools.html +1 -0
  124. package/docs/docs/modules/wxapi.html +1 -0
  125. package/docs/docs/modules/wxlayer_WxImplementation.html +1 -0
  126. package/docs/docs/modules/wxlayer_loader.html +1 -0
  127. package/docs/docs/modules/wxlayer_loadertools.html +1 -0
  128. package/docs/docs/modules/wxlayer_painter.html +1 -0
  129. package/docs/docs/modules/wxlayer_wxlayer.html +1 -0
  130. package/docs/docs/modules.html +1 -0
  131. package/docs/docs/types/types.ListenerMethod.html +1 -0
  132. package/docs/docs/types/types.SLine.html +2 -0
  133. package/docs/docs/types/types.WxDate.html +9 -0
  134. package/docs/docs/types/types.WxEventType.html +1 -0
  135. package/docs/docs/types/types.WxInstances.html +2 -0
  136. package/docs/docs/types/types.WxLayerVarsNames.html +2 -0
  137. package/docs/docs/types/types.WxURIs.html +2 -0
  138. package/docs/docs/types/utils_qtree.SubTrees.html +2 -0
  139. package/docs/docs/types/utils_qtree.SubTreesN.html +2 -0
  140. package/docs/docs/types/utils_qtree.TreeN.html +2 -0
  141. package/docs/docs/types/utils_wxtools.DataIntegrals.html +2 -0
  142. package/docs/docs/types/utils_wxtools.DataPictures.html +2 -0
  143. package/docs/docs/types/utils_wxtools.WxUnitTuple.html +2 -0
  144. package/docs/docs/types/utils_wxtools.colorMapTuple.html +2 -0
  145. package/docs/docs/variables/defaults_colorschemes.__colorSchemes_default_preset.html +1 -0
  146. package/docs/docs/variables/defaults_styles.__colorStyles_default_preset.html +1 -0
  147. package/docs/docs/variables/defaults_uconv.__units_default_preset.html +1 -0
  148. package/managers/BaseDatasetManager.ts +272 -0
  149. package/managers/WxAllDatasetsManager.ts +60 -0
  150. package/package.json +6 -3
  151. package/tsconfig.json +31 -0
  152. package/typedoc.json +4 -0
  153. package/types.ts +471 -0
  154. package/utils/qtree.ts +2 -1
  155. package/utils/tests_wxtools.ts +10 -10
  156. package/utils/wxtools.ts +1 -0
  157. package/wxapi.ts +222 -0
  158. package/wxlayer/WxImplementation.ts +374 -0
  159. package/wxlayer/loader.ts +305 -0
  160. package/wxlayer/loadertools.ts +380 -0
  161. package/wxlayer/painter.ts +451 -0
  162. package/wxlayer/wxlayer.ts +379 -0
@@ -1,4 +1,4 @@
1
- name: Lint
1
+ name: Biome Lint
2
2
 
3
3
  on:
4
4
  pull_request:
@@ -0,0 +1,17 @@
1
+ name: "Validate Pull Request Title"
2
+
3
+ on:
4
+ pull_request_target:
5
+ types: [opened, edited, synchronize]
6
+
7
+ permissions:
8
+ pull-requests: read
9
+
10
+ jobs:
11
+ main:
12
+ name: Validate PR title
13
+ runs-on: ubuntu-latest
14
+ steps:
15
+ - uses: amannn/action-semantic-pull-request@v5
16
+ env:
17
+ GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
package/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- WxTiles Leaflet - Weather Tiles
1
+ WxTiles Common - Weather Tiles
2
2
  Copyright (C) 2022 Meteorological Service of New Zealand Limited
3
3
 
4
4
  This program is free software: you can redistribute it and/or modify
package/biome.json CHANGED
@@ -6,7 +6,32 @@
6
6
  "linter": {
7
7
  "enabled": true,
8
8
  "rules": {
9
- "recommended": true
9
+ "recommended": true,
10
+ "suspicious": {
11
+ "noExplicitAny": "off"
12
+ },
13
+ "style": {
14
+ "noParameterAssign": "off"
15
+ }
16
+ }
17
+ },
18
+ "files": {
19
+ "ignore": ["docs", "dist"]
20
+ },
21
+ "javascript": {
22
+ "formatter": {
23
+ "trailingCommas": "all",
24
+ "quoteStyle": "single",
25
+ "jsxQuoteStyle": "single",
26
+ "arrowParentheses": "always",
27
+ "lineWidth": 160,
28
+ "indentStyle": "space",
29
+ "indentWidth": 2
30
+ }
31
+ },
32
+ "json": {
33
+ "formatter": {
34
+ "indentStyle": "space"
10
35
  }
11
36
  }
12
37
  }
@@ -0,0 +1,16 @@
1
+ /** Simple busy control to identify if action in progress */
2
+ export class ControlBusy {
3
+ public _busy: boolean;
4
+ constructor(private container: HTMLDivElement) {
5
+ this._busy = false;
6
+ }
7
+
8
+ /**
9
+ * Set busy state
10
+ * @param b busy state - true if busy
11
+ */
12
+ setBusy(b: boolean) {
13
+ this._busy = b;
14
+ this.container.style.backgroundColor = b ? "yellow" : "lightgray";
15
+ }
16
+ }
@@ -0,0 +1,102 @@
1
+ import type { WxAPI } from "../../wxapi";
2
+ import type { ControlElement } from "../WxAPIControl";
3
+ import type { ControlBusy } from "./WxAPIControl-busy";
4
+ import { WxAPIControlVariable } from "./WxAPIControl-variables";
5
+
6
+ /** Control for updating form filed datasets */
7
+ export class ControlDataset implements ControlElement {
8
+ public datasetInput: HTMLSelectElement;
9
+ public variables: WxAPIControlVariable;
10
+
11
+ /**
12
+ * Constructor
13
+ * @param wxapi WxAPI object - API to get datasets and variables
14
+ * @param busy: ControlBusy - if the control is busy
15
+ * @param onChange function - middleware to be run
16
+ */
17
+ constructor(
18
+ private readonly wxapi: WxAPI,
19
+ private readonly busy: ControlBusy,
20
+ private onChange: () => Promise<void> | undefined
21
+ ) {
22
+ this.datasetInput = document.createElement("select");
23
+ this.variables = new WxAPIControlVariable(wxapi, busy, this.onChange);
24
+ }
25
+
26
+ public attachToDom(container: HTMLDivElement) {
27
+ const datasetslabel = document.createElement("label");
28
+ datasetslabel.innerText = "Dataset";
29
+ container.appendChild(datasetslabel);
30
+ container.appendChild(this.datasetInput);
31
+ this.variables.attachToDom(container);
32
+ }
33
+
34
+ public getValue() {
35
+ return this.datasetInput.value;
36
+ }
37
+
38
+ public setValue(value: string) {
39
+ this.datasetInput.value = value;
40
+ }
41
+
42
+ /**
43
+ * Fill datasets
44
+ * @param dataset dataset - dataset name to be selected
45
+ */
46
+ private async fillDatasets({ dataset }: { dataset?: string }): Promise<void> {
47
+ this.datasetInput.options.length = 0;
48
+ const datasetsNames = await this.wxapi.getAllDatasetsNames();
49
+ for (const dataset of datasetsNames) {
50
+ const option = document.createElement("option");
51
+ option.value = dataset;
52
+ option.text = dataset;
53
+ this.datasetInput.appendChild(option);
54
+ }
55
+
56
+ if (dataset) {
57
+ this.datasetInput.value = dataset;
58
+ }
59
+ }
60
+
61
+ /**
62
+ * Dynamically update datasets
63
+ */
64
+ public async handleChange() {
65
+ if (!this.busy._busy) {
66
+ this.busy.setBusy(true);
67
+ const dataset = this.datasetInput.value;
68
+ await this.variables.fillVariables(dataset);
69
+
70
+ const variables = this.variables.variablesInput.value;
71
+ await this.onChange?.();
72
+ this.datasetInput.value = dataset;
73
+ this.variables.variablesInput.value = variables;
74
+ this.busy.setBusy(false);
75
+ }
76
+ }
77
+
78
+ public async init({
79
+ dataset,
80
+ variable,
81
+ cb,
82
+ }: {
83
+ dataset?: string;
84
+ variable?: string;
85
+ cb?: () => void;
86
+ }) {
87
+ this.variables.variablesInput.addEventListener("change", async () =>
88
+ this.variables.handleChange()
89
+ );
90
+
91
+ this.datasetInput.addEventListener("change", async () => {
92
+ await this.handleChange();
93
+ });
94
+
95
+ this.setValue(dataset || "");
96
+ this.variables.setValue(variable || "");
97
+ await this.fillDatasets({ dataset });
98
+ await this.variables.fillVariables(this.getValue());
99
+ await this.variables.handleChange();
100
+ await cb?.();
101
+ }
102
+ }
@@ -0,0 +1,66 @@
1
+ import type { ControlElement } from "../WxAPIControl";
2
+
3
+ /** Generate filter input / value */
4
+ const createFilterInput = () => {
5
+ // Add filter input field
6
+ const filter = document.createElement("input");
7
+ filter.type = "text";
8
+ filter.placeholder = "Type to filter datasets...";
9
+ filter.style.marginBottom = "5px";
10
+ return filter;
11
+ };
12
+
13
+ /** Control for updating form datasets based on filtering */
14
+ export class ControlFilter implements ControlElement {
15
+ private filterInput: HTMLInputElement;
16
+ constructor(filterValue?: string) {
17
+ this.filterInput = createFilterInput();
18
+ this.filterInput.value = filterValue || "";
19
+ }
20
+
21
+ public getValue() {
22
+ return this.filterInput.value.trim();
23
+ }
24
+
25
+ public setValue(value: string) {
26
+ this.filterInput.value = value;
27
+ }
28
+
29
+ public attachToDom(container: HTMLDivElement) {
30
+ // Add filter label
31
+ const filterlabel = document.createElement("label");
32
+ filterlabel.innerText = "Filter";
33
+ container.appendChild(filterlabel);
34
+ container.appendChild(this.filterInput);
35
+ }
36
+
37
+ public readonly init = ({ datasets }: { datasets: HTMLSelectElement }) => {
38
+ if (this.getValue()) {
39
+ this.filterUpdate({ datasets });
40
+ }
41
+ this.filterInput.addEventListener("input", () => {
42
+ this.filterUpdate({ datasets });
43
+ });
44
+ };
45
+
46
+ /**
47
+ * Filter dataset based on input value
48
+ * @param dataset - dataset to be used to populate variables
49
+ */
50
+ private filterUpdate = ({ datasets }: { datasets: HTMLSelectElement }) => {
51
+ const filterValue = this.getValue();
52
+
53
+ if (filterValue) {
54
+ // Filter the dataset options
55
+ for (const option of Array.from(datasets.options)) {
56
+ const isVisible = option.text.includes(filterValue);
57
+ option.style.display = isVisible ? "" : "none";
58
+ }
59
+ } else {
60
+ // Filter the dataset options
61
+ for (const option of Array.from(datasets.options)) {
62
+ (option as HTMLOptionElement).style.display = "";
63
+ }
64
+ }
65
+ };
66
+ }
@@ -0,0 +1,73 @@
1
+ import type { WxAPI } from "../../wxapi";
2
+ import type { ControlElement } from "../WxAPIControl";
3
+ import type { ControlBusy } from "./WxAPIControl-busy";
4
+
5
+ /** Control for updating form filed variables */
6
+ export class WxAPIControlVariable implements ControlElement {
7
+ public variablesInput: HTMLSelectElement;
8
+
9
+ /**
10
+ * Constructor
11
+ * @param wxapi WxAPI object - API to get datasets and variables
12
+ * @param busy: ControlBusy - if the control is busy
13
+ * @param onChange function - middleware to be run
14
+ */
15
+ constructor(
16
+ private readonly wxapi: WxAPI,
17
+ private readonly busy: ControlBusy,
18
+ private onChange: () => Promise<void> | undefined
19
+ ) {
20
+ this.variablesInput = document.createElement("select");
21
+ }
22
+
23
+ public getValue() {
24
+ return this.variablesInput.value;
25
+ }
26
+
27
+ public setValue(value: string) {
28
+ this.variablesInput.value = value;
29
+ }
30
+
31
+ public attachToDom(container: HTMLDivElement) {
32
+ const variableslabel = document.createElement("label");
33
+ variableslabel.innerText = "Variable";
34
+ container.appendChild(variableslabel);
35
+ container.appendChild(this.variablesInput);
36
+ }
37
+
38
+ public async handleChange() {
39
+ if (this.busy._busy) return;
40
+ this.busy.setBusy(true);
41
+ const variables = this.variablesInput.value;
42
+ await this.onChange?.();
43
+ this.variablesInput.value = variables;
44
+ this.busy.setBusy(false);
45
+ }
46
+
47
+ /**
48
+ * Fill variables for a dataset
49
+ * @param dataset - dataset to be used to populate variables
50
+ */
51
+ async fillVariables(dataset: string): Promise<void> {
52
+ const variable = this.getValue().trim();
53
+ this.variablesInput.options.length = 0;
54
+ // const dataset = this.datasets.value;
55
+ const variables = await this.wxapi.getDatasetAllVariables(dataset);
56
+ if (variables) {
57
+ for (const variable of variables) {
58
+ const option = document.createElement("option");
59
+ option.value = variable;
60
+ option.text = variable;
61
+ this.variablesInput.appendChild(option);
62
+ }
63
+ }
64
+
65
+ const isVariableInDataset = this.variablesInput.options.namedItem(variable);
66
+
67
+ if (isVariableInDataset) {
68
+ this.variablesInput.value = variable;
69
+ } else {
70
+ this.variablesInput.value = this.variablesInput.options[0]?.value || "";
71
+ }
72
+ }
73
+ }
@@ -1,7 +1,15 @@
1
- declare interface WxAPI {
2
- getAllDatasetsNames(): Promise<string[]>;
3
- getDatasetAllVariables(string): Promise<string[] | undefined>;
4
- };
1
+ import type { WxAPI } from '../wxapi';
2
+ import { ControlBusy } from './WxAPIControl/WxAPIControl-busy';
3
+ import { ControlDataset } from './WxAPIControl/WxAPIControl-datasets';
4
+ import { ControlFilter } from './WxAPIControl/WxAPIControl-filter';
5
+
6
+ export interface ControlElement {
7
+ attachToDom(container: HTMLDivElement): void;
8
+ getValue(): string;
9
+ setValue(value: string): void;
10
+ }
11
+
12
+ export type onChangeControl = (dataset: string, variable: string, resetStyleAndFlyTo?: boolean) => Promise<void>;
5
13
 
6
14
  // // Leaflet
7
15
  // const apiControl = new WxAPIControl();
@@ -11,121 +19,80 @@ declare interface WxAPI {
11
19
  // const apiControl = new WxAPIControl();
12
20
  // map.addControl(apiControl, 'top-right');
13
21
 
22
+ /** Control for selecting dataset and variable */
14
23
  export class WxAPIControl {
15
- private readonly _div: HTMLDivElement;
16
- readonly datasets: HTMLSelectElement;
17
- readonly variables: HTMLSelectElement;
18
- protected _busy = false;
19
-
20
- onchange?: (
21
- dataset: string,
22
- variable: string,
23
- resetStyleAndFlyTo?: boolean,
24
- ) => Promise<void>;
25
-
26
- constructor(
27
- private readonly wxapi: WxAPI,
28
- dataset?: string,
29
- variable?: string,
30
- ) {
31
- const div = document.createElement("div");
32
- div.className = "mapboxgl-ctrl leaflet-control";
33
- div.style.borderStyle = "solid";
34
- div.style.borderColor = "#000";
35
- div.style.backgroundColor = "#aaaaaaaa";
36
- div.style.padding = "5px";
37
- div.style.display = "flex";
38
- div.style.flexDirection = "column";
39
- this._div = div;
40
-
41
- const datasetslabel = document.createElement("label");
42
- datasetslabel.innerText = "Dataset";
43
- div.appendChild(datasetslabel);
44
-
45
- this.datasets = document.createElement("select");
46
- div.appendChild(this.datasets);
47
- this.fillDatasets(dataset, variable);
48
- this.datasets.addEventListener("change", async () => {
49
- if (this._busy) return;
50
- this.setBusy(true);
51
- const dataset = this.datasets.value;
52
- await this.fillVariables();
53
- const variables = this.variables.value;
54
- await this.onchange?.(this.datasets.value, this.variables.value);
55
- this.datasets.value = dataset;
56
- this.variables.value = variables;
57
- this.setBusy(false);
58
- });
59
-
60
- const variableslabel = document.createElement("label");
61
- variableslabel.innerText = "Variable";
62
- div.appendChild(variableslabel);
63
-
64
- this.variables = document.createElement("select");
65
- div.appendChild(this.variables);
66
- this.variables.addEventListener("change", async () => {
67
- if (this._busy) return;
68
- this.setBusy(true);
69
- const variables = this.variables.value;
70
- await this.onchange?.(this.datasets.value, this.variables.value);
71
- this.variables.value = variables;
72
- this.setBusy(false);
73
- });
74
-
75
- this.datasets.value = dataset || "";
76
- this.variables.value = variable || "";
77
- }
78
-
79
- setBusy(b: boolean) {
80
- this._busy = b;
81
- this._div.style.backgroundColor = b ? "yellow" : "lightgray";
82
- }
83
-
84
- async fillDatasets(dataset?: string, variable?: string): Promise<void> {
85
- this.datasets.options.length = 0;
86
- const datasetsNames = await this.wxapi.getAllDatasetsNames();
87
- for (const dataset of datasetsNames) {
88
- const option = document.createElement("option");
89
- option.value = dataset;
90
- option.text = dataset;
91
- this.datasets.appendChild(option);
92
- }
93
-
94
- if (dataset) {
95
- this.datasets.value = dataset;
96
- }
97
-
98
- await this.fillVariables(variable);
99
- }
100
-
101
- async fillVariables(variable?: string): Promise<void> {
102
- this.variables.options.length = 0;
103
- const dataset = this.datasets.value;
104
- const variables = await this.wxapi.getDatasetAllVariables(dataset);
105
- if (variables) {
106
- for (const variable of variables) {
107
- const option = document.createElement("option");
108
- option.value = variable;
109
- option.text = variable;
110
- this.variables.appendChild(option);
111
- }
112
- }
113
-
114
- if (variable && this.variables.options.length) {
115
- this.variables.value = variable;
116
- }
117
- }
118
-
119
- onAdd(/* map */) {
120
- return this._div;
121
- }
122
-
123
- onRemove() {
124
- this._div.parentNode?.removeChild(this._div);
125
- }
126
-
127
- // for Leaflet
128
- extender() {
129
- return { onAdd: () => this.onAdd(), onRemove: () => this.onRemove() };
130
- }
24
+ public busy: ControlBusy;
25
+ private readonly _div: HTMLDivElement;
26
+ private readonly filter: ControlFilter;
27
+ private readonly dataset: ControlDataset;
28
+
29
+ /** Callback for dataset and variable */
30
+ onchange?: onChangeControl;
31
+
32
+ /**
33
+ * Constructor
34
+ * @param wxapi WxAPI object - API to get datasets and variables
35
+ * @param dataset dataset - dataset name to be selected
36
+ * @param variable variable - variable name to be selected
37
+ * @param opts object - options for the control
38
+ * @param opts.filter string - initial filter value for datasets
39
+ */
40
+ constructor(
41
+ wxapi: WxAPI,
42
+ dataset?: string,
43
+ variable?: string,
44
+ opts: {
45
+ filter?: string;
46
+ } = {},
47
+ ) {
48
+ const div = document.createElement('div');
49
+ div.className = 'mapboxgl-ctrl leaflet-control';
50
+ div.style.borderStyle = 'solid';
51
+ div.style.borderColor = '#000';
52
+ div.style.backgroundColor = '#aaaaaaaa';
53
+ div.style.padding = '5px';
54
+ div.style.display = 'flex';
55
+ div.style.flexDirection = 'column';
56
+ this._div = div;
57
+
58
+ // Add filter input field
59
+ this.busy = new ControlBusy(div);
60
+ this.filter = new ControlFilter(opts.filter);
61
+ this.dataset = new ControlDataset(wxapi, this.busy, this.onChange);
62
+
63
+ this.filter.attachToDom(div);
64
+ this.dataset.attachToDom(div);
65
+
66
+ this.dataset.init({
67
+ dataset,
68
+ variable,
69
+ cb: () => this.filter.init({ datasets: this.dataset.datasetInput }),
70
+ });
71
+ }
72
+
73
+ protected onChange = async () => {
74
+ this.onchange?.(this.dataset.getValue(), this.dataset.variables.getValue());
75
+ };
76
+
77
+ /**
78
+ * Add control to map
79
+ * @param map map - map to add control
80
+ */
81
+ onAdd(/* map */) {
82
+ return this._div;
83
+ }
84
+
85
+ /**
86
+ * Remove control from map
87
+ */
88
+ onRemove() {
89
+ this._div.parentNode?.removeChild(this._div);
90
+ }
91
+
92
+ /**
93
+ * Extender for Leaflet
94
+ */
95
+ extender() {
96
+ return { onAdd: () => this.onAdd(), onRemove: () => this.onRemove() };
97
+ }
131
98
  }