Skip to content

Commit a885baf

Browse files
committed
#142 Layout refactoring ok
1 parent a11180c commit a885baf

15 files changed

Lines changed: 331 additions & 214 deletions

File tree

webapp/components/NetPyNE.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,14 @@ const styles = ({ zIndex, palette, spacing }) => ({
2020
content: { flexGrow:1, display: 'flex', flexDirection: 'row', position: 'relative' }
2121
});
2222

23-
import {
24-
DEFAULT_HLS_WIDGETS, getPythonDefaultConsoleWidget,
25-
DEFAULT_MORPHOLOGY_WIDGET,DEFAULT_PLOTS_WIDGETS
26-
} from '../constants'
23+
import { EDIT_WIDGETS, PYTHON_CONSOLE_WIDGET, WidgetStatus } from '../constants'
2724

2825

2926
class NetPyNE extends React.Component {
3027

3128
openPythonCallDialog (event) {
3229
const payload = { errorMessage: event['evalue'], errorDetails: event['traceback'].join('\n') }
33-
this.props.pythonCallErrorDialogBox(payload)
30+
this.props.pythonCallErrorDialogBox(payload);
3431
}
3532

3633
addMetadataToWindow (data) {
@@ -43,7 +40,7 @@ class NetPyNE extends React.Component {
4340

4441
componentDidMount () {
4542
GEPPETTO.on(GEPPETTO.Events.Error_while_exec_python_command, this.openPythonCallDialog, this);
46-
this.props.addWidgets([getPythonDefaultConsoleWidget(true) ]);
43+
this.props.setWidgets({ [PYTHON_CONSOLE_WIDGET.id]: { ...PYTHON_CONSOLE_WIDGET, panelName: PYTHON_CONSOLE_WIDGET.defaultPanel, status: WidgetStatus.ACTIVE } });
4744
GEPPETTO.on('jupyter_geppetto_extension_ready', data => {
4845
let project = { id: 1, name: 'Project', experiments: [{ "id": 1, "name": 'Experiment', "status": 'DESIGN' }] }
4946
GEPPETTO.Manager.loadProject(project, false);
@@ -53,7 +50,7 @@ class NetPyNE extends React.Component {
5350
const data = Utils.convertToJSON(response);
5451
this.addMetadataToWindow(data);
5552
this.props.modelLoaded();
56-
this.props.addWidgets(Object.values(DEFAULT_HLS_WIDGETS));
53+
this.props.setWidgets(EDIT_WIDGETS);
5754
GEPPETTO.trigger("spinner:hide");
5855
})
5956

webapp/components/general/NetPyNEIcons.js

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

webapp/components/general/NetPyNEPythonConsole.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ import React, { Component } from 'react'
22

33
import PythonConsole from '@geppettoengine/geppetto-client/js/components/interface/pythonConsole/PythonConsole';
44

5-
export default class NetPyNEPythonConsole extends Component {
5+
export class NetPyNEPythonConsole extends Component {
66

7+
shouldComponentUpdate () {
8+
return false;
9+
}
710

811
componentWillUnmount () {
9-
console.log("unmounting python console")
12+
console.info("unmounting python console");
1013
}
1114

1215
componentDidMount () {
@@ -17,3 +20,6 @@ export default class NetPyNEPythonConsole extends Component {
1720
return <PythonConsole pythonNotebookPath={"notebooks/notebook.ipynb"} />
1821
}
1922
}
23+
24+
25+
export default NetPyNEPythonConsole;

webapp/components/index.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import PythonControlledCapability from "geppetto-client/js/communication/geppett
44

55
import {
66
activateWidget,
7-
addWidgets,
8-
minimizeWidget,
9-
maximizeWidget,
7+
setWidgets,
108
updateWidget,
119
newWidget
1210
} from "../redux/actions/layout";
@@ -79,10 +77,10 @@ export const Dimensions = connect(
7977

8078
import _NetPyNE from "./NetPyNE";
8179
export const NetPyNE = connect(
82-
state => ({ editMode: state.general.editMode }),
80+
null,
8381
dispatch => ({
8482
pythonCallErrorDialogBox: payload => dispatch(openBackendErrorDialog(payload)),
85-
addWidgets: payload => dispatch(addWidgets(payload)),
83+
setWidgets: payload => dispatch(setWidgets(payload)),
8684
modelLoaded: () => dispatch(modelLoaded),
8785
newWidget: widget => dispatch(newWidget(widget)),
8886
activateWidget: widgetId => dispatch(activateWidget(widgetId))
@@ -193,12 +191,7 @@ export const ActionDialog = connect(
193191
})
194192
)(_ActionDialog)
195193

196-
import _NetPyNEPythonConsole from './general/NetPyNEPythonConsole';
197-
export const NetPyNEPythonConsole = connect(
198-
null,
199-
null
200-
)(_NetPyNEPythonConsole)
201-
194+
export { NetPyNEPythonConsole } from './general/NetPyNEPythonConsole';
202195

203196
import _Drawer from './settings/Drawer'
204197
export const Drawer = connect(

webapp/components/layout/LayoutManager.tsx

Lines changed: 69 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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

2830
const 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

webapp/components/layout/actions.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { WidgetStatus } from './model';
22

33
export const SET_LAYOUT = 'SET_LAYOUT';
4+
export const SET_WIDGETS = 'SET_WIDGETS';
45
export const UPDATE_WIDGET = 'UPDATE_WIDGET';
56
export const ACTIVATE_WIDGET = 'ACTIVATE_WIDGET';
67
export const ADD_WIDGET = 'ADD_WIDGET';
@@ -23,16 +24,28 @@ export const newWidget = ({ path, component, panelName, ...others }) => ({
2324
}
2425
});
2526

27+
export const addWidget = (widget) => ({
28+
type: ADD_WIDGET,
29+
data: widget
30+
});
31+
2632
export const addWidgets = (widgets) => ({
2733
type: ADD_WIDGETS,
2834
data: widgets
2935
});
3036

37+
export const setWidgets = (widgets) => ({
38+
type: SET_WIDGETS,
39+
data: widgets
40+
});
41+
3142
export const updateWidget = (newConf => ({
3243
type: UPDATE_WIDGET,
3344
data: newConf
3445
}))
3546

47+
48+
3649
export const setLayout = (newLayout => ({
3750
type: SET_LAYOUT,
3851
data: newLayout

webapp/components/layout/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const MINIMIZED_PANEL = "border_bottom";

webapp/components/layout/model.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export interface Widget {
2222
id: string;
2323
status: WidgetStatus;
2424
panelName: string;
25+
defaultPanel?: any;
2526
hideOnClose?: boolean;
2627
name: string;
2728
enableClose?: boolean;

webapp/components/layout/utils.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export function widget2Node (configuration: Widget) {
1414
enableRename: false,
1515
enableClose,
1616
// attr defined inside config, will also be available from within flexlayout nodes. For example: node.getNodeById(id).getConfig()
17-
config: configuration ,
17+
config: {...configuration} ,
1818
...others
1919
};
2020
}
@@ -47,16 +47,3 @@ export function isEqual (a: any, b: any): boolean {
4747
}
4848
return keys.every(k => isEqual(a[k], b[k]));
4949
}
50-
51-
export const getPythonDefaultConsoleWidget = (editMode: boolean): Widget => ({
52-
id: editMode ? 'pythonEdit' : 'pythonExplore',
53-
name: 'Python',
54-
status: WidgetStatus.ACTIVE,
55-
component: 'PythonConsole',
56-
panelName: "consolePanel",
57-
enableClose: true,
58-
enableDrag: true,
59-
enableRename: false,
60-
pos: 100
61-
}) ;
62-

0 commit comments

Comments
 (0)