projects/cobbler-frontend/src/app/items/snippet/overview/snippet-overview.component.ts
OnInit
OnDestroy
selector | cobbler-snippet-overview |
standalone | true |
imports |
MatTableModule
MatButtonModule
MatIconModule
MatMenuModule
MatTooltip
|
templateUrl | ./snippet-overview.component.html |
styleUrl | ./snippet-overview.component.scss |
Properties |
|
Methods |
constructor(userService: UserService, cobblerApiService: CobblerApiService, _snackBar: MatSnackBar, router: Router, dialog: MatDialog)
|
||||||||||||||||||
Parameters :
|
addSnippet |
addSnippet()
|
Returns :
void
|
deleteSnippet | ||||||
deleteSnippet(name: string)
|
||||||
Parameters :
Returns :
void
|
ngOnDestroy |
ngOnDestroy()
|
Returns :
void
|
ngOnInit |
ngOnInit()
|
Returns :
void
|
renameSnippet | ||||||
renameSnippet(name: string)
|
||||||
Parameters :
Returns :
void
|
Private retrieveSnippets |
retrieveSnippets()
|
Returns :
void
|
showSnippet | ||||||
showSnippet(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 { SnippetCreateComponent } from '../create/snippet-create.component';
@Component({
selector: 'cobbler-snippet-overview',
standalone: true,
imports: [
MatTableModule,
MatButtonModule,
MatIconModule,
MatMenuModule,
MatTooltip,
],
templateUrl: './snippet-overview.component.html',
styleUrl: './snippet-overview.component.scss',
})
export class SnippetOverviewComponent 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.retrieveSnippets();
}
ngOnDestroy(): void {
this.ngUnsubscribe.next();
this.ngUnsubscribe.complete();
}
private retrieveSnippets(): void {
this.cobblerApiService
.get_autoinstall_snippets(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');
},
});
}
addSnippet(): void {
const dialogRef = this.dialog.open(SnippetCreateComponent, {
width: '40%',
});
dialogRef.afterClosed().subscribe((result) => {
if (typeof result === 'string') {
this.router.navigate(['/items', 'snippet', result]);
}
});
}
showSnippet(name: string): void {
this.router.navigate(['/items', 'snippet', name]);
}
renameSnippet(name: string): void {
const dialogRef = this.dialog.open(DialogItemRenameComponent, {
data: {
itemType: 'Snippets',
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_snippet(name, this.userService.token)
.pipe(takeUntil(this.ngUnsubscribe))
.subscribe({
next: (snippet: string) => {
this.cobblerApiService
.write_autoinstall_snippet(
newItemName,
snippet,
this.userService.token,
)
.pipe(takeUntil(this.ngUnsubscribe))
.subscribe({
next: () => {
this.cobblerApiService
.remove_autoinstall_snippet(name, this.userService.token)
.pipe(takeUntil(this.ngUnsubscribe))
.subscribe({
next: () => {
this.retrieveSnippets();
},
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');
},
});
});
}
deleteSnippet(name: string): void {
this.cobblerApiService
.remove_autoinstall_snippet(name, this.userService.token)
.subscribe({
next: () => {
this.retrieveSnippets();
},
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">SNIPPETS</h1>
<span class="title-cell-button">
<button
mat-icon-button
(click)="this.addSnippet()"
matTooltip="Add Snippet"
>
<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)="showSnippet(element)">
<mat-icon>visibility</mat-icon>
<span>Show details</span>
</button>
<button mat-menu-item (click)="renameSnippet(element)">
<mat-icon>edit</mat-icon>
<span>Rename</span>
</button>
<button mat-menu-item (click)="deleteSnippet(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>