1+ import Actions from '@geppettoengine/geppetto-client/js/components/interface/flexLayout2/src/model/Actions' ;
2+ import { WidgetStatus } from '../../constants' ;
3+
4+ const onAction = ( action , widgets , model , udpateWidget , activateWidget , destroyWidget , maximizeWidget , minimizeWidget ) => {
5+ switch ( action . type ) {
6+ case Actions . SET_ACTIVE_TABSET :
7+ break ;
8+ case Actions . SELECT_TAB :
9+ activateWidget ( action . data . tabNode ) ;
10+ break ;
11+ case Actions . DELETE_TAB :
12+ onActionDeleteWidget ( action , widgets , model , destroyWidget ) ;
13+ break ;
14+ case Actions . MAXIMIZE_TOGGLE :
15+ onActionMaximizeWidget ( action , widgets , model , maximizeWidget ) ;
16+ break ;
17+ case Actions . ADJUST_SPLIT :
18+ break ;
19+ case Actions . ADD_NODE :{
20+ action . data . index = findWidgetInsertionIndex ( action , widgets , model )
21+ break
22+ }
23+ case Actions . MOVE_NODE :{
24+ // If the new tabset already exists, set insertion position
25+ if ( action . data . location === 'center' ) {
26+ action . data . index = findWidgetInsertionIndex ( action , widgets , model )
27+ }
28+ // store the tabset id where the node will be moved from
29+ var fromTabsetId = getTabsetId ( action , model )
30+ break
31+ }
32+ }
33+
34+ model . doAction ( action ) ;
35+
36+ // Flexlayout needs to create a tabset before we can update the panelName for the moved widget
37+ if ( action . type === Actions . MOVE_NODE ) {
38+ moveNode ( action , widgets , model , fromTabsetId , udpateWidget , activateWidget )
39+ }
40+ window . dispatchEvent ( new Event ( 'resize' ) ) ;
41+ }
42+
43+ const getTabsetId = ( action , model ) => {
44+ const widgetId = action . data . fromNode
45+ return model . getNodeById ( widgetId ) . getParent ( ) . getId ( )
46+ }
47+
48+ const moveNode = ( action , widgets , model , fromTabsetId , udpateWidget , activateWidget , minimizeWidget ) => {
49+ // Update widget.panelName for new tabset location
50+ const widgetId = action . data . fromNode
51+ const widget = { ...widgets [ widgetId ] }
52+ const panelName = getTabsetId ( action , model )
53+ widget . status = WidgetStatus . ACTIVE
54+ widget . panelName = panelName
55+ widget . instancePath = widgetId
56+ udpateWidget ( widget )
57+
58+ // Elect a new active widget in old panel
59+ const fromTabset = model . getNodeById ( fromTabsetId )
60+
61+ if ( fromTabset ) {
62+ const fromTabsetChildren = fromTabset . getChildren ( ) . filter ( child => child . getId ( ) !== action . data . fromNode )
63+ if ( fromTabsetChildren . length > 0 ) {
64+ const newActiveWidget = fromTabsetChildren [ 0 ] . getConfig ( )
65+ newActiveWidget . status = WidgetStatus . ACTIVE
66+ newActiveWidget . instancePath = newActiveWidget . id
67+ activateWidget ( newActiveWidget . id )
68+ }
69+ }
70+
71+
72+ }
73+
74+ const findWidgetInsertionIndex = ( action , widgets , model ) => {
75+ var position = 0
76+ if ( action . type === Actions . ADD_NODE ) {
77+ position = action . data . json . config . pos
78+ } else {
79+ position = widgets [ action . data . fromNode ] . pos
80+ }
81+
82+ const tabset = model . getNodeById ( action . data . toNode )
83+ const positions = tabset . getChildren ( ) . map ( node => node . getConfig ( ) . pos )
84+ var index = - 1
85+ for ( let i = 0 ; i < positions . length ; i ++ ) {
86+ if ( position < positions [ i ] ) {
87+ index = i
88+ break
89+ }
90+ }
91+ return index
92+ }
93+
94+ const findMaximizedWidget = widgets => Object . values ( widgets ) . find ( widget => widget && widget . status == WidgetStatus . MAXIMIZED )
95+
96+ const onActionDeleteWidget = ( action , widgets , model , destroyWidget ) => {
97+ const maximizedWidget = findMaximizedWidget ( widgets ) ;
98+ // change widget status
99+ destroyWidget ( action . data . node ) ;
100+ // check if the current maximized widget is the same than in the action dispatched
101+ if ( maximizedWidget && maximizedWidget . id == action . data . node ) {
102+ // find if there exists another widget in the maximized panel that could take its place
103+ const panelChildren = model . getActiveTabset ( ) . getChildren ( ) ;
104+ const index = panelChildren . findIndex ( child => child . getId ( ) == action . data . node ) ;
105+ // Understand if the tab to the left or right of the destroyed tab will be the next one to be maximized
106+ if ( index != - 1 && panelChildren . length > 1 ) {
107+ if ( index == 0 ) {
108+ onActionMaximizeWidget ( panelChildren [ 1 ] . getId ( ) , widgets , model ) ;
109+ } else {
110+ onActionMaximizeWidget ( panelChildren [ index - 1 ] . getId ( ) , widgets , model ) ;
111+ }
112+ }
113+ }
114+ }
115+
116+ const onActionMaximizeWidget = ( action , widgets , model , activateWidget , maximizeWidget ) => {
117+ const panel2maximize = model . getNodeById ( action . data . node ) ;
118+
119+ if ( panel2maximize . getChildren ( ) . length > 0 ) {
120+ const widgetId2maximize = panel2maximize . getSelectedNode ( ) . getId ( ) ;
121+ const maximizedWidget = this . findMaximizedWidget ( widgets ) ;
122+ if ( maximizedWidget ) {
123+ if ( maximizedWidget . id !== widgetId2maximize ) {
124+ maximizeWidget ( widgetId2maximize , maximizeWidget ) ;
125+ }
126+ activateWidget ( maximizedWidget . id , activateWidget ) ;
127+
128+ } else {
129+ maximizeWidget ( widgetId2maximize ) ;
130+ }
131+ }
132+
133+ }
134+
135+ export default onAction
0 commit comments