Skip to content

Commit

Permalink
Grid layout designer.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdriscoll committed May 20, 2019
1 parent 8ca07d1 commit 58eb48b
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 16 deletions.
48 changes: 44 additions & 4 deletions src/UniversalDashboard.Materialize/Components/ud-designer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import UdIcon from './ud-icon.jsx';
import UdTerminal from './ud-terminal';
import M from 'materialize-css';
import * as clipboard from "clipboard-polyfill"

export default class UDDesigner extends React.Component {

Expand All @@ -15,12 +16,16 @@ export default class UDDesigner extends React.Component {

componentDidMount() {
this.modalInstance = M.Modal.init(this.modal);

var elems = document.querySelectorAll('.fixed-action-btn');
this.fabInstance = M.FloatingActionButton.init(elems);
}

componentWillUnmount() {
this.modalInstance.destroy();
this.fabInstance.destroy();
}

toggleTerminal() {
if (this.state.open) {
this.modalInstance.close();
Expand All @@ -29,17 +34,52 @@ export default class UDDesigner extends React.Component {
}
}

getFromLS(key) {
let ls = {};
if (global.localStorage) {
try {
ls = JSON.parse(global.localStorage.getItem("rgl-8")) || {};
} catch (e) {
/*Ignore*/
}
}
return ls[key];
}

copyLayout() {

var layout = this.getFromLS("uddesign");

if (layout == null || layout.lg == null) {
M.toast({html: "No layout saved. Make sure you are using New-UDGridLayout."});
}

layout.lg.forEach(x => {
x.static = true
});

var stringLayout = JSON.stringify(layout);

clipboard.writeText(stringLayout).then(x => {
M.toast({ html: "Layout copied to clipboard!"});
});
}

render() {
return (
[
<div className="fixed-action-btn">
<a className="btn-floating btn-large red" onClick={this.toggleTerminal.bind(this)} id='btnDesignTerminal'>
<i className="large fa fa-terminal"></i>
<a className="btn-floating btn-large red">
<UdIcon icon="Edit" />
</a>
<ul>
<li><a className="btn-floating green" title="Copy Layout" onClick={this.copyLayout.bind(this)}><UdIcon icon="Copy" /></a></li>
<li><a className="btn-floating blue" onClick={this.toggleTerminal.bind(this)} id='btnDesignTerminal' title="Open Design Terminal"><UdIcon icon="Terminal" /></a></li>
</ul>
</div>,
<div ref={modal => this.modal = modal} className="modal bottom-sheet">
<div className="modal-content">
<h4><UdIcon icon={'terminal'}/> Design Terminal</h4>
<h4><UdIcon icon={'Terminal'}/> Design Terminal</h4>
<UdTerminal />
</div>
</div>
Expand Down
5 changes: 5 additions & 0 deletions src/UniversalDashboard.Materialize/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/UniversalDashboard.Materialize/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"dependencies": {
"chart.js": "2.7.3",
"classnames": "^2.2.6",
"clipboard-polyfill": "^2.8.0",
"griddle-react": "1.13.1",
"jquery": "^3.4.1",
"materialize-css": "1.0.0",
Expand Down
5 changes: 1 addition & 4 deletions src/UniversalDashboard/Controls/src/grid-layout.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ function New-UDGridLayout {
[Parameter()]
[scriptblock]$Content,
[Parameter()]
[Hashtable[]]$Layout,
[Parameter()]
[string]$LayoutJson,
[string]$Layout,
[Parameter()]
[int]$LargeColumns = 12,
[Parameter()]
Expand Down Expand Up @@ -62,7 +60,6 @@ function New-UDGridLayout {
rowHeight = $RowHeight
content = $Content.Invoke()
layout = $Layout
layoutJson = $LayoutJson
cols = $Columns
breakpoints = $Breakpoints
isDraggable = $Draggable.IsPresent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const UniversalDashboardService = {
component
});
},
design: false,
get: fetchGet,
post: fetchPost,
postRaw: fetchPostRaw,
Expand Down
2 changes: 2 additions & 0 deletions src/client/src/app/ud-dashboard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ export default class UdDashboard extends React.Component {

this.connectWebSocket(json.sessionId);

UniversalDashboard.design = dashboard.design;

this.setState({
dashboard: dashboard,
loading: false,
Expand Down
36 changes: 28 additions & 8 deletions src/client/src/app/ud-grid-layout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ export default class UDGridLayout extends React.Component {
super(props);

var layouts = null;
if (props.layoutJson) {
layouts = JSON.parse(props.layoutJson)
} else {
layouts = props.layout.map(x => {
x.i = "grid-element-" + x.i;
return x;
});
}
if (props.layout) {
layouts = JSON.parse(props.layout)

if (!Array.isArray) {
layouts = []
} else {
saveToLS("uddesign", layouts)
}

}

if (props.persist) {
var jsonLayouts = getFromLS("layouts");
Expand All @@ -27,6 +29,19 @@ export default class UDGridLayout extends React.Component {
}
};

if (UniversalDashboard.design) {
var jsonLayouts = getFromLS("uddesign");
if (jsonLayouts != null) {
layouts = JSON.parse(JSON.stringify(jsonLayouts))

if (layouts.lg != null) {
layouts.lg.forEach(x => {
x.static = false
});
}
}
}

this.state = {
layouts,
content: props.content
Expand All @@ -38,6 +53,11 @@ export default class UDGridLayout extends React.Component {
saveToLS("layouts", layouts);
this.setState({ layouts });
}

if (UniversalDashboard.design) {
saveToLS("uddesign", layouts);
this.setState({ layouts });
}
}

render() {
Expand Down

0 comments on commit 58eb48b

Please sign in to comment.