diff --git a/FFUDevelopment/BuildFFUVM_UI.ps1 b/FFUDevelopment/BuildFFUVM_UI.ps1 index bff7b9c..10ee23c 100644 --- a/FFUDevelopment/BuildFFUVM_UI.ps1 +++ b/FFUDevelopment/BuildFFUVM_UI.ps1 @@ -217,65 +217,6 @@ $window.Add_Loaded({ } Update-CopyButtonState -State $script:uiState # Initial check - # AppsScriptVariables Event Handlers - $script:uiState.Controls.chkDefineAppsScriptVariables.Add_Checked({ - $script:uiState.Controls.appsScriptVariablesPanel.Visibility = 'Visible' - }) - $script:uiState.Controls.chkDefineAppsScriptVariables.Add_Unchecked({ - $script:uiState.Controls.appsScriptVariablesPanel.Visibility = 'Collapsed' - }) - - $script:uiState.Controls.btnAddAppsScriptVariable.Add_Click({ - $key = $script:uiState.Controls.txtAppsScriptKey.Text.Trim() - $value = $script:uiState.Controls.txtAppsScriptValue.Text.Trim() - - if ([string]::IsNullOrWhiteSpace($key)) { - [System.Windows.MessageBox]::Show("Apps Script Variable Key cannot be empty.", "Input Error", "OK", "Warning") - return - } - # Check for duplicate keys - $existingKey = $script:uiState.Controls.lstAppsScriptVariables.Items | Where-Object { $_.Key -eq $key } - if ($existingKey) { - [System.Windows.MessageBox]::Show("An Apps Script Variable with the key '$key' already exists.", "Duplicate Key", "OK", "Warning") - return - } - - $newItem = [PSCustomObject]@{ - IsSelected = $false # Add IsSelected property - Key = $key - Value = $value - } - $script:uiState.Data.appsScriptVariablesDataList.Add($newItem) - $script:uiState.Controls.lstAppsScriptVariables.ItemsSource = $script:uiState.Data.appsScriptVariablesDataList.ToArray() - $script:uiState.Controls.txtAppsScriptKey.Clear() - $script:uiState.Controls.txtAppsScriptValue.Clear() - # Update the header checkbox state - if ($null -ne $script:uiState.Controls.chkSelectAllAppsScriptVariables) { - Update-SelectAllHeaderCheckBoxState -ListView $script:uiState.Controls.lstAppsScriptVariables -HeaderCheckBox $script:uiState.Controls.chkSelectAllAppsScriptVariables - } - }) - - $script:uiState.Controls.btnRemoveSelectedAppsScriptVariables.Add_Click({ - $itemsToRemove = @($script:uiState.Data.appsScriptVariablesDataList | Where-Object { $_.IsSelected }) - if ($itemsToRemove.Count -eq 0) { - [System.Windows.MessageBox]::Show("Please select one or more Apps Script Variables to remove.", "Selection Error", "OK", "Warning") - return - } - - foreach ($itemToRemove in $itemsToRemove) { - $script:uiState.Data.appsScriptVariablesDataList.Remove($itemToRemove) - } - $script:uiState.Controls.lstAppsScriptVariables.ItemsSource = $script:uiState.Data.appsScriptVariablesDataList.ToArray() - - # Update the header checkbox state - if ($null -ne $script:uiState.Controls.chkSelectAllAppsScriptVariables) { - # Check if variable exists - Update-SelectAllHeaderCheckBoxState -ListView $script:uiState.Controls.lstAppsScriptVariables -HeaderCheckBox $script:uiState.Controls.chkSelectAllAppsScriptVariables - } - }) - - - # Initial state for chkDefineAppsScriptVariables based on chkInstallApps if ($script:uiState.Controls.chkInstallApps.IsChecked) { $script:uiState.Controls.chkDefineAppsScriptVariables.Visibility = 'Visible' diff --git a/FFUDevelopment/FFUUI.Core/FFUUI.Core.Applications.psm1 b/FFUDevelopment/FFUUI.Core/FFUUI.Core.Applications.psm1 index 9a66041..2582cc7 100644 --- a/FFUDevelopment/FFUUI.Core/FFUUI.Core.Applications.psm1 +++ b/FFUDevelopment/FFUUI.Core/FFUUI.Core.Applications.psm1 @@ -69,7 +69,69 @@ function Add-BYOApplication { $State.Controls.txtAppSource.Text = "" Update-CopyButtonState -State $State } + +# Function to add a new Apps Script Variable from the UI +function Add-AppsScriptVariable { + [CmdletBinding()] + param( + [Parameter(Mandatory)] + [psobject]$State + ) + + $key = $State.Controls.txtAppsScriptKey.Text.Trim() + $value = $State.Controls.txtAppsScriptValue.Text.Trim() + + if ([string]::IsNullOrWhiteSpace($key)) { + [System.Windows.MessageBox]::Show("Apps Script Variable Key cannot be empty.", "Input Error", "OK", "Warning") + return + } + # Check for duplicate keys + $existingKey = $State.Controls.lstAppsScriptVariables.Items | Where-Object { $_.Key -eq $key } + if ($existingKey) { + [System.Windows.MessageBox]::Show("An Apps Script Variable with the key '$key' already exists.", "Duplicate Key", "OK", "Warning") + return + } + + $newItem = [PSCustomObject]@{ + IsSelected = $false # Add IsSelected property + Key = $key + Value = $value + } + $State.Data.appsScriptVariablesDataList.Add($newItem) + $State.Controls.lstAppsScriptVariables.ItemsSource = $State.Data.appsScriptVariablesDataList.ToArray() + $State.Controls.txtAppsScriptKey.Clear() + $State.Controls.txtAppsScriptValue.Clear() + # Update the header checkbox state + if ($null -ne $State.Controls.chkSelectAllAppsScriptVariables) { + Update-SelectAllHeaderCheckBoxState -ListView $State.Controls.lstAppsScriptVariables -HeaderCheckBox $State.Controls.chkSelectAllAppsScriptVariables + } +} +# Function to remove selected Apps Script Variables from the list +function Remove-SelectedAppsScriptVariable { + [CmdletBinding()] + param( + [Parameter(Mandatory)] + [psobject]$State + ) + + $itemsToRemove = @($State.Data.appsScriptVariablesDataList | Where-Object { $_.IsSelected }) + if ($itemsToRemove.Count -eq 0) { + [System.Windows.MessageBox]::Show("Please select one or more Apps Script Variables to remove.", "Selection Error", "OK", "Warning") + return + } + + foreach ($itemToRemove in $itemsToRemove) { + $State.Data.appsScriptVariablesDataList.Remove($itemToRemove) + } + $State.Controls.lstAppsScriptVariables.ItemsSource = $State.Data.appsScriptVariablesDataList.ToArray() + + # Update the header checkbox state + if ($null -ne $State.Controls.chkSelectAllAppsScriptVariables) { + Update-SelectAllHeaderCheckBoxState -ListView $State.Controls.lstAppsScriptVariables -HeaderCheckBox $State.Controls.chkSelectAllAppsScriptVariables + } +} + # Function to save BYO applications to JSON function Save-BYOApplicationList { [CmdletBinding()] diff --git a/FFUDevelopment/FFUUI.Core/FFUUI.Core.Handlers.psm1 b/FFUDevelopment/FFUUI.Core/FFUUI.Core.Handlers.psm1 index 8c715d7..1a80870 100644 --- a/FFUDevelopment/FFUUI.Core/FFUUI.Core.Handlers.psm1 +++ b/FFUDevelopment/FFUUI.Core/FFUUI.Core.Handlers.psm1 @@ -400,16 +400,45 @@ function Register-EventHandlers { $localState = $window.Tag Move-ListViewItemBottom -ListView $localState.Controls.lstApplications }) - - $State.Controls.btnCheckWingetModule.Add_Click({ + + # Apps Script Variables Event Handlers + $State.Controls.chkDefineAppsScriptVariables.Add_Checked({ + param($eventSource, $routedEventArgs) + $window = [System.Windows.Window]::GetWindow($eventSource) + $localState = $window.Tag + $localState.Controls.appsScriptVariablesPanel.Visibility = 'Visible' + }) + $State.Controls.chkDefineAppsScriptVariables.Add_Unchecked({ + param($eventSource, $routedEventArgs) + $window = [System.Windows.Window]::GetWindow($eventSource) + $localState = $window.Tag + $localState.Controls.appsScriptVariablesPanel.Visibility = 'Collapsed' + }) + + $State.Controls.btnAddAppsScriptVariable.Add_Click({ + param($eventSource, $routedEventArgs) + $window = [System.Windows.Window]::GetWindow($eventSource) + $localState = $window.Tag + Add-AppsScriptVariable -State $localState + }) + + $State.Controls.btnRemoveSelectedAppsScriptVariables.Add_Click({ + param($eventSource, $routedEventArgs) + $window = [System.Windows.Window]::GetWindow($eventSource) + $localState = $window.Tag + Remove-SelectedAppsScriptVariable -State $localState + }) + + $State.Controls.btnClearAppsScriptVariables.Add_Click({ param($eventSource, $routedEventArgs) $window = [System.Windows.Window]::GetWindow($eventSource) $localState = $window.Tag $postClearScriptBlock = { - $headerChk = $State.Controls.chkSelectAllAppsScriptVariables + # This scriptblock inherits the $localState variable from its parent scope. + $headerChk = $localState.Controls.chkSelectAllAppsScriptVariables if ($null -ne $headerChk) { - Update-SelectAllHeaderCheckBoxState -ListView $ListViewControl -HeaderCheckBox $headerChk + Update-SelectAllHeaderCheckBoxState -ListView $localState.Controls.lstAppsScriptVariables -HeaderCheckBox $headerChk } } @@ -419,6 +448,7 @@ function Register-EventHandlers { -ConfirmationTitle "Clear Apps Script Variables" ` -ConfirmationMessage "Are you sure you want to clear all Apps Script Variables?" ` -StatusMessage "Apps Script Variables list cleared." ` + -TextBoxesToClear @($localState.Controls.txtAppsScriptKey, $localState.Controls.txtAppsScriptValue) ` -PostClearAction $postClearScriptBlock })