@@ -21,9 +21,11 @@ import {
2121 DESTROY_WIDGET ,
2222 ACTIVATE_WIDGET ,
2323 SET_LAYOUT ,
24+ SET_WIDGETS ,
2425 setLayout
2526} from "./actions" ;
2627
28+ import { MINIMIZED_PANEL } from '.' ;
2729
2830const styles = ( theme ) => createStyles ( {
2931 container : {
@@ -66,7 +68,10 @@ class LayoutManager {
6668 this . enableMinimize = enableMinimize ;
6769 }
6870
69- addWidget ( widgetConfiguration ) {
71+ addWidget ( widgetConfiguration : Widget ) {
72+ if ( this . getWidget ( widgetConfiguration . id ) ) {
73+ return this . updateWidget ( widgetConfiguration ) ;
74+ }
7075 const { model } = this ;
7176 let tabset = model . getNodeById ( widgetConfiguration . panelName ) ;
7277 if ( tabset === undefined ) {
@@ -77,7 +82,7 @@ class LayoutManager {
7782 widget2Node ( widgetConfiguration ) ,
7883 widgetConfiguration . panelName ,
7984 DockLocation . CENTER ,
80- widgetConfiguration . index ? widgetConfiguration . index : - 1
85+ widgetConfiguration . pos ? widgetConfiguration . pos : - 1
8186 )
8287 ) ;
8388 }
@@ -141,16 +146,16 @@ class LayoutManager {
141146
142147 middleware = ( store ) => ( next ) => ( action ) => {
143148 const model = this . model ;
144-
149+ console . debug ( action ) ;
145150 switch ( action . type ) {
146151 case ADD_WIDGET : {
152+
147153 this . addWidget ( action . data ) ;
154+
148155 break ;
149156 }
150157 case ADD_WIDGETS : {
151- for ( let widget of action . data ) {
152- this . addWidget ( widget ) ;
153- }
158+ this . addWidgets ( action . data ) ;
154159 break ;
155160 }
156161 case UPDATE_WIDGET : {
@@ -160,14 +165,25 @@ class LayoutManager {
160165
161166 case DESTROY_WIDGET : {
162167 const widget = action . data ;
163- model . doAction ( Actions . deleteTab ( widget . id ) ) ;
168+ this . deleteWidget ( widget ) ;
164169 break ;
165170 }
166171
167172 case ACTIVATE_WIDGET : {
168- model . doAction ( Actions . updateNodeAttributes ( action . data . id , { status : WidgetStatus . ACTIVE } ) ) ;
169173 action . data . status = WidgetStatus . ACTIVE ;
174+ this . updateWidget ( action . data ) ;
175+ break ;
176+ }
177+ case SET_WIDGETS : {
178+ const newWidgets : Map < string , Widget > = action . data ;
179+ for ( let widget of this . getWidgets ( ) ) {
180+ if ( ! newWidgets [ widget . id ] ) {
181+ this . deleteWidget ( widget ) ;
182+ }
183+ }
184+ this . addWidgets ( Object . values ( newWidgets ) ) ;
170185 break ;
186+
171187 }
172188
173189 case SET_LAYOUT : {
@@ -184,6 +200,26 @@ class LayoutManager {
184200
185201 next ( setLayout ( model . toJson ( ) ) ) ;
186202 } ;
203+
204+ private addWidgets ( newWidgets : Array < Widget > ) {
205+ let active = null ;
206+ for ( let widget of newWidgets ) {
207+ if ( widget . status == WidgetStatus . ACTIVE ) {
208+ active = widget . id ;
209+ }
210+
211+ this . addWidget ( widget ) ;
212+
213+ }
214+ if ( active ) {
215+ this . model . doAction ( FlexLayout . Actions . selectTab ( active ) ) ;
216+ }
217+ }
218+
219+ private deleteWidget ( widget : any ) {
220+ this . model . doAction ( Actions . deleteTab ( widget . id ) ) ;
221+ }
222+
187223 getWidgets ( ) {
188224
189225 let nodes = [ ] ;
@@ -261,7 +297,8 @@ class LayoutManager {
261297
262298
263299 getWidget ( id ) : Widget {
264- return ( this . model . getNodeById ( id ) [ '_attributes' ] as ExtendedNode ) . config ;
300+ const node = this . model . getNodeById ( id ) ;
301+ return node && node [ '_attributes' ] ? ( node [ '_attributes' ] as ExtendedNode ) . config : null ;
265302 }
266303
267304
@@ -291,25 +328,38 @@ class LayoutManager {
291328
292329 minimizeWidget ( widgetId ) {
293330
294- var updatedWidget = this . getWidget ( widgetId ) ;
331+ var updatedWidget = { ... this . getWidget ( widgetId ) } ;
295332 if ( updatedWidget === undefined ) {
296333 return ;
297334 }
298335 updatedWidget . status = WidgetStatus . MINIMIZED ;
299- updatedWidget . panelName = "border_bottom" ;
336+ updatedWidget . defaultPanel = updatedWidget . panelName ;
337+ updatedWidget . panelName = MINIMIZED_PANEL ;
300338 this . updateWidget ( updatedWidget ) ;
301339 // this.model.doAction(FlexLayout.Actions.moveNode(widgetId, "border_bottom", FlexLayout.DockLocation.CENTER, 0));
302340 }
303341
304342 updateWidget ( widget : Widget ) {
305343 const { model } = this ;
306- if ( widget ) {
344+ if ( ! widget ) {
345+ debugger ;
346+ }
347+ const previousWidget = this . getWidget ( widget . id ) ;
348+ if ( previousWidget . status != widget . status ) {
349+ if ( previousWidget . status == WidgetStatus . MINIMIZED ) {
350+ this . restoreWidget ( widget ) ;
351+ }
352+ else {
353+ this . moveWidget ( widget ) ;
354+ }
355+ }
307356 this . widgetFactory . updateWidget ( widget ) ;
308357 model . doAction ( Actions . updateNodeAttributes ( widget . id , widget2Node ( widget ) ) ) ;
309- }
310- this . model . doAction ( FlexLayout . Actions . moveNode ( widget . id , widget . panelName , FlexLayout . DockLocation . CENTER , widget . pos ) ) ;
311-
358+ if ( widget . status == WidgetStatus . ACTIVE ) {
359+ model . doAction ( FlexLayout . Actions . selectTab ( widget . id ) ) ;
360+ }
312361 }
362+
313363 onActionMaximizeWidget ( action ) {
314364 // const { model } = this;
315365 // const panel2maximize = <Panel>model.getNodeById(action.data.node);
@@ -337,9 +387,10 @@ class LayoutManager {
337387 return this . tabsetIconFactory . factory ( node . getConfig ( ) ) ;
338388 }
339389
340- restoreWidget ( widget ) {
390+ restoreWidget ( widget : Widget ) {
341391 const { model } = this ;
342- const panelName = widget . panelName ;
392+ widget . panelName = widget . defaultPanel ;
393+ const panelName = widget . panelName ;
343394 let tabset = model . getNodeById ( panelName ) ;
344395 if ( tabset === undefined ) {
345396 this . createTabSet ( panelName ) ;
@@ -354,7 +405,7 @@ class LayoutManager {
354405 widget . id ,
355406 widget . panelName ,
356407 FlexLayout . DockLocation . CENTER ,
357- 0
408+ widget . pos
358409 )
359410 ) ;
360411 // Resize of canvas and SVG images
0 commit comments