dnet-applications/frontends/dnet-is-application/src/app/dsm/dsm.component.ts

158 lines
4.7 KiB
TypeScript

import { Component, Inject, AfterViewInit, OnInit, ViewChild } from '@angular/core';
import { BrowseTerm, Datasource, KeyValue } from '../model/controller.model';
import { ISService } from '../is.service';
import { ActivatedRoute, Params } from '@angular/router';
import { MatDialog, MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { MatTable, MatTableDataSource } from '@angular/material/table';
import { MatSort, Sort } from '@angular/material/sort';
import { Vocabulary, VocabularyTermSynonym } from '../model/controller.model';
import { Observable, combineLatest } from 'rxjs';
import { map } from 'rxjs/operators';
import { Page } from '../model/controller.model';
import { FormControl, FormGroup, FormGroupDirective, NgForm, Validators } from '@angular/forms';
import { EmitterVisitorContext } from '@angular/compiler';
import { Router } from '@angular/router';
import { PageEvent } from '@angular/material/paginator';
@Component({
selector: 'app-dsm-search',
templateUrl: './dsm-search.component.html',
styleUrls: ['./dsm.component.css']
})
export class DsmSearchComponent implements OnInit {
searchText:string = '';
browsableFields:KeyValue[] = [];
constructor(public service: ISService, public route: ActivatedRoute, public router: Router, public dialog: MatDialog) {
}
ngOnInit() {
this.service.dsmBrowsableFields((data: KeyValue[]) => this.browsableFields = data);
}
search() {
this.router.navigate(['/dsm/results/0/50'], {
queryParams: { value: this.searchText }
});
}
browseField(field:string, label:string) {
const dialogRef = this.dialog.open(DsmBrowseDialog, {
data: { field: field, label: label },
width: '80%'
});
}
}
@Component({
selector: 'app-dsm-results',
templateUrl: './dsm-results.component.html',
styleUrls: ['./dsm.component.css']
})
export class DsmResultsComponent implements OnInit {
results:Datasource[] = [];
nResults:number = 0;
currPage:number = 0;
nPages:number = 0;
pageSize:number = 0;
field?:string;
value:string = '';
constructor(public service: ISService, public route: ActivatedRoute, public router: Router, public dialog: MatDialog) {
}
ngOnInit(): void {
combineLatest([ this.route.params, this.route.queryParams ],
(params: Params, queryParams: Params) => ({ params, queryParams })
).subscribe((res: { params: Params; queryParams: Params }) => {
const { params, queryParams} = res;
this.currPage = parseInt(params['page']);
this.pageSize = parseInt(params['size']);
this.field = queryParams['field'];
this.value = queryParams['value'];
if (this.field) {
this.service.dsmSearchByField(this.field, this.value, this.currPage, this.pageSize, (page:Page<Datasource>) => {
this.results = page.content;
this.nResults = page.totalElements;
this.nPages = page.totalPages;
});
} else {
this.service.dsmSearch(this.value, this.currPage, this.pageSize, (page:Page<Datasource>) => {
this.results = page.content;
this.nResults = page.totalElements;
this.nPages = page.totalPages;
});
}
});
}
changePage(event: PageEvent) {
console.log(event.pageIndex);
let path = '/dsm/results/' + event.pageIndex + '/' + event.pageSize;
let qp = this.field ?
{ field: this.field, value: this.value } :
{ value: this.value };
this.router.navigate([path], {
queryParams: qp
});
}
}
@Component({
selector: 'app-dsm-api',
templateUrl: './dsm-api.component.html',
styleUrls: ['./dsm.component.css']
})
export class DsmApiComponent {
}
@Component({
selector: 'app-dsm-add-api',
templateUrl: './dsm-add-api.component.html',
styleUrls: ['./dsm.component.css']
})
export class DsmAddApiComponent {
}
@Component({
selector: 'dsm-browse-dialog',
templateUrl: 'dsm-browse-dialog.html',
styleUrls: ['./dsm.component.css']
})
export class DsmBrowseDialog implements OnInit{
datasource: MatTableDataSource<BrowseTerm> = new MatTableDataSource<BrowseTerm>([]);
colums: string[] = ['name', 'total'];
@ViewChild(MatSort) sort: MatSort | undefined
constructor(public dialogRef: MatDialogRef<DsmBrowseDialog>, @Inject(MAT_DIALOG_DATA) public data: any, public service: ISService) {
}
ngOnInit(): void {
this.service.dsmBrowse(this.data.field, (res:BrowseTerm[]) => this.datasource.data = res);
}
ngAfterViewInit() {
if (this.sort) this.datasource.sort = this.sort;
}
applyFilter(event: Event) {
const filterValue = (event.target as HTMLInputElement).value.trim().toLowerCase();
this.datasource.filter = filterValue;
}
onNoClick(): void {
this.dialogRef.close();
}
}