59 lines
1.7 KiB
TypeScript
59 lines
1.7 KiB
TypeScript
import { SelectionModel } from "@angular/cdk/collections";
|
|
import { Component, EventEmitter, Input, OnDestroy, Output } from "@angular/core";
|
|
import { Subject, takeUntil } from "rxjs";
|
|
|
|
import { ServiceAccountView } from "../models/view/service-account.view";
|
|
|
|
@Component({
|
|
selector: "sm-service-accounts-list",
|
|
templateUrl: "./service-accounts-list.component.html",
|
|
})
|
|
export class ServiceAccountsListComponent implements OnDestroy {
|
|
@Input()
|
|
get serviceAccounts(): ServiceAccountView[] {
|
|
return this._serviceAccounts;
|
|
}
|
|
set serviceAccounts(serviceAccounts: ServiceAccountView[]) {
|
|
this.selection.clear();
|
|
this._serviceAccounts = serviceAccounts;
|
|
}
|
|
private _serviceAccounts: ServiceAccountView[];
|
|
|
|
@Output() newServiceAccountEvent = new EventEmitter();
|
|
@Output() deleteServiceAccountsEvent = new EventEmitter<string[]>();
|
|
@Output() onServiceAccountCheckedEvent = new EventEmitter<string[]>();
|
|
|
|
private destroy$: Subject<void> = new Subject<void>();
|
|
|
|
selection = new SelectionModel<string>(true, []);
|
|
|
|
constructor() {
|
|
this.selection.changed
|
|
.pipe(takeUntil(this.destroy$))
|
|
.subscribe((_) => this.onServiceAccountCheckedEvent.emit(this.selection.selected));
|
|
}
|
|
|
|
ngOnDestroy(): void {
|
|
this.destroy$.next();
|
|
this.destroy$.complete();
|
|
}
|
|
|
|
isAllSelected() {
|
|
const numSelected = this.selection.selected.length;
|
|
const numRows = this.serviceAccounts.length;
|
|
return numSelected === numRows;
|
|
}
|
|
|
|
toggleAll() {
|
|
this.isAllSelected()
|
|
? this.selection.clear()
|
|
: this.selection.select(...this.serviceAccounts.map((s) => s.id));
|
|
}
|
|
|
|
bulkDeleteServiceAccounts() {
|
|
if (this.selection.selected.length >= 1) {
|
|
this.deleteServiceAccountsEvent.emit(this.selection.selected);
|
|
}
|
|
}
|
|
}
|