Modules/UniversalDashboard.Materialize/Scripts/treeview.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
    )

    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
        }
    }
}