Pode.Web

repository·develop·Indexed 42 days ago

https://github.com/badgerati/pode.web

A web template framework for the Pode PowerShell web server that provides high-level components such as forms, tables, and charts. Version 1.X utilizes client-side JavaScript rendering for improved performance and dynamism. It bundles libraries including Bootstrap, Chart.js, jQuery, and Monaco Editor to simplify hosting in restrictive domains.

Tokens
80.6K
Snippets
293
Records
385
Agent score
51%

What's inside Pode.Web

  1. Understand the bundled libraries in Pode.Web

    develop

    Pode.Web bundles several JavaScript and CSS libraries directly within the module. This bundling is designed to simplify hosting websites in restrictive domains by reducing the need for external asset fetching.

    The following primary libraries are included and available for use:

    • highlightjs: For syntax highlighting.
    • mdi (Material Design Icons): For icon sets.
    • bootstrap: For UI components and styling.
    • bs-stepper: For stepper components.
    • chart.js: For data visualization.
    • jquery: For DOM manipulation.
    • jquery-ui: For interactive UI widgets.
    • moment: For date and time manipulation.
    • monaco: For the Monaco Editor (code editing).
  2. Provide dynamic icons using New-PodeWebIcon

    develop

    When using elements like New-PodeWebButton, you can provide an icon in two ways:

    1. Static Name: Pass a string representing the icon name (e.g., -Icon 'github').
    2. Dynamic Icon Object: Pass an object created by New-PodeWebIcon. This allows you to apply properties like -Spin to the icon before assigning it to the element.
    $icon = New-PodeWebIcon -Name 'github' -Spin
    New-PodeWebButton -Name 'Repository' -Icon $icon -Url 'https://github.com/Badgerati/Pode.Web'
  3. How Element Groups work for logical grouping and serialization

    develop

    An Element Group is a logical grouping mechanism that has no impact on styling or rendering (similar to a Span). Its primary purpose is to enable data serialization. When a Button or an Event within an Element Group is triggered, all other elements in that same group (such as Textbox elements) are serialized, and their values are sent up to the triggering element's scriptblock via the $WebEvent.Data object.

    While Element Groups can be applied to any element, only Buttons and Events can take advantage of this serialization feature.

    New-PodeWebElementGroup -Content @(
        New-PodeWebTextbox -Name 'Name' -Type Text
        New-PodeWebButton -Name 'Click Me' -ScriptBlock {
            # The value of the textbox is available via $WebEvent.Data.Name
            Show-PodeWebToast -Message "Hi, $($WebEvent.Data.Name)!"
        }
    )
  4. How to perform manual updates from Tasks, Timers, or Schedules

    develop

    Tasks, Timers, and Schedules do not have access to the $WebEvent scope. To send updates from these background processes, you must manually create and set an 'AsyncEvent' scope using helper functions.

    Key helper functions include:

    • Set-PodeWebAsyncEvent: Sets the async scope for events to use.
    • Get-PodeWebAsyncEvent: Retrieves an existing async event.
    • New-PodeWebAsyncEvent: Creates a new async event (can target -All, -Group, or -ClientId).
    • Export-PodeWebAsyncEvent: Used within a $WebEvent scope to build an AsyncEvent object for passing to background tasks.
    • Set-PodeWebAsyncHeader: Informs the frontend that a request (like populating a table) will be completed asynchronously.
  5. Configure Display Names for Radio elements and options

    develop

    By default, the label for a Radio element and the text for its options are taken from the -Name property. To show a different visual label while keeping the underlying data key the same, use the -DisplayName parameter.

    • For the Radio element: -Name is the key used in $WebEvent.Data, while -DisplayName is what the user sees.
    • For Radio options: -Name is the value submitted, while -DisplayName is the visual text.
  6. Use DisplayName for visual labels

    develop

    Both New-PodeWebSelect and New-PodeWebOption support a -DisplayName parameter.

    • -Name: This is the functional identifier. It is used to retrieve the submitted value from $WebEvent.Data and is used as the default label if no display name is provided.
    • -DisplayName: This is a purely visual label shown to the user in the UI. Changing the display name does not change the underlying data key used for form submission.
  7. Configure AutoComplete for Textboxes

    develop

    You can provide autocomplete suggestions for a Textbox using the -AutoComplete parameter, which accepts a scriptblock that returns an array of strings.

    Autocomplete Modes

    • Once (Default): The scriptblock is executed once when the textbox is initially loaded. The results are loaded but only rendered once the user reaches the -AutoCompleteMinLength threshold.
    • Always: The scriptblock is invoked on every character change. This mode allows for dynamic filtering using the current input value via $WebEvent.Data.Value.

    Key Parameters

    • -AutoComplete: A scriptblock returning an array of strings.
    • -AutoCompleteType: Set to Always for dynamic filtering or Once (default) for initial load only.
    • -AutoCompleteMinLength: The number of characters required before autocomplete options are displayed (default is 1).
    New-PodeWebTextbox -Name 'Service Name' -AutoCompleteType Always -AutoComplete {
        return Get-Service | 
            Where-Object { $_.Name -ilike "*$($WebEvent.Data.Value)*" } | 
            Select-Object -ExpandProperty Name
    }
  8. Create multi-dimensional grids

    develop

    You can create complex, multi-dimensional layouts by nesting grids within cells. This allows for precise control over the grid structure. Alternatively, for simple uniform grids (like a 3x3 grid), you can use a single New-PodeWebGrid with the -Width parameter set to the desired number of columns.

    # Example: 3x3 grid using -Width
    New-PodeWebGrid -Width 3 -Cells @(
        New-PodeWebCell -Content @( New-PodeWebImage -Source '...' )
        New-PodeWebCell -Content @( New-PodeWebImage -Source '...' )
        New-PodeWebCell -Content @( New-PodeWebImage -Source '...' )
        New-PodeWebCell -Content @( New-PodeWebImage -Source '...' )
        New-PodeWebCell -Content @( New-PodeWebImage -Source '...' )
        New-PodeWebCell -Content @( New-PodeWebImage -Source '...' )
        New-PodeWebCell -Content @( New-PodeWebImage -Source '...' )
        New-PodeWebCell -Content @( New-PodeWebImage -Source '...' )
        New-PodeWebCell -Content @( New-PodeWebImage -Source '...' )
    )
  9. Use plain numbers or CSS values for size parameters

    develop

    Parameters ending in -Width, -Height, or -Size in Pode.Web accept either a plain number or a pure CSS string.

    • Plain Numbers: When a number is provided, Pode.Web appends a default unit (such as px or %) to the value.
    • CSS Values: You can provide a string containing a specific CSS unit (e.g., '25%', '10em', '50vw').

    Note: If 0 is supplied, some elements may default to auto instead of a zero-unit value. Check the specific element's documentation to see its default unit behavior.

    # Using plain numbers (appends default unit, e.g., px)
    New-PodeWebImage -Source '/url/to/image.png' -Width 100 -Height 100
    
    # Using CSS strings for specific units
    New-PodeWebImage -Source '/url/to/image.png' -Width '25%' -Height '10em'
  10. Choose between HTTP and SSE connection types

    develop

    Pode.Web supports two connection types via the -ConnectionType parameter in Initialize-PodeWebTemplates:

    • HTTP (Default): Uses the standard request/response pattern. Actions within a Form's scriptblock occur only after the scriptblock has completed.
    • SSE (Server-Sent Events): Allows asynchronous feedback. Actions can respond in 'real-time', allowing you to update the client on progress during lengthy processing.

    Warnings for SSE:

    • SSE connections are left open; excessive connections can cause CPU/performance issues.
    • Most browsers limit SSE to 6 connections per window (not per tab).
    • Stale connections on the server are automatically cleaned up after ~60 seconds.
    Import-Module -Name Pode.Web
    
    Start-PodeServer {
        Add-PodeEndpoint -Address localhost -Port 8080 -Protocol Http
        Initialize-PodeWebTemplates -Title 'Example' -ConnectionType Sse
    }
  11. Create Icon Presets for Toggling and Hovering

    develop

    Icon Presets allow you to define a secondary state for an icon (e.g., a different color, a different icon name, or a spinning animation) that can be triggered by user interaction.

    Use New-PodeWebIconPreset to create a preset. If you do not specify certain parameters in the preset, the base icon's details will be used as the fallback.

    Toggle Icons

    To create an icon that changes state when toggled, pass a preset to the -ToggleIcon parameter of New-PodeWebIcon. You can then use the Switch-PodeWebIcon action to trigger the swap.

    Hover Icons

    To create an icon that changes state when the user hovers over it, pass a preset to the -HoverIcon parameter. Pode.Web will automatically swap to the hover icon when the mouse enters the icon area.

    # Example: Toggle icon that turns yellow and spins
    $toggle = New-PodeWebIconPreset -Colour Yellow -Spin
    New-PodeWebIcon -Name 'cat' -ToggleIcon $toggle
    
    # Example: Toggle icon that changes the icon name completely
    $toggle = New-PodeWebIconPreset -Name 'cat'
    New-PodeWebIcon -Name 'home' -ToggleIcon $toggle
    
    # Example: Hover icon that changes from an outline to a filled checkbox
    $hover = New-PodeWebIconPreset -Name 'mdi-checkbox-multiple-marked'
    New-PodeWebIcon -Name 'mdi-checkbox-multiple-marked-outline' -HoverIcon $hover