@@ -10,17 +10,12 @@ export class AppreciableLayerBase extends Events {
1010 this . appendLayers = options . appendLayers || false ;
1111 this . unexpectedSourceNames = [ 'tdt-search-' , 'tdt-route-' , 'smmeasure' , 'mapbox-gl-draw' , 'maplibre-gl-draw' , / t r a c k l a y e r - \d + - l i n e / ] ;
1212 this . layersVisibleMap = new Map ( ) ;
13- this . layerCatalogs = [ ] ;
14- this . appreciableLayers = [ ] ;
1513 this . eventTypes = [ 'layerupdatechanged' ] ;
1614 this . _styleDataUpdatedHandler = this . _styleDataUpdatedHandler . bind ( this ) ;
1715 }
1816
1917 setSelfLayers ( layers ) {
2018 this . layers = layers ;
21- if ( this . appreciableLayers . length > 0 ) {
22- this . _styleDataUpdatedHandler ( ) ;
23- }
2419 }
2520
2621 createAppreciableLayers ( ) {
@@ -36,11 +31,13 @@ export class AppreciableLayerBase extends Events {
3631 }
3732
3833 getLayerCatalog ( ) {
39- return this . layerCatalogs ;
34+ const layerCatalog = this . createLayerCatalogs ( ) ;
35+ this . _updateLayerCatalogsVisible ( layerCatalog ) ;
36+ return layerCatalog ;
4037 }
4138
4239 getLayers ( ) {
43- return this . appreciableLayers ;
40+ return this . createAppreciableLayers ( ) ;
4441 }
4542
4643 getSelfLayers ( appreciableLayers = this . getLayers ( ) ) {
@@ -50,19 +47,15 @@ export class AppreciableLayerBase extends Events {
5047 ) ;
5148 }
5249
53- toggleLayerVisible ( layerId , visible ) {
54- const item = this . _findLayerCatalog ( this . layerCatalogs , layerId ) ;
55- if ( ! item ) {
56- return ;
57- }
50+ toggleLayerVisible ( layer , visible ) {
5851 const visibility = visible ? 'visible' : 'none' ;
59- if ( item . type === 'group' ) {
60- const visbleId = this . _getLayerVisibleId ( item ) ;
52+ if ( layer . type === 'group' ) {
53+ const visbleId = this . _getLayerVisibleId ( layer ) ;
6154 this . layersVisibleMap . set ( visbleId , visible ) ;
62- const targetLayers = getLayerInfosFromCatalogs ( item . children ) ;
55+ const targetLayers = getLayerInfosFromCatalogs ( layer . children ) ;
6356 this . setLayersVisible ( targetLayers , visibility ) ;
6457 } else {
65- this . setLayersVisible ( [ item ] , visibility ) ;
58+ this . setLayersVisible ( [ layer ] , visibility ) ;
6659 }
6760 }
6861
@@ -74,7 +67,7 @@ export class AppreciableLayerBase extends Events {
7467 ( layer . CLASS_INSTANCE && layer . CLASS_INSTANCE . show && layer . CLASS_INSTANCE . hide )
7568 ) {
7669 visibility === 'visible' ? layer . CLASS_INSTANCE . show ( ) : layer . CLASS_INSTANCE . hide ( ) ;
77- this . _styleDataUpdatedHandler ( ) ;
70+ this . map . style . fire ( 'data' , { dataType : 'style' } ) ;
7871 return ;
7972 }
8073 layer . renderLayers . forEach ( ( layerId ) => {
@@ -98,7 +91,7 @@ export class AppreciableLayerBase extends Events {
9891 if ( ! this . map ) {
9992 return ;
10093 }
101- this . _initializeData ( ) ;
94+ this . _styleDataUpdatedHandler ( ) ;
10295 this . _registerMapEvent ( ) ;
10396 }
10497
@@ -122,20 +115,19 @@ export class AppreciableLayerBase extends Events {
122115 }
123116
124117 _initSourceList ( detailLayers ) {
125- const datas = detailLayers . reduce ( ( sourceList , layer ) => {
118+ const datas = detailLayers . slice ( ) . reverse ( ) . reduce ( ( sourceList , layer ) => {
119+ const id = layer . renderSource . sourceLayer ? `${ layer . renderSource . id } -${ + new Date ( ) } ` : layer . id ;
126120 let matchItem = sourceList . find ( ( item ) => {
127- const sourceId = layer . renderSource . id || layer . id ;
128- return item . id === sourceId ;
121+ return item . id === id ;
129122 } ) ;
130123 if ( ! matchItem ) {
131- const sourceListItem = new SourceModel ( layer ) ;
124+ const sourceListItem = new SourceModel ( layer , id ) ;
132125 sourceList . push ( sourceListItem ) ;
133126 matchItem = sourceListItem ;
134127 }
135128 matchItem . addLayer ( layer ) ;
136129 return sourceList ;
137130 } , [ ] ) ;
138- datas . reverse ( ) ;
139131 return datas ;
140132 }
141133
@@ -278,23 +270,14 @@ export class AppreciableLayerBase extends Events {
278270 }
279271 }
280272
281- _initializeData ( ) {
282- this . appreciableLayers = this . createAppreciableLayers ( ) ;
283- this . layerCatalogs = this . createLayerCatalogs ( ) ;
284- this . _updateLayerCatalogsVisible ( this . layerCatalogs ) ;
285- }
286-
287273 _registerMapEvent ( ) {
288274 this . map . on ( 'styledata' , this . _styleDataUpdatedHandler ) ;
289275 }
290276
291277 _styleDataUpdatedHandler ( ) {
292- this . _initializeData ( ) ;
293- if ( ! this . _appendLayers ) {
294- this . triggerEvent ( 'layerupdatechanged' , {
295- layers : this . appreciableLayers ,
296- layerCatalog : this . layerCatalogs
297- } ) ;
278+ this . triggerEvent ( 'layerupdatechanged' , {
279+ layers : this . getLayers ( ) ,
280+ layerCatalog : this . getLayerCatalog ( )
281+ } ) ;
298282 }
299283 }
300- }
0 commit comments