Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/188 range fields fix #785

Merged
merged 6 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion webapp/components/general/AdapterComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export default class AdapterComponent extends Component {

// Call to conversion function
const newValue = this.props.convertToPython(this.state);
if (newValue != undefined && this.state.value != newValue) {
if (newValue != undefined && this.state.value != newValue && this.props.onChange) {
this.props.onChange(null, null, newValue);
}
}
Expand Down
165 changes: 111 additions & 54 deletions webapp/components/general/NetPyNECoordsRange.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,29 @@ import Utils from '../../Utils';
export default class NetPyNECoordsRange extends Component {
constructor (props) {
super(props);
this.state = { rangeType: undefined };
this.state = { rangeType: undefined, rangeValue: [undefined, undefined] };

this._isMounted = false;
}

triggerUpdate (updateMethod) {
// common strategy when triggering processing of a value change, delay it, every time there is a change we reset
if (this.updateTimer != undefined) {
clearTimeout(this.updateTimer);
}
this.updateTimer = setTimeout(updateMethod, 1000);
}

componentDidMount () {
this._isMounted = true;
this.updateLayout();
}

componentDidUpdate (prevProps, prevState) {
if (this.props.name != prevProps.name) {
this.triggerUpdate(() => {
const message = `netpyne_geppetto.${this.props.model}['${this.props.name}']${(this.props.conds != undefined) ? `['${this.props.conds}']` : ''}`;
Utils
.evalPythonMessage(`[key in ${message} for key in ['${this.props.items[0].value}', '${this.props.items[1].value}']]`)
.then((response) => {
if (response[0] && this._isMounted === true) {
this.setState({ rangeType: this.props.items[0].value });
} else if (response[1] && this._isMounted === true) {
this.setState({ rangeType: this.props.items[1].value });
} else if (this._isMounted === true) {
this.setState({ rangeType: undefined });
}
});
const message = `netpyne_geppetto.${this.props.model}['${this.props.name}']${(this.props.conds != undefined) ? `['${this.props.conds}']` : ''}`;
Utils
.evalPythonMessage(`[key in ${message} for key in ['${this.props.items[0].value}', '${this.props.items[1].value}']]`)
.then((response) => {
if (response[0] && this._isMounted === true) {
this.setState({ rangeType: this.props.items[0].value });
} else if (response[1] && this._isMounted === true) {
this.setState({ rangeType: this.props.items[1].value });
} else if (this._isMounted === true) {
this.setState({ rangeType: undefined });
}
});
} else if (this.props.conds != prevProps.conds) {
this.updateLayout();
Expand All @@ -58,17 +48,34 @@ export default class NetPyNECoordsRange extends Component {

const message = `netpyne_geppetto.${model}['${name}']${(conds !== undefined)
? `['${conds}']` : ''}`;
const evalMessage = `[key in ${message} for key in ['${items[0].value}', '${items[1].value}']]` ;

Utils
.evalPythonMessage(`[key in ${message} for key in ['${items[0].value}', '${items[1].value}']]`)
.then((response) => {
if (response[0] && this._isMounted === true) {
this.setState({ rangeType: items[0].value });
} else if (response[1] && this._isMounted === true) {
this.setState({ rangeType: items[1].value });
} else if (this._isMounted === true) {
this.setState({ rangeType: undefined });
}
});
.evalPythonMessage(evalMessage)
.then((response) => {

let rangeType = undefined ;

if (response[0] && this._isMounted === true) {
rangeType = items[0].value ;
} else if (response[1] && this._isMounted === true) {
rangeType = items[1].value ;
}

this.setState({ rangeType });

if (rangeType)
{
const pythonMessage = `netpyne_geppetto.${model}['${name}']['${conds}']['${this.state.rangeType}']` ;

Utils
.evalPythonMessage(pythonMessage)
.then((response) => {
if (response && response.length > 0 ) {
this.setState({ rangeValue: response });
}});
}
});
}

createMenuItems = () => this.props.items.map((obj) => (
Expand All @@ -85,6 +92,71 @@ export default class NetPyNECoordsRange extends Component {
this._isMounted = false;
}

handleRangeTypeChange(event) {
const {
model,
conds,
name,
} = this.props;

const rangeType = event.target.value ;

const pythonMessageDelAll = `netpyne_geppetto.${model}['${name}']['${conds}'] = {}`;
Utils.execPythonMessage(
pythonMessageDelAll
);

const rangeValue = this.state.rangeValue ;

if (!rangeValue.some(e => e === undefined))
{
const pythonMessage = `netpyne_geppetto.${model}['${name}']['${conds}']['${rangeType}'] = [${rangeValue}]` ;
Utils.execPythonMessage(
pythonMessage
);
}

this.setState({ rangeType})
}

//preConds: pop, cellType, cellModel, x, y, z, xnorm, ynorm, znorm
handleCoordParamChange(index, newValue) {
const {
model,
conds,
name,
} = this.props;

function getOppositeObject(list, givenValue) {
const index = list.findIndex(obj => obj.value === givenValue);

if (index === -1 || list.length !== 2) {
return null;
}
return list[1 - index];
}

if (newValue === '' || (/^\d+$/.test(newValue))) {
const opossiteRangeType = getOppositeObject(this.props.items, this.state.rangeType) ;

if (opossiteRangeType)
{
const pythonMessageDelOpposite = `netpyne_geppetto.${model}['${name}']['${conds}'].pop('${opossiteRangeType.value}', None)`;
Utils.execPythonMessage(
pythonMessageDelOpposite
);
}

const rangeValue = this.state.rangeValue ;
rangeValue[index] = newValue ;
const pythonMessage = `netpyne_geppetto.${model}['${name}']['${conds}']['${this.state.rangeType}'] = [${rangeValue}]` ;
Utils.execPythonMessage(
pythonMessage
);
this.setState({ rangeValue })
}
}

render () {
if (this.props.conds != undefined) {
var meta = `${this.props.model}.${this.props.conds}.${this.props.items[0].value}`;
Expand All @@ -95,42 +167,27 @@ export default class NetPyNECoordsRange extends Component {
}
const min = `${this.props.id}MinRange`;
const max = `${this.props.id}MaxRange`;

const minVal = this.state.rangeValue[0];
const maxVal = this.state.rangeValue[1];

return (
<div>
<NetPyNEField id={meta}>
<SelectField
id={`${this.props.id}Select`}
label="Range type"
value={this.state.rangeType || ''}
onChange={(event) => this.setState({ rangeType: event.target.value })}
onChange={(event) => { this.handleRangeTypeChange(event); }}
>
{this.createMenuItems()}
</SelectField>
</NetPyNEField>
{(this.state.rangeType != undefined)
? (
<Box width="100%" p={1}>
<AdapterComponent
model={path}
convertToPython={(state) => {
if (!state[state.lastUpdated].toString()
.endsWith('.')
&& ((!isNaN(parseFloat(state[min]))) && (!isNaN(parseFloat(state[max]))))) {
return [parseFloat(state[min]), parseFloat(state[max])];
}
}}
convertFromPython={(prevProps, prevState, value) => {
if (value != undefined && prevProps.value != value && value != '') {
const output = {};
output[min] = value[0];
output[max] = value[1];
return output;
}
}}
>
<TextField label="Minimum" id={min} variant="filled" fullWidth />
<TextField label="Maximum" id={max} variant="filled" fullWidth />
</AdapterComponent>
<TextField label="Minimum" id={min} variant="filled" value={minVal} fullWidth onChange={ (e) => { this.handleCoordParamChange(0, parseInt(e.target.value)) } } />
<TextField label="Maximum" id={max} variant="filled" value={maxVal} fullWidth onChange={ (e) => { this.handleCoordParamChange(1, parseInt(e.target.value)) } } />
</Box>
)
: null}
Expand Down
Loading