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 { AutoCompleteConfiguration } from "../../shared/components/autocomplete/AutoCompleteConfiguration"; import { ProjectCriteria } from "../../models/criteria/project/ProjectCriteria"; import { ProjectService } from "../../services/project/project.service"; import { DmpUsersModel } from "../../models/dmpUsers/DmpUsersModel"; import { AddResearchersComponent } from "../../add-researchers/add-researchers.component"; import { ViewContainerRef } from '@angular/core'; import { TdDialogService } from '@covalent/core'; import { AvailableProfilesComponent } from "../../available-profiles/available-profiles.component"; import { AutoCompleteChipConfiguration } from "../../shared/components/autocompleteChips/AutoCompleteChipConfiguration"; 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"; @Component({ selector: 'app-dmp-editor-component', templateUrl: 'dmp-editor.component.html', styleUrls: ['./dmp-editor.component.scss'], providers: [DataManagementPlanService, ExternalSourcesService, ProjectService, DataManagementPlanProfileService], encapsulation: ViewEncapsulation.None }) export class DataManagementPlanEditorComponent implements AfterViewInit { 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: AutoCompleteConfiguration; organisationsAutoCompleteConfiguration: AutoCompleteChipConfiguration; 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.organisationsAutoCompleteConfiguration = new AutoCompleteChipConfiguration(this.externalSourcesService.searchDMPOrganizations.bind(this.externalSourcesService), organisationRequestItem); if (itemId != null) { this.isNew = false; this.dataManagementPlanService.getSingle(itemId).map(data => data as DataManagementPlanModel) .subscribe(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.associatedUsers = data.associatedUsers; }); } else { this.dataManagementPlan = new DataManagementPlanModel(); setTimeout(() => { this.formGroup = this.dataManagementPlan.buildForm(); if (this.formGroup.get("profile") && this.formGroup.get("profile").value) { this.textCtrl.patchValue(this.formGroup.get("profile").value); } }); } }); this.route .queryParams .subscribe(params => { this.createNewVersion = params["clone"]; }); } 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, extraClasses: ['snackbar-success'] }) 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): void { this.filteredOrganisations = undefined; if (value) { this.filteringOrganisationsAsync = true; this.externalSourcesService.searchDMPOrganizations(value).subscribe(items => { this.filteredOrganisations = items; this.filteringOrganisationsAsync = false; // this.filteredOrganisations = items.filter((filteredObj: any) => { // return this.objectsModel ? this.objectsModel.indexOf(filteredObj) < 0 : true; // }); }); } } filterResearchers(value: string): void { this.filteredResearchers = undefined; if (value) { this.filteringResearchersAsync = true; this.externalSourcesService.searchDMPResearchers({ criteria: { name: value, like: null } }).subscribe(items => { this.filteredResearchers = items; this.filteringResearchersAsync = false; // this.filteredOrganisations = items.filter((filteredObj: any) => { // return this.objectsModel ? this.objectsModel.indexOf(filteredObj) < 0 : true; // }); }); } } filterProfiles(value: string): void { this.filteredProfiles = undefined; if (value) { this.filteredProfilesAsync = true; // this.externalSourcesService.searchDMPProfiles(value).subscribe(items => { // this.filteredProfiles = items; // this.filteredProfilesAsync = false; // // this.filteredOrganisations = items.filter((filteredObj: any) => { // // return this.objectsModel ? this.objectsModel.indexOf(filteredObj) < 0 : true; // // }); // }); const request = new RequestItem(); let criteria = new DatasetProfileCriteria(); criteria.like = value; request.criteria = criteria; this._service.searchDMPProfiles(request).subscribe(items => { this.filteredProfiles = items; this.filteredProfilesAsync = false; }); } } 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: { } }); dialogRef.afterClosed().subscribe(result => { this.formGroup.get("profiles").setValue(result); }); 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"]; } }