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

164 lines
4.7 KiB
TypeScript
Raw Normal View History

2023-02-01 09:58:36 +01:00
import { Component, Inject, OnInit } from '@angular/core';
2023-02-01 11:51:23 +01:00
import { ISService } from '../common/is.service';
2023-01-25 16:08:32 +01:00
import { ActivatedRoute } from '@angular/router';
import { MatDialog, MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
2023-02-01 11:51:23 +01:00
import { ResourceType, SimpleResource } from '../common/is.model';
2023-02-01 09:58:36 +01:00
import { FormControl, FormGroup } from '@angular/forms';
2023-01-25 16:08:32 +01:00
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-27 15:04:51 +01:00
constructor(public service: ISService, public route: ActivatedRoute, public dialog: MatDialog) {
2023-01-25 16:08:32 +01:00
}
ngOnInit() {
this.route.params.subscribe(params => {
this.typeId = params['type'];
2023-01-27 08:58:33 +01:00
this.service.loadResourceType(this.typeId, (data: ResourceType) => this.type = data);
2023-01-25 16:08:32 +01:00
this.reload()
});
}
reload() {
if (this.typeId) {
2023-01-27 08:58:33 +01:00
this.service.loadSimpleResources(this.typeId, (data: SimpleResource[]) => this.resources = data);
2023-01-25 16:08:32 +01:00
}
}
openNewDialog(): void {
2023-01-27 15:04:51 +01:00
const dialogRef = this.dialog.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 {
2023-01-27 15:04:51 +01:00
const dialogRef = this.dialog.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-27 08:58:33 +01:00
this.service.loadSimpleResourceContent(r.id, (data: string) => {
2023-01-27 15:04:51 +01:00
const dialogRef = this.dialog.open(ResContentDialog, {
2023-01-27 08:58:33 +01:00
data: {
id: r.id,
contentType: this.type.contentType,
content: data
},
width: '80%'
});
dialogRef.afterClosed().subscribe(result => {
if (result) this.reload();
});
2023-01-25 16:08:32 +01:00
});
}
deleteResource(r:SimpleResource) {
if (confirm('Are you sure?')) {
2023-01-27 11:44:30 +01:00
this.service.deleteSimpleResource(r.id, (data: void) => this.reload());
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-27 08:58:33 +01:00
constructor(public dialogRef: MatDialogRef<ResContentDialog>, @Inject(MAT_DIALOG_DATA) public data: any, public service: ISService) {
2023-01-26 10:55:02 +01:00
this.contentFormControl.setValue(data.content);
}
onSubmit():void {
let content = this.contentFormControl.value;
if (content) {
2023-01-27 08:58:33 +01:00
this.service.saveSimpleResourceContent(this.data.id, content, (data: void) => this.dialogRef.close(1), 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-27 08:58:33 +01:00
constructor(public dialogRef: MatDialogRef<ResMetadataDialog>, @Inject(MAT_DIALOG_DATA) public data: any, public service: ISService) {
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);
2023-01-27 08:58:33 +01:00
this.service.saveSimpleResourceMedatata(res, (data: void) => this.dialogRef.close(1), 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-27 08:58:33 +01:00
constructor(public dialogRef: MatDialogRef<ResCreateNewDialog>, @Inject(MAT_DIALOG_DATA) public data: any, public service: ISService) {}
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!;
2023-01-27 08:58:33 +01:00
this.service.addSimpleResource(name, type, description, content, (data: void) => this.dialogRef.close(1), this.newResourceForm);
2023-01-26 10:55:02 +01:00
}
2023-01-25 16:08:32 +01:00
onNoClick(): void {
this.dialogRef.close();
}
}