diff --git a/src/dashboard/dashboard.js b/src/dashboard/dashboard.js index bfc9daa..4b44a05 100644 --- a/src/dashboard/dashboard.js +++ b/src/dashboard/dashboard.js @@ -37,7 +37,7 @@ import AppDispatcher from '../common/app-dispatcher'; import ScenarioStore from '../scenario/scenario-store'; import 'react-contexify/dist/ReactContexify.min.css'; import WidgetContainer from '../widget/widget-container'; -import EditableWidgetContainer from '../widget/editable-widget-container'; +import Widget from "../widget/widget"; class Dashboard extends Component { @@ -505,8 +505,8 @@ class Dashboard extends Component { const grid = this.state.dashboard.grid; const boxClasses = classNames('section', 'box', { 'fullscreen-padding': this.props.isFullscreen }); - let draggable = this.state.editing; let dropZoneHeight = this.state.dashboard.height; + return (
@@ -528,119 +528,111 @@ class Dashboard extends Component {
- -
- -
e.preventDefault()}> - {this.state.editing && - - } - {!draggable ? ( - - {this.state.widgets != null && Object.keys(this.state.widgets).map(widgetKey => ( - - - - ))} - - ) : ( - - {this.state.widgets != null && Object.keys(this.state.widgets).map(widgetKey => ( - - - - ))} - - - )} - - - - - - this.closeEditSignalsModal(direction)} - direction="Output" - signals={this.state.signals} - configID={null} - configs={this.state.configs} - sessionToken={this.state.sessionToken} - /> - this.closeEditSignalsModal(direction)} - direction="Input" - signals={this.state.signals} - configID={null} - configs={this.state.configs} - sessionToken={this.state.sessionToken} - /> - - -
+
- ); + +
e.preventDefault()}> + {this.state.editing && + + } + + + {this.state.widgets != null && Object.keys(this.state.widgets).map(widgetKey => ( + +
+ + + + +
+ + ))} +
+ + + + + + this.closeEditSignalsModal(direction)} + direction="Output" + signals={this.state.signals} + configID={null} + configs={this.state.configs} + sessionToken={this.state.sessionToken} + /> + this.closeEditSignalsModal(direction)} + direction="Input" + signals={this.state.signals} + configID={null} + configs={this.state.configs} + sessionToken={this.state.sessionToken} + /> +
+ ); } } diff --git a/src/dashboard/widget-area.js b/src/dashboard/widget-area.js index 07db2f8..b9a93a7 100644 --- a/src/dashboard/widget-area.js +++ b/src/dashboard/widget-area.js @@ -45,10 +45,18 @@ class WidgetArea extends React.Component { render() { - return + return {this.props.children} - + ; } } diff --git a/src/widget/editable-widget-container.js b/src/widget/editable-widget-container.js deleted file mode 100644 index a926f5f..0000000 --- a/src/widget/editable-widget-container.js +++ /dev/null @@ -1,142 +0,0 @@ -/** - * 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 . - ******************************************************************************/ - -import React from 'react'; -import PropTypes from 'prop-types'; -import classNames from 'classnames'; -import { Rnd } from 'react-rnd'; - -class EditableWidgetContainer extends React.Component { - constructor(props) { - super(props); - - this.rnd = null; - } - - snapToGrid(value) { - if (this.props.grid === 1) { - return value; - } - return Math.round(value / this.props.grid) * this.props.grid; - } - - borderWasClicked = event => { - if (event.button !== 2) { - return; - } - }; - - dragStop = (event, data) => { - const widget = this.props.widget; - widget.x = this.snapToGrid(data.x); - widget.y = this.snapToGrid(data.y); - - - if (widget.x !== data.x || widget.y !== data.y) { - this.rnd.updatePosition({ x: widget.x, y: widget.y }); - } - - if (this.props.onWidgetChange != null) { - this.props.onWidgetChange(widget, this.props.index); - } - }; - - resizeStop = (event, direction, ref, delta, position) => { - const widget = this.props.widget; - - // resize depends on direction - if (direction === 'left' || direction === 'topLeft' || direction === 'bottomLeft') { - widget.x -= delta.width; - } - - if (direction === 'top' || direction === 'topLeft' || direction === 'topRight') { - widget.y -= delta.height; - } - - widget.width += delta.width; - widget.height += delta.height; - - if (this.props.onWidgetChange != null) { - this.props.onWidgetChange(widget, this.props.index); - } - - /* hand over new dimensions to child element so that the rotation is displayed correctly - * already before the dashboard changes are saved - - if (this.props.widget.type === 'Line') { - this.refs.child0.illustrateDuringEdit(widget.width, widget.height); - }*/ - }; - - render() { - const widget = this.props.widget; - - let resizingRestricted = false; - if (widget.customProperties.resizeRightLeftLock || widget.customProperties.resizeTopBottomLock) { - resizingRestricted = true; - } - - - const resizing = { - bottom: !(widget.customProperties.resizeTopBottomLock || widget.isLocked), - bottomLeft: !(resizingRestricted || widget.isLocked), - bottomRight: !(resizingRestricted || widget.isLocked), - left: !(widget.customProperties.resizeRightLeftLock || widget.isLocked), - right: !(widget.customProperties.resizeRightLeftLock || widget.isLocked), - top: !(widget.customProperties.resizeTopBottomLock || widget.isLocked), - topLeft: !(resizingRestricted || widget.isLocked), - topRight: !(resizingRestricted || widget.isLocked) - }; - - const gridArray = [this.props.grid, this.props.grid]; - - const widgetClasses = classNames({ - 'editing-widget': true, - 'locked': widget.isLocked - }); - - return { this.rnd = c; }} - default={{ x: Number(widget.x), y: Number(widget.y), width: widget.width, height: widget.height }} - minWidth={widget.minWidth} - minHeight={widget.minHeight} - maxWidth={widget.customProperties.maxWidth || '100%'} - lockAspectRatio={Boolean(widget.customProperties.lockAspect)} - bounds={'parent'} - className={widgetClasses} - onResizeStart={this.borderWasClicked} - onResizeStop={this.resizeStop} - onDragStop={this.dragStop} - dragGrid={gridArray} - resizeGrid={gridArray} - zindex={widget.z} - enableResizing={resizing} - disableDragging={widget.isLocked} - > - {this.props.children} - ; - } -} - -EditableWidgetContainer.propTypes = { - widget: PropTypes.object.isRequired, - index: PropTypes.number.isRequired, - grid: PropTypes.number, - onWidgetChange: PropTypes.func -}; - -export default EditableWidgetContainer diff --git a/src/widget/widget-container.js b/src/widget/widget-container.js index 5b2e064..77efc3c 100644 --- a/src/widget/widget-container.js +++ b/src/widget/widget-container.js @@ -17,28 +17,172 @@ import React from 'react'; import PropTypes from 'prop-types'; +import classNames from 'classnames'; +import { Rnd } from 'react-rnd'; +import WidgetContextMenu from '../widget/widget-context-menu'; +import {contextMenu} from "react-contexify"; class WidgetContainer extends React.Component { + constructor(props) { + super(props); + + this.rnd = null; + } + + snapToGrid(value) { + if (this.props.grid === 1) { + return value; + } + return Math.round(value / this.props.grid) * this.props.grid; + } + + borderWasClicked = event => { + if (event.button !== 2) { + return; + } + }; + + dragStop = (event, data) => { + const widget = this.props.widget; + widget.x = this.snapToGrid(data.x); + widget.y = this.snapToGrid(data.y); + + + if (widget.x !== data.x || widget.y !== data.y) { + this.rnd.updatePosition({ x: widget.x, y: widget.y }); + } + + if (this.props.onWidgetChange != null) { + this.props.onWidgetChange(widget, this.props.index); + } + }; + + resizeStop = (event, direction, ref, delta, position) => { + const widget = this.props.widget; + + // resize depends on direction + if (direction === 'left' || direction === 'topLeft' || direction === 'bottomLeft') { + widget.x -= delta.width; + } + + if (direction === 'top' || direction === 'topLeft' || direction === 'topRight') { + widget.y -= delta.height; + } + + widget.width += delta.width; + widget.height += delta.height; + + if (this.props.onWidgetChange != null) { + this.props.onWidgetChange(widget, this.props.index); + } + + /* hand over new dimensions to child element so that the rotation is displayed correctly + * already before the dashboard changes are saved + + if (this.props.widget.type === 'Line') { + this.refs.child0.illustrateDuringEdit(widget.width, widget.height); + }*/ + }; + + showMenu(e, index, editing) { + e.preventDefault(); + contextMenu.show({ + event: e, + id: 'widgetMenu' + index, + }) + } + render() { - const containerStyle = { - width: Number(this.props.widget.width), - height: Number(this.props.widget.height), - left: Number(this.props.widget.x), - top: Number(this.props.widget.y), - zIndex: Number(this.props.widget.z), - position: 'absolute' + const widget = this.props.widget; + let contextMenu = () + + if ( !this.props.editing ){ + const containerStyle = { + width: Number(widget.width), + height: Number(widget.height), + left: Number(widget.x), + top: Number(widget.y), + zIndex: Number(widget.z), + position: 'absolute' + }; + + return
this.showMenu(e, this.props.index, this.props.editing)}> + {this.props.children} + {contextMenu} +
; + } + + let resizingRestricted = false; + if (widget.customProperties.resizeRightLeftLock || widget.customProperties.resizeTopBottomLock) { + resizingRestricted = true; + } + + const resizing = { + bottom: !(widget.customProperties.resizeTopBottomLock || widget.isLocked), + bottomLeft: !(resizingRestricted || widget.isLocked), + bottomRight: !(resizingRestricted || widget.isLocked), + left: !(widget.customProperties.resizeRightLeftLock || widget.isLocked), + right: !(widget.customProperties.resizeRightLeftLock || widget.isLocked), + top: !(widget.customProperties.resizeTopBottomLock || widget.isLocked), + topLeft: !(resizingRestricted || widget.isLocked), + topRight: !(resizingRestricted || widget.isLocked) }; - return
- {this.props.children} -
; + const gridArray = [this.props.grid, this.props.grid]; + + const widgetClasses = classNames({ + 'editing-widget': true, + 'locked': widget.isLocked + }); + + return (
this.showMenu(e, this.props.index, this.props.editing)}> + { this.rnd = c; }} + size={{width: Number(widget.width), height: Number(widget.height)}} + position={{x: Number(widget.x), y: Number(widget.y),}} + minWidth={widget.minWidth} + minHeight={widget.minHeight} + maxWidth={widget.customProperties.maxWidth || '100%'} + lockAspectRatio={Boolean(widget.customProperties.lockAspect)} + bounds={'parent'} + className={widgetClasses} + onResizeStart={this.borderWasClicked} + onResizeStop={this.resizeStop} + onDragStop={this.dragStop} + dragGrid={gridArray} + resizeGrid={gridArray} + zindex={widget.z} + enableResizing={resizing} + disableDragging={widget.isLocked} + > + {this.props.children} + + + {contextMenu} +
); } } WidgetContainer.propTypes = { widget: PropTypes.object.isRequired, - children: PropTypes.node, //TODO is .node correct here? Was .children before leading to compile error + index: PropTypes.number.isRequired, + grid: PropTypes.number, + onWidgetChange: PropTypes.func, + children: PropTypes.node, + editing: PropTypes.bool.isRequired, }; export default WidgetContainer diff --git a/src/widget/widget-context-menu.js b/src/widget/widget-context-menu.js index 2bb64e6..47f0317 100644 --- a/src/widget/widget-context-menu.js +++ b/src/widget/widget-context-menu.js @@ -17,10 +17,11 @@ import React from 'react'; import PropTypes from 'prop-types'; -import { Menu, Item, Separator, contextMenu } from 'react-contexify'; -import Widget from './widget'; +import { Menu, Item, Separator } from 'react-contexify'; +import "react-contexify/dist/ReactContexify.css"; class WidgetContextMenu extends React.Component { + editWidget = event => { if (this.props.onEdit != null) { this.props.onEdit(this.props.widget, this.props.index); @@ -93,26 +94,6 @@ class WidgetContextMenu extends React.Component { } }; - showMenu = e => { - let index = this.props.index - if (this.props.editing){ - contextMenu.show({ - event: e, - id: 'widgetMenu' + index, - position: { - x: 'inherit', - y: 'inherit', - } - }) - } - else { - contextMenu.show({ - event: e, - id: 'widgetMenu' + index, - }) - } - } - render() { const isLocked = this.props.widget.locked; @@ -122,16 +103,7 @@ class WidgetContextMenu extends React.Component { }; return ( - -
- - +
Edit Duplicate @@ -158,8 +130,8 @@ WidgetContextMenu.propTypes = { index: PropTypes.number.isRequired, widget: PropTypes.object.isRequired, onEdit: PropTypes.func.isRequired, - onDelete: PropTypes.func, - onChange: PropTypes.func + onDelete: PropTypes.func.isRequired, + onChange: PropTypes.func.isRequired }; export default WidgetContextMenu