-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Grid layout * Persist. * Fix tests. * Add event handler.
- Loading branch information
1 parent
d53ba8c
commit b87b21a
Showing
7 changed files
with
204 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
function New-UDGridLayout { | ||
param( | ||
[Parameter()] | ||
[string]$Id = (New-Guid).ToString(), | ||
[Parameter()] | ||
[int]$RowHeight = 30, | ||
[Parameter()] | ||
[scriptblock]$Content, | ||
[Parameter()] | ||
[Hashtable[]]$Layout, | ||
[Parameter()] | ||
[int]$LargeColumns = 12, | ||
[Parameter()] | ||
[int]$MediumColumns = 10, | ||
[Parameter()] | ||
[int]$SmallColumns = 6, | ||
[Parameter()] | ||
[int]$ExtraSmallColumns = 4, | ||
[Parameter()] | ||
[int]$ExtraExtraSmallColumns = 2, | ||
[Parameter()] | ||
[int]$LargeBreakpoint = 1200, | ||
[Parameter()] | ||
[int]$MediumBreakpoint = 996, | ||
[Parameter()] | ||
[int]$SmallBreakpoint = 768, | ||
[Parameter()] | ||
[int]$ExtraSmallBreakpoint = 480, | ||
[Parameter()] | ||
[int]$ExtraExtraSmallBreakpoint = 0, | ||
[Parameter()] | ||
[switch]$Draggable, | ||
[Parameter()] | ||
[switch]$Resizable, | ||
[Parameter()] | ||
[switch]$Persist, | ||
[Parameter()] | ||
[object]$OnLayoutChanged | ||
) | ||
|
||
End { | ||
$Breakpoints = @{ | ||
lg = $LargeBreakpoint | ||
md = $MediumBreakpoint | ||
sm = $SmallBreakpoint | ||
xs = $ExtraSmallBreakpoint | ||
xxs = $ExtraExtraSmallBreakpoint | ||
} | ||
|
||
$Columns = @{ | ||
lg = $LargeColumns | ||
md = $MediumColumns | ||
sm = $SmallColumns | ||
xs = $ExtraSmallColumns | ||
xxs = $ExtraExtraSmallColumns | ||
} | ||
|
||
if ($OnLayoutChanged -is ([ScriptBlock])) { | ||
$OnLayoutChanged = New-UDEndpoint -Endpoint $OnLayoutChanged | ||
} | ||
elseif ($OnLayoutChanged -isnot ([UniversalDashboard.Models.Endpoint])) { | ||
throw "OnLayoutChanged must be a ScriptBlock of UDEndpoint" | ||
} | ||
|
||
@{ | ||
type = "grid-layout" | ||
id = "grid-element-$Id" | ||
className = "layout" | ||
rowHeight = $RowHeight | ||
content = $Content.Invoke() | ||
layout = $Layout | ||
cols = $Columns | ||
breakpoints = $Breakpoints | ||
isDraggable = $Draggable.IsPresent | ||
isResizable = $Resizable.IsPresent | ||
persist = $Persist.IsPresent | ||
endpoint = $OnLayoutChanged.Name | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import React from 'react'; | ||
import { WidthProvider, Responsive } from "react-grid-layout"; | ||
const ResponsiveReactGridLayout = WidthProvider(Responsive); | ||
|
||
require('react-grid-layout/css/styles.css'); | ||
require('react-resizable/css/styles.css'); | ||
|
||
export default class UDGridLayout extends React.Component { | ||
|
||
constructor(props) { | ||
super(props); | ||
|
||
var layouts = this.props.layout.map(x => { | ||
x.i = "grid-element-" + x.i; | ||
return x; | ||
}); | ||
if (this.props.persist) { | ||
var jsonLayouts = getFromLS("layouts"); | ||
if (jsonLayouts != null) { | ||
layouts = JSON.parse(JSON.stringify(jsonLayouts)) | ||
} | ||
}; | ||
|
||
this.state = { | ||
layouts | ||
}; | ||
} | ||
|
||
onLayoutChange(layout, layouts) { | ||
|
||
if (this.props.endpoint) { | ||
UniversalDashboard.publish('element-event', { | ||
type: "clientEvent", | ||
eventId: this.props.endpoint, | ||
eventName: 'onLayoutChanged', | ||
eventData: JSON.stringify(layouts) | ||
}); | ||
} | ||
|
||
if (this.props.persist) { | ||
saveToLS("layouts", layouts); | ||
this.setState({ layouts }); | ||
} | ||
} | ||
|
||
render() { | ||
|
||
var elements = this.props.content.map(x => | ||
<div key={"grid-element-" + x.id}> | ||
{UniversalDashboard.renderComponent(x)} | ||
</div> | ||
); | ||
|
||
return ( | ||
<ResponsiveReactGridLayout className={this.props.className} layouts={this.state.layouts} cols={this.props.cols} rowHeight={this.props.rowHeight} onLayoutChange={(layout, layouts) => | ||
this.onLayoutChange(layout, layouts) | ||
}> | ||
{elements} | ||
</ResponsiveReactGridLayout> | ||
) | ||
} | ||
} | ||
|
||
function getFromLS(key) { | ||
let ls = {}; | ||
if (global.localStorage) { | ||
try { | ||
ls = JSON.parse(global.localStorage.getItem("rgl-8")) || {}; | ||
} catch (e) { | ||
/*Ignore*/ | ||
} | ||
} | ||
return ls[key]; | ||
} | ||
|
||
function saveToLS(key, value) { | ||
if (global.localStorage) { | ||
global.localStorage.setItem( | ||
"rgl-8", | ||
JSON.stringify({ | ||
[key]: value | ||
}) | ||
); | ||
} | ||
} |