Modules/UniversalDashboard.MaterialUI/Scripts/list.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
function New-UDMuList {
    param(
        [Parameter ()][string]$Id = ([Guid]::NewGuid()).ToString(),
        [Parameter ()][scriptblock]$Content,
        [Parameter ()][string]$SubHeader,
        [Parameter ()][Hashtable]$Style
    )
    End
    {
        @{
            type = 'mu-list'
            isPlugin = $true
            assetId = $MUAssetId

            id = $Id
            content = $Content.Invoke()
            subHeader = $SubHeader
            style = $Style
        }
    }
}


function New-UDMuListItem {
    [CmdletBinding()]
    param(
        [Parameter ()][string]$Id = ([Guid]::NewGuid()).ToString(),
        [Parameter ()][ValidateSet("Icon","Avatar")][string]$AvatarType,
        [Parameter ()][object]$OnClick, 
        [Parameter ()][switch]$IsButton, 
        [Parameter ()][string]$Label, 
        [Parameter ()][scriptblock]$Content, 
        [Parameter ()][switch]$IsEndPoint, 
        [Parameter ()][switch]$AutoRefresh,
        [Parameter ()][int]$RefreshInterval = 5,
        [Parameter ()][string]$SubTitle,
        [Parameter ()][PSTypeName('UniversalDashboard.MaterialUI.Icon')]$Icon,
        [Parameter ()][string]$Source,
        [Parameter ()][scriptblock]$SecondaryAction,
        [Parameter ()][Hashtable]$LabelStyle,
        [Parameter ()][Hashtable]$Style
    )
    # DynamicParam {
        
    # $paramDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary

    # if ($AvatarType -eq "Icon") {
    # #create a new ParameterAttribute Object
    # $IconAttribute = New-Object System.Management.Automation.ParameterAttribute
    # $IconAttribute.Mandatory = $true
    # $IconAttribute.HelpMessage = "Use New-UDIcon to create new icon"
    # $attributeCollection = new-object System.Collections.ObjectModel.Collection[System.Attribute]
    # #add our custom attribute
    # $attributeCollection.Add($IconAttribute)
    # #add our paramater specifying the attribute collection
    # $IconParam = New-Object System.Management.Automation.RuntimeDefinedParameter('Icon', [object], $attributeCollection)
    # $paramDictionary.Add('Icon', $IconParam)

    # }
    # elseif($AvatarType -eq "Avatar"){
    # #create a new ParameterAttribute Object
    # $AvatarAttribute = New-Object System.Management.Automation.ParameterAttribute
    # $AvatarAttribute.Mandatory = $true
    # $AvatarAttribute.HelpMessage = "Enter the path to the avatar image it can be local or url"
    # $AvatarCollection = new-object System.Collections.ObjectModel.Collection[System.Attribute]

    # #add our custom attribute
    # $AvatarCollection.Add($AvatarAttribute)
    # #add our paramater specifying the attribute collection
    # $AvatarParam = New-Object System.Management.Automation.RuntimeDefinedParameter('Source', [string], $AvatarCollection)
    # $paramDictionary.Add('Source', $AvatarParam)
    # }
    # return $paramDictionary
    # }
    begin{}
    Process{}
    End 
    {
        if ($null -ne $OnClick) {
            if ($OnClick -is [scriptblock]) {
                $OnClick = New-UDEndpoint -Endpoint $OnClick -Id ($Id + "onClick")
            }
            elseif ($OnClick -isnot [UniversalDashboard.Models.Endpoint]) {
                throw "OnClick must be a script block or UDEndpoint"
            }else{
                $OnClick = $null
            }
        }

        if($IsEndPoint){
            $EndPoint = New-UDEndpoint -Endpoint $Content -Id $Id
        }

        if($null -ne $Content){
            $CardContent = $Content.Invoke()
        }else{
            $CardContent = $null
        }

        if($null -ne $SecondaryAction){
            $Action = $SecondaryAction.Invoke()
        }else{
            $Action = $null
        }
        
        @{
            type = 'mu-list-item'
            isPlugin = $true
            assetId = $MUAssetId

            id = $Id
            subTitle = $SubTitle
            label = $Label
            onClick = $OnClick
            content = $CardContent
            secondaryAction = $Action
            icon =  $Icon
            source = $Source
            avatarType = $AvatarType
            isButton = $IsButton.IsPresent
            isEndpoint = $IsEndPoint.IsPresent
            refreshInterval = $RefreshInterval
            autoRefresh = $AutoRefresh.IsPresent
            labelStyle = $LabelStyle
            style = $Style
        }
    }
}