From f3559b3fae3e116b78314b5fb9730d9e3d2cbfe3 Mon Sep 17 00:00:00 2001 From: Adam Driscoll Date: Sun, 8 Sep 2019 13:45:29 -0600 Subject: [PATCH 1/3] Fixes #1086 --- .../Components/ud-treeview.jsx | 26 +++++- .../Scripts/treeview.ps1 | 86 +++++++++++++++++++ .../Tests/treeview.tests.ps1 | 15 ++++ src/UniversalDashboard.Materialize/build.ps1 | 2 + .../Integration/TreeView.Tests.ps1 | 51 ----------- .../Manifest.Tests.ps1 | 2 +- .../Cmdlets/NewTreeNodeCommand.cs | 35 -------- .../Cmdlets/NewTreeViewCommand.cs | 63 -------------- .../New-UDModuleManifest.ps1 | 8 +- 9 files changed, 130 insertions(+), 158 deletions(-) create mode 100644 src/UniversalDashboard.Materialize/Scripts/treeview.ps1 delete mode 100644 src/UniversalDashboard.UITest/Integration/TreeView.Tests.ps1 delete mode 100644 src/UniversalDashboard/Cmdlets/NewTreeNodeCommand.cs delete mode 100644 src/UniversalDashboard/Cmdlets/NewTreeViewCommand.cs diff --git a/src/UniversalDashboard.Materialize/Components/ud-treeview.jsx b/src/UniversalDashboard.Materialize/Components/ud-treeview.jsx index 0cc92d8a..a998df0c 100644 --- a/src/UniversalDashboard.Materialize/Components/ud-treeview.jsx +++ b/src/UniversalDashboard.Materialize/Components/ud-treeview.jsx @@ -13,9 +13,19 @@ export default class UDTreeView extends React.Component { onToggle(node, toggled) { - if (this.props.hasCallback) { UniversalDashboard.post('/api/internal/component/element/' + this.props.id, { nodeId: node.id}, function(data) { + + if (data.error) { + UniversalDashboard.toaster.error(data.error); + return; + } + + if (node.leaf) + { + return; + } + node.children = data; if(this.state.cursor){this.state.cursor.active = false;} @@ -30,6 +40,11 @@ export default class UDTreeView extends React.Component { }.bind(this)) } else { + if (node.leaf) + { + return; + } + if(this.state.cursor){this.state.cursor.active = false;} node.active = true; if(node.children){ @@ -37,19 +52,22 @@ export default class UDTreeView extends React.Component { } this.setState({ cursor: node }) } - - } render(){ decorators.Header = ({style, node}) => { const iconType = node.icon; + var expandedIconType = node.expandedIcon; const iconStyle = {marginRight: '5px'}; + if (!expandedIconType) { + expandedIconType = iconType; + } + var icon = UniversalDashboard.renderComponent({ type: 'icon', - icon: iconType, + icon: node.toggled ? expandedIconType : iconType, style: iconStyle }) diff --git a/src/UniversalDashboard.Materialize/Scripts/treeview.ps1 b/src/UniversalDashboard.Materialize/Scripts/treeview.ps1 new file mode 100644 index 00000000..babd4ab9 --- /dev/null +++ b/src/UniversalDashboard.Materialize/Scripts/treeview.ps1 @@ -0,0 +1,86 @@ +function New-UDTreeView { + param( + [Parameter()] + [string]$Id = [Guid]::NewGuid(), + [Parameter(Mandatory)] + [Hashtable]$Node, + [Parameter()] + [object]$OnNodeClicked, + [Parameter()] + [DashboardColor]$BackgroundColor, + [Parameter()] + [DashboardColor]$FontColor = 'black', + [Parameter()] + [DashboardColor]$ActiveBackgroundColor = '0xDFE8E4', + [Parameter()] + [DashboardColor]$ToggleColor = 'black' + ) + + End { + if ($null -ne $OnNodeClicked) { + if ($OnNodeClicked -is [scriptblock]) { + $OnNodeClicked = New-UDEndpoint -Endpoint $OnNodeClicked -Id $Id + } + elseif ($OnNodeClicked -isnot [UniversalDashboard.Models.Endpoint]) { + throw "OnNodeClicked must be a script block or UDEndpoint" + } + } + + + @{ + assetId = $AssetId + isPlugin = $true + id = $Id + type = 'ud-treeview' + + node = $Node + hasCallback = $null -ne $OnNodeClicked + backgroundColor = $BackgroundColor.HtmlColor + fontColor = $FontColor.HtmlColor + activeBackgroundColor = $ActiveBackgroundColor.HtmlColor + toggleColor = $ToggleColor.HtmlColor + } + } +} + +function New-UDTreeNode { + param( + [Parameter(Mandatory, Position = 1)] + [string]$Name, + [Parameter()] + [string]$Id, + [Parameter()] + [ScriptBlock]$Children, + [Parameter()] + [UniversalDashboard.Models.FontAwesomeIcons]$Icon, + [Parameter()] + [UniversalDashboard.Models.FontAwesomeIcons]$ExpandedIcon, + [Parameter()] + [Switch]$Leaf + ) + + End { + if ($PSBoundParameters.ContainsKey("Icon")) { + $IconName = [UniversalDashboard.Models.FontAwesomeIconsExtensions]::GetIconName($Icon) + } + + if ($PSBoundParameters.ContainsKey("ExpandedIcon")) { + $ExpandedIconName = [UniversalDashboard.Models.FontAwesomeIconsExtensions]::GetIconName($ExpandedIcon) + } + + $ChildrenArray = $null + if ($PSBoundParameters.ContainsKey("Children")) + { + $ChildrenArray = & $Children + } + + @{ + name = $Name + id = $Id + children = $ChildrenArray + icon = $IconName + expandedIcon = $ExpandedIconName + leaf = $leaf.IsPresent + } + } +} \ No newline at end of file diff --git a/src/UniversalDashboard.Materialize/Tests/treeview.tests.ps1 b/src/UniversalDashboard.Materialize/Tests/treeview.tests.ps1 index 94cb6ee4..cef7bf09 100644 --- a/src/UniversalDashboard.Materialize/Tests/treeview.tests.ps1 +++ b/src/UniversalDashboard.Materialize/Tests/treeview.tests.ps1 @@ -21,8 +21,21 @@ Describe "New-UDTreeView" { } } } + + $Node = New-UDTreeNode -Name "Treeview" -Id "treeview" -Icon folder -ExpandedIcon folder_open + New-UDTreeView -Node $Node -OnNodeClicked { + param($Body) + $Obj = $Body | ConvertFrom-Json + + if ($Obj.NodeId -eq 'treeview') + { + New-UDTreeNode -Name 'Level 2' -Id 'level2' -Icon file -Leaf + } + } } + Wait-Debugger + It "expands dynamically" { Find-SeElement -Id 'domain' -Driver $Driver | Invoke-SeClick Start-Sleep 1 @@ -30,6 +43,8 @@ Describe "New-UDTreeView" { Start-Sleep 1 Find-SeElement -Id '11' -Driver $Driver | Invoke-SeClick } + + } } diff --git a/src/UniversalDashboard.Materialize/build.ps1 b/src/UniversalDashboard.Materialize/build.ps1 index b5464b37..d62f252c 100644 --- a/src/UniversalDashboard.Materialize/build.ps1 +++ b/src/UniversalDashboard.Materialize/build.ps1 @@ -64,6 +64,8 @@ $manifestParameters = @{ "New-UDTabContainer" "New-UDTable", "New-UDGridLayout" + "New-UDTreeView" + "New-UDTreeNode" ) RequiredModules = @() } diff --git a/src/UniversalDashboard.UITest/Integration/TreeView.Tests.ps1 b/src/UniversalDashboard.UITest/Integration/TreeView.Tests.ps1 deleted file mode 100644 index d772d8e2..00000000 --- a/src/UniversalDashboard.UITest/Integration/TreeView.Tests.ps1 +++ /dev/null @@ -1,51 +0,0 @@ -param([Switch]$Release) - -Import-Module "$PSScriptRoot\..\TestFramework.psm1" -Force -$ModulePath = Get-ModulePath -Release:$Release -$BrowserPort = Get-BrowserPort -Release:$Release - -Import-Module $ModulePath -Force - -Get-UDDashboard | Stop-UDDashboard -Describe "TreeView" { - Context "blank tree view" { - $Dashboard = New-UdDashboard -Title "Tree View" -Content { - New-UDRow -Columns { - New-UDColumn -Size 3 -Content { - $Root = New-UDTreeNode -Name '1' -Id '1' - New-UDTreeView -ActiveBackgroundColor '#DFE8E4' -Node $Root -OnNodeClicked { - param($Body) - - $Obj = $Body | ConvertFrom-Json - $Depth = ([int]$Obj.NodeId) + 1 - - New-UDTreeNode -Name ($Depth).ToString() -Id ($Depth).ToString() - } - } - } - } - - $Server = Start-UDDashboard -Port 10001 -Dashboard $dashboard - $Driver = Start-SeFirefox - Enter-SeUrl -Driver $Driver -Url "http://localhost:$BrowserPort" - Start-Sleep 2 - - It "should expand dynamically" { - 1..5 | % { - $Element = Find-SeElement -Id $_ -Driver $Driver - $Element | Should not be $null - Invoke-SeClick $Element - Start-Sleep 1 - } - } - - Stop-SeDriver $Driver - Stop-UDDashboard -Server $Server - } -} - - - - - - diff --git a/src/UniversalDashboard.UITest/Manifest.Tests.ps1 b/src/UniversalDashboard.UITest/Manifest.Tests.ps1 index 91adb0fa..3244dcca 100644 --- a/src/UniversalDashboard.UITest/Manifest.Tests.ps1 +++ b/src/UniversalDashboard.UITest/Manifest.Tests.ps1 @@ -11,7 +11,7 @@ Get-UDDashboard | Stop-UDDashboard Describe "Manifest" { It "should have correct version" { - (Get-Module 'UniversalDashboard.Community').Version | Should be "2.5.3" + (Get-Module 'UniversalDashboard.Community').Version | Should be "2.6.0" } It "should have correct exported commands" { diff --git a/src/UniversalDashboard/Cmdlets/NewTreeNodeCommand.cs b/src/UniversalDashboard/Cmdlets/NewTreeNodeCommand.cs deleted file mode 100644 index e57bce60..00000000 --- a/src/UniversalDashboard/Cmdlets/NewTreeNodeCommand.cs +++ /dev/null @@ -1,35 +0,0 @@ -using Newtonsoft.Json; -using NLog; -using UniversalDashboard.Models; -using System.Management.Automation; -using System.Collections; -using UniversalDashboard.Services; -using System.Linq; -using System; - -namespace UniversalDashboard.Cmdlets -{ - [Cmdlet(VerbsCommon.New, "UDTreeNode")] - public class NewTreeNodeCommand : PSCmdlet - { - private readonly Logger Log = LogManager.GetLogger(nameof(NewTreeNodeCommand)); - - [Parameter(Mandatory = true, Position = 1)] - public string Name { get; set; } - [Parameter()] - public string Id { get; set; } - [Parameter] - public ScriptBlock Children { get; set; } - [Parameter] - public FontAwesomeIcons Icon { get; set; } - protected override void EndProcessing() - { - WriteObject(new TreeNode { - Id = Id, - Name = Name, - Children = Children?.Invoke().Select(m => m.BaseObject).Cast(), - Icon = Icon.GetIconName() - }); - } - } -} diff --git a/src/UniversalDashboard/Cmdlets/NewTreeViewCommand.cs b/src/UniversalDashboard/Cmdlets/NewTreeViewCommand.cs deleted file mode 100644 index 3d268c59..00000000 --- a/src/UniversalDashboard/Cmdlets/NewTreeViewCommand.cs +++ /dev/null @@ -1,63 +0,0 @@ -using Newtonsoft.Json; -using NLog; -using UniversalDashboard.Models; -using System.Management.Automation; -using System.Collections; -using UniversalDashboard.Services; -using System.Linq; -using System.Drawing; - -namespace UniversalDashboard.Cmdlets -{ - [Cmdlet(VerbsCommon.New, "UDTreeView")] - public class NewTreeViewCommand : CallbackCmdlet - { - private readonly Logger Log = LogManager.GetLogger(nameof(NewTreeNodeCommand)); - - [Parameter(Mandatory = true)] - public TreeNode Node { get; set; } - - [Parameter] - public object OnNodeClicked { get; set; } - - [Parameter] - public DashboardColor BackgroundColor { get; set; } = new DashboardColor(Color.White); - - [Parameter] - public DashboardColor FontColor { get; set; } = new DashboardColor(Color.Black); - - [Parameter] - public DashboardColor ActiveBackgroundColor { get; set; } = new DashboardColor(0xDFE8E4); - - [Parameter] - public DashboardColor ToggleColor { get; set; } = new DashboardColor(Color.Black); - - protected override void EndProcessing() - { - Endpoint callback = null; - - var scriptBlock = OnNodeClicked as ScriptBlock; - if (scriptBlock != null) - { - callback = GenerateCallback(Id, scriptBlock, null); - } - else - { - var psobject = OnNodeClicked as PSObject; - callback = psobject?.BaseObject as Endpoint; - - } - - WriteObject(new TreeView - { - Id = Id, - Callback = callback, - Node = Node, - ToggleColor = ToggleColor?.HtmlColor, - BackgroundColor = BackgroundColor?.HtmlColor, - FontColor = FontColor?.HtmlColor, - ActiveBackgroundColor = ActiveBackgroundColor?.HtmlColor - }); - } - } -} diff --git a/src/UniversalDashboard/New-UDModuleManifest.ps1 b/src/UniversalDashboard/New-UDModuleManifest.ps1 index 5221fa78..54fd60be 100644 --- a/src/UniversalDashboard/New-UDModuleManifest.ps1 +++ b/src/UniversalDashboard/New-UDModuleManifest.ps1 @@ -5,11 +5,11 @@ param( Remove-Item (Join-Path $outputDirectory 'UniversalDashboard.Community.psd1') -ErrorAction SilentlyContinue -Force -$version = "2.5.3" +$version = "2.6.0" #$prerelease = "-beta2" $ReleaseNotes = " - For release notes, see: https://docs.universaldashboard.io/updates/v2.5.3 + For release notes, see: https://docs.universaldashboard.io/updates/v2.6.0 " $manifestParameters = @{ @@ -95,6 +95,8 @@ $manifestParameters = @{ "New-UDImageCarousel" "New-UDImageCarouselItem" "New-UDSplitPane" + "New-UDTreeNode" + "New-UDTreeView" #Material UI 'New-UDMuAvatar' @@ -152,8 +154,6 @@ $manifestParameters = @{ "Hide-UDModal", "Hide-UDToast" "Publish-UDFolder" - "New-UDTreeNode" - "New-UDTreeView" "New-UDEndpointInitialization" "New-UDSideNav" "New-UDSideNavItem" From 9718e855abb9b746a75caf0a64336a9918f716ad Mon Sep 17 00:00:00 2001 From: Adam Driscoll Date: Sun, 8 Sep 2019 13:46:06 -0600 Subject: [PATCH 2/3] Tests --- src/UniversalDashboard.Materialize/Tests/treeview.tests.ps1 | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/UniversalDashboard.Materialize/Tests/treeview.tests.ps1 b/src/UniversalDashboard.Materialize/Tests/treeview.tests.ps1 index cef7bf09..53c46972 100644 --- a/src/UniversalDashboard.Materialize/Tests/treeview.tests.ps1 +++ b/src/UniversalDashboard.Materialize/Tests/treeview.tests.ps1 @@ -34,8 +34,6 @@ Describe "New-UDTreeView" { } } - Wait-Debugger - It "expands dynamically" { Find-SeElement -Id 'domain' -Driver $Driver | Invoke-SeClick Start-Sleep 1 From 4a76e12367d65a324b2a7b7490d23c2b3e0f343b Mon Sep 17 00:00:00 2001 From: Adam Driscoll Date: Sun, 8 Sep 2019 13:57:18 -0600 Subject: [PATCH 3/3] Remove -Leaf --- .../Components/ud-treeview.jsx | 9 ++------- src/UniversalDashboard.Materialize/Scripts/treeview.ps1 | 5 +---- .../Tests/treeview.tests.ps1 | 2 +- 3 files changed, 4 insertions(+), 12 deletions(-) diff --git a/src/UniversalDashboard.Materialize/Components/ud-treeview.jsx b/src/UniversalDashboard.Materialize/Components/ud-treeview.jsx index a998df0c..b0b40140 100644 --- a/src/UniversalDashboard.Materialize/Components/ud-treeview.jsx +++ b/src/UniversalDashboard.Materialize/Components/ud-treeview.jsx @@ -21,10 +21,10 @@ export default class UDTreeView extends React.Component { return; } - if (node.leaf) + if (!data || data.length === 0) { return; - } + } node.children = data; if(this.state.cursor){this.state.cursor.active = false;} @@ -40,11 +40,6 @@ export default class UDTreeView extends React.Component { }.bind(this)) } else { - if (node.leaf) - { - return; - } - if(this.state.cursor){this.state.cursor.active = false;} node.active = true; if(node.children){ diff --git a/src/UniversalDashboard.Materialize/Scripts/treeview.ps1 b/src/UniversalDashboard.Materialize/Scripts/treeview.ps1 index babd4ab9..8e84734a 100644 --- a/src/UniversalDashboard.Materialize/Scripts/treeview.ps1 +++ b/src/UniversalDashboard.Materialize/Scripts/treeview.ps1 @@ -54,9 +54,7 @@ function New-UDTreeNode { [Parameter()] [UniversalDashboard.Models.FontAwesomeIcons]$Icon, [Parameter()] - [UniversalDashboard.Models.FontAwesomeIcons]$ExpandedIcon, - [Parameter()] - [Switch]$Leaf + [UniversalDashboard.Models.FontAwesomeIcons]$ExpandedIcon ) End { @@ -80,7 +78,6 @@ function New-UDTreeNode { children = $ChildrenArray icon = $IconName expandedIcon = $ExpandedIconName - leaf = $leaf.IsPresent } } } \ No newline at end of file diff --git a/src/UniversalDashboard.Materialize/Tests/treeview.tests.ps1 b/src/UniversalDashboard.Materialize/Tests/treeview.tests.ps1 index 53c46972..4449171c 100644 --- a/src/UniversalDashboard.Materialize/Tests/treeview.tests.ps1 +++ b/src/UniversalDashboard.Materialize/Tests/treeview.tests.ps1 @@ -29,7 +29,7 @@ Describe "New-UDTreeView" { if ($Obj.NodeId -eq 'treeview') { - New-UDTreeNode -Name 'Level 2' -Id 'level2' -Icon file -Leaf + New-UDTreeNode -Name 'Level 2' -Id 'level2' -Icon file } } }