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

200 lines
5.9 KiB
TypeScript
Raw Normal View History

2023-01-25 16:08:32 +01:00
import { Component, Inject,AfterViewInit, ViewChild, OnInit } from '@angular/core';
import { ISService } from '../is.service';
2023-01-26 14:56:39 +01:00
import { ISUtilsService } from '../is-utils.service';
2023-01-25 16:08:32 +01:00
import { MatTableDataSource } from '@angular/material/table';
import { MatSort, Sort } from '@angular/material/sort';
import { ActivatedRoute } from '@angular/router';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { MatDialog, MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { ResourceType, SimpleResource } from '../model/controller.model';
import {FormControl, FormGroup, FormGroupDirective, NgForm, Validators} from '@angular/forms';
import { ResourceLoader } from '@angular/compiler';
2023-01-24 14:11:39 +01:00
@Component({
selector: 'app-resources',
templateUrl: './resources.component.html',
styleUrls: ['./resources.component.css']
})
2023-01-25 16:08:32 +01:00
export class ResourcesComponent implements OnInit {
typeId:string = '';
type:ResourceType = { id: '', name: '', contentType: '', count: 0, simple: true };
resources:SimpleResource[] = [];
2023-01-26 08:28:12 +01:00
searchText:string = '';
2023-01-25 16:08:32 +01:00
2023-01-26 14:56:39 +01:00
constructor(public service: ISService, public utils:ISUtilsService, public route: ActivatedRoute, public newDialog: MatDialog, public contentDialog: MatDialog, public metadataDialog: MatDialog) {
2023-01-25 16:08:32 +01:00
}
ngOnInit() {
this.route.params.subscribe(params => {
this.typeId = params['type'];
this.service.loadResourceType(this.typeId).subscribe({
next: (data: ResourceType) => this.type = data,
2023-01-26 14:56:39 +01:00
error: error => this.utils.snackError(error)
2023-01-25 16:08:32 +01:00
});
this.reload()
});
}
reload() {
if (this.typeId) {
this.service.loadSimpleResources(this.typeId).subscribe({
next: (data: SimpleResource[]) => this.resources = data,
2023-01-26 14:56:39 +01:00
error: error => this.utils.snackError(error)
2023-01-25 16:08:32 +01:00
});
}
}
openNewDialog(): void {
const dialogRef = this.newDialog.open(ResCreateNewDialog, {
2023-01-26 10:55:02 +01:00
data: this.type,
width: '80%'
2023-01-25 16:08:32 +01:00
});
dialogRef.afterClosed().subscribe(result => {
if (result) this.reload();
});
}
2023-01-24 14:11:39 +01:00
2023-01-25 16:08:32 +01:00
openMetadataDialog(r:SimpleResource): void {
const dialogRef = this.metadataDialog.open(ResMetadataDialog, {
2023-01-26 10:55:02 +01:00
data: r,
width: '80%'
2023-01-25 16:08:32 +01:00
});
dialogRef.afterClosed().subscribe(result => {
if (result) this.reload();
});
}
openContentDialog(r:SimpleResource): void {
2023-01-26 10:55:02 +01:00
this.service.loadSimpleResourceContent(r.id).subscribe({
next: (data: string) => {
const dialogRef = this.contentDialog.open(ResContentDialog, {
data: {
id: r.id,
contentType: this.type.contentType,
content: data
},
width: '80%'
});
dialogRef.afterClosed().subscribe(result => {
if (result) this.reload();
});
},
2023-01-26 14:56:39 +01:00
error: error => this.utils.snackError(error)
2023-01-25 16:08:32 +01:00
});
}
deleteResource(r:SimpleResource) {
if (confirm('Are you sure?')) {
this.service.deleteSimpleResource(r).subscribe({
next: (data: void) => this.reload(),
2023-01-26 14:56:39 +01:00
error: error => this.utils.snackError(error)
2023-01-25 16:08:32 +01:00
});
}
}
2023-01-24 14:11:39 +01:00
}
2023-01-25 16:08:32 +01:00
@Component({
selector: 'res-content-dialog',
templateUrl: 'content-dialog.html',
styleUrls: ['resources.component.css']
})
export class ResContentDialog {
2023-01-26 11:14:41 +01:00
contentFormControl = new FormControl('');
2023-01-26 10:55:02 +01:00
contentForm = new FormGroup({
content : this.contentFormControl
});
2023-01-26 14:56:39 +01:00
constructor(public dialogRef: MatDialogRef<ResContentDialog>, @Inject(MAT_DIALOG_DATA) public data: any, public service: ISService, public utils: ISUtilsService) {
2023-01-26 10:55:02 +01:00
this.contentFormControl.setValue(data.content);
}
onSubmit():void {
let content = this.contentFormControl.value;
if (content) {
this.service.saveSimpleResourceContent(this.data.id, content).subscribe({
next: (data: void) => {
this.dialogRef.close(1)
},
2023-01-26 14:56:39 +01:00
error: error => this.utils.prepareFormError(error, this.contentForm)
2023-01-26 10:55:02 +01:00
});
}
2023-01-25 16:08:32 +01:00
}
onNoClick(): void {
this.dialogRef.close();
}
}
@Component({
selector: 'res-metadata-dialog',
templateUrl: 'metadata-dialog.html',
styleUrls: ['resources.component.css']
})
export class ResMetadataDialog {
metadataForm = new FormGroup({
2023-01-26 11:14:41 +01:00
name: new FormControl(''),
description : new FormControl('')
2023-01-25 16:08:32 +01:00
});
2023-01-26 14:56:39 +01:00
constructor(public dialogRef: MatDialogRef<ResMetadataDialog>, @Inject(MAT_DIALOG_DATA) public data: any, public service: ISService, public utils: ISUtilsService) {
2023-01-25 16:08:32 +01:00
this.metadataForm.get('name')?.setValue(data.name);
if (data.description) {
this.metadataForm.get('description')?.setValue(data.description);
}
}
onSubmit():void {
const res = Object.assign({}, this.data, this.metadataForm.value);
this.service.saveSimpleResourceMedatata(res).subscribe({
next: (data: void) => {
this.dialogRef.close(1)
},
2023-01-26 14:56:39 +01:00
error: error => this.utils.prepareFormError(error, this.metadataForm)
2023-01-25 16:08:32 +01:00
});
}
onNoClick(): void {
this.dialogRef.close();
}
}
@Component({
selector: 'res-new-dialog',
templateUrl: 'new-dialog.html',
styleUrls: ['resources.component.css']
})
export class ResCreateNewDialog {
2023-01-26 10:55:02 +01:00
newResourceForm = new FormGroup({
2023-01-26 11:14:41 +01:00
name: new FormControl(''),
description : new FormControl(''),
content : new FormControl('')
2023-01-26 10:55:02 +01:00
});
2023-01-26 14:56:39 +01:00
constructor(public dialogRef: MatDialogRef<ResCreateNewDialog>, @Inject(MAT_DIALOG_DATA) public data: any, public service: ISService, public utils: ISUtilsService) {}
2023-01-25 16:08:32 +01:00
2023-01-26 10:55:02 +01:00
onSubmit():void {
let name:string = this.newResourceForm.get('name')?.value!;
let type:string = this.data.id!;
let description:string = this.newResourceForm.get('description')?.value!;
let content:string = this.newResourceForm.get('content')?.value!;
this.service.addSimpleResource(name, type, description, content).subscribe({
next: (data: void) => {
this.dialogRef.close(1)
},
2023-01-26 14:56:39 +01:00
error: error => this.utils.prepareFormError(error, this.newResourceForm)
2023-01-26 10:55:02 +01:00
});
}
2023-01-25 16:08:32 +01:00
onNoClick(): void {
this.dialogRef.close();
}
}