projects/cobbler-frontend/src/app/items/template/overview/template-overview.component.ts
OnInit
OnDestroy
selector | cobbler-template-overview |
standalone | true |
imports |
MatTableModule
MatButtonModule
MatIconModule
MatMenuModule
MatTooltip
|
templateUrl | ./template-overview.component.html |
styleUrl | ./template-overview.component.scss |
Properties |
|
Methods |
constructor(userService: UserService, cobblerApiService: CobblerApiService, _snackBar: MatSnackBar, router: Router, dialog: MatDialog)
|
||||||||||||||||||
Parameters :
|
addTemplate |
addTemplate()
|
Returns :
void
|
deleteTemplate | ||||||
deleteTemplate(name: string)
|
||||||
Parameters :
Returns :
void
|
ngOnDestroy |
ngOnDestroy()
|
Returns :
void
|
ngOnInit |
ngOnInit()
|
Returns :
void
|
renameTemplate | ||||||
renameTemplate(name: string)
|
||||||
Parameters :
Returns :
void
|
Private retrieveTemplates |
retrieveTemplates()
|
Returns :
void
|
showTemplate | ||||||
showTemplate(name: string)
|
||||||
Parameters :
Returns :
void
|
dataSource |
Type : Array<string>
|
Default value : []
|
displayedColumns |
Type : string[]
|
Default value : ['name', 'actions']
|
Private ngUnsubscribe |
Default value : new Subject<void>()
|
table |
Type : MatTable<string>
|
Decorators :
@ViewChild(MatTable)
|
Public userService |
Type : UserService
|
import { Component, Inject, OnDestroy, OnInit, ViewChild } from '@angular/core';
import { MatButtonModule } from '@angular/material/button';
import { MatDialog } from '@angular/material/dialog';
import { MatIconModule } from '@angular/material/icon';
import { MatMenuModule } from '@angular/material/menu';
import { MatSnackBar } from '@angular/material/snack-bar';
import { MatTable, MatTableModule } from '@angular/material/table';
import { MatTooltip } from '@angular/material/tooltip';
import { Router } from '@angular/router';
import { CobblerApiService } from 'cobbler-api';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import { DialogItemRenameComponent } from '../../../common/dialog-item-rename/dialog-item-rename.component';
import { UserService } from '../../../services/user.service';
import Utils from '../../../utils';
import { TemplateCreateComponent } from '../create/template-create.component';
@Component({
selector: 'cobbler-template-overview',
standalone: true,
imports: [
MatTableModule,
MatButtonModule,
MatIconModule,
MatMenuModule,
MatTooltip,
],
templateUrl: './template-overview.component.html',
styleUrl: './template-overview.component.scss',
})
export class TemplateOverviewComponent implements OnInit, OnDestroy {
// Unsubscribe
private ngUnsubscribe = new Subject<void>();
// Table
displayedColumns: string[] = ['name', 'actions'];
dataSource: Array<string> = [];
@ViewChild(MatTable) table: MatTable<string>;
constructor(
public userService: UserService,
private cobblerApiService: CobblerApiService,
private _snackBar: MatSnackBar,
private router: Router,
@Inject(MatDialog) readonly dialog: MatDialog,
) {}
ngOnInit(): void {
this.retrieveTemplates();
}
ngOnDestroy(): void {
this.ngUnsubscribe.next();
this.ngUnsubscribe.complete();
}
private retrieveTemplates(): void {
this.cobblerApiService
.get_autoinstall_templates(this.userService.token)
.subscribe({
next: (value) => {
this.dataSource = value;
},
error: (error) => {
// HTML encode the error message since it originates from XML
this._snackBar.open(Utils.toHTML(error.message), 'Close');
},
});
}
addTemplate(): void {
const dialogRef = this.dialog.open(TemplateCreateComponent, {
width: '40%',
});
dialogRef.afterClosed().subscribe((result) => {
if (typeof result === 'string') {
this.router.navigate(['/items', 'template', result]);
}
});
}
showTemplate(name: string): void {
this.router.navigate(['/items', 'template', name]);
}
renameTemplate(name: string): void {
const dialogRef = this.dialog.open(DialogItemRenameComponent, {
data: {
itemType: 'Template',
itemName: name,
itemUid: '',
},
});
dialogRef.afterClosed().subscribe((newItemName) => {
if (newItemName === undefined) {
// Cancel means we don't need to rename the file
return;
}
this.cobblerApiService
.read_autoinstall_template(name, this.userService.token)
.pipe(takeUntil(this.ngUnsubscribe))
.subscribe({
next: (template: string) => {
this.cobblerApiService
.write_autoinstall_template(
newItemName,
template,
this.userService.token,
)
.pipe(takeUntil(this.ngUnsubscribe))
.subscribe({
next: () => {
this.cobblerApiService
.remove_autoinstall_template(name, this.userService.token)
.pipe(takeUntil(this.ngUnsubscribe))
.subscribe({
next: () => {
this.retrieveTemplates();
},
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');
},
});
},
error: (error) => {
// HTML encode the error message since it originates from XML
this._snackBar.open(Utils.toHTML(error.message), 'Close');
},
});
});
}
deleteTemplate(name: string): void {
this.cobblerApiService
.remove_autoinstall_template(name, this.userService.token)
.subscribe({
next: () => {
this.retrieveTemplates();
},
error: (error) => {
// HTML encode the error message since it originates from XML
this._snackBar.open(Utils.toHTML(error.message), 'Close');
},
});
}
}
<div class="title-table">
<div class="title-row">
<h1 class="title title-cell-text">TEMPLATES</h1>
<span class="title-cell-button">
<button
mat-icon-button
(click)="this.addTemplate()"
matTooltip="Add Template"
>
<mat-icon>add</mat-icon>
</button></span
>
</div>
</div>
<table mat-table [dataSource]="dataSource" class="mat-elevation-z8">
<!-- Name Column -->
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef>Name</th>
<td mat-cell *matCellDef="let element">{{ element }}</td>
</ng-container>
<ng-container matColumnDef="actions">
<th mat-header-cell *matHeaderCellDef></th>
<td mat-cell *matCellDef="let element">
<button mat-icon-button [matMenuTriggerFor]="menu">
<mat-icon>more_vert</mat-icon>
</button>
<mat-menu #menu="matMenu">
<button mat-menu-item (click)="showTemplate(element)">
<mat-icon>visibility</mat-icon>
<span>Show details</span>
</button>
<button mat-menu-item (click)="renameTemplate(element)">
<mat-icon>edit</mat-icon>
<span>Rename</span>
</button>
<button mat-menu-item (click)="deleteTemplate(element)">
<mat-icon>delete</mat-icon>
<span>Delete</span>
</button>
</mat-menu>
</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns"></tr>
</table>