Scripts/New-UDWindow.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
<#
.SYNOPSIS
    Movable window in your Universal Dashboard.
.DESCRIPTION
    Movable window in your Universal Dashboard
.PARAMETER Id
    An id for the component default value will be generated by new-guid.
.PARAMETER Title
    Title in string-format for your Window.
.PARAMETER X
    X coordinate to set the initial position of your window. Default 0
.PARAMETER Y
    Y coordinate to set the initial position of your window. Default 0
.PARAMETER Content
    Content inside your Window in the form of a scriptblock.
.PARAMETER MinimizedSize
    Width of your Window (in pixels) when it is minimzed, note: this should be global across all Windows per page. Default is 200.
    Settings a wider width might allow more text in the title, but limit the max count of minimized windows per line on the "dock"
.PARAMETER onClose
    onClose endpoint / scriptblock that is triggered whenever the window is "closed". If nothing is presented the window will close without promting user for validation upon pressing the red button.
.EXAMPLE
    PS C:\> New-UDWindow -title "Hello" -content {new-udhtml -markup "<p>hello2</p>"}
    Makes a movable window, with the title "Hello", and content of "Hello2"
.INPUTS
    Inputs (if any)
.OUTPUTS
    Output (if any)
.NOTES
     
#>

function New-UDWindow {
    param(
        [Parameter()]
        [string]$Id = (New-Guid).ToString(),
        [Parameter()]
        [string]$Title,
        [Parameter()]
        [int]$X = 0,
        [Parameter()]
        [int]$Y = 0,
        [Parameter()]
        [scriptblock]$Content,
        # Minimized size in px
        [Parameter(
            Mandatory = $false
        )]
        [int]
        $MinimizedSize = 200,
        [Parameter()]
        [object]$OnClose
    )
    Begin {
        if ($null -ne $OnClose) {
            if ($OnClose -is [scriptblock]) {
                $OnClose = New-UDEndpoint -Endpoint $OnClose -Id $Id
            }
            elseif ($OnClose -isnot [UniversalDashboard.Models.Endpoint]) {
                throw "OnClick must be a script block or UDEndpoint"
            }
        }
    }

    End {

        @{
            # The AssetID of the main JS File
            assetId         = $AssetId
            # Tell UD this is a plugin
            isPlugin        = $true
            # This ID must be the same as the one used in the JavaScript to register the control with UD
            type            = "UD-Window"
            # An ID is mandatory
            id              = $Id

            # This is where you can put any other properties. They are passed to the React control's props
            # The keys are case-sensitive in JS.
            title           = $Title
            x               = $X
            y               = $Y
            content         = $Content.Invoke()
            minimizedSize   = $MinimizedSize
            onClose         = $onClose.Name
        }

    }
}