/**
* This file is part of VILLASweb.
*
* VILLASweb is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* VILLASweb is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with VILLASweb. If not, see
, prevState: Readonly, snapshot: SS) {
// open web sockets if ICs are already known and sockets are not opened yet
if (this.state.ics !== undefined && !Dashboard.webSocketsOpened) {
if (this.state.ics.length > 0) {
console.log("Starting to open IC websockets:", this.state.ics);
AppDispatcher.dispatch({
type: 'ics/open-sockets',
data: this.state.ics
});
Dashboard.webSocketsOpened = true;
}
}
if (prevState.dashboard === undefined && this.state.dashboard !== undefined) {
// the dashboard was loaded, so that the scenarioID is available
// load configs of scenario
AppDispatcher.dispatch({
type: 'configs/start-load',
token: this.state.sessionToken,
param: '?scenarioID=' + this.state.dashboard.scenarioID
});
// load files of scenario
AppDispatcher.dispatch({
type: 'files/start-load',
param: '?scenarioID=' + this.state.dashboard.scenarioID,
token: this.state.sessionToken
});
}
}
componentWillUnmount() {
// close web sockets of ICs
console.log("Starting to close all web sockets");
AppDispatcher.dispatch({
type: 'ics/close-sockets',
});
}
handleKeydown(e) {
switch (e.key) {
case ' ':
case 'p':
this.setState({ paused: !this.state.paused });
break;
case 'e':
this.setState({ editing: !this.state.editing });
break;
case 'f':
this.props.toggleFullscreen();
break;
default:
}
}
transformToWidgetsList(widgets) {
return Object.keys(widgets).map((key) => widgets[key]);
}
handleDrop(widget) {
widget.dashboardID = this.state.dashboard.id;
AppDispatcher.dispatch({
type: 'widgets/start-add',
token: this.state.sessionToken,
data: widget
});
};
widgetChange(widget, index, callback = null) {
let temp = this.state.widgetChangeData;
temp.push(widget);
this.setState({ widgetChangeData: temp });
}
editWidget(widget, index) {
this.setState({ editModal: true, modalData: widget, modalIndex: index });
};
duplicateWidget(widget) {
let widgetCopy = JSON.parse(JSON.stringify(widget));
delete widgetCopy.id;
widgetCopy.x = widgetCopy.x + 50;
widgetCopy.y = widgetCopy.y + 50;
AppDispatcher.dispatch({
type: 'widgets/start-add',
token: this.state.sessionToken,
data: widgetCopy
});
};
startEditFiles() {
let tempFiles = [];
this.state.files.forEach(file => {
tempFiles.push({
id: file.id,
name: file.name
});
})
this.setState({ filesEditModal: true, filesEditSaveState: tempFiles });
}
closeEditFiles() {
this.state.widgets.map(widget => {
if(widget.type === "Image"){
widget.customProperties.update = true;
}
})
this.setState({ filesEditModal: false });
}
closeEdit(data) {
if (data == null) {
AppDispatcher.dispatch({
type: 'widgets/start-load',
token: this.state.sessionToken,
param: '?dashboardID=' + this.state.dashboard.id
});
this.setState({ editModal: false });
return;
}
if(data.type === "Image")
{
data.customProperties.update = true;
}
AppDispatcher.dispatch({
type: 'widgets/start-edit',
token: this.state.sessionToken,
data: data
});
this.setState({ editModal: false });
};
deleteWidget(widget, index) {
AppDispatcher.dispatch({
type: 'widgets/start-remove',
data: widget,
token: this.state.sessionToken
});
};
startEditing() {
let originalIDs = [];
this.state.widgets.forEach(widget => {
originalIDs.push(widget.id);
if (widget.type === 'Slider' || widget.type === 'NumberInput' || widget.type === 'Button') {
AppDispatcher.dispatch({
type: 'widgets/start-edit',
token: this.state.sessionToken,
data: widget
});
}
else if (widget.type === 'Image'){
widget.customProperties.update = true;
}
});
this.setState({ editing: true, widgetOrigIDs: originalIDs });
};
saveEditing() {
this.state.widgets.forEach(widget => {
if (widget.type === 'Image'){
widget.customProperties.update = true;
}
});
// Provide the callback so it can be called when state change is applied
// TODO: Check if callback is needed
AppDispatcher.dispatch({
type: 'dashboards/start-edit',
data: this.state.dashboard,
token: this.state.sessionToken
});
this.state.widgetChangeData.forEach(widget => {
AppDispatcher.dispatch({
type: 'widgets/start-edit',
token: this.state.sessionToken,
data: widget
});
});
this.setState({ editing: false, widgetChangeData: [] });
};
cancelEditing() {
//raw widget has no id -> cannot be deleted in its original form
this.state.widgets.forEach(widget => {
if (widget.type === 'Image'){
widget.customProperties.update = true;
}
let tempID = this.state.widgetOrigIDs.find(element => element === widget.id);
if (typeof tempID === 'undefined') {
AppDispatcher.dispatch({
type: 'widgets/start-remove',
data: widget,
token: this.state.sessionToken
});
}
})
AppDispatcher.dispatch({
type: 'widgets/start-load',
token: this.state.sessionToken,
param: '?dashboardID=' + this.state.dashboard.id
});
AppDispatcher.dispatch({
type: 'dashboards/start-load',
data: this.props.match.params.dashboard,
token: this.state.sessionToken
});
this.setState({ editing: false, widgetChangeData: [] });
};
setGrid(value) {
let dashboard = this.state.dashboard;
dashboard.grid = value;
this.setState({ dashboard });
this.forceUpdate();
};
setDashboardSize(value) {
const maxHeight = Object.values(this.state.widgets).reduce((currentHeight, widget) => {
const absolutHeight = widget.y + widget.height;
return absolutHeight > currentHeight ? absolutHeight : currentHeight;
}, 0);
let dashboard = this.state.dashboard;
if (value === -1) {
let tempHeight = this.state.dashboard.height - 50;
if (tempHeight >= 400 && tempHeight >= (maxHeight + 80)) {
dashboard.height = tempHeight;
this.setState({ dashboard });
}
}
else {
dashboard.height = this.state.dashboard.height + 50;
this.setState({ dashboard });
}
this.forceUpdate();
}
pauseData() {
this.setState({ paused: true });
};
unpauseData() {
this.setState({ paused: false });
};
editInputSignals() {
this.setState({ editInputSignalsModal: true });
};
editOutputSignals() {
this.setState({ editOutputSignalsModal: true });
};
closeEditSignalsModal(direction) {
if (direction === "in") {
this.setState({ editInputSignalsModal: false });
} else if (direction === "out") {
this.setState({ editOutputSignalsModal: false });
}
}
render() {
if (this.state.dashboard === undefined) {
return {this.state.dashboard.name}