@emuanalytics/flow-rdf-core 4.1.8 → 4.3.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 (27) hide show
  1. package/dist/index.d.mts +322 -4
  2. package/dist/index.d.ts +322 -4
  3. package/dist/index.mjs +6 -0
  4. package/dist/lib/map/flow-core-map.mjs +120 -13
  5. package/dist/lib/map/plugins/index.d.mts +252 -223
  6. package/dist/lib/map/plugins/index.d.ts +252 -223
  7. package/dist/lib/map/plugins/selection/selection.plugin.mjs +73 -7
  8. package/dist/lib/map/plugins/selection/source-selection.mjs +12 -0
  9. package/dist/lib/map-style/composite/builder.mjs +31 -0
  10. package/dist/lib/map-style/composite/editing.mjs +123 -0
  11. package/dist/lib/map-style/composite/expand.mjs +182 -0
  12. package/dist/lib/map-style/composite/stored-format.mjs +18 -0
  13. package/dist/lib/map-style/composite/validate.mjs +376 -0
  14. package/dist/lib/map-style/flow-style/builder.mjs +97 -0
  15. package/dist/lib/map-style/flow-style/data-driven.mjs +164 -0
  16. package/dist/lib/map-style/flow-style/expand.mjs +145 -0
  17. package/dist/lib/map-style/flow-style/literals.mjs +58 -0
  18. package/dist/lib/map-style/flow-style/stored-format.mjs +93 -0
  19. package/dist/lib/map-style/flow-style/validate.mjs +117 -0
  20. package/dist/lib/map-style/flow-style/values.mjs +249 -0
  21. package/dist/lib/map-style/map-data-source.mjs +9 -0
  22. package/dist/lib/map-style/map-layer.mjs +555 -17
  23. package/dist/lib/map-style/map-style.mjs +250 -39
  24. package/dist/package.json.mjs +1 -1
  25. package/dist/shared/{flow-rdf-core.1ee62df9.d.mts → flow-rdf-core.62a36d8b.d.mts} +1077 -234
  26. package/dist/shared/{flow-rdf-core.1ee62df9.d.ts → flow-rdf-core.62a36d8b.d.ts} +1077 -234
  27. package/package.json +1 -1
@@ -1,8 +1,13 @@
1
1
  import * as maplibreSpec from '@emuanalytics/maplibre-gl-style-spec';
2
- import { makeObservable, observable, computed, action } from 'mobx';
3
- import { isDefined, isObjectFull, isNil } from '../utils/type-checks.mjs';
4
- import { ReactiveExpression } from '../expressions/reactive-expression.mjs';
2
+ import { makeObservable, observable, computed, action, toJS, comparer } from 'mobx';
3
+ import { isNil, isBoolean, isDefined, isObjectFull, isString, isIn } from '../utils/type-checks.mjs';
4
+ import { expandCompositeLayer } from './composite/expand.mjs';
5
+ import { saveCompositeLayerSpec, loadCompositeLayerSpec } from './composite/stored-format.mjs';
6
+ import { expandFlowStyle } from './flow-style/expand.mjs';
7
+ import { saveStoredFlowStyle, loadStoredFlowStyle, setStoredFlowStyleProperty } from './flow-style/stored-format.mjs';
8
+ import { flowStyleVersion } from './flow-style/values.mjs';
5
9
  import { FlowCoreError } from '../errors/flow-error.mjs';
10
+ import { ReactiveExpression } from '../expressions/reactive-expression.mjs';
6
11
 
7
12
  var __defProp = Object.defineProperty;
