projects/cobbler-frontend/src/app/items/image/overview/image-overview.component.ts
OnInit
OnDestroy
selector | cobbler-image-overview |
standalone | true |
imports |
MatTableModule
MatIconModule
MatButtonModule
MatMenuModule
MatTooltipModule
|
templateUrl | ./image-overview.component.html |
styleUrl | ./image-overview.component.scss |
Properties |
|
Methods |
constructor(userService: UserService, cobblerApiService: CobblerApiService, _snackBar: MatSnackBar, router: Router, dialog: MatDialog)
|
||||||||||||||||||
Parameters :
|
addImage |
addImage()
|
Returns :
void
|
deleteImage |
deleteImage(uid: string, name: string)
|
Returns :
void
|
ngOnDestroy |
ngOnDestroy()
|
Returns :
void
|
ngOnInit |
ngOnInit()
|
Returns :
void
|
renameImage |
renameImage(uid: string, name: string)
|
Returns :
void
|
Private retrieveImages |
retrieveImages()
|
Returns :
void
|
showImage |
showImage(uid: string, name: string)
|
Returns :
void
|
dataSource |
Type : Array<Image>
|
Default value : []
|
displayedColumns |
Type : string[]
|
Default value : [
'name',
'arch',
'breed',
'os_version',
'actions',
]
|
Private ngUnsubscribe |
Default value : new Subject<void>()
|
table |
Type : MatTable<Image>
|
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 { MatTooltipModule } from '@angular/material/tooltip';
import { Router } from '@angular/router';
import { CobblerApiService, Image } 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 { ImageCreateComponent } from '../create/image-create.component';
@Component({
selector: 'cobbler-image-overview',
standalone: true,
imports: [
MatTableModule,
MatIconModule,
MatButtonModule,
MatMenuModule,
MatTooltipModule,
],
templateUrl: './image-overview.component.html',
styleUrl: './image-overview.component.scss',
})
export class ImageOverviewComponent implements OnInit, OnDestroy {
// Unsubscribe
private ngUnsubscribe = new Subject<void>();
// Table
displayedColumns: string[] = [
'name',
'arch',
'breed',
'os_version',
'actions',
];
dataSource: Array<Image> = [];
@ViewChild(MatTable) table: MatTable<Image>;
constructor(
public userService: UserService,
private cobblerApiService: CobblerApiService,
private _snackBar: MatSnackBar,
private router: Router,
@Inject(MatDialog) readonly dialog: MatDialog,
) {}
ngOnInit(): void {
this.retrieveImages();
}
ngOnDestroy(): void {
this.ngUnsubscribe.next();
this.ngUnsubscribe.complete();
}
private retrieveImages(): void {
this.cobblerApiService
.get_images()
.pipe(takeUntil(this.ngUnsubscribe))
.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');
},
});
}
addImage(): void {
const dialogRef = this.dialog.open(ImageCreateComponent, { width: '40%' });
dialogRef.afterClosed().subscribe((result) => {
if (typeof result === 'string') {
this.router.navigate(['/items', 'image', result]);
}
});
}
showImage(uid: string, name: string): void {
this.router.navigate(['/items', 'image', name]);
}
renameImage(uid: string, name: string): void {
const dialogRef = this.dialog.open(DialogItemRenameComponent, {
data: {
itemType: 'Image',
itemName: name,
itemUid: uid,
},
});
dialogRef.afterClosed().subscribe((newItemName) => {
if (newItemName === undefined) {
// Cancel means we don't need to rename the image
return;
}
this.cobblerApiService
.get_image_handle(name, this.userService.token)
.pipe(takeUntil(this.ngUnsubscribe))
.subscribe({
next: (imageHandle) => {
this.cobblerApiService
.rename_image(imageHandle, newItemName, this.userService.token)
.pipe(takeUntil(this.ngUnsubscribe))
.subscribe({
next: () => {
this.retrieveImages();
},
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');
},
});
});
}
deleteImage(uid: string, name: string): void {
this.cobblerApiService
.remove_distro(name, this.userService.token, false)
.pipe(takeUntil(this.ngUnsubscribe))
.subscribe({
next: () => {
this.retrieveImages();
},
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">IMAGES</h1>
<span class="title-cell-button">
<button mat-icon-button (click)="this.addImage()" matTooltip="Add Image">
<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.name }}</td>
</ng-container>
<!-- Arch Column -->
<ng-container matColumnDef="arch">
<th mat-header-cell *matHeaderCellDef>Architecture</th>
<td mat-cell *matCellDef="let element">{{ element.arch }}</td>
</ng-container>
<!-- Breed Column -->
<ng-container matColumnDef="breed">
<th mat-header-cell *matHeaderCellDef>Breed</th>
<td mat-cell *matCellDef="let element">{{ element.breed }}</td>
</ng-container>
<!-- OsVersion Column -->
<ng-container matColumnDef="os_version">
<th mat-header-cell *matHeaderCellDef>Operating System Version</th>
<td mat-cell *matCellDef="let element">{{ element.os_version }}</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)="showImage(element.uid, element.name)">
<mat-icon>visibility</mat-icon>
<span>Show details</span>
</button>
<button mat-menu-item (click)="renameImage(element.uid, element.name)">
<mat-icon>edit</mat-icon>
<span>Rename</span>
</button>
<button mat-menu-item (click)="deleteImage(element.uid, element.name)">
<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>