@eox/layercontrol 1.9.0 → 1.10.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eox/layercontrol",
3
- "version": "1.9.0",
3
+ "version": "1.10.0",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",
@@ -11,7 +11,7 @@
11
11
  "@eox/map": "*",
12
12
  "@types/lodash.throttle": "^4.1.9",
13
13
  "@types/sortablejs": "^1.15.1",
14
- "ol": "^10.10.0",
14
+ "ol": "^10.11.0",
15
15
  "vite": "^8.2.2"
16
16
  },
17
17
  "engines": {
@@ -1,6 +1,5 @@
1
1
  import { LitElement, html, css } from "lit";
2
- import { getLegendConfig } from "../helpers";
3
- import { getStartVals } from "../helpers";
2
+ import { getLegendConfig, getStartVals, getGeoZarrBandRange } from "../helpers";
4
3
  import { dataChangeMethod, applyUpdatedStyles } from "../methods/layer-config";
5
4
  import { when } from "lit/directives/when.js";
6
5
  import _throttle from "lodash.throttle";
@@ -85,8 +84,9 @@ export class EOxLayerControlLayerConfig extends LitElement {
85
84
  * schema: Record<string,any>;
86
85
  * element: string;
87
86
  * type?: "tileUrl" | "style";
88
- * style?: import("ol/layer/WebGLTile").Style
89
- * legend?: layerConfigLegend | layerConfigLegend[]
87
+ * style?: import("ol/layer/WebGLTile").Style;
88
+ * legend?: layerConfigLegend | layerConfigLegend[];
89
+ * [key: string]: any;
90
90
  * }}
91
91
  */
92
92
  this.layerConfig = null;
@@ -113,11 +113,88 @@ export class EOxLayerControlLayerConfig extends LitElement {
113
113
  this.colormapRegistry = null;
114
114
  }
115
115
 
