Files
DSC-Configuration-Compiler/Functions/Load-Templates.ps1
Torsten Brendgen 01a535eb39 Initial commit
2026-04-13 15:40:51 +02:00

126 lines
5.9 KiB
PowerShell

function Load-Templates {
$flow.SuspendLayout()
$flow.Controls.Clear()
# Lokales Tracking-Objekt
$selectionTracker = @{
LastSelectedListBox = $null
}
if (-not (Test-Path -Path $($settingsManager.Get("TemplatePath")))) {
$statusBar.SetText("Status", "Root-Ordner nicht gefunden: $rootPath")
}
else {
try {
$Categories = Get-ChildItem -Path $($settingsManager.Get("TemplatePath")) -Directory -ErrorAction Stop
}
catch {
$statusBar.SetText("Status", "Fehler beim Laden: $($_.Exception.Message)")
[System.Windows.Forms.MessageBox]::Show(
"Konnte Kategorien nicht laden: $($_.Exception.Message)",
"Fehler",
[System.Windows.Forms.MessageBoxButtons]::OK,
[System.Windows.Forms.MessageBoxIcon]::Error
)
return
}
if ($Categories.Count -eq 0) {
$labelNoCategories = (
[LabelBuilder]::new("Keine Unterordner gefunden.", 24).
SetDock('Top').
SetTextAlign([System.Drawing.ContentAlignment]::MiddleLeft).
SetPadding(5, 0, 0, 0).
Build()
)
$flow.Controls.Add($labelNoCategories)
return
}
else {
foreach ($Categorie in $Categories) {
$GroupBox = (
[GroupBoxBuilder]::new($Categorie.Name).
#SetAutoSize($true).
SetWidth(0).
SetHeight(90).
SetMargin(4).
SetPadding(10).
AddTo($flow)
)
$GroupBox.Tag = $Categorie.Name
$Files = Get-ChildItem -Path $Categorie.FullName -Filter "*.psd1" -File -ErrorAction SilentlyContinue | Sort-Object Name
if ($Files.Count -eq 0) {
$ListBox = (
[ListBoxBuilder]::new().
SetDock('Fill'). # Füllt die GroupBox aus
AddItem("Keine Templates").
AddTo($GroupBox)
)
}
else {
$ListBox = (
[ListBoxBuilder]::new().
SetDock('Fill').
AddSelectedIndexChangedHandler({
param($s, $e)
if ($null -ne $selectionTracker.LastSelectedListBox -and
$selectionTracker.LastSelectedListBox -ne $s) {
# Deselektiere die vorherige ListBox
$selectionTracker.LastSelectedListBox.ClearSelected()
}
# Speichere die aktuelle ListBox
$selectionTracker.LastSelectedListBox = $s
$selectedItem = $s.SelectedItem
if ($null -ne $selectedItem -and ($selectedItem -isnot [string])) {
$currentGroupBox = $s.Parent
$statusBar.SetText("Status", "Ausgewählt: $($selectedItem.FullName) [Kategorie: $($currentGroupBox.Tag)]")
Show-Template -Template $([TemplateBuilder]::new($selectedItem.FullName).ResolveDeploymentDataVariables())
}
}.GetNewClosure()).
AddMouseDoubleClickHandler({
param($s, $e)
$selectedItem = $s.SelectedItem
if ($null -ne $selectedItem -and ($selectedItem -isnot [string])) {
$fullPath = $selectedItem.FullName
if (-not (Test-Path $fullPath)) {
[System.Windows.Forms.MessageBox]::Show("Datei nicht gefunden: $fullPath", "Fehler", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
return
}
$newItem = New-Object PSObject -Property @{ BaseName = [System.IO.Path]::GetFileNameWithoutExtension($fullPath); FullName = $fullPath }
if (-not ($selectionList.Items | Where-Object { $_.FullName -eq $newItem.FullName })) {
$selectionList.Items.Add($newItem) | Out-Null
$stateManager.SetState("hasSelection", $true)
$stateManager.SetState("isMerged", $false)
$stateManager.UpdateAllButtons()
$statusBar.SetText("Status", "Zur Auswahl hinzugefügt: $fullPath")
}
else {
$statusBar.SetText("Status", "Datei bereits in der Auswahl")
}
}
})
)
foreach ($file in $files) {
$item = New-Object PSObject -Property @{ BaseName = $file.BaseName; FullName = $file.FullName }
$ListBox.AddItem($item)
$ListBox.AddTo($GroupBox)
}
}
}
$flow.ResumeLayout()
}
}
$form.Form.Invoke([Action] {
$splitPanel.AutoSizeSplitterToPanel1Content()
})
}