@eox/layercontrol 1.8.0 → 1.8.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eox/layercontrol",
3
- "version": "1.8.0",
3
+ "version": "1.8.1",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",
@@ -12,7 +12,7 @@
12
12
  "@types/lodash.throttle": "^4.1.9",
13
13
  "@types/sortablejs": "^1.15.1",
14
14
  "ol": "^10.9.0",
15
- "vite": "^8.0.16"
15
+ "vite": "^8.1.0"
16
16
  },
17
17
  "engines": {
18
18
  "npm": ">=8.0.0",
@@ -1,33 +1,3 @@
1
- /**
2
- * Recursively traverses the schema object to extract startVals based on values of nested properties.
3
- *
4
- * @param {{[key: string]: any}} schema - The schema object to traverse.
5
- * @param {{[key: string]: any}} nestedValues - values of nested properties to extract startVals.
6
- * @returns {object} - Object containing the startVals.
7
- */
8
- export function getNestedStartVals(schema, nestedValues) {
9
- let startVals = {};
10
-
11
- for (const key in schema) {
12
- const type = schema[key].type;
13
- // Extract startVal based on type
14
- if (type && type !== "object" && nestedValues[key] !== undefined) {
15
- startVals[key] =
16
- type === "number" ? Number(nestedValues[key]) : nestedValues[key];
17
- } else if (typeof schema[key] === "object" && schema[key]?.properties) {
18
- // Recursively traverse nested properties
19
- const nestedStartVals = getNestedStartVals(
20
- schema[key].properties,
21
- nestedValues,
22
- );
23
- if (Object.keys(nestedStartVals).length > 0) {
24
- startVals[key] = nestedStartVals;
25
- }
26
- }
27
- }
28
- return startVals;
29
- }
30
-
31
1
  /**
32
2
  * Retrieves startVals from Query Params or style variables based on layer configuration.
33
3
  *
@@ -76,10 +46,109 @@ export function getStartVals(layer, layerConfig) {
76
46
  }
77
47
  } else return null;
78
48
 
49
+ // fall back for schemas passed as a bare map of field definitions
50
+ const fieldSchemas = collectFieldSchemas(layerConfig.schema);
79
51
  const startVals = getNestedStartVals(
80
- layerConfig.schema?.properties || layerConfig.schema,
52
+ Object.keys(fieldSchemas).length ? fieldSchemas : layerConfig.schema,
81
53
  nestedValues,
54
+ layerConfig.schema,
82
55
  );
83
56
 
84
57
  return Object.keys(startVals).length ? startVals : null;
85
58
  }
59
+
60
+ /**
61
+ * Collects a schema's field definitions from `properties` and from `anyOf`/`oneOf`/`allOf`
62
+ * branches, resolving `$ref` branches against the root schema. json-editor uses those
63
+ * combinators for "pick one variant" forms, where `properties` alone is empty.
64
+ *
65
+ * @param {{[key: string]: any}} schema - The schema object to collect fields from.
66
+ * @param {{[key: string]: any}} [rootSchema] - The root schema `$ref`s resolve against.
67
+ * @param {Set<string>} [seenRefs] - Already-resolved refs, guards against circular `$ref`s.
68
+ * @returns {{[key: string]: any}} - Field definitions keyed by property name.
69
+ */
70
+ function collectFieldSchemas(
71
+ schema,
72
+ rootSchema = schema,
73
+ seenRefs = new Set(),
74
+ ) {
75
+ if (!schema || typeof schema !== "object") return {};
76
+
77
+ const fields = {};
78
+ if (typeof schema.$ref === "string" && !seenRefs.has(schema.$ref)) {
79
+ seenRefs.add(schema.$ref);
80
+ Object.assign(
81
+ fields,
82
+ collectFieldSchemas(
83
+ resolveLocalRef(schema.$ref, rootSchema),
84
+ rootSchema,
85
+ seenRefs,
86
+ ),
87
+ );
88
+ }
89
+ for (const key of ["anyOf", "oneOf", "allOf"]) {
90
+ if (Array.isArray(schema[key])) {
91
+ for (const branch of schema[key]) {
92
+ Object.assign(
93
+ fields,
94
+ collectFieldSchemas(branch, rootSchema, seenRefs),
95
+ );
96
+ }
97
+ }
98
+ }
99
+ // `properties` overlays last so constraint-only branch entries can't shadow real fields
100
+ return Object.assign(fields, schema.properties);
101
+ }
102
+
103
+ /**
104
+ * Recursively traverses the schema object to extract startVals based on values of nested properties.
105
+ *
106
+ * @param {{[key: string]: any}} schema - The schema object to traverse.
107
+ * @param {{[key: string]: any}} nestedValues - values of nested properties to extract startVals.
108
+ * @param {{[key: string]: any}} [rootSchema] - The root schema `$ref`s resolve against.
109
+ * @returns {object} - Object containing the startVals.
110
+ */
111
+ export function getNestedStartVals(schema, nestedValues, rootSchema = schema) {
112
+ let startVals = {};
113
+
114
+ for (const key in schema) {
115
+ const type = schema[key]?.type;
116
+ // Extract startVal based on type
117
+ if (type && type !== "object" && nestedValues[key] !== undefined) {
118
+ const value = ["number", "integer"].includes(type)
119
+ ? Number(nestedValues[key])
120
+ : nestedValues[key];
121
+ // keep the raw URL value instead of NaN (e.g. "auto" on a number field)
122
+ startVals[key] = Number.isNaN(value) ? nestedValues[key] : value;
123
+ } else {
124
+ // Recursively traverse nested properties
125
+ const nestedStartVals = getNestedStartVals(
126
+ collectFieldSchemas(schema[key], rootSchema),
127
+ nestedValues,
128
+ rootSchema,
129
+ );
130
+ if (Object.keys(nestedStartVals).length > 0) {
131
+ startVals[key] = nestedStartVals;
132
+ }
133
+ }
134
+ }
135
+ return startVals;
136
+ }
137
+
138
+ /**
139
+ * Resolves a local `$ref` pointer (e.g. `#/definitions/foo`) against the root schema.
140
+ *
141
+ * @param {string} ref - The `$ref` value.
142
+ * @param {{[key: string]: any}} rootSchema - The schema holding `definitions`/`$defs`.
143
+ * @returns {{[key: string]: any} | undefined} - The referenced schema, or undefined for external/unresolvable refs.
144
+ */
145
+ function resolveLocalRef(ref, rootSchema) {
146
+ if (!ref.startsWith("#/")) return undefined;
147
+ return ref
148
+ .slice(2)
149
+ .split("/")
150
+ .reduce(
151
+ (node, part) => node?.[part.replace(/~1/g, "/").replace(/~0/g, "~")],
152
+ rootSchema,
153
+ );
154
+ }
@@ -1,21 +1,24 @@
1
+ /**
2
+ * Retrieves startVals from Query Params or style variables based on layer configuration.
3
+ *
4
+ * @param {import("ol/layer").Layer} layer - The layer object.
5
+ * @param {import("../components/layer-config").EOxLayerControlLayerConfig['layerConfig']} layerConfig - Configuration object for the layer.
6
+ * @returns {object | null} - Object containing the startVals or null if not found.
7
+ */
8
+ export function getStartVals(layer: import("ol/layer").Layer, layerConfig: import("../components/layer-config").EOxLayerControlLayerConfig["layerConfig"]): object | null;
1
9
  /**
2
10
  * Recursively traverses the schema object to extract startVals based on values of nested properties.
3
11
  *
4
12
  * @param {{[key: string]: any}} schema - The schema object to traverse.
5
13
  * @param {{[key: string]: any}} nestedValues - values of nested properties to extract startVals.
14
+ * @param {{[key: string]: any}} [rootSchema] - The root schema `$ref`s resolve against.
6
15
  * @returns {object} - Object containing the startVals.
7
16
  */
8
17
  export function getNestedStartVals(schema: {
9
18
  [key: string]: any;
10
19
  }, nestedValues: {
11
20
  [key: string]: any;
21
+ }, rootSchema?: {
22
+ [key: string]: any;
12
23
  }): object;
13
- /**
14
- * Retrieves startVals from Query Params or style variables based on layer configuration.
15
- *
16
- * @param {import("ol/layer").Layer} layer - The layer object.
17
- * @param {import("../components/layer-config").EOxLayerControlLayerConfig['layerConfig']} layerConfig - Configuration object for the layer.
18
- * @returns {object | null} - Object containing the startVals or null if not found.
19
- */
20
- export function getStartVals(layer: import("ol/layer").Layer, layerConfig: import("../components/layer-config").EOxLayerControlLayerConfig["layerConfig"]): object | null;
21
24
  //# sourceMappingURL=get-start-vals.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"get-start-vals.d.ts","sourceRoot":"","sources":["../../src/helpers/get-start-vals.js"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,2CAJW;IAAC,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;CAAC,gBACpB;IAAC,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;CAAC,GAClB,MAAM,CAuBlB;AAED;;;;;;GAMG;AACH,oCAJW,OAAO,UAAU,EAAE,KAAK,eACxB,OAAO,4BAA4B,EAAE,0BAA0B,CAAC,aAAa,CAAC,GAC5E,MAAM,GAAG,IAAI,CAiDzB"}
1
+ {"version":3,"file":"get-start-vals.d.ts","sourceRoot":"","sources":["../../src/helpers/get-start-vals.js"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,oCAJW,OAAO,UAAU,EAAE,KAAK,eACxB,OAAO,4BAA4B,EAAE,0BAA0B,CAAC,aAAa,CAAC,GAC5E,MAAM,GAAG,IAAI,CAoDzB;AA6CD;;;;;;;GAOG;AACH,2CALW;IAAC,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;CAAC,gBACpB;IAAC,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;CAAC,eACpB;IAAC,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;CAAC,GAClB,MAAM,CA2BlB"}