@eox/layercontrol 0.22.0 → 0.23.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 (80) hide show
  1. package/README.md +4 -0
  2. package/dist/eox-layercontrol.js +836 -839
  3. package/dist/eox-layercontrol.umd.cjs +119 -122
  4. package/package.json +9 -13
  5. package/src/components/add-layers.js +416 -0
  6. package/src/components/layer-config.js +212 -0
  7. package/src/components/layer-datetime.js +128 -0
  8. package/src/components/layer-group.js +193 -0
  9. package/src/components/layer-legend.js +135 -0
  10. package/src/components/layer-list.js +209 -0
  11. package/src/components/layer-tools.js +353 -0
  12. package/src/components/layer.js +244 -0
  13. package/src/components/optional-list.js +124 -0
  14. package/src/components/tab-sliders.js +0 -0
  15. package/src/components/tools-items.js +199 -0
  16. package/src/enums/index.js +16 -0
  17. package/src/enums/stories/assets/cropomhusc-feature-collection.json +5274 -0
  18. package/src/enums/stories/index.js +536 -0
  19. package/src/helpers/check-properties.js +41 -0
  20. package/src/helpers/clean-json-input.js +29 -0
  21. package/src/helpers/create-sortable.js +104 -0
  22. package/src/helpers/detect-map-url-type.js +20 -0
  23. package/src/helpers/fetch-capabilities.js +32 -0
  24. package/src/helpers/filter-layers.js +45 -0
  25. package/src/helpers/get-layer-type.js +35 -0
  26. package/src/helpers/get-legend-config.js +25 -0
  27. package/src/helpers/get-start-vals.js +73 -0
  28. package/src/helpers/hide-layers-based-on-property.js +11 -0
  29. package/src/helpers/index.js +22 -0
  30. package/src/helpers/is-layer-json-valid.js +19 -0
  31. package/src/helpers/is-layer-visible-based-on-zoom-state.js +31 -0
  32. package/src/helpers/is-layer-zoom-state-req.js +17 -0
  33. package/src/helpers/is-map-url-valid.js +22 -0
  34. package/src/helpers/layer-tools.js +82 -0
  35. package/src/helpers/update-url.js +32 -0
  36. package/src/main.js +269 -0
  37. package/src/methods/add-layer/add-layer.js +33 -0
  38. package/src/methods/add-layer/index.js +8 -0
  39. package/src/methods/add-layer/json-input-change.js +15 -0
  40. package/src/methods/add-layer/open-close-tab.js +21 -0
  41. package/src/methods/add-layer/url-input-change.js +17 -0
  42. package/src/methods/add-layer/url-layer.js +37 -0
  43. package/src/methods/add-layer/wms-search-url.js +44 -0
  44. package/src/methods/layer/first-updated.js +65 -0
  45. package/src/methods/layer/index.js +4 -0
  46. package/src/methods/layer/input-click.js +43 -0
  47. package/src/methods/layer-config/apply-updated-styles.js +98 -0
  48. package/src/methods/layer-config/data-change.js +44 -0
  49. package/src/methods/layer-config/index.js +7 -0
  50. package/src/methods/layer-list/first-updated.js +55 -0
  51. package/src/methods/layer-list/index.js +3 -0
  52. package/src/methods/layercontrol/first-update.js +22 -0
  53. package/src/methods/layercontrol/index.js +4 -0
  54. package/src/methods/layercontrol/layer-list-change.js +23 -0
  55. package/src/methods/optional-list/add-to-list.js +44 -0
  56. package/src/methods/optional-list/index.js +3 -0
  57. package/test/_mockMap.js +0 -154
  58. package/test/cases/general/check-layer-legend.js +0 -67
  59. package/test/cases/general/check-layer-size.js +0 -25
  60. package/test/cases/general/check-pre-open-layer-tools.js +0 -26
  61. package/test/cases/general/check-pre-open-section.js +0 -29
  62. package/test/cases/general/disable-drag.js +0 -26
  63. package/test/cases/general/empty-groups.js +0 -41
  64. package/test/cases/general/exclusive-layers.js +0 -35
  65. package/test/cases/general/hide-layers.js +0 -23
  66. package/test/cases/general/index.js +0 -10
  67. package/test/cases/general/render-optional-layer.js +0 -31
  68. package/test/cases/general/show-layer-title.js +0 -23
  69. package/test/cases/layer-update/add-external-layer.js +0 -96
  70. package/test/cases/layer-update/add-layer-from-optional.js +0 -51
  71. package/test/cases/layer-update/check-if-layer-pushed-to-group.js +0 -33
  72. package/test/cases/layer-update/check-layer-changed-event.js +0 -29
  73. package/test/cases/layer-update/check-layer-is-pushed-to-root.js +0 -22
  74. package/test/cases/layer-update/check-layer-removed-from-group.js +0 -33
  75. package/test/cases/layer-update/check-layer-removed-from-root.js +0 -27
  76. package/test/cases/layer-update/check-layer-size-after-multiple-calls.js +0 -34
  77. package/test/cases/layer-update/check-layer-zoom-state.js +0 -54
  78. package/test/cases/layer-update/index.js +0 -9
  79. package/test/general.cy.js +0 -61
  80. package/test/layersUpdate.cy.js +0 -72