8
13
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
@@ -17,11 +22,29 @@ const _MapLayer = class _MapLayer {
17
22
  __publicField(this, "_filter", null);
18
23
  __publicField(this, "_paintProps", {});
19
24
  __publicField(this, "_layoutProps", {});
20
- this._options = {
21
- ..._MapLayer._defaultOptions,
22
- ...options
23
- };
24
- this.getPropertyMetadata("visibility", options.type);
25
+ __publicField(this, "_visibility", null);
26
+ __publicField(this, "_subLayers", null);
27
+ __publicField(this, "_ownedSources", null);
28
+ __publicField(this, "_ownedSourcesListeners", /* @__PURE__ */ new Set());
29
+ __publicField(this, "_flow", null);
30
+ __publicField(this, "_composite", null);
31
+ __publicField(this, "_mapSources", null);
32
+ __publicField(this, "_issues", []);
33
+ // A flow layer's `interactive` option when it isn't a boolean: the default applies, with a warning in version 2
34
+ __publicField(this, "_invalidInteractive");
35
+ const isComposite = options.type === "composite";
36
+ this._options = isComposite ? (
37
+ // A composite layer's own zoom range applies only if set: its sub-layers have theirs
38
+ { interactive: true, ...options, source: "" }
39
+ ) : { ..._MapLayer._defaultOptions, ...options };
40
+ const isFlow = options.type === "flow";
41
+ if (isFlow && !isNil(options.interactive) && !isBoolean(options.interactive)) {
42
+ this._invalidInteractive = options.interactive;
43
+ this._options.interactive = true;
44
+ }
45
+ if (!isFlow && !isComposite) {
46
+ this.getPropertyMetadata("visibility", options.type);
47
+ }
25
48
  makeObservable(
26
49
  this,
27
50
  {
@@ -30,8 +53,17 @@ const _MapLayer = class _MapLayer {
30
53
  _layoutProps: observable,
31
54
  _filter: observable,
32
55
  _options: observable,
56
+ _visibility: observable.ref,
57
+ _subLayers: observable.ref,
58
+ _ownedSources: observable.ref,
59
+ _flow: observable.ref,
60
+ _composite: observable.ref,
61
+ _mapSources: observable.ref,
62
+ _issues: observable.ref,
33
63
  //computed
34
64
  layerSpecification: computed,
65
+ layerSpecifications: computed,
66
+ compositeIssues: computed,
35
67
  // actions
36
68
  bindState: action,
37
69
  setMinZoom: action,
@@ -41,22 +73,37 @@ const _MapLayer = class _MapLayer {
41
73
  setBelow: action,
42
74
  setStyle: action,
43
75
  setStyleProperty: action,
44
- setFilter: action
76
+ setFilter: action,
77
+ setVisibility: action,
78
+ setSubLayers: action,
79
+ setOwnedSources: action,
80
+ setMapSources: action,
81
+ setVersion1FlowStyle: action
45
82
  },
46
83
  { name: this.formattedName(options.id) }
47
84
  );
48
85
  if (options.filter) {
49
86
  this.setFilter(options.filter);
50
87
  }
51
- if (options.style) {
88
+ if (isDefined(options.visibility)) {
89
+ this.setVisibility(options.visibility);
90
+ }
91
+ if (isFlow || isComposite) {
92
+ this.setStyle(options.style);
93
+ } else if (options.style) {
52
94
  this.setStyle(options.style);
53
95
  }
54
96
  }
97
+ /**
98
+ * The specification of the layer's own MapLibre layer. A layer that renders as several MapLibre layers, such as a
99
+ * flow layer, doesn't render this one.
100
+ */
55
101
  get layerSpecification() {
56
102
  void this._options.above;
57
103
  void this._options.below;
58
104
  const layer = {
59
105
  id: this._options.id,
106
+ // A flow layer's own type, 'flow', is never rendered: it has sub-layers
60
107
  type: this._options.type,
61
108
  source: this._options.source
62
109
  };
@@ -88,14 +135,124 @@ const _MapLayer = class _MapLayer {
88
135
  return accum;
89
136
  }, {});
90
137
  }
138
+ if (this._visibility) {
139
+ layer.layout = { ...layer.layout, visibility: this.visibilityValue(this._visibility) };
140
+ }
141
+ if (isDefined(this._options.metadata)) {
142
+ layer.metadata = toJS(this._options.metadata);
143
+ }
91
144
  return layer;
92
145
  }
146
+ /**
147
+ * The specifications of every MapLibre layer the layer renders as, in draw order: its sub-layers', or its own.
148
+ *
149
+ * @internal
150
+ */
151
+ get layerSpecifications() {
152
+ if (!this._subLayers) {
153
+ return [this.layerSpecification];
154
+ }
155
+ return this._subLayers.map((subLayer) => this.subLayerSpecification(subLayer));
156
+ }
157
+ /**
158
+ * IDs of the MapLibre layers the layer renders as, in draw order.
159
+ *
160
+ * @internal
161
+ */
162
+ get maplibreLayerIds() {
163
+ return this._subLayers ? this._subLayers.map(({ options }) => options.id) : [this._options.id];
164
+ }
165
+ /**
166
+ * IDs of the MapLibre layers whose features click and hover events report, as this layer.
167
+ *
168
+ * @internal
169
+ */
170
+ get interactiveLayerIds() {
171
+ if (!this.interactive) {
172
+ return [];
173
+ }
174
+ return this._subLayers ? this._subLayers.filter(({ options }) => options.interactive !== false).map(({ options }) => options.id) : [this._options.id];
175
+ }
176
+ /**
177
+ * IDs of the map data sources the layer's MapLibre layers display.
178
+ *
179
+ * @internal
180
+ */
181
+ get sourceIds() {
182
+ if (!this._subLayers) {
183
+ return [this._options.source];
184
+ }
185
+ return [...new Set(this._subLayers.map(({ layer }) => layer.source))];
186
+ }
187
+ /**
188
+ * The layer's sub-layers, or `null` for a layer that renders as one MapLibre layer.
189
+ *
190
+ * @internal
191
+ */
192
+ get subLayers() {
193
+ return this._subLayers ? this._subLayers.map(({ options }) => options) : null;
194
+ }
195
+ /**
196
+ * The data sources the layer brings with it (see {@link setOwnedSources}), or `null` for none.
197
+ *
198
+ * @internal
199
+ */
200
+ get ownedSources() {
201
+ return this._ownedSources;
202
+ }
203
+ /**
204
+ * Layer metadata, copied to the `metadata` of the layer's MapLibre layers.
205
+ *
206
+ * @experimental
207
+ */
208
+ get metadata() {
209
+ return this._options.metadata;
210
+ }
211
+ /**
212
+ * A flow layer's style, in its stored format and version, ready to save. A copy: change the layer with
213
+ * {@link setStyle} and {@link setStyleProperty}. `undefined` for a MapLibre layer.
214
+ *
215
+ * Reading it in a MobX reaction, or a component that renders from state, tracks changes to the style.
216
+ *
217
+ * @experimental
218
+ */
219
+ get flowStyle() {
220
+ return this._flow ? saveStoredFlowStyle(this._flow.stored) : void 0;
221
+ }
222
+ /**
223
+ * A composite layer's spec, in its stored format and version (a version 1 spec stays version 1), ready to save. A
224
+ * copy: change the layer with {@link setStyle}. `undefined` for any other layer type.
225
+ *
226
+ * Reading it in a MobX reaction, or a component that renders from state, tracks changes to the spec.
227
+ *
228
+ * @experimental
229
+ */
230
+ get compositeSpec() {
231
+ return this._composite ? saveCompositeLayerSpec(this._composite.stored) : void 0;
232
+ }
233
+ /**
234
+ * The problems with a flow layer's style or a composite layer's spec, updated whenever the style changes. An `error`
235
+ * means a property, a sub-layer, a data source or the whole style isn't rendered; a `warning` means it is rendered
236
+ * anyway. Each has the path of the problem in the style or spec, such as `layers[1].style.fillColor`. Empty for a
237
+ * MapLibre layer, which throws on a bad style instead.
238
+ *
239
+ * A composite layer on a map also lists each sub-layer whose data source is neither in the spec nor on the map, and
240
+ * so isn't rendered, and the map emits each new issue of a composite layer as an `error` event.
241
+ *
242
+ * The list is observable: read it in a MobX reaction to follow it.
243
+ *
244
+ * @experimental
245
+ */
246
+ get issues() {
247
+ return this._composite ? this.compositeIssues : this._issues;
248
+ }
93
249
  get id() {
94
250
  return this._options.id;
95
251
  }
96
252
  get type() {
97
253
  return this._options.type;
98
254
  }
255
+ /** The data source the layer displays. Empty for a composite layer, whose sub-layers name their own. */
99
256
  get source() {
100
257
  return this._options.source;
101
258
  }
@@ -115,6 +272,8 @@ const _MapLayer = class _MapLayer {
115
272
  }
116
273
  Object.values(this._paintProps).forEach((prop) => prop.bindState(state));
117
274
  Object.values(this._layoutProps).forEach((prop) => prop.bindState(state));
275
+ this._visibility?.bindState(state);
276
+ this._subLayers?.forEach(({ layer }) => layer.bindState(state));
118
277
  return this;
119
278
  }
