|
|
@@ -1,5 +1,7 @@
|
|
|
import { Component, ViewChild } from '@angular/core';
|
|
|
+import { ActivatedRoute, Router } from '@angular/router';
|
|
|
import { faAngleRight, faPlus, faSave, faServer } from '@fortawesome/free-solid-svg-icons';
|
|
|
+import { NgbAccordion, NgbPanelChangeEvent } from '@ng-bootstrap/ng-bootstrap';
|
|
|
|
|
|
import { ServiceCheckFormComponent } from 'src/app/components/service-check-form/service-check-form.component';
|
|
|
import { ServiceApiService } from 'src/app/services/service-api.service';
|
|
|
@@ -12,24 +14,65 @@ import { ServerApiService } from 'src/app/services/server-api.service';
|
|
|
host: { class: 'd-flex flex-column h-100' }
|
|
|
})
|
|
|
export class AdminServiceChecksPageComponent {
|
|
|
+ public PANEL_ID_PFX = 'acc-svc-checks-';
|
|
|
+ public PANEL_ID_REG = /^acc-svc-checks-(-?\d+)$/;
|
|
|
+
|
|
|
@ViewChild(ServiceCheckFormComponent) formRef!: ServiceCheckFormComponent;
|
|
|
- public serverConfigs: ServerConfig[] = [];
|
|
|
+ @ViewChild('acc') accordeonRef!: NgbAccordion;
|
|
|
+ public serverConfigs?: ServerConfig[];
|
|
|
public serviceChecks: HttpCheckConfig[] = [];
|
|
|
public fa = { save: faSave, server: faServer, angleRight: faAngleRight, plus: faPlus };
|
|
|
|
|
|
public activeId = 0;
|
|
|
+ public params?: { serverID?: number; checkID?: number; activeIds?: string[] };
|
|
|
|
|
|
- constructor(private serviceApi: ServiceApiService, apiService: ServerApiService) {
|
|
|
+ constructor(apiService: ServerApiService, route: ActivatedRoute, private router: Router, private serviceApi: ServiceApiService) {
|
|
|
apiService.serverConfigs$.subscribe(data => {
|
|
|
this.serverConfigs = data;
|
|
|
- if (data.length > 0) {
|
|
|
- this.fetchServiceChecks(data[this.activeId].id);
|
|
|
+ this.syncInit();
|
|
|
+ });
|
|
|
+ route.params.subscribe({
|
|
|
+ next: params => {
|
|
|
+ if (!params) return;
|
|
|
+ this.params = {
|
|
|
+ serverID: params['serverID'] ? Number(params['serverID']) : undefined,
|
|
|
+ checkID: params['checkID'] ? Number(params['checkID']) : undefined
|
|
|
+ };
|
|
|
+ this.syncInit();
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
ngOnInit(): void {}
|
|
|
|
|
|
+ async syncInit() {
|
|
|
+ if (!this.params) return;
|
|
|
+ if (!this.serverConfigs) return;
|
|
|
+
|
|
|
+ if (this.serverConfigs.length > 0) {
|
|
|
+ if (!this.params.serverID) {
|
|
|
+ // redirect to first server's id
|
|
|
+ this.router.navigateByUrl(`/admin/svc/${this.serverConfigs[0].id}`);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // set selected nav item and load service checks
|
|
|
+ this.activeId = this.serverConfigs.findIndex(c => c.id === this.params?.serverID);
|
|
|
+ await this.fetchServiceChecks(this.params.serverID);
|
|
|
+
|
|
|
+ if (this.params.checkID) {
|
|
|
+ // open accordeon panel
|
|
|
+ if (this.params.checkID === -1) {
|
|
|
+ this.addServiceCheck(this.params.serverID);
|
|
|
+ } else {
|
|
|
+ const activePanelId = `${this.PANEL_ID_PFX}${this.params.checkID}`;
|
|
|
+ this.params.activeIds = [activePanelId];
|
|
|
+ setTimeout(() => this.scrollIntoView(activePanelId));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
async fetchServiceChecks(serverId: number) {
|
|
|
try {
|
|
|
this.serviceChecks = await this.serviceApi.loadServiceChecks(serverId);
|
|
|
@@ -38,12 +81,52 @@ export class AdminServiceChecksPageComponent {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- saveServiceCheck(event: Event) {
|
|
|
+ private scrollIntoView(panelId: string) {
|
|
|
+ const contentPanel = document.getElementById(panelId);
|
|
|
+ let parent: HTMLElement | null = null;
|
|
|
+ while ((parent = contentPanel?.parentElement ?? null)) {
|
|
|
+ if (parent.classList.contains('accordion-item')) break;
|
|
|
+ }
|
|
|
+ parent?.scrollIntoView();
|
|
|
+ }
|
|
|
+
|
|
|
+ onAccordeonChange(event: NgbPanelChangeEvent) {
|
|
|
+ if (event.nextState) {
|
|
|
+ const checkId = this.PANEL_ID_REG.exec(event.panelId)?.[1];
|
|
|
+ if (this.params) this.params.checkID = Number(checkId);
|
|
|
+ history.pushState(null, '', `/admin/svc/${this.params?.serverID}/${checkId}`);
|
|
|
+ } else {
|
|
|
+ if (this.params) this.params.checkID = undefined;
|
|
|
+ history.pushState(null, '', `/admin/svc/${this.params?.serverID}`);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async saveServiceCheck(event: Event) {
|
|
|
event.stopPropagation();
|
|
|
- this.formRef?.save();
|
|
|
+ try {
|
|
|
+ if (!this.params || !this.params.serverID) return;
|
|
|
+ const savedCheck = await this.formRef?.save();
|
|
|
+ const panelId = `${this.PANEL_ID_PFX}${savedCheck.id}`;
|
|
|
+
|
|
|
+ if (this.params.checkID === -1) {
|
|
|
+ await this.fetchServiceChecks(this.params.serverID);
|
|
|
+ this.params.activeIds = [panelId];
|
|
|
+ this.onAccordeonChange({ nextState: true, panelId, preventDefault: () => {} });
|
|
|
+ } else {
|
|
|
+ const idx = this.serviceChecks.findIndex(c => savedCheck.id === c.id);
|
|
|
+ if (idx >= 0) this.serviceChecks[idx] = savedCheck;
|
|
|
+ }
|
|
|
+ } catch (error) {
|
|
|
+ console.error(error);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
addServiceCheck(serverId: number) {
|
|
|
- this.serviceChecks.unshift({ serverId, checks: [] as string[] } as HttpCheckConfig);
|
|
|
+ const panelId = `${this.PANEL_ID_PFX}-1`;
|
|
|
+ this.serviceChecks.unshift({ id: -1, serverId, checks: [] as string[] } as HttpCheckConfig);
|
|
|
+ if (this.params) {
|
|
|
+ this.params.activeIds = [panelId];
|
|
|
+ }
|
|
|
+ this.onAccordeonChange({ nextState: true, panelId, preventDefault: () => {} });
|
|
|
}
|
|
|
}
|