import { Component, ViewChild, OnInit, AfterViewInit, ViewEncapsulation } from "@angular/core"; import { MatPaginator, MatSort, MatSnackBar, MatDialog, MatChipList, MatChip } from "@angular/material"; import { Router, ActivatedRoute, Params } from "@angular/router"; import { TranslateService } from "@ngx-translate/core"; import { DataSource } from "@angular/cdk/table"; import { Observable } from "rxjs/Observable"; import { JsonSerializer } from "../../utilities/JsonSerializer"; import { FormGroup, FormControl } from "@angular/forms"; import { SnackBarNotificationComponent } from "../../shared/components/notificaiton/snack-bar-notification.component"; import { BaseErrorModel } from "../../models/error/BaseErrorModel"; import { DataManagementPlanService } from "../../services/data-management-plan/data-management-plan.service"; import { DataManagementPlanModel } from "../../models/data-managemnt-plans/DataManagementPlanModel"; import { ExternalSourcesService } from "../../services/external-sources/external-sources.service"; import { ExternalSourcesItemModel } from "../../models/external-sources/ExternalSourcesItemModel"; import { RequestItem } from "../../models/criteria/RequestItem"; import { DatasetProfileCriteria } from "../../models/criteria/dataset/DatasetProfileCriteria"; import { DataManagementPlanCriteriaComponent } from "../../shared/components/criteria/data-management-plan/dmp-criteria.component"; import { DatasetProfileModel } from "../../models/datasets/DatasetProfileModel"; import { ProjectCriteria } from "../../models/criteria/project/ProjectCriteria"; import { ProjectService } from "../../services/project/project.service"; import { DmpUsersModel } from "../../models/dmpUsers/DmpUsersModel"; import { AddResearchersComponent } from "../../shared/components/add-researchers/add-researchers.component"; import { ViewContainerRef } from '@angular/core'; import { TdDialogService } from '@covalent/core'; import { AvailableProfilesComponent } from "../../shared/components/available-profiles/available-profiles.component"; import { BaseCriteria } from "../../models/criteria/BaseCriteria"; import { DataManagementPlanProfileService } from "../../services/data-management-plan-profile/datamanagement-profile.service"; import { DataManagementPlanProfileListingModel } from "../../models/data-management-plan-profile/DataManagementPlanProfileListingModel"; import { DataManagementPlanProfileCriteria } from "../../models/criteria/dmp-profile/DataManagementPlanProfileCriteria"; import { DataManagementPlanProfile } from "../../models/data-management-plan-profile/DataManagementPlanProfile"; import { LanguageResolverService } from "../../services/language-resolver/language-resolver.service"; import { IBreadCrumbComponent } from "../../shared/components/breadcrumb/definition/IBreadCrumbComponent"; import { BreadcrumbItem } from "../../shared/components/breadcrumb/definition/breadcrumb-item"; import { SingleAutoCompleteConfiguration } from "../../shared/components/autocompletes/single/single-auto-complete-configuration"; import { MultipleAutoCompleteConfiguration } from "../../shared/components/autocompletes/multiple/multiple-auto-complete-configuration"; @Component({ selector: 'app-dmp-editor-component', templateUrl: 'dmp-editor.component.html', styleUrls: ['./dmp-editor.component.scss'], encapsulation: ViewEncapsulation.None }) export class DataManagementPlanEditorComponent implements AfterViewInit, IBreadCrumbComponent { breadCrumbs: Observable; isNew = true; textCtrl = new FormControl(); dataManagementPlan: DataManagementPlanModel; formGroup: FormGroup = null; filteringOrganisationsAsync: boolean = false; filteringResearchersAsync: boolean = false; filteredProfilesAsync: boolean = false; filteredOrganisations: ExternalSourcesItemModel[]; filteredResearchers: ExternalSourcesItemModel[]; filteredProfiles: DatasetProfileModel[]; projectAutoCompleteConfiguration: SingleAutoCompleteConfiguration; profilesAutoCompleteConfiguration: MultipleAutoCompleteConfiguration; organisationsAutoCompleteConfiguration: MultipleAutoCompleteConfiguration; researchersAutoCompleteConfiguration: MultipleAutoCompleteConfiguration; createNewVersion; associatedUsers: Array filteredOptions: Observable constructor( private dmpProfileService: DataManagementPlanProfileService, private dataManagementPlanService: DataManagementPlanService, private projectService: ProjectService, private externalSourcesService: ExternalSourcesService, private route: ActivatedRoute, public snackBar: MatSnackBar, public router: Router, public language: TranslateService, private _service: DataManagementPlanService, public dialog: MatDialog, private _dialogService: TdDialogService, private _viewContainerRef: ViewContainerRef, private languageResolverService: LanguageResolverService ) { this.filteredOptions = dmpProfileService.getAll({ criteria: new DataManagementPlanProfileCriteria() }); } ngAfterViewInit() { this.route.params.subscribe((params: Params) => { const itemId = params['id']; let projectRequestItem: RequestItem = new RequestItem(); projectRequestItem.criteria = new ProjectCriteria(); let organisationRequestItem: RequestItem = new RequestItem(); organisationRequestItem.criteria = new BaseCriteria(); //this.projectAutoCompleteConfiguration = new AutoCompleteConfiguration(this.projectService.getWithExternal.bind(this.projectService), projectRequestItem); this.projectAutoCompleteConfiguration = { filterFn: this.searchProject.bind(this), items: this.searchProject(''), displayFn: (item) => item["label"], titleFn: (item) => item["label"], //mapFn: (item) => new JsonSerializer().fromJSONArray(item, ProjectReference).map(item => item.toDropdownList()), loadDataOnStart: true }; this.profilesAutoCompleteConfiguration = { filterFn: this.filterProfiles.bind(this), initialItems: (excludedItems: any[]) => this.filterProfiles('').map(result => result.filter(x => excludedItems.map(x => x.id).indexOf(x.id) == -1)), displayFn: (item) => item["label"], titleFn: (item) => item["label"], //mapFn: (item) => new JsonSerializer().fromJSONArray(item, ProjectReference).map(item => item.toDropdownList()), loadDataOnStart: true }; this.organisationsAutoCompleteConfiguration = { filterFn: this.filterOrganisations.bind(this), initialItems: (excludedItems: any[]) => this.filterOrganisations('').map(result => result.filter(x => excludedItems.map(x => x.id).indexOf(x.id) == -1)), displayFn: (item) => item["name"], titleFn: (item) => item["name"], loadDataOnStart: true }; this.researchersAutoCompleteConfiguration = { filterFn: this.filterResearchers.bind(this), initialItems: (excludedItems: any[]) => this.filterResearchers('').map(result => result.filter(x => excludedItems.map(x => x.id).indexOf(x.id) == -1)), displayFn: (item) => item["name"], titleFn: (item) => item["name"], loadDataOnStart: true }; if (itemId != null) { this.isNew = false; this.dataManagementPlanService.getSingle(itemId).map(data => data as DataManagementPlanModel) .subscribe(async data => { this.dataManagementPlan = JsonSerializer.fromJSONObject(data, DataManagementPlanModel); this.formGroup = this.dataManagementPlan.buildForm(); if (this.formGroup.get("profile") && this.formGroup.get("profile").value) { this.textCtrl.patchValue(this.formGroup.get("profile").value); } this.breadCrumbs = Observable.of([ { parentComponentName: "DataManagementPlanListingComponent", label: 'DMPs', url: "dmps", notFoundResolver: [await this.projectService.getSingle(this.dataManagementPlan.project.id).map(x => ({ label: x.label, url: '/projects/edit/' + x.id }) as BreadcrumbItem).toPromise()] } ] ) this.associatedUsers = data.associatedUsers; }); } else { this.dataManagementPlan = new DataManagementPlanModel(); setTimeout(async () => { this.formGroup = this.dataManagementPlan.buildForm(); if (this.formGroup.get("profile") && this.formGroup.get("profile").value) { this.textCtrl.patchValue(this.formGroup.get("profile").value); } this.breadCrumbs = Observable.of([ { parentComponentName: "DataManagementPlanListingComponent", label: 'DMPs', url: "dmps", } ]) }); } }); this.route .queryParams .subscribe(params => { this.createNewVersion = params["clone"]; }); } searchProject(query: string) { let projectRequestItem: RequestItem = new RequestItem(); projectRequestItem.criteria = new ProjectCriteria(); projectRequestItem.criteria.like = query; return this.projectService.getWithExternal(projectRequestItem); } formSubmit(): void { //this.touchAllFormFields(this.formGroup); if (!this.isFormValid()) { return; } this.onSubmit(); } public isFormValid() { return this.formGroup.valid; } onSubmit(): void { this.dataManagementPlanService.createDataManagementPlan(this.formGroup.value).subscribe( complete => this.onCallbackSuccess(), error => this.onCallbackError(error) ) } onCallbackSuccess(): void { this.snackBar.openFromComponent(SnackBarNotificationComponent, { data: { message: this.isNew ? 'GENERAL.SNACK-BAR.SUCCESSFUL-CREATION' : 'GENERAL.SNACK-BAR.SUCCESSFUL-UPDATE', language: this.language }, duration: 3000, }) this.router.navigate(['/dmps']); } onCallbackError(error: any) { this.setErrorModel(error.error); //this.validateAllFormFields(this.formGroup); } public setErrorModel(errorModel: BaseErrorModel) { Object.keys(errorModel).forEach(item => { (this.dataManagementPlan.errorModel)[item] = (errorModel)[item]; }) } public cancel(): void { this.router.navigate(['/dmps']); } public invite(): void { this.router.navigate(['/invite/' + this.dataManagementPlan.id]); } filterOrganisations(value: string): Observable { this.filteredOrganisations = undefined; this.filteringOrganisationsAsync = true; return this.externalSourcesService.searchDMPOrganizations(value) } filterResearchers(value: string): Observable { this.filteredResearchers = undefined; this.filteringResearchersAsync = true; return this.externalSourcesService.searchDMPResearchers({ criteria: { name: value, like: null } }) } filterProfiles(value: string): Observable { this.filteredProfiles = undefined; this.filteredProfilesAsync = true; const request = new RequestItem(); let criteria = new DatasetProfileCriteria(); criteria.like = value; request.criteria = criteria; return this._service.searchDMPProfiles(request) } addResearcher(rowId: any, rowName: any) { let dialogRef = this.dialog.open(AddResearchersComponent, { height: '255px', width: '700px', data: { dmpId: rowId, dmpName: rowName } }); } availableProfiles() { let dialogRef = this.dialog.open(AvailableProfilesComponent, { height: '355px', width: '700px', data: { profiles: this.formGroup.get("profiles") } }); return false; } openConfirm(dmpLabel, id): void { this._dialogService.openConfirm({ message: 'Are you sure you want to delete the "' + dmpLabel + '"', disableClose: true || false, // defaults to false viewContainerRef: this._viewContainerRef, //OPTIONAL title: 'Confirm', //OPTIONAL, hides if not provided cancelButton: 'No', //OPTIONAL, defaults to 'CANCEL' acceptButton: 'Yes' //OPTIONAL, defaults to 'ACCEPT' // width: '500px', //OPTIONAL, defaults to 400px }).afterClosed().subscribe((accept: boolean) => { if (accept) { this.dataManagementPlanService.delete(id).subscribe(() => { this.router.navigate(['/dmps']) }); } else { // DO SOMETHING ELSE } }); } selectOption(option: any) { this.dataManagementPlan.definition = null; this.formGroup.get("profile").patchValue(option, { emitEvent: false }) this.dmpProfileService.getSingle(option.id).subscribe(result => { this.dataManagementPlan.definition = result.definition; }) } displayWith(item: any) { if (!item) return null; return item["label"]; } redirectToProject() { this.router.navigate(["projects/edit/" + this.dataManagementPlan.project.id]) } redirectToDatasets() { this.router.navigate(["datasets/dmp/" + this.dataManagementPlan.id]) } newVersion(id: String, label: String) { this.router.navigate(['/dmps/new_version/' + id, { dmpLabel: label }]); } clone(id: String) { this.router.navigate(['/dmps/clone/' + id]); } viewVersions(rowId: String, rowLabel: String) { this.router.navigate(['/dmps/viewversions/' + rowId], { queryParams: { groupLabel: rowLabel } }); } }