116
+ connectedCallback() {
117
+ super.connectedCallback();
118
+ if (this.layer?.on) {
119
+ this.layer.on("propertychange", this.#layerPropertyChangeListener);
120
+ }
121
+ }
122
+
123
+ disconnectedCallback() {
124
+ super.disconnectedCallback();
125
+ if (this.layer?.un) {
126
+ this.layer.un("propertychange", this.#layerPropertyChangeListener);
127
+ }
128
+ }
129
+
130
+ #layerPropertyChangeListener = (e) => {
131
+ const propEvent = /** @type {import("ol/Object").ObjectEvent} */ (e);
132
+ if (propEvent.key === "layerConfig") {
133
+ this.layerConfig = this.layer.get("layerConfig");
134
+ const freshStartVals = getStartVals(this.layer, this.layerConfig) || {};
135
+ const schemaProps = this.layerConfig?.schema?.properties;
136
+ const isAutofill =
137
+ this.layerConfig?.autofill === true ||
138
+ this.layerConfig?.schema?.autofill === true;
139
+
140
+ if (
141
+ this.layerConfig?.min !== undefined &&
142
+ (isAutofill || schemaProps?.min !== undefined)
143
+ ) {
144
+ this.#data.min = this.layerConfig.min;
145
+ }
146
+ if (
147
+ this.layerConfig?.max !== undefined &&
148
+ (isAutofill || schemaProps?.max !== undefined)
149
+ ) {
150
+ this.#data.max = this.layerConfig.max;
151
+ }
152
+ const activeVar =
153
+ this.layer.get("_lastVariable") ||
154
+ this.layerConfig?.band ||
155
+ (Array.isArray(this.layerConfig?.bands)
156
+ ? this.layerConfig.bands[0]
157
+ : undefined);
158
+ if (
159
+ activeVar &&
160
+ (isAutofill ||
161
+ schemaProps?.variable !== undefined ||
162
+ schemaProps?.band !== undefined)
163
+ ) {
164
+ if (schemaProps?.band !== undefined) {
165
+ this.#data.band = activeVar;
166
+ } else {
167
+ this.#data.variable = activeVar;
168
+ }
169
+ }
170
+ this.#startVals = { ...this.#data, ...freshStartVals };
171
+ if (schemaProps && !isAutofill) {
172
+ const allowedKeys = new Set(Object.keys(schemaProps));
173
+ for (const k of Object.keys(this.#startVals)) {
174
+ if (!allowedKeys.has(k)) {
175
+ delete this.#startVals[k];
176
+ }
177
+ }
178
+ }
179
+ this.#data = { ...this.#startVals };
180
+ this.requestUpdate();
181
+ }
182
+ };
183
+
116
184
  /** Decide what type of throttling to do based on layerConfig type
117
185
  *
118
186
  * @param {import("lit").PropertyValues} changedProperties - The changed properties.
119
187
  */
120
188
  updated(changedProperties) {
189
+ if (changedProperties.has("layer")) {
190
+ const oldLayer = changedProperties.get("layer");
191
+ if (oldLayer?.un) {
192
+ oldLayer.un("propertychange", this.#layerPropertyChangeListener);
193
+ }
194
+ if (this.layer?.on) {
195
+ this.layer.on("propertychange", this.#layerPropertyChangeListener);
196
+ }
197
+ }
121
198
  if (changedProperties.has("layerConfig")) {
122
199
  const throttleTime =
123
200
  this.layerConfig?.type === "style" || this.layerConfig?.style
@@ -125,21 +202,51 @@ export class EOxLayerControlLayerConfig extends LitElement {
125
202
  : 1000;
126
203
 
127
204
  this.throttleDataChange = _throttle(this.#handleDataChange, throttleTime);
128
- this.requestUpdate();
129
205
  }
130
206
  }
131
207
  /**
132
208
  * Handles changes in eox-jsonform values.
133
209
  *
134
- * @param {{ detail: { value: string; }; }} e
210
+ * @param {{ detail: Record<string, any>; }} e
135
211
  */
136
- #handleDataChange(e) {
137
- this.#data = e.detail;
138
- if (this.layerConfig.type === "style" || this.layerConfig.style) {
212
+ async #handleDataChange(e) {
213
+ const prevVar =
214
+ this.#data?.variable ??
215
+ this.#data?.band ??
216
+ this.layer?.get?.("_lastVariable") ??
217
+ this.layerConfig?.band ??
218
+ (Array.isArray(this.layerConfig?.bands)
219
+ ? this.layerConfig.bands[0]
220
+ : undefined);
221
+ const currentVar = e.detail?.variable ?? e.detail?.band;
222
+ const isVarChange =
223
+ currentVar !== undefined &&
224
+ prevVar !== undefined &&
225
+ String(currentVar) !== String(prevVar);
226
+
227
+ if (isVarChange) {
228
+ const { targetMin, targetMax } = getGeoZarrBandRange(
229
+ this.layer,
230
+ String(currentVar),
231
+ this.layerConfig,
232
+ );
233
+ if (e.detail) {
234
+ if ("min" in e.detail || this.layerConfig?.min !== undefined) {
235
+ e.detail.min = targetMin;
236
+ }
237
+ if ("max" in e.detail || this.layerConfig?.max !== undefined) {
238
+ e.detail.max = targetMax;
239
+ }
240
+ }
241
+ }
242
+
243
+ this.#data = { ...e.detail };
244
+
245
+ if (this.layerConfig?.type === "style" || this.layerConfig?.style) {
139
246
  const supportStyleConfig =
140
247
  "setStyle" in this.layer || "updateStyleVariables" in this.layer;
141
248
  if (supportStyleConfig) {
142
- applyUpdatedStyles(this.#data, this.layer, this.layerConfig);
249
+ await applyUpdatedStyles(this.#data, this.layer, this.layerConfig);
143
250
  } else {
144
251
  console.error(
145
252
  `Layer type ${
@@ -154,11 +261,59 @@ export class EOxLayerControlLayerConfig extends LitElement {
154
261
  this,
155
262
  );
156
263
  }
264
+ if (isVarChange) {
265
+ this.layerConfig = this.layer?.get?.("layerConfig") || this.layerConfig;
266
+ const freshStartVals = getStartVals(this.layer, this.layerConfig) || {};
267
+ const schemaProps = this.layerConfig?.schema?.properties;
268
+ const isAutofill =
269
+ this.layerConfig?.autofill === true ||
270
+ this.layerConfig?.schema?.autofill === true;
271
+ this.#startVals = {
272
+ ...this.#data,
273
+ ...freshStartVals,
274
+ ...(currentVar &&
275
+ (isAutofill ||
276
+ schemaProps?.variable !== undefined ||
277
+ schemaProps?.band !== undefined)
278
+ ? schemaProps?.band !== undefined
279
+ ? { band: currentVar }
280
+ : { variable: currentVar }
281
+ : {}),
282
+ ...(this.#data.min !== undefined &&
283
+ (isAutofill || schemaProps?.min !== undefined)
284
+ ? { min: this.#data.min }
285
+ : {}),
286
+ ...(this.#data.max !== undefined &&
287
+ (isAutofill || schemaProps?.max !== undefined)
288
+ ? { max: this.#data.max }
289
+ : {}),
290
+ };
291
+ if (schemaProps && !isAutofill) {
292
+ const allowedKeys = new Set(Object.keys(schemaProps));
293
+ for (const k of Object.keys(this.#startVals)) {
294
+ if (!allowedKeys.has(k)) {
295
+ delete this.#startVals[k];
296
+ }
297
+ }
298
+ }
299
+ this.#data = { ...this.#startVals };
300
+
301
+ const jsonform = /** @type {any} */ (
302
+ this.renderRoot?.querySelector("eox-jsonform")
303
+ );
304
+ if (
305
+ jsonform?.editor &&
306
+ !jsonform.editor.destroyed &&
307
+ jsonform.editor.ready
308
+ ) {
309
+ jsonform.editor.setValue(this.#data);
310
+ }
311
+ }
157
312
  this.dispatchEvent(
158
313
  new CustomEvent("layerConfig:change", {
159
314
  bubbles: true,
160
315
  detail: {
161
- jsonformValue: e.detail,
316
+ jsonformValue: this.#data,
162
317
  layer: this.layer,
163
318
  },
164
319
  }),
@@ -178,10 +333,21 @@ export class EOxLayerControlLayerConfig extends LitElement {
178
333
  */
179
334
  render() {
180
335
  // Fetch initial values for the layer and its configuration
181
- this.#startVals = getStartVals(this.layer, this.layerConfig);
182
- if (Object.keys(this.#data).length !== 0) {
183
- this.#startVals = this.#data;
336
+ const freshStartVals = getStartVals(this.layer, this.layerConfig) || {};
337
+ this.#startVals = { ...freshStartVals, ...this.#data };
338
+ const schemaProps = this.layerConfig?.schema?.properties;
339
+ const isAutofill =
340
+ this.layerConfig?.autofill === true ||
341
+ this.layerConfig?.schema?.autofill === true;
342
+ if (schemaProps && !isAutofill) {
343
+ const allowedKeys = new Set(Object.keys(schemaProps));
344
+ for (const k of Object.keys(this.#startVals)) {
345
+ if (!allowedKeys.has(k)) {
346
+ delete this.#startVals[k];
347
+ }
348
+ }
184
349
  }
350
+ this.#data = { ...this.#startVals };
185
351
  if (!customElements.get("eox-jsonform")) {
186
352
  console.error("Please import @eox/jsonform in order to use layerconfig");
187
353
  }
@@ -191,6 +357,7 @@ export class EOxLayerControlLayerConfig extends LitElement {
191
357
  disable_edit_json: true,
192
358
  disable_collapse: true,
193
359
  disable_properties: true,
360
+ no_additional_properties: true,
194
361
  };
195
362
  return html`
196
363
  <style>
@@ -143,6 +143,19 @@ export class EOxLayerControlLayerTools extends LitElement {
143
143
  return this.noShadow ? this : super.createRenderRoot();
144
144
  }
145
145
 
146
+ firstUpdated() {
147
+ this.layer?.on?.("propertychange", (e) => {
148
+ const propEvent = /** @type {import("ol/Object").ObjectEvent} */ (e);
149
+ if (
150
+ propEvent.key === "layerConfig" ||
151
+ propEvent.key === "layerDatetime" ||
152
+ propEvent.key === "timeControlValues"
153
+ ) {
154
+ this.requestUpdate();
155
+ }
156
+ });
157
+ }
158
+
146
159
  updated(changedProperties) {
147
160
  if (
148
161
  this.toolsAutoExpand &&
@@ -14,8 +14,9 @@ export function getStartVals(layer, layerConfig) {
14
14
  // extract style variables from layer
15
15
  let styleVars =
16
16
  "updateStyleVariables" in layer
17
- ? /** @type {import("ol/layer/WebGLTile").default} */
18
- (layer)["style_"]?.variables
17
+ ? /** @type {any} */ (layer).styleVariables_ ||
18
+ /** @type {any} */ (layer)["style_"]?.variables ||
19
+ layer.get?.("style")?.variables
19
20
  : layerConfig.style?.variables;
20
21
  if ((layerConfig.type === "style" || layerConfig.style) && styleVars) {
21
22
  nestedValues = { ...styleVars };
@@ -46,6 +47,29 @@ export function getStartVals(layer, layerConfig) {
46
47
  }
47
48
  } else return null;
48
49
 
50
+ const cfg = /** @type {any} */ (layerConfig);
51
+ if (nestedValues.min === undefined && cfg.min !== undefined) {
52
+ nestedValues.min = cfg.min;
53
+ }
54
+ if (nestedValues.max === undefined && cfg.max !== undefined) {
55
+ nestedValues.max = cfg.max;
56
+ }
57
+ if (nestedValues.bands === undefined && cfg.bands !== undefined) {
58
+ nestedValues.bands = cfg.bands;
59
+ }
60
+ if (nestedValues.band === undefined && cfg.band !== undefined) {
61
+ nestedValues.band = cfg.band;
62
+ }
63
+ if (nestedValues.variable === undefined) {
64
+ nestedValues.variable =
65
+ cfg.variable ??
66
+ cfg.band ??
67
+ (Array.isArray(cfg.bands) && cfg.bands.length > 0
68
+ ? cfg.bands[0]
69
+ : undefined) ??
70
+ layer.get?.("_lastVariable");
71
+ }
72
+
49
73
  // fall back for schemas passed as a bare map of field definitions
50
74
  const fieldSchemas = collectFieldSchemas(layerConfig.schema);
51
75
  const startVals = getNestedStartVals(
@@ -127,6 +151,36 @@ export function getNestedStartVals(
127
151
  : nestedValues[key];
128
152
  // keep the raw URL value instead of NaN (e.g. "auto" on a number field)
129
153
  startVals[key] = Number.isNaN(value) ? nestedValues[key] : value;
154
+ } else if (key === "variable" && type && type !== "object" && layer) {
155
+ const source = /** @type {any} */ (
156
+ layer.getSource ? layer.getSource() : null
157
+ );
158
+ const bands =
159
+ layer.get?.("_lastVariable") ||
160
+ (typeof source?.getBands === "function" ? source.getBands() : null) ||
161
+ source?.bands_ ||
162
+ layer.get?.("_jsonDefinition")?.source?.bands ||
163
+ layer.get?.("source")?.bands;
164
+ const bandVal =
165
+ Array.isArray(bands) && bands.length > 0
166
+ ? bands[0]
167
+ : typeof bands === "string"
168
+ ? bands
169
+ : undefined;
170
+ if (
171
+ bandVal &&
172
+ (!Array.isArray(schema[key]?.enum) ||
173
+ schema[key].enum.includes(bandVal))
174
+ ) {
175
+ startVals[key] = bandVal;
176
+ } else if (schema[key]?.default !== undefined) {
177
+ startVals[key] = schema[key].default;
178
+ } else if (
179
+ Array.isArray(schema[key]?.enum) &&
180
+ schema[key].enum.length > 0
181
+ ) {
182
+ startVals[key] = schema[key].enum[0];
183
+ }
130
184
  } else if (
131
185
  type &&
132
186
  type !== "object" &&
@@ -140,19 +194,6 @@ export function getNestedStartVals(
140
194
  schema[key].enum.length > 0
141
195
  ) {
142
196
  startVals[key] = schema[key].enum[0];
143
- } else if (key === "variable" && type && type !== "object" && layer) {
144
- const source = /** @type {any} */ (
145
- layer.getSource ? layer.getSource() : null
146
- );
147
- const bands =
148
- (typeof source?.getBands === "function" ? source.getBands() : null) ||
149
- source?.bands_ ||
150
- layer.get?.("_lastVariable") ||
151
- layer.get?.("_jsonDefinition")?.source?.bands ||
152
- layer.get?.("source")?.bands;
153
- if (bands) {
154
- startVals[key] = Array.isArray(bands) ? bands[0] : bands;
155
- }
156
197
  } else {
157
198
  // Recursively traverse nested properties
158
199
  const nestedStartVals = getNestedStartVals(
@@ -22,3 +22,9 @@ export {
22
22
  } from "./layer-tools";
23
23
  export { hideLayersBasedOnProperties } from "./hide-layers-based-on-property";
24
24
  export { handleDatetimeUpdate } from "./handle-datetime-update";
25
+ export {
26
+ getGeoZarrBandRange,
27
+ updateGeoZarrBand,
28
+ computeStep,
29
+ buildMinMaxProperties,
30
+ } from "./update-geozarr-band";