projects/cobbler-frontend/src/app/items/menu/edit/menu-edit.component.ts
OnInit
OnDestroy
selector | cobbler-menu-edit |
standalone | true |
imports |
FormsModule
MatButton
MatCheckbox
MatFormField
MatIcon
MatIconButton
MatInput
MatLabel
MatTooltip
ReactiveFormsModule
KeyValueEditorComponent
MultiSelectComponent
|
templateUrl | ./menu-edit.component.html |
styleUrl | ./menu-edit.component.scss |
Properties |
|
Methods |
constructor(route: ActivatedRoute, userService: UserService, cobblerApiService: CobblerApiService, _snackBar: MatSnackBar, router: Router, dialog: MatDialog)
|
|||||||||||||||||||||
Parameters :
|
cancelEdit |
cancelEdit()
|
Returns :
void
|
copyMenu |
copyMenu(uid: string, name: string)
|
Returns :
void
|
editMenu |
editMenu()
|
Returns :
void
|
ngOnDestroy |
ngOnDestroy()
|
Returns :
void
|
ngOnInit |
ngOnInit()
|
Returns :
void
|
refreshData |
refreshData()
|
Returns :
void
|
removeMenu |
removeMenu()
|
Returns :
void
|
saveMenu |
saveMenu()
|
Returns :
void
|
showAsRendered |
showAsRendered()
|
Returns :
void
|
Private Readonly _formBuilder |
Default value : inject(FormBuilder)
|
Protected Readonly CobblerInputChoices |
Default value : CobblerInputChoices
|
isEditMode |
Type : boolean
|
Default value : false
|
menu |
Type : Menu
|
menuEditableInputData |
Type : Array<CobblerInputData>
|
Default value : [
...cobblerItemEditableData,
{
formControlName: 'display_name',
inputType: CobblerInputChoices.TEXT,
label: 'Display Name',
disabled: true,
readonly: false,
defaultValue: '',
inherited: false,
},
]
|
menuFormGroup |
Default value : this._formBuilder.group({})
|
menuReadonlyFormGroup |
Default value : this._formBuilder.group({})
|
menuReadonlyInputData |
Default value : cobblerItemReadonlyData
|
name |
Type : string
|
Private ngUnsubscribe |
Default value : new Subject<void>()
|
import { Component, Inject, inject, OnDestroy, OnInit } from '@angular/core';
import { FormBuilder, FormsModule, ReactiveFormsModule } from '@angular/forms';
import { MatButton, MatIconButton } from '@angular/material/button';
import { MatCheckbox } from '@angular/material/checkbox';
import { MatDialog } from '@angular/material/dialog';
import { MatFormField, MatLabel } from '@angular/material/form-field';
import { MatIcon } from '@angular/material/icon';
import { MatInput } from '@angular/material/input';
import { MatSnackBar } from '@angular/material/snack-bar';
import { MatTooltip } from '@angular/material/tooltip';
import { ActivatedRoute, Router } from '@angular/router';
import { CobblerApiService, Menu } from 'cobbler-api';
import { combineLatest, Observable, Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import { DialogBoxConfirmCancelEditComponent } from '../../../common/dialog-box-confirm-cancel-edit/dialog-box-confirm-cancel-edit.component';
import { DialogItemCopyComponent } from '../../../common/dialog-item-copy/dialog-item-copy.component';
import { UserService } from '../../../services/user.service';
import Utils, { CobblerInputChoices, CobblerInputData } from '../../../utils';
import { DialogBoxItemRenderedComponent } from '../../../common/dialog-box-item-rendered/dialog-box-item-rendered.component';
import { KeyValueEditorComponent } from '../../../common/key-value-editor/key-value-editor.component';
import { MultiSelectComponent } from '../../../common/multi-select/multi-select.component';
import {
cobblerItemEditableData,
cobblerItemReadonlyData,
} from '../../metadata';
@Component({
selector: 'cobbler-menu-edit',
standalone: true,
imports: [
FormsModule,
MatButton,
MatCheckbox,
MatFormField,
MatIcon,
MatIconButton,
MatInput,
MatLabel,
MatTooltip,
ReactiveFormsModule,
KeyValueEditorComponent,
MultiSelectComponent,
],
templateUrl: './menu-edit.component.html',
styleUrl: './menu-edit.component.scss',
})
export class MenuEditComponent implements OnInit, OnDestroy {
// Bring Enum to HTML scope
protected readonly CobblerInputChoices = CobblerInputChoices;
// Unsubscribe
private ngUnsubscribe = new Subject<void>();
// Form data
menuReadonlyInputData = cobblerItemReadonlyData;
menuEditableInputData: Array<CobblerInputData> = [
...cobblerItemEditableData,
{
formControlName: 'display_name',
inputType: CobblerInputChoices.TEXT,
label: 'Display Name',
disabled: true,
readonly: false,
defaultValue: '',
inherited: false,
},
];
// Form
name: string;
menu: Menu;
private readonly _formBuilder = inject(FormBuilder);
menuReadonlyFormGroup = this._formBuilder.group({});
menuFormGroup = this._formBuilder.group({});
isEditMode: boolean = false;
constructor(
private route: ActivatedRoute,
private userService: UserService,
private cobblerApiService: CobblerApiService,
private _snackBar: MatSnackBar,
private router: Router,
@Inject(MatDialog) readonly dialog: MatDialog,
) {
this.name = this.route.snapshot.paramMap.get('name');
Utils.fillupItemFormGroup(
this.menuReadonlyFormGroup,
this.menuFormGroup,
this.menuReadonlyInputData,
this.menuEditableInputData,
);
}
ngOnInit(): void {
this.refreshData();
}
ngOnDestroy(): void {
this.ngUnsubscribe.next();
this.ngUnsubscribe.complete();
}
refreshData(): void {
this.cobblerApiService
.get_menu(this.name, false, false, this.userService.token)
.pipe(takeUntil(this.ngUnsubscribe))
.subscribe({
next: (value) => {
this.menu = value;
this.menuReadonlyFormGroup.patchValue({
name: this.menu.name,
uid: this.menu.uid,
mtime: Utils.floatToDate(this.menu.mtime).toString(),
ctime: Utils.floatToDate(this.menu.ctime).toString(),
depth: this.menu.depth,
is_subobject: this.menu.is_subobject,
});
this.menuFormGroup.patchValue({
comment: this.menu.comment,
display_name: this.menu.display_name,
});
},
error: (error) => {
// HTML encode the error message since it originates from XML
this._snackBar.open(Utils.toHTML(error.message), 'Close');
},
});
}
removeMenu(): void {
this.cobblerApiService
.remove_menu(this.name, this.userService.token, false)
.pipe(takeUntil(this.ngUnsubscribe))
.subscribe({
next: (value) => {
if (value) {
this.router.navigate(['/items', 'menu']);
}
// HTML encode the error message since it originates from XML
this._snackBar.open(
'Delete failed! Check server logs for more information.',
'Close',
);
},
error: (error) => {
// HTML encode the error message since it originates from XML
this._snackBar.open(Utils.toHTML(error.message), 'Close');
},
});
}
editMenu(): void {
this.isEditMode = true;
this.menuFormGroup.enable();
}
cancelEdit(): void {
const dialogRef = this.dialog.open(DialogBoxConfirmCancelEditComponent, {
data: {
name: this.menu.name,
},
});
dialogRef.afterClosed().subscribe((dialogResult) => {
if (dialogResult === false) {
// False means the user want's to continue
return;
}
this.isEditMode = false;
this.menuFormGroup.disable();
this.refreshData();
});
}
showAsRendered(): void {
this.cobblerApiService
.get_menu_as_rendered(this.menu.name, this.userService.token)
.subscribe((value) => {
this.dialog.open(DialogBoxItemRenderedComponent, {
data: {
itemType: 'Menu',
uid: this.menu.uid,
name: this.menu.name,
renderedData: value,
},
});
});
}
copyMenu(uid: string, name: string): void {
const dialogRef = this.dialog.open(DialogItemCopyComponent, {
data: {
itemType: 'Menu',
itemName: name,
itemUid: uid,
},
});
dialogRef.afterClosed().subscribe((newItemName) => {
if (newItemName === undefined) {
// Cancel means we don't need to rename the menu
return;
}
this.cobblerApiService
.get_menu_handle(name, this.userService.token)
.pipe(takeUntil(this.ngUnsubscribe))
.subscribe({
next: (menuHandle) => {
this.cobblerApiService
.copy_menu(menuHandle, newItemName, this.userService.token)
.pipe(takeUntil(this.ngUnsubscribe))
.subscribe({
next: () => {
this.router.navigate(['/items', 'menu', newItemName]);
},
error: (error) => {
// HTML encode the error message since it originates from XML
this._snackBar.open(Utils.toHTML(error.message), 'Close');
},
});
},
error: (error) => {
// HTML encode the error message since it originates from XML
this._snackBar.open(Utils.toHTML(error.message), 'Close');
},
});
});
}
saveMenu(): void {
let dirtyValues = Utils.deduplicateDirtyValues(
this.menuFormGroup,
Utils.getDirtyValues(this.menuFormGroup),
);
this.cobblerApiService
.get_menu_handle(this.name, this.userService.token)
.pipe(takeUntil(this.ngUnsubscribe))
.subscribe({
next: (menuHandle) => {
let modifyObservables: Observable<boolean>[] = [];
dirtyValues.forEach((value, key) => {
modifyObservables.push(
this.cobblerApiService.modify_menu(
menuHandle,
key,
value,
this.userService.token,
),
);
});
combineLatest(modifyObservables).subscribe({
next: () => {
this.cobblerApiService
.save_menu(menuHandle, this.userService.token)
.subscribe({
next: () => {
this.isEditMode = false;
this.menuFormGroup.disable();
this.refreshData();
},
error: (error) => {
this._snackBar.open(Utils.toHTML(error.message), 'Close');
},
});
},
error: (error) => {
this._snackBar.open(Utils.toHTML(error.message), 'Close');
},
});
},
error: (error) => {
this._snackBar.open(Utils.toHTML(error.message), 'Close');
},
});
}
}
<div class="title-table">
<div class="title-row">
<h1 class="title title-cell-text">Name: {{ name }}</h1>
<span class="title-cell-button">
<button
mat-icon-button
[disabled]="isEditMode"
(click)="this.refreshData()"
matTooltip="Refresh data"
>
<mat-icon>refresh</mat-icon>
</button>
</span>
<span class="title-cell-button">
<button
mat-icon-button
[disabled]="isEditMode"
(click)="this.copyMenu(this.menu.uid, this.menu.name)"
matTooltip="Copy"
>
<mat-icon>content_copy</mat-icon>
</button>
</span>
<span class="title-cell-button">
<button
mat-icon-button
[disabled]="isEditMode"
(click)="this.showAsRendered()"
matTooltip="Show rendered data"
>
<mat-icon>info</mat-icon>
</button>
</span>
<span class="title-cell-button">
<button
mat-icon-button
[disabled]="isEditMode"
(click)="this.editMenu()"
matTooltip="Edit"
>
<mat-icon>edit</mat-icon>
</button>
</span>
<span class="title-cell-button">
<button
mat-icon-button
[disabled]="isEditMode"
(click)="this.removeMenu()"
matTooltip="Delete"
>
<mat-icon>delete</mat-icon>
</button>
</span>
@if (isEditMode) {
<span class="title-cell-button">
<button
mat-icon-button
(click)="this.cancelEdit()"
matTooltip="Cancel edit"
>
<mat-icon>cancel</mat-icon>
</button>
</span>
}
</div>
</div>
<form class="form-replicate" [formGroup]="menuReadonlyFormGroup">
@for (input of menuReadonlyInputData; track input) {
@switch (input.inputType) {
@case (CobblerInputChoices.TEXT) {
<mat-form-field class="form-field-full-width">
<mat-label>{{ input.label }}</mat-label>
<input
matInput
type="{{ input.inputType }}"
formControlName="{{ input.formControlName }}"
readonly="{{ input.readonly }}"
/>
</mat-form-field>
}
@case (CobblerInputChoices.NUMBER) {
<mat-form-field class="form-field-full-width">
<mat-label>{{ input.label }}</mat-label>
<input
matInput
type="{{ input.inputType }}"
formControlName="{{ input.formControlName }}"
readonly="{{ input.readonly }}"
/>
</mat-form-field>
}
@case (CobblerInputChoices.CHECKBOX) {
<mat-checkbox
class="form-field-full-width"
formControlName="{{ input.formControlName }}"
[disableRipple]="true"
(click)="$event.preventDefault()"
>
{{ input.label }}
</mat-checkbox>
}
}
}
</form>
<form class="form-replicate" [formGroup]="menuFormGroup">
@for (input of menuEditableInputData; track input) {
@switch (input.inputType) {
@case (CobblerInputChoices.TEXT) {
<mat-form-field class="form-field-full-width">
<mat-label>{{ input.label }}</mat-label>
<input
matInput
type="{{ input.inputType }}"
formControlName="{{ input.formControlName }}"
readonly="{{ input.readonly }}"
/>
</mat-form-field>
}
@case (CobblerInputChoices.NUMBER) {
<mat-form-field class="form-field-full-width">
<mat-label>{{ input.label }}</mat-label>
<input
matInput
type="{{ input.inputType }}"
formControlName="{{ input.formControlName }}"
readonly="{{ input.readonly }}"
/>
</mat-form-field>
}
@case (CobblerInputChoices.CHECKBOX) {
<mat-checkbox
class="form-field-full-width"
formControlName="{{ input.formControlName }}"
[disableRipple]="true"
(click)="$event.preventDefault()"
>
{{ input.label }}
</mat-checkbox>
}
@case (CobblerInputChoices.MULTI_SELECT) {
<ng-container class="form-field-full-width">
<cobbler-multi-select
label="{{ input.label }}"
formControlName="{{ input.formControlName }}"
></cobbler-multi-select>
@if (input.inherited) {
<mat-checkbox
formControlName="{{ input.formControlName }}_inherited"
>Inherited</mat-checkbox
>
}
</ng-container>
}
@case (CobblerInputChoices.KEY_VALUE) {
<ng-container class="form-field-full-width">
<cobbler-key-value-editor
label="{{ input.label }}"
formControlName="{{ input.formControlName }}"
></cobbler-key-value-editor>
@if (input.inherited) {
<mat-checkbox
formControlName="{{ input.formControlName }}_inherited"
>Inherited</mat-checkbox
>
}
</ng-container>
}
}
}
@if (isEditMode) {
<div>
<button mat-button (click)="saveMenu()">Save Menu</button>
</div>
}
</form>