Skip to content

Commit 8498085

Browse files
committed
#417 integrated new actions to handle instances
1 parent 5c8e6bd commit 8498085

7 files changed

Lines changed: 138 additions & 66 deletions

File tree

webapp/components/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { openBackendErrorDialog, closeBackendErrorDialog } from '../redux/action
1414
import {
1515
updateCards, editModel, simulateNetwork, createNetwork, closeDialog,
1616
createAndSimulateNetwork, showNetwork, pythonCall, modelLoaded, deleteNetParamsObj, resetModel,
17-
setDefaultWidgets,
17+
setDefaultWidgets, changeInstanceColor,
1818
} from '../redux/actions/general';
1919

2020
import {
@@ -244,8 +244,11 @@ export const NetPyNEInstantiated = connect(
244244
(state) => ({
245245
modelState: state.general.modelState,
246246
theme: state.general.theme,
247+
data: state.general.instances,
248+
}),
249+
(dispatch) => ({
250+
selectInstances: (instances) => dispatch(changeInstanceColor(instances)),
247251
}),
248-
null,
249252
)(_NetPyNEInstantiated);
250253

251254
export const NetWorkControlButtons = connect(

webapp/components/instantiation/NetPyNEInstantiated.js

Lines changed: 25 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import ReactDOM from 'react-dom';
33
import { withStyles } from '@material-ui/core';
44
import Canvas from '@metacell/geppetto-meta-ui/3d-canvas/Canvas';
55
import CameraControls from '@metacell/geppetto-meta-ui/camera-controls/CameraControls';
6+
import { applySelection, mapToCanvasData } from '@metacell/geppetto-meta-ui/3d-canvas/showcase/utils/SelectionUtils';
67
// TODO: replace this with the list viewer during refactoring
78
// import ControlPanel from 'geppetto-client/js/components/interface/controlPanel/controlpanel';
89

@@ -15,10 +16,10 @@ import { THEMES } from '../../constants';
1516
const CANVAS_LIGHT = 'canvas-toolbar-btns-light';
1617
const CANVAS_DARK = 'canvas-toolbar-btns-dark';
1718
const SELECTION_COLOR = {
18-
r: 0.8, g: 0.8, b: 0, a: 1,
19+
r: 0, g: 0.8, b: 0.8, a: 1,
1920
};
2021
const DEFAULT_COLOR = {
21-
r: 1, g: 0, b: 0, a: 0.5,
22+
g: 0.50, b: 0.60, r: 1, a: 0.80,
2223
};
2324

2425
const styles = () => ({
@@ -38,14 +39,6 @@ class NetPyNEInstantiated extends React.Component {
3839
hasModelLoaded: false,
3940
intersected: [],
4041
tooltipVisible: false,
41-
data: [
42-
{
43-
instancePath: 'network',
44-
color: {
45-
g: 0.50, b: 0.60, r: 1, a: 0.80,
46-
},
47-
},
48-
],
4942
selected: {},
5043
cameraOptions: {
5144
angle: 60,
@@ -66,11 +59,26 @@ class NetPyNEInstantiated extends React.Component {
6659
},
6760
};
6861
this.dimensions = {
69-
width: '1980px',
70-
height: '1900px',
62+
width: '100%',
63+
height: '100%',
7164
};
7265
this.canvasRef = React.createRef();
73-
this.controlPanelToggle = this.controlPanelToggle.bind(this);
66+
67+
this.onSelection = this.onSelection.bind(this);
68+
}
69+
70+
onSelection (selectedInstances) {
71+
const { selectInstances, data } = this.props;
72+
selectInstances(applySelection(data, selectedInstances));
73+
}
74+
75+
getParentSize () {
76+
if (this.canvasRef.current === null) {
77+
return false;
78+
}
79+
// eslint-disable-next-line react/no-find-dom-node
80+
const node = ReactDOM.findDOMNode(this);
81+
return node.parentNode.getBoundingClientRect();
7482
}
7583

7684
updateBtnsWithTheme = (removeClass, addClass) => {
@@ -102,46 +110,11 @@ class NetPyNEInstantiated extends React.Component {
102110
this.timer = setTimeout(() => this.resizeIfNeeded(), 100);
103111
}
104112

105-
getParentSize () {
106-
if (this.canvasRef.current === null) {
107-
return false;
108-
}
109-
// eslint-disable-next-line react/no-find-dom-node
110-
const node = ReactDOM.findDOMNode(this);
111-
return node.parentNode.getBoundingClientRect();
112-
}
113-
114-
controlPanelToggle () {
115-
if (!this.state.controlPanelInitialized) {
116-
this.setState({ controlPanelInitialized: true });
117-
} else {
118-
$('#controlpanel')
119-
.show();
120-
}
121-
}
122-
123-
mapToCanvasData (data) {
124-
return data.map((item) => (
125-
{
126-
color: item.selected ? SELECTION_COLOR : item.color,
127-
instancePath: item.instancePath,
128-
}
129-
));
130-
}
131-
132113
render () {
133-
const {
134-
update,
135-
canvasBtnCls,
136-
controlPanelInitialized,
137-
} = this.state;
138-
const { theme } = this.props;
139-
const { controlPanelToggle } = this;
140-
const bgColor = theme === THEMES.LIGHT ? canvasBgLight : theme === THEMES.BLACK ? canvasBgDark : 'transparent';
141-
142-
const { data, cameraOptions } = this.state;
114+
const { cameraOptions } = this.state;
115+
const { data } = this.props;
143116

144-
const canvasData = this.mapToCanvasData(data);
117+
const canvasData = mapToCanvasData(data);
145118

146119
let camOptions = cameraOptions;
147120
if (this.lastCameraUpdate) {
@@ -155,7 +128,6 @@ class NetPyNEInstantiated extends React.Component {
155128
};
156129
}
157130

158-
159131
return (
160132
<div className="instantiatedContainer">
161133
<Canvas
@@ -164,6 +136,7 @@ class NetPyNEInstantiated extends React.Component {
164136
key="CanvasContainer"
165137
data={canvasData}
166138
backgroundColor={bgRegular}
139+
onSelection={this.onSelection}
167140
/>
168141
</div>
169142
);

webapp/constants.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ export const MODEL_STATE = {
1616
SIMULATED: 'SIMULATED',
1717
};
1818

19+
export const DEFAULT_COLOR = {
20+
g: 0.50, b: 0.60, r: 1, a: 0.80,
21+
};
22+
1923
export const NETPYNE_COMMANDS = {
2024
instantiateModel: 'netpyne_geppetto.instantiateNetPyNEModelInGeppetto',
2125
simulateModel: 'netpyne_geppetto.simulateNetPyNEModelInGeppetto',

webapp/redux/actions/general.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ export const AUTOMATIC_SIMULATION = 'AUTOMATIC_SIMULATION';
2020
export const IMPORT_APPLICATION_STATE = 'IMPORT_APPLICATION_STATE';
2121
export const SET_THEME = 'SET_THEME';
2222

23+
export const ADD_CANVAS_INSTANCES = 'ADD_CANVAS_INSTANCES';
2324
export const CHANGE_INSTANCE_COLOR = 'CHANGE_INSTANCE_COLOR';
25+
export const REMOVE_CANVAS_INSTANCES = 'REMOVE_CANVAS_INSTANCES';
2426

2527
// Actions
2628
export const updateCards = { type: UPDATE_CARDS };
@@ -91,3 +93,13 @@ export const changeInstanceColor = (instance, color) => ({
9193
color,
9294
},
9395
});
96+
97+
export const addInstancesToCanvas = (instances) => ({
98+
type: ADD_CANVAS_INSTANCES,
99+
instances,
100+
});
101+
102+
export const removeInstancesFromCanvas = (instances) => ({
103+
type: REMOVE_CANVAS_INSTANCES,
104+
instances,
105+
});

webapp/redux/middleware/middleware.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ import {
2020
RESET_MODEL,
2121
showNetwork,
2222
MODEL_LOADED,
23+
changeInstanceColor,
24+
addInstancesToCanvas,
25+
removeInstancesFromCanvas,
2326
} from '../actions/general';
2427
import { openBackendErrorDialog } from '../actions/errors';
2528
import { closeDrawerDialogBox } from '../actions/drawer';
@@ -137,6 +140,18 @@ export default (store) => (next) => (action) => {
137140
next(GeppettoActions.waitData('Loading the NetPyNE Model', GeppettoActions.clientActions.MODEL_LOADED));
138141
next(action);
139142
break;
143+
case GeppettoActions.clientActions.MODEL_LOADED:
144+
if (store.getState()?.general?.modelState === Constants.MODEL_STATE.NOT_INSTANTIATED) {
145+
const networkPath = window.Instances.getInstance('network');
146+
if (networkPath) {
147+
store.dispatch(addInstancesToCanvas([{
148+
instancePath: networkPath.getInstancePath(),
149+
color: Constants.DEFAULT_COLOR,
150+
}]));
151+
}
152+
}
153+
next(action);
154+
break;
140155
case UPDATE_CARDS:
141156
console.log('Triggered card update');
142157
next(action);
@@ -265,7 +280,7 @@ export default (store) => (next) => (action) => {
265280
GEPPETTO.trigger(GEPPETTO.Events.Show_spinner, GEPPETTO.Resources.PARSING_MODEL);
266281
dehydrateCanvas();
267282
GEPPETTO.Manager.loadModel(response);
268-
GEPPETTO.CommandController.log('Instantiation / Simulation completed.');
283+
console.log('Instantiation / Simulation completed.');
269284

270285
store.dispatch(showNetwork);
271286
}

webapp/redux/reducers/general.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export const GENERAL_DEFAULT_STATE = {
1414
automaticSimulation: false,
1515
automaticInstantiation: false,
1616
theme: 'gui',
17+
instances: [],
1718
};
1819

1920
// reducer function
@@ -51,6 +52,12 @@ export default function reduceGeneral (state = GENERAL_DEFAULT_STATE, action) {
5152
return { ...state, theme: action.payload };
5253
}
5354
case Actions.CHANGE_INSTANCE_COLOR: {
55+
return { ...state, instances: [...action.instances] };
56+
}
57+
case Actions.ADD_CANVAS_INSTANCES: {
58+
return { ...state, instances: [...state.instances, ...action.instances] };
59+
}
60+
case Actions.REMOVE_CANVAS_INSTANCES: {
5461
return { ...state };
5562
}
5663
default: {

0 commit comments

Comments
 (0)