import {Input, Output, Component, OnInit, Inject, Injector, EventEmitter} from '@angular/core'; import {SimpleComponent} from '../shared/form/field-simple.component'; import {FieldBase} from '../shared/form/field-base'; import {FormGroup, FormControl, Validators} from '@angular/forms'; import * as _ from "lodash-es"; import {Checks} from './helpers'; import {LabarchivesService} from '../labarchives.service'; // STEST-22 declare var jQuery: any; /** * Contributor Model * * @author moisbo * */ export class LabarchivesLinkField extends FieldBase { closeLabel: string; processing: boolean; workspaceDetailsTitle: string; workspaceDefinition: string; currentWorkspace: any; processingLabel: string; processingMessage: string; processingSuccess: string; processingFail: string; processingStatus: string; processingNoPermission: string; checks: Checks; rdmp: string; @Input() LinkItem: EventEmitter = new EventEmitter(); labarchivesService: LabarchivesService; constructor(options: any, injector: any) { super(options, injector); this.labarchivesService = this.getFromInjector(LabarchivesService); this.closeLabel = 'Close'; this.processing = false; this.workspaceDetailsTitle = 'Workspace'; this.workspaceDefinition = ''; this.currentWorkspace = {}; this.processingLabel = options['processingLabel'] || 'Processing...'; this.processingMessage = ''; this.checks = new Checks(); this.processingSuccess = options['processingSuccess'] || 'Success'; this.processingFail = options['processingFail'] || 'There was a problem linking your workspace, please try again later'; this.processingStatus = ''; this.processingNoPermission = options['processingNoPermission'] || 'You are not allowed to modify this item' } init() { this.rdmp = this.fieldMap._rootComp.rdmp; } registerEvents() { this.fieldMap['List'].field['link'].subscribe(this.linkItem.bind(this)); } createFormModel(valueElem: any = undefined): any { if (valueElem) { this.value = valueElem; } this.formModel = new FormControl(this.value || []); if (this.value) { this.setValue(this.value); } return this.formModel; } setValue(value: any) { this.formModel.patchValue(value, {emitEvent: false}); this.formModel.markAsTouched(); } setEmptyValue() { this.value = []; return this.value; } async linkItem(item: any) { try { jQuery('#linkModal').modal('show'); this.processing = true; this.processingStatus = 'linking'; const link = await this.labarchivesService.link(item, this.rdmp); this.processingStatus = 'done'; this.checks.link = true; this.processing = false; if (link.status) { this.checks.linkCreated = true; this.checks.master = true; this.processingFail = undefined; } else if(link.message === 'cannot insert node'){ this.processingFail = this.processingNoPermission; this.checks.linkWithOther = true } } catch (e) { this.processing = false; this.checks.linkWithOther = true; } } } /** * Component that Links Workspaces to Workspace Records in Stash */ @Component({ selector: 'ws-labarchiveslink', template: ` Link Workspace {{ field.workspaceDetailsTitle }} {{ item.label }} : {{ field.currentWorkspace[item.name]}} {{ field.processingLabel }} {{ field.processingMessage }} {{ field.processingSuccess }} {{ field.processingFail }} ` }) export class LabarchivesLinkComponent extends SimpleComponent { field: LabarchivesLinkField; ngOnInit() { this.field.init(); this.field.registerEvents(); } }
{{ item.label }} : {{ field.currentWorkspace[item.name]}}
{{ field.processingMessage }}
{{ field.processingSuccess }}
{{ field.processingFail }}