package/src/main.js ADDED
@@ -0,0 +1,269 @@
1
+ import { LitElement, html } from "lit";
2
+ import { when } from "lit/directives/when.js";
3
+ import "./components/layer-list";
4
+ import "./components/optional-list";
5
+ import "./components/add-layers";
6
+ import { filterLayers } from "./helpers";
7
+ import {
8
+ firstUpdatedMethod,
9
+ layerListChangeMethod,
10
+ } from "./methods/layercontrol";
11
+
12
+ /**
13
+ * Display layers and groups of a connected OpenLayers map
14
+ *
15
+ * ## Layer properties
16
+ * In order to be displayed correctly, the OpenLayers map layers need some custom properties (using e.g. `layer.set(property, value)`).
17
+ *
18
+ * #### `id?: string`
19
+ * The layer id. Not required but recommended. Can also be any other layer property (defined via the `idProperty` property or `id-property`attribute - see API).
20
+ *
21
+ * #### `title?: string`
22
+ * The title of the layer displayed in the layer control. Not required, but recommended in order to display human-readable layer titles. Can be any other layer property (defined via the `titleProperty` property or `title-property`attribute - see API).
23
+ *
24
+ * #### `layerControlHide?: Boolean`
25
+ * Completely hide a layer from the layer control.
26
+ *
27
+ * #### `layerControlOptional?: Boolean`
28
+ * Initially hide a layer from the layer control, but make it available as an optional layer. If the layer is selected and added, it will be set to visible and pushed to the top of the layer list or (if originally configured within a layer group) to the top of the layer group.
29
+ *
30
+ * #### `layerControlExclusive?: Boolean`
31
+ * Make layers mutually exclusive. If two or more layers (on the same level, i.e. at root or inside a layer group) have this property, then only one of them can be visualized at a time.
32
+ *
33
+ * #### `layerControlExpand?: Boolean`
34
+ * Pre-expand a layer dropdown so that it is always open when the component initializes.
35
+ *
36
+ * #### `layerControlToolsExpand?: Boolean`
37
+ * Pre-expand the layer tools so they are visible when the component initializes.
38
+ *
39
+ * #### `layerConfig?: Object`
40
+ * Configuration options for the layer (displayed in the layer tools' "config" tab)
41
+ *
42
+ * #### `layerDateTime?: Object`
43
+ * Partial eox-timecontrol config passed to the "datetime" tool
44
+ *
45
+ * ### `layerLegend`?: Object
46
+ * Creates a dynamic color legend based on range & domain of values. Extenteds partial config of `color-legend-element`
47
+ *
48
+ * @element eox-layercontrol
49
+ */
50
+ export class EOxLayerControl extends LitElement {
51
+ // Define static properties for the component
52
+ static properties = {
53
+ for: { type: String },
54
+ idProperty: { attribute: "id-property" },
55
+ map: { attribute: false, state: true },
56
+ titleProperty: { attribute: "title-property", type: String },
57
+ showLayerZoomState: { attribute: "show-layer-zoom-state", type: Boolean },
58
+ tools: { attribute: false },
59
+ addExternalLayers: { attribute: false },
60
+ unstyled: { type: Boolean },
61
+ styleOverride: { type: String },
62
+ toolsAsList: { type: Boolean },
63
+ };
64
+
65
+ /**
66
+ * Instance of `eox-map` which is a wrapper for the OL
67
+ *
68
+ * @type {import("../../map/src/main").EOxMap}
69
+ */
70
+ #eoxMap;
71
+
72
+ // Constructor to set default values for properties
73
+ constructor() {
74
+ super();
75
+
76
+ /**
77
+ * Query selector of an `eox-map` (`String`, passed as an attribute or property)
78
+ * or an `eox-map` DOM element (`HTMLElement`, passed as property)
79
+ *
80
+ * @type {String|HTMLElement}
81
+ */
82
+ this.for = "eox-map";
83
+
84
+ /**
85
+ * Layer id property
86
+ *
87
+ * @type {String}
88
+ */
89
+ this.idProperty = "id";
90
+
91
+ /**
92
+ * The native OL map instance
93
+ *
94
+ * @type {import("ol").Map}
95
+ * @see {@link https://openlayers.org/en/latest/apidoc/module-ol_Map-Map.html}
96
+ */
97
+ this.map = null;
98
+
99
+ /**
100
+ * Layer title property
101
+ *
102
+ * @type {String}
103
+ */
104
+ this.titleProperty = "title";
105
+
106
+ /**
107
+ * Show layer state based on zoom level or not
108
+ *
109
+ * @type {Boolean}
110
+ */
111
+ this.showLayerZoomState = false;
112
+
113
+ /**
114
+ * Layer tools
115
+ *
116
+ * @type {Array<String>}
117
+ */
118
+ this.tools = ["info", "opacity", "datetime", "config", "remove", "sort"];
119
+
120
+ /**
121
+ * Enable-disable external layer
122
+ *
123
+ * @type {Boolean}
124
+ */
125
+ this.addExternalLayers = false;
126
+
127
+ /**
128
+ * Render the element without additional styles
129
+ *
130
+ * @type {Boolean}
131
+ */
132
+ this.unstyled = false;
133
+
134
+ /**
135
+ * Overrides elements current CSS.
136
+ *
137
+ * @type {String}
138
+ */
139
+ this.styleOverride = "";
140
+
141
+ /**
142
+ * If enabled, the tools section will be rendered as list.
143
+ *
144
+ * @type {Boolean}
145
+ */
146
+ this.toolsAsList = false;
147
+ }
148
+
149
+ /**
150
+ * Called when the component is first updated
151
+ * Updated #eoxMap after first update.
152
+ */
153
+ firstUpdated() {
154
+ this.eoxMap = firstUpdatedMethod(this);
155
+ }
156
+
157
+ updated(changedProperties) {
158
+ if (changedProperties.has("for")) {
159
+ this.eoxMap = firstUpdatedMethod(this);
160
+ }
161
+ }
162
+
163
+ get eoxMap() {
164
+ return this.#eoxMap;
165
+ }
166
+
167
+ set eoxMap(value) {
168
+ const oldValue = this.#eoxMap;
169
+ this.#eoxMap = value;
170
+ this.requestUpdate("eoxMap", oldValue);
171
+ }
172
+
173
+ /**
174
+ * Event handler for changes in the layer list
175
+ *
176
+ * @param {CustomEvent & {target: Element}} evt
177
+ */
178
+ #handleLayerControlLayerListChange(evt) {
179
+ layerListChangeMethod(evt, this);
180
+ /**
181
+ * A generic layer change event; could be a layer visibility, group length updates and others.
182
+ * Passes the changed layer in the `detail`.
183
+ */
184
+ this.dispatchEvent(new CustomEvent("layerchange", { detail: evt.detail }));
185
+ }
186
+
187
+ /**
188
+ * Dispatches datetime updates from layer datetime to the layercontrol
189
+ * @param {CustomEvent} evt
190
+ */
191
+ #handleDatetimeUpdate(evt) {
192
+ this.dispatchEvent(
193
+ new CustomEvent("datetime:updated", { detail: evt.detail }),
194
+ );
195
+ }
196
+
197
+ render() {
198
+ // Checks if there are any layers with the 'layerControlOptional' property set to true
199
+ const layers = this.map?.getLayers().getArray();
200
+ const layerControlOptionalCondition =
201
+ layers && filterLayers(layers, "layerControlOptional", true)?.length > 0;
202
+
203
+ return html`
204
+ <style>
205
+ ${!this.unstyled && this.#styleEOX}
206
+ ${this.styleOverride}
207
+ </style>
208
+
209
+ <!-- Conditional rendering of add layers component -->
210
+ ${when(
211
+ this.addExternalLayers && this.#eoxMap?.addOrUpdateLayer,
212
+ () => html`
213
+ <eox-layercontrol-add-layers
214
+ .noShadow=${true}
215
+ .eoxMap=${this.#eoxMap}
216
+ .unstyled=${this.unstyled}
217
+ ></eox-layercontrol-add-layers>
218
+ `,
219
+ )}
220
+
221
+ <!-- Conditional rendering of layer list component -->
222
+ ${when(
223
+ this.map,
224
+ () => html`
225
+ <eox-layercontrol-layer-list
226
+ .noShadow=${true}
227
+ class="layers"
228
+ .idProperty=${this.idProperty}
229
+ .layers=${this.map.getLayers()}
230
+ .map=${this.map}
231
+ .titleProperty=${this.titleProperty}
232
+ .showLayerZoomState=${this.showLayerZoomState}
233
+ .tools=${this.tools}
234
+ .unstyled=${this.unstyled}
235
+ .toolsAsList=${this.toolsAsList}
236
+ @changed=${this.#handleLayerControlLayerListChange}
237
+ @datetime:updated=${this.#handleDatetimeUpdate}
238
+ ></eox-layercontrol-layer-list>
239
+ `,
240
+ )}
241
+
242
+ <!-- Conditional rendering of optional list component -->
243
+ ${when(
244
+ layerControlOptionalCondition,
245
+ () => html`
246
+ <eox-layercontrol-optional-list
247
+ .noShadow=${true}
248
+ .idProperty=${this.idProperty}
249
+ .layers=${this.map.getLayers()}
250
+ .titleProperty=${this.titleProperty}
251
+ @changed=${() => this.requestUpdate()}
252
+ ></eox-layercontrol-optional-list>
253
+ `,
254
+ )}
255
+ `;
256
+ }
257
+
258
+ #styleEOX = `
259
+ :host, :root {
260
+ font-family: Roboto, sans-serif;
261
+ --padding: 0.5rem;
262
+
263
+ display: block;
264
+ padding: var(--padding) 0;
265
+ }
266
+ `;
267
+ }
268
+
269
+ customElements.define("eox-layercontrol", EOxLayerControl);
@@ -0,0 +1,33 @@
1
+ import { cleanJSONInput } from "../../helpers";
2
+
3
+ /**
4
+ * Handles the addition of one or multiple layers to the map based on the input.
5
+ * Parses the layers input into JSON format and adds or updates the layers accordingly.
6
+ * Supports both single and multiple layer additions.
7
+ *
8
+ * @param {import("../../components/add-layers").EOxLayerControlAddLayers} EoxLayerControlAddLayers - Instance of EOxLayerControlAddLayers
9
+ */
10
+ const addLayer = (EoxLayerControlAddLayers) => {
11
+ /**
12
+ * @type {{data: []}} Converting any array into json and parsing it using JSON.parse
13
+ **/
14
+ const layers = JSON.parse(
15
+ `{"data":${cleanJSONInput(EoxLayerControlAddLayers.jsonInput)}}`,
16
+ );
17
+ // Check if the parsed data is an array
18
+ if (Array.isArray(layers.data)) {
19
+ // Iterate over each layer in the array and add/update it in the map
20
+ layers.data.forEach((layer) => {
21
+ EoxLayerControlAddLayers.eoxMap.addOrUpdateLayer(layer);
22
+ });
23
+ } else {
24
+ // If the parsed data is not an array, directly add/update the layer in the map
25
+ EoxLayerControlAddLayers.eoxMap.addOrUpdateLayer(layers.data);
26
+ }
27
+
28
+ // Resetting `jsonInput` with null value and re-rendering the component
29
+ EoxLayerControlAddLayers.jsonInput = null;
30
+ EoxLayerControlAddLayers.requestUpdate();
31
+ };
32
+
33
+ export default addLayer;
@@ -0,0 +1,8 @@
1
+ // Importing methods from respective files and exporting them
2
+
3
+ export { default as jsonInputChangeMethod } from "./json-input-change"; // Handles changes in the input field, parsing entered data into JSON format.
4
+ export { default as addLayerMethod } from "./add-layer"; // Handles the addition of one or multiple layers
5
+ export { default as urlInputChangeMethod } from "./url-input-change"; // Handles the change event in the URL input field
6
+ export { default as wmsSearchURLMethod } from "./wms-search-url"; // Handles the URL search process
7
+ export { default as urlLayerMethod } from "./url-layer"; // Handles changes in the URL input field
8
+ export { default as openCloseTabMethod } from "./open-close-tab"; // Handles the opening or closing of tabs
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Handles changes in the input field, parsing entered data into JSON format.
3
+ *
4
+ * @param {{target: { value: string }}} evt - The input change event.
5
+ * @param {import("../../components/add-layers").EOxLayerControlAddLayers} EoxLayerControlAddLayers - Instance of EOxLayerControlAddLayers
6
+ */
7
+ const jsonInputChange = (evt, EoxLayerControlAddLayers) => {
8
+ // Update the stored layers input with the cleaned JSON data
9
+ EoxLayerControlAddLayers.jsonInput = evt.target.value;
10
+
11
+ // Request a UI update to reflect changes
12
+ EoxLayerControlAddLayers.requestUpdate();
13
+ };
14
+
15
+ export default jsonInputChange;
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Handles the opening or closing of tabs in the EOxLayerControlAddLayers instance.
3
+ * Updates relevant properties based on the selected tab.
4
+ *
5
+ * @param {"url" | "json" | null} tab - The tab identifier to open or close.
6
+ * @param {import("../../components/add-layers").EOxLayerControlAddLayers} EoxLayerControlAddLayers - Instance of EOxLayerControlAddLayers.
7
+ */
8
+ const openCloseTab = (tab, EoxLayerControlAddLayers) => {
9
+ // Update the 'open' property of EOxLayerControlAddLayers based on the provided tab
10
+ EoxLayerControlAddLayers.open = tab || null;
11
+
12
+ // Reset input-related properties when closing the tab
13
+ EoxLayerControlAddLayers.urlInput = null;
14
+ EoxLayerControlAddLayers.jsonInput = null;
15
+ EoxLayerControlAddLayers.wmsCapabilities = null;
16
+
17
+ // Request an update to reflect changes in the UI
18
+ EoxLayerControlAddLayers.requestUpdate();
19
+ };
20
+
21
+ export default openCloseTab;
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Handles the change event in the URL input field
3
+ * and updates the stored URL input.
4
+ *
5
+ * @param {Event} evt - The input change event.
6
+ * @param {import("../../components/add-layers").EOxLayerControlAddLayers} EoxLayerControlAddLayers - Instance of EOxLayerControlAddLayers
7
+ */
8
+ const urlInputChange = (evt, EoxLayerControlAddLayers) => {
9
+ // Update the stored URL input value in the EOxLayerControlAddLayers instance
10
+ // @ts-expect-error TODO
11
+ EoxLayerControlAddLayers.urlInput = evt.target.value;
12
+
13
+ // Request an update in the UI to reflect the changes
14
+ EoxLayerControlAddLayers.requestUpdate();
15
+ };
16
+
17
+ export default urlInputChange;
@@ -0,0 +1,37 @@
1
+ import { detectMapURLType } from "../../helpers";
2
+
3
+ /**
4
+ * Handles changes in the input field and prepares EOxLayerControlAddLayers
5
+ * properties based on the provided layer.
6
+ *
7
+ * @param {{"Name": string}} layer - The layer information to handle.
8
+ * @param {import("../../components/add-layers").EOxLayerControlAddLayers} EoxLayerControlAddLayers - Instance of EOxLayerControlAddLayers.
9
+ */
10
+ const urlLayer = (layer, EoxLayerControlAddLayers) => {
11
+ // Extract the 'Name' property from the provided layer
12
+ const { Name: id } = layer;
13
+
14
+ // Detect the type of URL (WMS or XYZ) based on the URL input in EoxLayerControlAddLayers
15
+ const urlType = detectMapURLType(EoxLayerControlAddLayers.urlInput) || "XYZ";
16
+
17
+ // Create an EOxJSON layer object
18
+ const layerEOxJSON = {
19
+ type: "Tile",
20
+ properties: {
21
+ id: id,
22
+ title: id, // Set the title property to the same as id
23
+ },
24
+ source: {
25
+ type: urlType,
26
+ url: EoxLayerControlAddLayers.urlInput, // Set the URL from the instance
27
+ params: {
28
+ LAYERS: id, // Set the 'LAYERS' parameter to the provided layer 'Name'
29
+ },
30
+ },
31
+ };
32
+
33
+ // Convert the layerEOxJSON object to a JSON string and assign it to the jsonInput property
34
+ EoxLayerControlAddLayers.jsonInput = JSON.stringify(layerEOxJSON);
35
+ };
36
+
37
+ export default urlLayer;
@@ -0,0 +1,44 @@
1
+ import { detectMapURLType, fetchCapabilities } from "../../helpers";
2
+
3
+ /**
4
+ * Handles the URL search process in the EOxLayerControlAddLayers context.
5
+ * It utilizes the provided instance to process the URL input for WMS or XYZ layers.
6
+ *
7
+ * @param {import("../../components/add-layers").EOxLayerControlAddLayers} EoxLayerControlAddLayers - Instance of EOxLayerControlAddLayers.
8
+ * @return {Promise<{"Name": string} | false>} - An object with layer information if URL type is XYZ, otherwise false.
9
+ */
10
+ export default async function wmsSearchURL(EoxLayerControlAddLayers) {
11
+ // Get the URL input from the EOxLayerControlAddLayers instance
12
+ const url = EoxLayerControlAddLayers.urlInput;
13
+
14
+ // Enable loader for search and reset `wmsCapabilities`
15
+ EoxLayerControlAddLayers.wmsCapabilities = null;
16
+ EoxLayerControlAddLayers.searchLoad = true;
17
+ EoxLayerControlAddLayers.requestUpdate();
18
+
19
+ // If URL input is empty, return false
20
+ if (!url) return false;
21
+
22
+ // Check if the URL type is XYZ
23
+ if (detectMapURLType(url) === "XYZ") {
24
+ // If the URL type is XYZ, return a layer object with the Name property set to the URL
25
+ return { Name: url };
26
+ } else {
27
+ try {
28
+ // If the URL type is not XYZ, fetch WMS capabilities data
29
+ const data = await fetchCapabilities(url);
30
+
31
+ // Set the fetched WMS capabilities to the instance and request an update
32
+ EoxLayerControlAddLayers.wmsCapabilities = data;
33
+ } catch (_) {
34
+ // Error when API Fails
35
+ } finally {
36
+ // Disable loader for search
37
+ EoxLayerControlAddLayers.searchLoad = false;
38
+ EoxLayerControlAddLayers.requestUpdate();
39
+ }
40
+
41
+ // Return false as WMS capabilities are being fetched asynchronously
42
+ return false;
43
+ }
44
+ }
@@ -0,0 +1,65 @@
1
+ import {
2
+ isLayerVisibleBasedOnZoomState,
3
+ isLayerZoomStateRequired,
4
+ } from "../../helpers";
5
+
6
+ /**
7
+ * Check and update layer zoom visibility at beginning
8
+ * and register "change:resolution" ones at the beginning if `showLayerZoomState` is present
9
+ *
10
+ * @param {import("../../components/layer").EOxLayerControlLayer} EOxLayerControlLayer - Instance of EOxLayerControlLayer
11
+ */
12
+ const firstUpdatedMethod = (EOxLayerControlLayer) => {
13
+ /**
14
+ * Check and update layer visibility based on zoom level
15
+ * and only update DOM if visibility gets updated
16
+ */
17
+ const updateLayerZoomVisibility = () => {
18
+ const newLayerVisibilityBasedOnZoom = isLayerVisibleBasedOnZoomState(
19
+ EOxLayerControlLayer.layer,
20
+ EOxLayerControlLayer.map,
21
+ EOxLayerControlLayer.showLayerZoomState,
22
+ );
23
+
24
+ let visibilityChanged = false;
25
+
26
+ // Checking if visibility changed or not and updating `currLayerVisibilityBasedOnZoom`
27
+ if (
28
+ !newLayerVisibilityBasedOnZoom &&
29
+ EOxLayerControlLayer.currLayerVisibilityBasedOnZoom
30
+ ) {
31
+ EOxLayerControlLayer.currLayerVisibilityBasedOnZoom = false;
32
+ visibilityChanged = true;
33
+ } else if (
34
+ newLayerVisibilityBasedOnZoom &&
35
+ !EOxLayerControlLayer.currLayerVisibilityBasedOnZoom
36
+ ) {
37
+ EOxLayerControlLayer.currLayerVisibilityBasedOnZoom = true;
38
+ visibilityChanged = true;
39
+ }
40
+
41
+ // if visibilityChanged trigger UI update
42
+ if (visibilityChanged) {
43
+ EOxLayerControlLayer.requestUpdate();
44
+ EOxLayerControlLayer.dispatchEvent(
45
+ new CustomEvent("change:resolution", { bubbles: true }),
46
+ );
47
+ }
48
+ };
49
+
50
+ if (
51
+ isLayerZoomStateRequired(
52
+ EOxLayerControlLayer.layer,
53
+ EOxLayerControlLayer.showLayerZoomState,
54
+ )
55
+ ) {
56
+ updateLayerZoomVisibility();
57
+
58
+ // Initialize change:resolution event ones
59
+ EOxLayerControlLayer.map
60
+ .getView()
61
+ .on("change:resolution", () => updateLayerZoomVisibility());
62
+ }
63
+ };
64
+
65
+ export default firstUpdatedMethod;
@@ -0,0 +1,4 @@
1
+ // Importing methods from respective files and exporting them
2
+
3
+ export { default as firstUpdatedMethod } from "./first-updated"; // Check and update layer zoom visibility at beginning
4
+ export { default as inputClickMethod } from "./input-click"; // Handles the click event on an input element to control layer visibility.
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Handles the click event on an input element to control layer visibility.
3
+ *
4
+ * @param {{target: { checked: boolean }}} evt - The input change event triggering the method.
5
+ * @param {import("../../components/layer").EOxLayerControlLayer} EOxLayerControlLayer - Instance of EOxLayerControlLayer.
6
+ */
7
+ const inputClickMethod = (evt, EOxLayerControlLayer) => {
8
+ const layer = EOxLayerControlLayer.layer; // The layer instance associated with the control.
9
+
10
+ // Set the visibility of the layer based on the input's checked state.
11
+ layer.setVisible(evt.target.checked);
12
+
13
+ // Check if the layer is checked and marked as 'layerControlExclusive'.
14
+ if (evt.target.checked && layer.get("layerControlExclusive")) {
15
+ /**
16
+ * @type NodeListOf<Element & {layer: any, requestUpdate: function}>
17
+ */
18
+ const siblings = EOxLayerControlLayer.closest(
19
+ ".layers > ul",
20
+ ).querySelectorAll("eox-layercontrol-layer");
21
+
22
+ // Loop through the sibling layers to handle exclusive visibility.
23
+ siblings.forEach((sibling) => {
24
+ if (
25
+ sibling.layer !== layer &&
26
+ sibling.layer?.get("layerControlExclusive")
27
+ ) {
28
+ sibling.layer.setVisible(false);
29
+ sibling.requestUpdate();
30
+ }
31
+ });
32
+ }
33
+
34
+ // Dispatch a 'changed' event to signal a change in the layer's state.
35
+ EOxLayerControlLayer.dispatchEvent(
36
+ new CustomEvent("changed", { bubbles: true, detail: layer }),
37
+ );
38
+
39
+ // Request an update for the layer control to reflect the changes.
40
+ EOxLayerControlLayer.requestUpdate();
41
+ };
42
+
43
+ export default inputClickMethod;
@@ -0,0 +1,98 @@
1
+ /**
2
+ * @param {Record<string,number>} jsonformOutput
3
+ * @param { import("ol/layer/Layer").default} layer
4
+ * @param { import("../../components/layer-config").EOxLayerControlLayerConfig['layerConfig']} layerConfig
5
+ * */
6
+ export default function (jsonformOutput, layer, layerConfig) {
7
+ // check whether the layer is Vector or Tile
8
+ const isTile = "updateStyleVariables" in layer;
9
+ const isVector = "setStyle" in layer;
10
+
11
+ const styles = isTile
12
+ ? /** @type {import('ol/layer/WebGLTile').default} */ (layer)["style_"]
13
+ : layerConfig.style;
14
+ let styleVars = styles?.variables;
15
+ if (styleVars) {
16
+ const updatedValues = flattenObject(jsonformOutput);
17
+ /** @type {Record<string,any>} */
18
+ styles.variables = {
19
+ ...styleVars,
20
+ ...updatedValues,
21
+ };
22
+
23
+ // check if it supports updating the variables using ol first
24
+ if (isTile) {
25
+ /** @type {import('ol/layer/WebGLTile').default} */ (
26
+ layer
27
+ ).updateStyleVariables(updatedValues);
28
+ } else if (isVector) {
29
+ const updatedStyles = updateVectorLayerStyle(styles);
30
+ /** @type {import('ol/layer/Vector').default} */ (layer).setStyle(
31
+ updatedStyles,
32
+ );
33
+ }
34
+ }
35
+ }
36
+
37
+ /***
38
+ * @param {Record<string,any>} obj
39
+ **/
40
+ export const flattenObject = (obj) => {
41
+ /**
42
+ * the flattened object to be returned
43
+ * @type {Record<string,number>} */
44
+ const flat = {};
45
+ // loop through the keys of the object
46
+ for (const key in obj) {
47
+ // if the property is of type object
48
+ if (typeof obj[key] == "object" && obj[key] !== null) {
49
+ // flatten it recursively
50
+ const flatObject = flattenObject(
51
+ /** @type {Record<string,any>} */ (obj[key]),
52
+ );
53
+ // assign all of its values to the flat object to be returned
54
+ for (const nestedKey in flatObject) {
55
+ flat[nestedKey] = flatObject?.[nestedKey];
56
+ }
57
+ } else {
58
+ // the property is of a primitive value
59
+ // assign it to the object to be returned
60
+ flat[key] = obj?.[key];
61
+ }
62
+ }
63
+ return flat;
64
+ };
65
+
66
+ /**
67
+ * updating the variables assigned in the layer style
68
+ * from the `styles.variables` property
69
+ * @param {Record<string,any>} styles
70
+ * @returns
71
+ */
72
+ function updateVectorLayerStyle(styles) {
73
+ // pass back flat style if contained in config
74
+ let returnStyle = styles;
75
+ // Check if variables are defined and need to be "burned in" first
76
+ if ("variables" in styles) {
77
+ // stringify all the styles to be able to search quickly
78
+ let rawStyle = JSON.stringify(styles);
79
+ // extract updated variables
80
+ const { variables } = styles;
81
+ // loop through the variables keys
82
+ for (const key in variables) {
83
+ // ol styles expects numbers to be assigned as typeof number
84
+ if (typeof variables[key] === "number") {
85
+ //@ts-expect-error ol styles expects number values to be assigned as type number
86
+ rawStyle = rawStyle.replaceAll(`["var","${key}"]`, variables[key]);
87
+ } else {
88
+ // replace all styles variables set of the specific key with the variables value
89
+ rawStyle = rawStyle.replaceAll(
90
+ `["var","${key}"]`,
91
+ `"${variables[key]}"`,
92
+ );
93
+ }
94
+ }
95
+ returnStyle = JSON.parse(rawStyle);
96
+ }
97
+ return returnStyle;
98
+ }