Public/Elements.ps1
function New-PodeWebTextbox { [CmdletBinding(DefaultParameterSetName='Single')] param( [Parameter(Mandatory=$true)] [string] $Name, [Parameter()] [string] $Id, [Parameter(ParameterSetName='Single')] [ValidateSet('Text', 'Email', 'Password', 'Number', 'Date', 'Time', 'File')] [string] $Type ='Text', [Parameter()] [string] $Placeholder, [Parameter(ParameterSetName='Multi')] [int] $Height = 4, [Parameter()] [string] $HelpText, [Parameter(ParameterSetName='Single')] [string] $PrependText, [Parameter(ParameterSetName='Single')] [string] $PrependIcon, [Parameter()] [string] $Value, [Parameter(ParameterSetName='Single')] [scriptblock] $AutoComplete, [Parameter(ParameterSetName='Multi')] [switch] $Multiline, [switch] $Preformat, [switch] $ReadOnly, [Parameter(ParameterSetName='Single')] [Alias('NoAuth')] [switch] $NoAuthentication ) $Id = Get-PodeWebElementId -Tag Txt -Id $Id -Name $Name if ($Height -le 0) { $Height = 4 } $auth = $null if (!$NoAuthentication) { $auth = (Get-PodeWebState -Name 'auth') } $element = @{ ElementType = 'Textbox' Component = $ComponentData Name = $Name ID = $Id Type = $Type Multiline = $Multiline.IsPresent Placeholder = $Placeholder Height = $Height Preformat = $Preformat.IsPresent HelpText = $HelpText ReadOnly = $ReadOnly.IsPresent IsAutoComplete = ($null -ne $AutoComplete) Value = $Value Prepend = @{ Enabled = (![string]::IsNullOrWhiteSpace($PrependText) -or ![string]::IsNullOrWhiteSpace($PrependIcon)) Text = $PrependText Icon = $PrependIcon } } if ($null -ne $AutoComplete) { Add-PodeRoute -Method Post -Path "/elements/autocomplete/$($Id)" -Authentication $auth -ScriptBlock { $global:ElementData = $using:element $global:InputData = $WebEvent.Data $result = Invoke-PodeScriptBlock -ScriptBlock $using:AutoComplete -Return if ($null -eq $result) { $result = @() } Write-PodeJsonResponse -Value @{ Values = $result } } } return $element } function New-PodeWebFileUpload { [CmdletBinding()] param( [Parameter(Mandatory=$true)] [string] $Name, [Parameter()] [string] $Id ) $Id = Get-PodeWebElementId -Tag File -Id $Id -Name $Name return @{ ElementType = 'FileUpload' Component = $ComponentData Name = $Name ID = $Id } } function New-PodeWebParagraph { [CmdletBinding()] param( [Parameter()] [string] $Id, [Parameter(Mandatory=$true, ParameterSetName='Value')] [string] $Value, [Parameter(Mandatory=$true, ParameterSetName='Elements')] [hashtable[]] $Elements ) $Id = Get-PodeWebElementId -Tag Para -Id $Id return @{ ElementType = 'Paragraph' Component = $ComponentData ID = $Id Value = $Value Elements = $Elements } } function New-PodeWebCodeBlock { [CmdletBinding()] param( [Parameter()] [string] $Id, [Parameter(Mandatory=$true)] [string] $Value, [Parameter()] [string] $Language = [string]::Empty, [switch] $Scrollable, [switch] $NoHighlight ) # id $Id = Get-PodeWebElementId -Tag Codeblock -Id $Id # language if ($NoHighlight) { $Language = 'plaintext' } return @{ ElementType = 'CodeBlock' Component = $ComponentData ID = $Id Value = $Value Language = $Language.ToLowerInvariant() Scrollable = $Scrollable.IsPresent } } function New-PodeWebCode { [CmdletBinding()] param( [Parameter()] [string] $Id, [Parameter(Mandatory=$true)] [string] $Value ) $Id = Get-PodeWebElementId -Tag Code -Id $Id return @{ ElementType = 'Code' Component = $ComponentData ID = $Id Value = $Value } } function New-PodeWebCheckbox { [CmdletBinding()] param( [Parameter(Mandatory=$true)] [string] $Name, [Parameter()] [string] $Id, [Parameter(ParameterSetName='Multiple')] [string[]] $Options, [Parameter(ParameterSetName='Multiple')] [switch] $Inline, [switch] $AsSwitch, [switch] $Checked, [switch] $Disabled ) $Id = Get-PodeWebElementId -Tag Chkbox -Id $Id -Name $Name if (($null -eq $Options) -or ($Options.Length -eq 0)) { $Options = @('true') } return @{ ElementType = 'Checkbox' Component = $ComponentData Name = $Name ID = $Id Options = @($Options) Inline = $Inline.IsPresent AsSwitch = $AsSwitch.IsPresent Checked = $Checked.IsPresent Disabled = $Disabled.IsPresent } } function New-PodeWebRadio { [CmdletBinding()] param( [Parameter(Mandatory=$true)] [string] $Name, [Parameter()] [string] $Id, [Parameter(Mandatory=$true)] [string[]] $Options, [switch] $Inline, [switch] $Disabled ) $Id = Get-PodeWebElementId -Tag Radio -Id $Id -Name $Name return @{ ElementType = 'Radio' Component = $ComponentData Name = $Name ID = $Id Options = @($Options) Inline = $Inline.IsPresent Disabled = $Disabled.IsPresent } } function New-PodeWebSelect { [CmdletBinding()] param( [Parameter(Mandatory=$true)] [string] $Name, [Parameter()] [string] $Id, [Parameter()] [string[]] $Options, [Parameter()] [string] $SelectedValue, [switch] $Multiple ) if (Test-PodeIsEmpty $Options) { throw "Select options are required" } $Id = Get-PodeWebElementId -Tag Select -Id $Id -Name $Name return @{ ElementType = 'Select' Component = $ComponentData Name = $Name ID = $Id Options = @($Options) SelectedValue = $SelectedValue Multiple = $Multiple.IsPresent } } function New-PodeWebRange { [CmdletBinding()] param( [Parameter(Mandatory=$true)] [string] $Name, [Parameter()] [string] $Id, [Parameter()] [int] $Value = 0, [Parameter()] [int] $Min = 0, [Parameter()] [int] $Max = 100, [switch] $Disabled, [switch] $ShowValue ) $Id = Get-PodeWebElementId -Tag Range -Id $Id -Name $Name if ($Value -lt $Min) { $Value = $Min } if ($Value -gt $Max) { $Value = $Max } return @{ ElementType = 'Range' Component = $ComponentData Name = $Name ID = $Id Value = $Value Min = $Min Max = $Max Disabled = $Disabled.IsPresent ShowValue = $ShowValue.IsPresent } } function New-PodeWebProgress { [CmdletBinding()] param( [Parameter()] [string] $Name, [Parameter()] [string] $Id, [Parameter()] [int] $Value = 0, [Parameter()] [int] $Min = 0, [Parameter()] [int] $Max = 100, [Parameter()] [ValidateSet('Blue', 'Grey', 'Green', 'Red', 'Yellow', 'Cyan', 'Light', 'Dark')] [string] $Colour = 'Blue', [switch] $ShowValue, [switch] $Striped, [switch] $Animated ) $Id = Get-PodeWebElementId -Tag Progress -Id $Id $colourType = Convert-PodeWebColourToClass -Colour $Colour if ($Value -lt $Min) { $Value = $Min } if ($Value -gt $Max) { $Value = $Max } $percentage = 0 if ($Value -gt 0) { $percentage = ($Value / $Max) * 100.0 } return @{ ElementType = 'Progress' Component = $ComponentData Name = $Name ID = $Id Value = $Value Min = $Min Max = $Max Percentage = $percentage ShowValue = $ShowValue.IsPresent Striped = ($Striped.IsPresent -or $Animated.IsPresent) Animated = $Animated.IsPresent Colour = $Colour ColourType = $ColourType } } function New-PodeWebImage { [CmdletBinding()] param( [Parameter()] [string] $Id, [Parameter(Mandatory=$true)] [string] $Source, [Parameter()] [string] $Alt, [Parameter()] [ValidateSet('Left', 'Right', 'Center')] [string] $Location = 'Left', [Parameter()] [int] $Height = 0, [Parameter()] [int] $Width = 0 ) $Id = Get-PodeWebElementId -Tag Img -Id $Id if ($Height -lt 0) { $Height = 0 } if ($Width -lt 0) { $Width = 0 } return @{ ElementType = 'Image' Component = $ComponentData ID = $Id Source = $Source Alt = $Alt Location = $Location Height = $Height Width = $Width } } function New-PodeWebHeader { [CmdletBinding()] param( [Parameter()] [string] $Id, [Parameter(Mandatory=$true)] [ValidateSet(1, 2, 3, 4, 5, 6)] [int] $Size, [Parameter(Mandatory=$true)] [string] $Value, [Parameter()] [string] $Secondary ) $Id = Get-PodeWebElementId -Tag Header -Id $Id return @{ ElementType = 'Header' Component = $ComponentData ID = $Id Size = $Size Value = $Value Secondary = $Secondary } } function New-PodeWebQuote { [CmdletBinding()] param( [Parameter()] [string] $Id, [Parameter()] [ValidateSet('Left', 'Right', 'Center')] [string] $Location, [Parameter(Mandatory=$true)] [string] $Value, [Parameter()] [string] $Source ) $Id = Get-PodeWebElementId -Tag Quote -Id $Id return @{ ElementType = 'Quote' Component = $ComponentData ID = $Id Location = $Location Value = $Value Source = $Source } } function New-PodeWebList { [CmdletBinding()] param( [Parameter()] [string] $Id, [Parameter(Mandatory=$true)] [string[]] $Items, [switch] $Numbered ) $Id = Get-PodeWebElementId -Tag List -Id $Id return @{ ElementType = 'List' Component = $ComponentData ID = $Id Items = $Items Numbered = $Numbered.IsPresent } } function New-PodeWebLink { [CmdletBinding()] param( [Parameter()] [string] $Id, [Parameter(Mandatory=$true)] [string] $Source, [Parameter(Mandatory=$true)] [string] $Value, [switch] $NewTab ) $Id = Get-PodeWebElementId -Tag A -Id $Id return @{ ElementType = 'Link' Component = $ComponentData ID = $Id Source = $Source Value = $Value Newtab = $NewTab.IsPresent } } function New-PodeWebText { [CmdletBinding()] param( [Parameter()] [string] $Id, [Parameter()] [string] $Value, [Parameter()] [ValidateSet('Normal', 'Underlined', 'StrikeThrough', 'Deleted', 'Inserted', 'Italics', 'Bold', 'Small')] [string] $Style = 'Normal' ) return @{ ElementType = 'Text' Component = $ComponentData ID = $Id Value = $Value Style = $Style } } function New-PodeWebHidden { [CmdletBinding()] param( [Parameter(Mandatory=$true)] [string] $Name, [Parameter()] [string] $Id, [Parameter(Mandatory=$true)] [string] $Value ) $Id = Get-PodeWebElementId -Tag Hidden -Id $Id -Name $Name return @{ ElementType = 'Hidden' Component = $ComponentData Name = $Name ID = $Id Value = $Value } } function New-PodeWebCredential { [CmdletBinding()] param( [Parameter(Mandatory=$true)] [string] $Name, [Parameter()] [string] $Id, [Parameter()] [string] $HelpText, [switch] $ReadOnly ) $Id = Get-PodeWebElementId -Tag Cred -Id $Id -Name $Name return @{ ElementType = 'Credential' Component = $ComponentData Name = $Name ID = $Id HelpText = $HelpText ReadOnly = $ReadOnly.IsPresent } } function New-PodeWebRaw { [CmdletBinding()] param( [Parameter(Mandatory=$true)] [string] $Value ) return @{ ElementType = 'Raw' Component = $ComponentData Value = $Value } } function New-PodeWebButton { [CmdletBinding()] param( [Parameter(Mandatory=$true)] [string] $Name, [Parameter()] [string] $Id, [Parameter()] [string] $DataValue, [Parameter()] [string] $Icon, [Parameter(Mandatory=$true)] [scriptblock] $ScriptBlock, [Parameter()] [ValidateSet('Blue', 'Grey', 'Green', 'Red', 'Yellow', 'Cyan', 'Light', 'Dark')] [string] $Colour = 'Blue', [Parameter()] [Alias('NoAuth')] [switch] $NoAuthentication, [switch] $IconOnly ) $Id = Get-PodeWebElementId -Tag Btn -Id $Id -Name $Name $colourType = Convert-PodeWebColourToClass -Colour $Colour $auth = $null if (!$NoAuthentication) { $auth = (Get-PodeWebState -Name 'auth') } $element = @{ ElementType = 'Button' Component = $ComponentData Name = $Name ID = $Id DataValue = $DataValue Icon = $Icon IconOnly = $IconOnly.IsPresent Colour = $Colour ColourType = $ColourType } $routePath = "/elements/button/$($Id)" Remove-PodeRoute -Method Post -Path $routePath Add-PodeRoute -Method Post -Path $routePath -Authentication $auth -ScriptBlock { $global:ElementData = $using:element $global:InputData = $WebEvent.Data $result = Invoke-PodeScriptBlock -ScriptBlock $using:ScriptBlock -Return if ($null -eq $result) { $result = @() } Write-PodeJsonResponse -Value $result } return $element } function New-PodeWebAlert { [CmdletBinding()] param( [Parameter()] [string] $Id, [Parameter()] [ValidateSet('Note', 'Tip', 'Important', 'Info', 'Warning', 'Error')] [string] $Type = 'Note', [Parameter(Mandatory=$true, ParameterSetName='Value')] [string] $Value, [Parameter(Mandatory=$true, ParameterSetName='Elements')] [hashtable[]] $Elements ) $Id = Get-PodeWebElementId -Tag Alert -Id $Id $classType = Convert-PodeWebAlertTypeToClass -Type $Type $iconType = Convert-PodeWebAlertTypeToIcon -Type $Type return @{ ElementType = 'Alert' Component = $ComponentData ID = $Id Type = $Type ClassType = $classType IconType = $iconType Value = $Value Elements = $Elements } } function New-PodeWebIcon { [CmdletBinding()] param( [Parameter(Mandatory=$true)] [string] $Name ) return @{ ElementType = 'Icon' Component = $ComponentData Name = $Name } } function New-PodeWebBadge { [CmdletBinding()] param( [Parameter()] [string] $Id, [Parameter()] [ValidateSet('Blue', 'Grey', 'Green', 'Red', 'Yellow', 'Cyan', 'Light', 'Dark')] [string] $Colour = 'Blue', [Parameter(Mandatory=$true)] [string] $Value ) $Id = Get-PodeWebElementId -Tag Alert -Id $Id $colourType = Convert-PodeWebColourToClass -Colour $Colour return @{ ElementType = 'Badge' Component = $ComponentData ID = $Id Colour = $Colour ColourType = $ColourType Value = $Value } } |