@defra/interactive-map 0.0.62-alpha → 0.0.63-alpha
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 +1 @@
|
|
|
1
|
-
import t from"@arcgis/core/layers/VectorTileLayer.js";import e from"@arcgis/core/layers/FeatureLayer.js";import i from"@arcgis/core/layers/GroupLayer.js";import{g as
|
|
1
|
+
import t from"@arcgis/core/layers/VectorTileLayer.js";import e from"@arcgis/core/layers/FeatureLayer.js";import i from"@arcgis/core/layers/GroupLayer.js";import{g as a,L as s}from"./layerAdapter.js";import{D as r,l as y}from"./im-datasets-plugin.js";import"preact/compat";import"preact/jsx-runtime";class o extends r{applyLayerPaintProperties(t){const{mapStyle:e}=this._datasetRegistry;return this.hasStroke&&(t["line-color"]=a(this.style.stroke,e.id)),this.hasFill&&(t["fill-color"]=a(this.style.fill,e.id)),t}get esriGroupId(){return void 0===this._datasetDefinition.esriGroupId?this.parent?.esriGroupId:this._datasetDefinition.esriGroupId}get useServerStyle(){return Boolean(this._datasetDefinition.esriUseServerStyle)}get renderer(){if("FeatureService"!==this.type)return;const t=this._datasetDefinition.style?.renderer||this.parent?.renderer;if(!t)return;const{mapStyle:e}=this._datasetRegistry,i=structuredClone(t);return i.symbol?.color&&(i.symbol.color=a(i.symbol.color,e.id)),i.symbol?.outline?.color&&(i.symbol.outline.color=a(i.symbol.outline.color,e.id)),i}}const l=(t,e)=>{if("baseLayer"===t.id)return-1/0;if(t.id.includes("ketchLayer"))return 1/0;const i="group"===t.type?t.layers?.items?.[0]?.id:t.id;return e.indexOf(i)};class p extends s{constructor(t,e,i,a){super(),this._datasetRegistry=a,this.ready=new Promise(t=>{this.resolveReady=t}),this._mapProvider=t,this._map=t.map,this._mapVisibilityLayers={},this._mapOpacityLayers={},this._groupLayers={}}createDataset(t,e){return new o(t,e)}async init(){try{await this._init()}finally{this.resolveReady()}}async _init(){const t=this._datasetRegistry.topLevelDatasets(),e=t.filter(t=>"visible"===t.visibility),i=t.filter(t=>"visible"!==t.visibility),a=async(t,e=!1)=>this._addLayers(t).then(()=>{if(!e)return;const i=this._mapVisibilityLayers[t.id];t.sublayers?.forEach(t=>{"visible"===t.visibility&&this._applyStyleLayerPaintProperties(t,i)}),this.applyDatasetVisibility(t.id)}).catch(async e=>{y.error(`Error adding layer for dataset ${t.id}:`,e),await this.removeDataset(t.id)});for(const t of e)await a(t,!0);this._reorderLayers();for(const t of i)await a(t);await this.onMapStyleChange(),await this.applyGlobalOpacity(),await Promise.all(t.map(t=>this.applyDatasetVisibility(t.id))),this._reorderLayers()}_reorderLayers(){const t=this._map?.allLayers?.items;if(!t?.length)return;const e=this._datasetRegistry._orderedDatasets;[...t.filter(t=>"group"!==t.parent?.type)].sort((t,i)=>l(t,e)-l(i,e)).forEach((t,e)=>this._map.reorder(t,e)),this._reorderGroupLayers()}_reorderGroupLayers(){const t=this._datasetRegistry._orderedDatasets;Object.values(this._groupLayers).forEach(e=>{[...e?.allLayers?.items||[]].filter(Boolean).sort((e,i)=>l(e,t)-l(i,t)).forEach((t,i)=>e?.reorder?.(t,i))})}_addGroupLayer(t){if(!this._groupLayers[t]){const e=new i({id:t,opacity:1,visible:!0});this._groupLayers[t]=e,this._map.add(e)}return this._groupLayers[t]}async _addFeatureLayers(t){const i=new e({id:t.id,url:t.tiles,renderer:t.renderer,opacity:1,visible:!1});this._mapVisibilityLayers[t.id]=i,this._mapOpacityLayers[t.id]=i;try{return this._map.add(i),i.when()}catch(e){y.error(`Error adding FeatureLayer for dataset ${t.id}:`,e)}return null}async _addLayers(e){const{type:i,esriGroupId:a,tiles:s}=e;if(!s)return null;if("FeatureService"===i)return this._addFeatureLayers(e);const r=a?this._addGroupLayer(a):this._map,y=new t({id:e.id,url:e.tiles,opacity:1,visible:!1});return this._mapVisibilityLayers[e.id]=y,this._mapOpacityLayers[e.id]=a?r:y,r.add(y),y.when()}async addDataset(t){const e=this._datasetRegistry.getDataset(t);if(!e)return void y.warn(`addDataset called, but Dataset with id ${t} not found in registry`);await this._addLayers(e);const{parentId:i}=e,a=this._mapVisibilityLayers[i||t];this._reorderLayers(),this.applyDatasetOpacity(t),this._applyStyleLayerPaintProperties(e,a),this.applyDatasetVisibility(t)}async removeDataset(t){const e=this._datasetRegistry.getDataset(t);if(!e)return;const{esriGroupId:i}=e,a=this._mapVisibilityLayers[t],s=i?this._groupLayers[i]:null,r=s||this._map;a&&(r.remove(a),delete this._mapVisibilityLayers[t],delete this._mapOpacityLayers[t]),0===s?.layers.length&&(this._map.remove(s),delete this._groupLayers[i])}_applyRegistryDatasetVisibility(t){const{id:e,isSublayer:i,parentId:a}=t,s="visible"===t.visibility,r=this._mapVisibilityLayers[i?a:e];r&&(i?this._applyStyleLayerVisibility(t,r):(s&&t.sublayers.forEach(t=>this._applyStyleLayerVisibility(t,r)),r.visible=s))}async applyDatasetVisibility(t){const e=this._datasetRegistry.getDataset(t);e&&this._applyRegistryDatasetVisibility(e)}async applyGlobalVisibility(){await this.ready,this._datasetRegistry.forEachDataset(t=>this._applyRegistryDatasetVisibility(t))}async applyDatasetOpacity(t){const e=this._mapOpacityLayers[t],i=this._datasetRegistry.getDataset(t);e&&i&&(e.opacity=i.opacity)}async applyGlobalOpacity(){Object.entries(this._mapOpacityLayers).forEach(([t,e])=>{const i=this._datasetRegistry.getDataset(t);i&&(e.opacity=i.opacity)})}_applyStyleLayerVisibility(t,e){const{esriStyleLayerId:i}=t;i&&e&&this.ready.then(()=>{e.setStyleLayerVisibility(i,t.visibility)})}_applyStyleLayerPaintProperties(t,e){const{esriStyleLayerId:i,useServerStyle:a}=t;if(a||!i||!e)return;const s=e.getPaintProperties(i);s&&e.setPaintProperties(i,t.applyLayerPaintProperties(s))}async onMapStyleChange(){this._datasetRegistry.forEach(t=>{const{id:e,isSublayer:i,parent:a}=t,s=this._mapVisibilityLayers[i?a.id:e];s&&("FeatureService"===t.type?s.renderer=t.renderer:(this._applyStyleLayerVisibility(t,s),this._applyStyleLayerPaintProperties(t,s)))})}async onMapSizeChange(){}async applyFeatureFilter(...t){console.log("TODO: applyFeatureFilter",t)}async setData(...t){console.log("TODO: setData",t)}async applyStyle(...t){console.log("TODO: applyStyle",t)}}export{p as default};
|
|
@@ -45,10 +45,25 @@ export default class EsriLayerAdapter extends LayerAdapter {
|
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
async init () {
|
|
48
|
+
try {
|
|
49
|
+
await this._init()
|
|
50
|
+
} finally {
|
|
51
|
+
// Always settle `ready` so a failed layer doesn't block later toggles
|
|
52
|
+
this.resolveReady()
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async _init () {
|
|
48
57
|
const topLevelDatasets = this._datasetRegistry.topLevelDatasets()
|
|
58
|
+
const visibleDatasets = topLevelDatasets.filter(dataset => dataset.visibility === 'visible')
|
|
59
|
+
const hiddenDatasets = topLevelDatasets.filter(dataset => dataset.visibility !== 'visible')
|
|
60
|
+
|
|
49
61
|
// ensure the datasets are added in order
|
|
50
|
-
const _add = async (registryDataset) => {
|
|
62
|
+
const _add = async (registryDataset, setVisibility = false) => {
|
|
51
63
|
return this._addLayers(registryDataset).then(() => {
|
|
64
|
+
if (!setVisibility) {
|
|
65
|
+
return
|
|
66
|
+
}
|
|
52
67
|
const mapLayer = this._mapVisibilityLayers[registryDataset.id]
|
|
53
68
|
registryDataset.sublayers?.forEach(sublayer => {
|
|
54
69
|
if (sublayer.visibility === 'visible') {
|
|
@@ -56,22 +71,24 @@ export default class EsriLayerAdapter extends LayerAdapter {
|
|
|
56
71
|
}
|
|
57
72
|
})
|
|
58
73
|
this.applyDatasetVisibility(registryDataset.id) // NOSONAR: Not changing for now as used in production
|
|
74
|
+
}).catch(async (error) => {
|
|
75
|
+
// A failed layer must not stop the remaining datasets loading; drop it so later steps skip it
|
|
76
|
+
logger.error(`Error adding layer for dataset ${registryDataset.id}:`, error)
|
|
77
|
+
await this.removeDataset(registryDataset.id)
|
|
59
78
|
})
|
|
60
79
|
}
|
|
61
80
|
|
|
62
81
|
// Add the visible datasets first - to speed up rendering
|
|
63
|
-
for (const registryDataset of
|
|
64
|
-
|
|
65
|
-
await _add(registryDataset) // NOSONAR: Await inside a for loop is fine, parent function is async
|
|
66
|
-
}
|
|
82
|
+
for (const registryDataset of visibleDatasets) {
|
|
83
|
+
await _add(registryDataset, true) // NOSONAR: Await inside a for loop is fine, parent function is async
|
|
67
84
|
}
|
|
85
|
+
|
|
68
86
|
// Reorder layers after adding the initially visible datasets
|
|
69
87
|
this._reorderLayers()
|
|
70
88
|
// Add the non-visible datasets next
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
}
|
|
89
|
+
|
|
90
|
+
for (const registryDataset of hiddenDatasets) {
|
|
91
|
+
await _add(registryDataset) // NOSONAR: Await inside a for loop is fine, parent function is async
|
|
75
92
|
}
|
|
76
93
|
|
|
77
94
|
// onMapStyleChange: handles showing and hiding sublayers based on the current mapStyle
|
|
@@ -84,7 +101,6 @@ export default class EsriLayerAdapter extends LayerAdapter {
|
|
|
84
101
|
// Finally show all layers that are visible based on the dataset/mapStyle visibility
|
|
85
102
|
await Promise.all(topLevelDatasets.map(registryDataset => this.applyDatasetVisibility(registryDataset.id)))
|
|
86
103
|
this._reorderLayers()
|
|
87
|
-
this.resolveReady()
|
|
88
104
|
}
|
|
89
105
|
|
|
90
106
|
_reorderLayers () {
|
|
@@ -242,6 +258,7 @@ export default class EsriLayerAdapter extends LayerAdapter {
|
|
|
242
258
|
}
|
|
243
259
|
|
|
244
260
|
async applyGlobalVisibility () {
|
|
261
|
+
await this.ready
|
|
245
262
|
this._datasetRegistry.forEachDataset(registryDataset => this._applyRegistryDatasetVisibility(registryDataset))
|
|
246
263
|
}
|
|
247
264
|
|
|
@@ -285,6 +285,44 @@ describe('esriLayerAdapter', () => {
|
|
|
285
285
|
})
|
|
286
286
|
})
|
|
287
287
|
|
|
288
|
+
describe('ready', () => {
|
|
289
|
+
const isSettled = async (promise) => {
|
|
290
|
+
const pending = Symbol('pending')
|
|
291
|
+
return (await Promise.race([promise, Promise.resolve(pending)])) !== pending
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
it('is pending until init completes, then resolves', async () => {
|
|
295
|
+
const freshAdapter = new EsriLayerAdapter(mapProvider, null, null, datasetRegistry)
|
|
296
|
+
expect(await isSettled(freshAdapter.ready)).toBe(false)
|
|
297
|
+
await freshAdapter.init()
|
|
298
|
+
expect(await isSettled(freshAdapter.ready)).toBe(true)
|
|
299
|
+
})
|
|
300
|
+
|
|
301
|
+
it('resolves even when init throws unexpectedly', async () => {
|
|
302
|
+
const freshAdapter = new EsriLayerAdapter(mapProvider, null, null, datasetRegistry)
|
|
303
|
+
jest.spyOn(freshAdapter, 'onMapStyleChange').mockRejectedValue(new Error('boom'))
|
|
304
|
+
await expect(freshAdapter.init()).rejects.toThrow('boom')
|
|
305
|
+
expect(await isSettled(freshAdapter.ready)).toBe(true)
|
|
306
|
+
})
|
|
307
|
+
})
|
|
308
|
+
|
|
309
|
+
describe('init failure', () => {
|
|
310
|
+
it('skips a layer that fails to load and still loads the rest', async () => {
|
|
311
|
+
const failingAdapter = new EsriLayerAdapter(mapProvider, null, null, datasetRegistry)
|
|
312
|
+
const addLayers = failingAdapter._addLayers.bind(failingAdapter)
|
|
313
|
+
jest.spyOn(failingAdapter, '_addLayers').mockImplementation(async (dataset) => {
|
|
314
|
+
await addLayers(dataset)
|
|
315
|
+
if (dataset.id === 'flood-zones') {
|
|
316
|
+
throw new Error('404')
|
|
317
|
+
}
|
|
318
|
+
})
|
|
319
|
+
await expect(failingAdapter.init()).resolves.toBeUndefined()
|
|
320
|
+
await expect(failingAdapter.ready).resolves.toBeUndefined()
|
|
321
|
+
expect(failingAdapter._mapVisibilityLayers['flood-zones']).toBeUndefined()
|
|
322
|
+
expect(failingAdapter._mapVisibilityLayers['flood-zones-cc']).toBeDefined()
|
|
323
|
+
})
|
|
324
|
+
})
|
|
325
|
+
|
|
288
326
|
// ─── onMapSizeChange ─────────────────────────────────────────────────────────
|
|
289
327
|
|
|
290
328
|
describe('onMapSizeChange', () => {
|