Modules/UniversalDashboard.Materialize/Scripts/collapsible.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
84
85
86
87
function New-UDCollapsible {
    param(
        [Parameter()]
        [String]$Id = (New-Guid),
        [Parameter(Mandatory = $true, Position = 0)]
        [ScriptBlock]$Items,
        [Parameter()]
        [UniversalDashboard.Models.DashboardColor]$BackgroundColor = 'White',
        [Parameter()]
        [UniversalDashboard.Models.DashboardColor]$FontColor = 'Black',
        [Parameter()]
        [Switch]$Popout,
        [Parameter()]
        [ValidateSet("Expandable", "Accordion")]
        [String]$Type = 'Expandable'
    )

    @{
        type = 'ud-collapsible'
        isPlugin = $true

        id = $id
        items = $Items.Invoke()
        backgroundColor = $BackgroundColor.HtmlColor
        color = $Color.HtmlColor
        popout = $Popout.IsPresent
        accordion = $Type -eq 'Accordion'
    }
}

function New-UDCollapsibleItem {
    [CmdletBinding(DefaultParameterSetName = "content")]
    param(
        [Parameter()]
        [String]$Id = (New-Guid),
        [Parameter()]
        [String]$Title,
        [Parameter()]
        [UniversalDashboard.Models.FontAwesomeIcons]$Icon,
        [Parameter(ParameterSetName = "content")]
        [ScriptBlock]$Content,
        [Parameter(ParameterSetName = "endpoint")]
        [object]$Endpoint,
        [Parameter(ParameterSetName = "endpoint")]
        [Switch]$AutoRefresh,
        [Parameter(ParameterSetName = "endpoint")]
        [int]$RefreshInterval = 5,
        [Parameter()]
        [Switch]$Active,
        [Parameter()]
        [UniversalDashboard.Models.DashboardColor]$BackgroundColor = 'White',
        [Parameter()]
        [UniversalDashboard.Models.DashboardColor]$FontColor = 'Black'
    )

    if ($PSCmdlet.ParameterSetName -eq 'Content') {
        $pContent = $Content.Invoke()
    }
    else {
        if ($null -ne $Endpoint) {
            if ($Endpoint -is [scriptblock]) {
                $Endpoint = New-UDEndpoint -Endpoint $Endpoint -Id $Id
            }
            elseif ($Endpoint -isnot [UniversalDashboard.Models.Endpoint]) {
                throw "Endpoint must be a script block or UDEndpoint"
            }
        }
    }

    $iconName = $null
    if ($PSBoundParameters.ContainsKey("Icon")) {
        $iconName = [UniversalDashboard.Models.FontAwesomeIconsExtensions]::GetIconName($Icon)
    }

    @{
        id = $Id 
        title = $Title 
        content = $pContent 
        endpoint = $Endpoint.Name 
        autoRefresh = $AutoRefresh.IsPresent
        refreshInterval = $RefreshInterval
        active = $Active.IsPresent
        backgroundColor = $BackgroundColor.HtmlColor
        color = $Color.HtmlColor
        icon = $iconName
    }
}