120
279
  setMinZoom(zoom) {
@@ -137,8 +296,36 @@ const _MapLayer = class _MapLayer {
137
296
  this._options.below = layerId;
138
297
  return this;
139
298
  }
140
- setStyle(styleProgram) {
141
- const parsedProgram = ReactiveExpression.parseProgram(styleProgram);
299
+ /**
300
+ * Replaces the layer's style.
301
+ *
302
+ * - **MapLibre layer types**: sets each property of a style program, such as one built with {@link layerStyle}.
303
+ * Properties the program doesn't set are kept. Throws on an invalid program or an unknown property.
304
+ * - **Flow layers**: replaces the whole flow style with a version 2 {@link FlowStyle}, such as one built with
305
+ * {@link flowLayerStyle}. The layer's MapLibre layers are updated in place, without reloading the source. Never
306
+ * throws: a property with a problem isn't rendered, the rest is, and {@link issues} lists the problems.
307
+ * - **Composite layers**: replaces the whole spec with a {@link CompositeLayerSpecification}, such as one edited with
308
+ * {@link updateCompositeFlowStyle}, in one update of the map: MapLibre layers whose IDs persist keep their place
309
+ * and are restyled, data sources whose options are unchanged aren't reloaded, and a spec equal to the current one
310
+ * changes nothing. The spec keeps its own version. Never throws: {@link issues} lists the problems.
311
+ *
312
+ * @param style - Style program for a MapLibre layer, flow style for a flow layer, or spec for a composite layer.
313
+ */
314
+ setStyle(style) {
315
+ if (this._options.type === "flow") {
316
+ this.replaceFlowStyle(style);
317
+ return;
318
+ }
319
+ if (this._options.type === "composite") {
320
+ this.replaceCompositeSpec(style);
321
+ return;
322
+ }
323
+ if (typeof style !== "string") {
324
+ throw new FlowCoreError(
325
+ `${this.formattedName()}: a '${this._options.type}' layer's style is a style program string; a flow style needs type 'flow'`
326
+ );
327
+ }
328
+ const parsedProgram = ReactiveExpression.parseProgram(style);
142
329
  const props = Object.keys(parsedProgram);
143
330
  props.forEach((prop) => {
144
331
  const metadata = this.getPropertyMetadata(prop, this._options.type);
@@ -147,12 +334,41 @@ const _MapLayer = class _MapLayer {
147
334
  });
148
335
  }
149
336
  /**
150
- * Set an individual style property.
337
+ * Sets or removes one style property. Its value follows different rules for MapLibre layers and flow layers.
151
338
  *
152
- * @param property Property name.
153
- * @param expression Property style expression.
339
+ * - **MapLibre layer types**: `property` is a MapLibre paint or layout property, such as `circle-color`, and a string
340
+ * `value` is a reactive expression, so `'red'` is the color red and `'@color'` the feature property `color`.
341
+ * Throws on an unknown property or an expression that doesn't parse.
342
+ * - **Flow layers**: `property` is a {@link FlowStyle} property, such as `fillColor`, and `value` follows flow
343
+ * rules, as in {@link flowLayerStyle}: a bare value is a literal, so `'@color'` is the text `@color`; `expr('…')`
344
+ * makes a reactive expression, such as `expr('@color')` or `expr("@@hover ? 'yellow' : 'white'")`; and a
345
+ * {@link FlowStyleDataDriven} table makes an expression with the table attached. The layer's MapLibre layers are
346
+ * updated in place, without reloading the source, and {@link flowStyle} returns the edited style. Never throws:
347
+ * {@link issues} lists any problems.
348
+ * - **Composite layers**: throws. Edit the spec instead, with {@link updateCompositeFlowStyle}, and give it to
349
+ * {@link setStyle}.
350
+ *
351
+ * @example
352
+ * The same edit on a MapLibre layer and on a flow layer:
353
+ * ```js
354
+ * circleLayer.setStyleProperty('circle-stroke-color', "@@hover ? 'yellow' : 'white'");
355
+ * flowLayer.setStyleProperty('strokeColor', expr("@@hover ? 'yellow' : 'white'"));
356
+ * ```
357
+ *
358
+ * @param property - Property name.
359
+ * @param value - Property value, or `null` to remove the property.
154
360
  */
155
- setStyleProperty(property, expression) {
361
+ setStyleProperty(property, value) {
362
+ if (this._options.type === "flow") {
363
+ this.setFlowStyleProperty(property, value);
364
+ return;
365
+ }
366
+ if (this._options.type === "composite") {
367
+ throw new FlowCoreError(
368
+ `${this.formattedName()}: a composite layer is edited through its spec: give an edited spec, such as one from updateCompositeFlowStyle(), to setStyle()`
369
+ );
370
+ }
371
+ const expression = value;
156
372
  const metadata = this.getPropertyMetadata(property, this._options.type);
157
373
  const props = metadata.type === "paint" ? this._paintProps : this._layoutProps;
158
374
  if (isNil(expression)) {
@@ -165,9 +381,16 @@ const _MapLayer = class _MapLayer {
165
381
  props[property] = expr;
166
382
  }
167
383
  }
384
+ /**
385
+ * Sets or clears the layer's filter.
386
+ *
387
+ * @param expression - Reactive expression that a feature must match to be drawn. Specify `null`, or an empty
388
+ * string, to clear the filter.
389
+ * @throws `FlowCoreExpressionError` if the expression doesn't parse.
390
+ */
168
391
  setFilter(expression) {
169
392
  this._filter = null;
170
- if (isDefined(expression)) {
393
+ if (isString(expression) && expression.trim() !== "") {
171
394
  let expr = new ReactiveExpression(expression).bindState(this._state);
172
395
  if (expr.parseError) {
173
396
  throw expr.parseError;
@@ -178,6 +401,309 @@ const _MapLayer = class _MapLayer {
178
401
  this._filter = expr;
179
402
  }
180
403
  }
404
+ /**
405
+ * Shows or hides the layer, and every MapLibre layer of a flow or composite layer, overriding the `visibility` of
406
+ * their styles.
407
+ *
408
+ * @param visibility - Reactive expression, such as `'$showAirports'`, or boolean. Specify `null` to leave visibility
409
+ * to the styles.
410
+ * @throws `FlowCoreExpressionError` if the expression doesn't parse.
411
+ * @experimental
412
+ */
413
+ setVisibility(visibility) {
414
+ if (isNil(visibility)) {
415
+ this._visibility = null;
416
+ return;
417
+ }
418
+ const expr = new ReactiveExpression(isBoolean(visibility) ? String(visibility) : visibility).bindState(this._state);
419
+ if (expr.parseError) {
420
+ throw expr.parseError;
421
+ }
422
+ this._visibility = expr;
423
+ }
424
+ /**
425
+ * Makes the layer render as an ordered group of MapLibre layers, its sub-layers, instead of one. The first sub-layer
426
+ * is drawn lowest. Replacing the sub-layers updates the map in place: MapLibre layers whose IDs persist are restyled,
427
+ * and their sources aren't reloaded. The layer's own style program isn't rendered while it has sub-layers.
428
+ *
429
+ * @param subLayers - Sub-layers in draw order. An empty list renders nothing. Specify `null` to render the layer as
430
+ * one MapLibre layer again.
431
+ * @throws {@link FlowCoreError} if two sub-layers have the same ID, or a sub-layer's type, style program or filter
432
+ * is invalid. The layer's sub-layers are then unchanged.
433
+ * @internal
434
+ */
435
+ setSubLayers(subLayers) {
436
+ if (isNil(subLayers)) {
437
+ this._subLayers = null;
438
+ return;
439
+ }
440
+ const ids = /* @__PURE__ */ new Set();
441
+ const built = subLayers.map((options) => {
442
+ if (ids.has(options.id)) {
443
+ throw new FlowCoreError(`${this.formattedName()}: duplicate sub-layer id '${options.id}'`);
444
+ }
445
+ ids.add(options.id);
446
+ const layer = new _MapLayer({
447
+ id: options.id,
448
+ type: options.type,
449
+ source: options.source ?? this._options.source,
450
+ style: options.style,
451
+ filter: options.filter
452
+ }).bindState(this._state);
453
+ return { options: { ...options }, layer };
454
+ });
455
+ this._subLayers = built;
456
+ }
457
+ /**
458
+ * Declares the data sources the layer brings with it, so that its sub-layers can display them. The map's
459
+ * `MapStyle` adds each one, by ID, while a layer on the map brings it, and shares it between the layers that
460
+ * bring the same ID: the first layer added to the map gives its options, and a later one that gives different
461
+ * options is reported as a style problem. A source the app added with the same ID is used instead, and is never
462
+ * removed. Changing the list updates the map in place: a source whose options are unchanged keeps its
463
+ * `MapDataSource`, so it isn't reloaded.
464
+ *
465
+ * @param sources - Options of each data source. Specify `null`, or an empty list, to bring none.
466
+ * @throws {@link FlowCoreError} if two sources have the same ID, or a source's type is invalid. The layer's sources
467
+ * are then unchanged.
468
+ * @internal
469
+ */
470
+ setOwnedSources(sources) {
471
+ const ids = /* @__PURE__ */ new Set();
472
+ sources?.forEach((source) => {
473
+ if (ids.has(source.id)) {
474
+ throw new FlowCoreError(`${this.formattedName()}: duplicate data source id '${source.id}'`);
475
+ }
476
+ ids.add(source.id);
477
+ if (!isIn(source.type, ["vector", "geojson", "raster"])) {
478
+ throw new FlowCoreError(
479
+ `${this.formattedName()}: data source '${source.id}' has unrecognized type '${source.type}'`
480
+ );
481
+ }
482
+ });
483
+ this._ownedSources = sources?.length ? Object.freeze(sources.map((source) => ({ ...source }))) : null;
484
+ this._ownedSourcesListeners.forEach((listener) => listener());
485
+ }
486
+ /**
487
+ * Calls a listener, synchronously, whenever {@link setOwnedSources} is called: `MapStyle` uses it to add and
488
+ * remove the sources in the same action.
489
+ *
490
+ * @returns A function that removes the listener.
491
+ * @internal
492
+ */
493
+ observeOwnedSources(listener) {
494
+ this._ownedSourcesListeners.add(listener);
495
+ return () => this._ownedSourcesListeners.delete(listener);
496
+ }
497
+ /**
498
+ * Tells the layer which data sources are on its map, while it is on one: a composite layer lists each sub-layer
499
+ * whose data source is missing in {@link issues}. `MapStyle` sets it when the layer is added, and clears it when the
500
+ * layer is removed.
501
+ *
502
+ * @param lookup - Whether a data source ID is on the map, read reactively; `null` when the layer isn't on a map.
503
+ * @internal
504
+ */
505
+ setMapSources(lookup) {
506
+ this._mapSources = lookup;
507
+ }
508
+ /**
509
+ * Loads a version 1 flow style, the legacy stored format, into a flow layer, with its geometry. The layer then keeps
510
+ * version 1: {@link setStyleProperty} writes version 1 forms, {@link setStyle} converts a version 2 style to version 1
511
+ * (keeping the layer's geometry), and {@link flowStyle} returns version 1, so the style saves back in the format old
512
+ * CAA Analyser reads. Loading and saving an unchanged style gives a deep-equal style. Never throws.
513
+ *
514
+ * Use `createVersion1FlowLayer()` (`flow-style/version1-flow-layer.ts`) to build such a layer.
515
+ *
516
+ * @param style - The style as stored: a version 1 style (`type: 'flow'`, `version` 1 or missing), or anything else,
517
+ * which {@link issues} reports.
518
+ * @param geometry - The geometry: the composite sub-layer's `type`, `circle`, `line` or `fill`.
519
+ * @internal
520
+ */
521
+ setVersion1FlowStyle(style, geometry) {
522
+ if (this._options.type !== "flow") {
523
+ throw new FlowCoreError(`${this.formattedName()}: a version 1 flow style needs type 'flow'`);
524
+ }
525
+ this.updateFlowLayer({ stored: loadStoredFlowStyle(style), version1: { geometry }, loadIssues: [] });
526
+ }
527
+ /** Replaces a flow layer's style, under the version rules of the layer. */
528
+ replaceFlowStyle(style) {
529
+ const version1 = this._flow?.version1;
530
+ this.updateFlowLayer(
531
+ version1 && flowStyleVersion(style) === 2 ? { ...this.convertToVersion1(style, version1.geometry), version1 } : { stored: loadStoredFlowStyle(style), version1, loadIssues: [] }
532
+ );
533
+ }
534
+ /**
535
+ * A version 2 style, written in version 1 form for a version 1 layer: each property as {@link setStyleProperty}
536
+ * writes it. The layer's geometry stays; a different one in the style is a warning.
537
+ */
538
+ convertToVersion1(style, geometry) {
539
+ let stored = loadStoredFlowStyle({ type: "flow", version: 1 });
540
+ for (const key of Object.keys(style)) {
541
+ if (key !== "type" && key !== "version") {
542
+ stored = setStoredFlowStyleProperty(stored, key, style[key]);
543
+ }
544
+ }
545
+ const loadIssues = style.type === geometry ? [] : [
546
+ {
547
+ path: "type",
548
+ message: `The layer keeps version 1 and its geometry, ${describeValue(geometry)}: the style's type, ${describeValue(style.type)}, is not used.`,
549
+ severity: "warning"
550
+ }
551
+ ];
552
+ return { stored, loadIssues };
553
+ }
554
+ /** Sets or removes one property of a flow layer's style, under the style's version rules. */
555
+ setFlowStyleProperty(property, value) {
556
+ const flow = this._flow;
557
+ try {
558
+ this.updateFlowLayer({
559
+ ...flow,
560
+ stored: setStoredFlowStyleProperty(flow.stored, property, value),
561
+ loadIssues: []
562
+ });
563
+ } catch (e) {
564
+ this._issues = [...this._issues, unreadable(property, e)];
565
+ }
566
+ }
567
+ /** Sets a flow layer's style, and renders it as sub-layers. */
568
+ updateFlowLayer(flow) {
569
+ this._flow = flow;
570
+ const { stored, version1, loadIssues } = flow;
571
+ const issues = [...loadIssues];
572
+ let subLayers = [];
573
+ if (!version1 && stored.version === 1 && stored.style?.type === "flow") {
574
+ issues.push({
575
+ path: "version",
576
+ message: "A flow layer takes a version 2 flow style, with `version: 2` and its geometry as `type`. Build one with `flowLayerStyle()`.",
577
+ severity: "error"
578
+ });
579
+ } else {
580
+ const expanded = expandFlowStyle(this._options.id, stored.style, {
581
+ geometry: version1?.geometry,
582
+ interactive: this._invalidInteractive
583
+ });
584
+ issues.push(...expanded.issues);
585
+ subLayers = expanded.subLayers.map(({ id, type, style, interactive }) => ({
586
+ id,
587
+ type,
588
+ style,
589
+ interactive,
590
+ metadata: { flowLayer: this._options.id }
591
+ }));
592
+ }
593
+ try {
594
+ this.setSubLayers(subLayers);
595
+ } catch (e) {
596
+ issues.push(unreadable("", e));
597
+ this.setSubLayers([]);
598
+ }
599
+ this._issues = issues;
600
+ }
601
+ /** Replaces a composite layer's spec: its sources and sub-layers, in the action that calls it. */
602
+ replaceCompositeSpec(spec) {
603
+ if (this._composite && comparer.structural(this._composite.stored.spec, spec)) {
604
+ return;
605
+ }
606
+ const stored = loadCompositeLayerSpec(spec);
607
+ const expanded = expandCompositeLayer(this._options.id, stored.spec);
608
+ const issues = [...expanded.issues];
609
+ try {
610
+ this.setOwnedSources(expanded.sources);
611
+ } catch (e) {
612
+ issues.push(unreadable("dataSources", e));
613
+ this.setOwnedSources(null);
614
+ }
615
+ try {
616
+ this.setSubLayers(expanded.subLayers);
617
+ } catch {
618
+ const valid = expanded.subLayers.filter((subLayer) => {
619
+ try {
620
+ new _MapLayer({
621
+ id: subLayer.id,
622
+ type: subLayer.type,
623
+ source: "",
624
+ style: subLayer.style,
625
+ filter: subLayer.filter
626
+ });
627
+ return true;
628
+ } catch (e) {
629
+ issues.push(unreadable(this.compositeSubLayerPath(stored.spec, subLayer), e));
630
+ return false;
631
+ }
632
+ });
633
+ this.setSubLayers(valid);
634
+ }
635
+ this._composite = { stored, expanded: { ...expanded, issues } };
636
+ }
637
+ /**
638
+ * A composite layer's issues: the expansion's, and, on a map, its sub-layers' missing data sources. A sub-layer
639
+ * source that isn't in the spec's data sources is a warning in the spec alone; on the map it's nothing if the map
640
+ * has the source, and an error if it doesn't, since the sub-layer then isn't rendered.
641
+ */
642
+ get compositeIssues() {
643
+ const { stored, expanded } = this._composite;
644
+ const hasSource = this._mapSources;
645
+ if (!hasSource) {
646
+ return expanded.issues;
647
+ }
648
+ return expanded.issues.flatMap((issue) => {
649
+ const index = /^layers\[(\d+)\]\.source$/.exec(issue.path)?.[1];
650
+ if (issue.severity !== "warning" || index === void 0) {
651
+ return [issue];
652
+ }
653
+ const { layers, dataSources } = stored.spec;
654
+ const source = layers[Number(index)].source;
655
+ if (hasSource(source)) {
656
+ return [];
657
+ }
658
+ const inSpec = Array.isArray(dataSources) && dataSources.some((dataSource) => dataSource?.id === source);
659
+ return [
660
+ {
661
+ path: issue.path,
662
+ message: `Data source '${source}' isn't on the map: ` + (inSpec ? "it is in `dataSources` but isn't added (see its errors)" : "it isn't in `dataSources`, and the application hasn't added it") + ". The sub-layer isn't rendered.",
663
+ severity: "error"
664
+ }
665
+ ];
666
+ });
667
+ }
668
+ /** The spec path of the sub-layer that gave a MapLibre layer, from its metadata. */
669
+ compositeSubLayerPath(spec, subLayer) {
670
+ const id = subLayer.metadata?.compositeSubLayer;
671
+ const index = (spec.layers ?? []).findIndex((layer) => layer?.id === id);
672
+ return index >= 0 ? `layers[${index}]` : "";
673
+ }
674
+ /** The MapLibre layer specification of a sub-layer, with the layer's shared settings applied. */
675
+ subLayerSpecification({ options, layer }) {
676
+ const { minzoom: _minzoom, maxzoom: _maxzoom, filter, ...spec } = layer.layerSpecification;
677
+ const result = { ...spec };
678
+ const sourceLayer = options.source ? options.sourceLayer : options.sourceLayer ?? this._options.sourceLayer;
679
+ if (isDefined(sourceLayer)) {
680
+ result["source-layer"] = sourceLayer;
681
+ }
682
+ const minZoom = maxDefined(this._options.minZoom, options.minZoom);
683
+ const maxZoom = minDefined(this._options.maxZoom, options.maxZoom);
684
+ if (isDefined(minZoom)) {
685
+ result.minzoom = minZoom;
686
+ }
687
+ if (isDefined(maxZoom)) {
688
+ result.maxzoom = maxZoom;
689
+ }
690
+ const layerFilter = this._filter?.maplibreExpression;
691
+ if (isDefined(layerFilter) && isDefined(filter)) {
692
+ result.filter = ["all", layerFilter, filter];
693
+ } else if (isDefined(layerFilter) || isDefined(filter)) {
694
+ result.filter = layerFilter ?? filter;
695
+ }
696
+ if (this._visibility) {
697
+ result.layout = { ...result.layout, visibility: this.visibilityValue(this._visibility) };
698
+ }
699
+ if (isDefined(this._options.metadata) || isDefined(options.metadata)) {
700
+ result.metadata = { ...toJS(this._options.metadata), ...toJS(options.metadata) };
701
+ }
702
+ return result;
703
+ }
704
+ visibilityValue(expr) {
705
+ return expr.value ? "visible" : "none";
706
+ }
181
707
  formattedName(id) {
182
708
  return `MapLayer<${id || this._options.id}>`;
183
709
  }
@@ -219,5 +745,17 @@ __publicField(_MapLayer, "_defaultOptions", {
219
745
  maxZoom: 18
220
746
  });
221
747
  let MapLayer = _MapLayer;
748
+ function unreadable(path, e) {
749
+ return { path, message: `The style could not be rendered: ${e?.message ?? String(e)}`, severity: "error" };
750
+ }
751
+ function describeValue(value) {
752
+ return typeof value === "string" ? `'${value}'` : JSON.stringify(value) ?? String(value);
753
+ }
754
+ function maxDefined(a, b) {
755
+ return isDefined(a) && isDefined(b) ? Math.max(a, b) : a ?? b;
756
+ }
757
+ function minDefined(a, b) {
758
+ return isDefined(a) && isDefined(b) ? Math.min(a, b) : a ?? b;
759
+ }
222
760
 
223
761
  export { MapLayer };