5
5
IJupyterGISModel
6
6
} from '@jupytergis/schema' ;
7
7
import { DOMUtils } from '@jupyterlab/apputils' ;
8
+ import { IStateDB } from '@jupyterlab/statedb' ;
8
9
import {
9
10
Button ,
10
11
LabIcon ,
@@ -20,7 +21,7 @@ import React, {
20
21
import { icons } from '../../constants' ;
21
22
import { nonVisibilityIcon , visibilityIcon } from '../../icons' ;
22
23
import { IControlPanelModel } from '../../types' ;
23
- import { ILeftPanelClickHandlerParams , ILeftPanelOptions } from '../leftpanel' ;
24
+ import { ILayerPanelOptions , ILeftPanelClickHandlerParams } from '../leftpanel' ;
24
25
25
26
const LAYERS_PANEL_CLASS = 'jp-gis-layerPanel' ;
26
27
const LAYER_GROUP_CLASS = 'jp-gis-layerGroup' ;
@@ -36,10 +37,11 @@ const LAYER_TEXT_CLASS = 'jp-gis-layerText';
36
37
* The layers panel widget.
37
38
*/
38
39
export class LayersPanel extends Panel {
39
- constructor ( options : ILeftPanelOptions ) {
40
+ constructor ( options : ILayerPanelOptions ) {
40
41
super ( ) ;
41
42
this . _model = options . model ;
42
43
this . _onSelect = options . onSelect ;
44
+ this . _state = options . state ;
43
45
44
46
this . id = 'jupytergis::layerTree' ;
45
47
this . addClass ( LAYERS_PANEL_CLASS ) ;
@@ -49,6 +51,7 @@ export class LayersPanel extends Panel {
49
51
< LayersBodyComponent
50
52
model = { this . _model }
51
53
onSelect = { this . _onSelect }
54
+ state = { this . _state }
52
55
> </ LayersBodyComponent >
53
56
)
54
57
) ;
@@ -117,6 +120,7 @@ export class LayersPanel extends Panel {
117
120
} ;
118
121
119
122
private _model : IControlPanelModel | undefined ;
123
+ private _state : IStateDB ;
120
124
private _onSelect : ( {
121
125
type,
122
126
item,
@@ -129,6 +133,7 @@ export class LayersPanel extends Panel {
129
133
*/
130
134
interface IBodyProps {
131
135
model : IControlPanelModel ;
136
+ state : IStateDB ;
132
137
onSelect : ( { type, item, nodeId } : ILeftPanelClickHandlerParams ) => void ;
133
138
}
134
139
@@ -196,6 +201,7 @@ function LayersBodyComponent(props: IBodyProps): JSX.Element {
196
201
gisModel = { model }
197
202
group = { layer }
198
203
onClick = { onItemClick }
204
+ state = { props . state }
199
205
/>
200
206
)
201
207
) }
@@ -209,14 +215,15 @@ function LayersBodyComponent(props: IBodyProps): JSX.Element {
209
215
interface ILayerGroupProps {
210
216
gisModel : IJupyterGISModel | undefined ;
211
217
group : IJGISLayerGroup | undefined ;
218
+ state : IStateDB ;
212
219
onClick : ( { type, item, nodeId } : ILeftPanelClickHandlerParams ) => void ;
213
220
}
214
221
215
222
/**
216
223
* The component to handle group of layers.
217
224
*/
218
225
function LayerGroupComponent ( props : ILayerGroupProps ) : JSX . Element {
219
- const { group, gisModel, onClick } = props ;
226
+ const { group, gisModel, onClick, state } = props ;
220
227
221
228
if ( group === undefined ) {
222
229
return < > </ > ;
@@ -233,6 +240,13 @@ function LayerGroupComponent(props: ILayerGroupProps): JSX.Element {
233
240
234
241
useEffect ( ( ) => {
235
242
setId ( DOMUtils . createDomID ( ) ) ;
243
+
244
+ const getExpandedState = async ( ) => {
245
+ const groupState = await state . fetch ( group . name ) ;
246
+ setOpen ( groupState ? groupState [ 'expanded' ] : false ) ;
247
+ } ;
248
+
249
+ getExpandedState ( ) ;
236
250
} , [ ] ) ;
237
251
238
252
/**
@@ -255,6 +269,11 @@ function LayerGroupComponent(props: ILayerGroupProps): JSX.Element {
255
269
onClick ( { type : 'group' , item : name , nodeId : childId , event } ) ;
256
270
} ;
257
271
272
+ const handleExpand = async ( ) => {
273
+ state . save ( group . name , { expanded : ! open } ) ;
274
+ setOpen ( ! open ) ;
275
+ } ;
276
+
258
277
return (
259
278
< div
260
279
className = { `${ LAYER_ITEM_CLASS } ${ LAYER_GROUP_CLASS } ` }
@@ -264,7 +283,7 @@ function LayerGroupComponent(props: ILayerGroupProps): JSX.Element {
264
283
data-id = { name }
265
284
>
266
285
< div
267
- onClick = { ( ) => setOpen ( ! open ) }
286
+ onClick = { handleExpand }
268
287
onContextMenu = { handleRightClick }
269
288
className = { `${ LAYER_GROUP_HEADER_CLASS } ${ selected ? ' jp-mod-selected' : '' } ` }
270
289
onDragOver = { Private . onDragOver }
@@ -298,6 +317,7 @@ function LayerGroupComponent(props: ILayerGroupProps): JSX.Element {
298
317
gisModel = { gisModel }
299
318
group = { layer }
300
319
onClick = { onClick }
320
+ state = { props . state }
301
321
/>
302
322
)
303
323
) }
0 commit comments