Newer
Older

Alex ORLUC
committed
import { Component, ViewChild, OnInit, TemplateRef, ViewContainerRef } from '@angular/core';

Alex ORLUC
committed
import { HttpClient } from '@angular/common/http';
import { LANG } from '../../translate.component';
import { NotificationService } from '../../notification.service';
import { MatPaginator } from '@angular/material/paginator';
import { MatSort } from '@angular/material/sort';
import { MatTableDataSource } from '@angular/material/table';
import { HeaderService } from '../../../service/header.service';

Alex ORLUC
committed
import { AppService } from '../../../service/app.service';
import { tap, finalize, catchError, filter, exhaustMap, map } from 'rxjs/operators';
import { ConfirmComponent } from '../../../plugins/modal/confirm.component';
import { MatDialogRef, MatDialog } from '@angular/material/dialog';

Florian Azizian
committed
import { AlertComponent } from '../../../plugins/modal/alert.component';
import { FunctionsService } from '../../../service/functions.service';
import { of } from 'rxjs/internal/observable/of';

Alex ORLUC
committed
@Component({
templateUrl: 'indexing-models-administration.component.html',
styleUrls: ['indexing-models-administration.component.scss'],
providers: [AppService]

Alex ORLUC
committed
})
export class IndexingModelsAdministrationComponent implements OnInit {

Alex ORLUC
committed
@ViewChild('adminMenuTemplate', { static: true }) adminMenuTemplate: TemplateRef<any>;

Alex ORLUC
committed
lang: any = LANG;
search: string = null;
indexingModels: any[] = [];

Alex ORLUC
committed
loading: boolean = false;
displayedColumns = ['id', 'category', 'label', 'private', 'default', 'enabled', 'actions'];
dataSource = new MatTableDataSource(this.indexingModels);
@ViewChild(MatPaginator, { static: false }) paginator: MatPaginator;
@ViewChild(MatSort, { static: false }) sort: MatSort;
dialogRef: MatDialogRef<any>;
applyFilter(filterValue: string) {
filterValue = filterValue.trim(); // Remove whitespace
filterValue = filterValue.toLowerCase(); // MatTableDataSource defaults to lowercase matches
this.dataSource.filter = filterValue;
this.dataSource.filterPredicate = (template, filterTarget: string) => {
return this.functions.filterUnSensitive(template, filterTarget, ['id', 'label']);

Alex ORLUC
committed
constructor(
public http: HttpClient,
private notify: NotificationService,
private headerService: HeaderService,
public appService: AppService,

Alex ORLUC
committed
public functions: FunctionsService,
private viewContainerRef: ViewContainerRef

Alex ORLUC
committed
ngOnInit(): void {

Alex ORLUC
committed
this.headerService.injectInSideBarLeft(this.adminMenuTemplate, this.viewContainerRef, 'adminMenu');

Alex ORLUC
committed

Alex ORLUC
committed
this.http.get('../rest/indexingModels?showDisabled=true').pipe(
map((data: any) => {
return data.indexingModels.filter((info: any) => info.private === false);
}),

Alex ORLUC
committed
tap((data: any) => {
this.indexingModels = data;
this.headerService.setHeader(this.lang.administration + ' ' + this.lang.indexingModels);
setTimeout(() => {
this.dataSource = new MatTableDataSource(this.indexingModels);
this.dataSource.paginator = this.paginator;
this.dataSource.sortingDataAccessor = this.functions.listSortingDataAccessor;
this.sort.active = 'label';
this.sort.direction = 'asc';
this.dataSource.sort = this.sort;
}, 0);

Alex ORLUC
committed
}),
finalize(() => this.loading = false),
catchError((err: any) => {
this.notify.handleErrors(err);
return of(false);
})
).subscribe();
}

Alex ORLUC
committed

Florian Azizian
committed
if (!indexingModel.used) {
this.dialogRef = this.dialog.open(ConfirmComponent, { panelClass: 'maarch-modal', autoFocus: false, disableClose: true, data: { title: this.lang.delete, msg: this.lang.confirmAction } });

Florian Azizian
committed
this.dialogRef.afterClosed().pipe(
filter((data: string) => data === 'ok'),
exhaustMap(() => this.http.delete('../rest/indexingModels/' + indexingModel.id)),

Florian Azizian
committed
tap(() => {
if (this.indexingModels[i].id === indexingModel.id) {

Florian Azizian
committed
this.indexingModels.splice(Number(i), 1);
}

Florian Azizian
committed
this.dataSource = new MatTableDataSource(this.indexingModels);
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
this.notify.success(this.lang.indexingModelDeleted);
}),
catchError((err: any) => {
this.notify.handleSoftErrors(err);

Florian Azizian
committed
return of(false);
})
).subscribe();
} else {
this.dialog.open(AlertComponent, { panelClass: 'maarch-modal', autoFocus: false, disableClose: true, data: { title: indexingModel.label, msg: this.lang.canNotDeleteIndexingModel } });

Florian Azizian
committed
}

Alex ORLUC
committed
}
disableIndexingModel(indexingModel: any) {
this.dialogRef = this.dialog.open(ConfirmComponent, { panelClass: 'maarch-modal', autoFocus: false, disableClose: true, data: { title: this.lang.disable, msg: this.lang.confirmAction } });
this.dialogRef.afterClosed().pipe(
filter((data: string) => data === 'ok'),
exhaustMap(() => this.http.request('PUT', '../rest/indexingModels/' + indexingModel.id + '/disable')),
tap((data: any) => {
for (const i in this.indexingModels) {
if (this.indexingModels[i].id === indexingModel.id) {
this.indexingModels[i].enabled = false;
}
}
this.notify.success(this.lang.indexingModelDisabled);
}),
catchError((err: any) => {
this.notify.handleErrors(err);
return of(false);
})
).subscribe();
}
enableIndexingModel(indexingModel: any) {
this.dialogRef = this.dialog.open(ConfirmComponent, { panelClass: 'maarch-modal', autoFocus: false, disableClose: true, data: { title: this.lang.enable, msg: this.lang.confirmAction } });
this.dialogRef.afterClosed().pipe(
filter((data: string) => data === 'ok'),
exhaustMap(() => this.http.request('PUT', '../rest/indexingModels/' + indexingModel.id + '/enable')),
tap((data: any) => {
for (let i in this.indexingModels) {
if (this.indexingModels[i].id === indexingModel.id) {
this.indexingModels[i].enabled = true;
}
}
this.notify.success(this.lang.indexingModelEnabled);
}),
catchError((err: any) => {
this.notify.handleErrors(err);
return of(false);
})
).subscribe();
}