import {Output, EventEmitter, Component, OnInit, Inject, Injector, Input} from '@angular/core'; import {FormGroup, FormControl, Validators, NgForm} from '@angular/forms'; import {SimpleComponent} from '../shared/form/field-simple.component'; import {FieldBase} from '../shared/form/field-base'; import {LabarchivesService} from "../labarchives.service"; export class LabarchivesListField extends FieldBase { columns: any; syncLabel: any; accessDeniedObjects: any = []; failedObjects: any = []; rdmpLinkLabel: string; loading: boolean; labUser: any = {}; rdmp: string; workspaces: any; linkedLabel: string; linkedAnotherLabel: string; linkLabel: string; linkProblem: string; @Input() user: any; @Output() link: EventEmitter = new EventEmitter(); labarchivesService: LabarchivesService; constructor(options: any, injector: any) { super(options, injector); this.labarchivesService = this.getFromInjector(LabarchivesService); this.columns = [ {'label': 'Name', 'property': 'name'}, {'label': 'Default', 'property': 'isDefault'} ]; this.rdmpLinkLabel = 'Plan'; this.syncLabel = 'Sync'; this.linkedLabel = options['linkedLabel'] || 'Linked'; this.linkedAnotherLabel = options['linkedAnotherLabel'] || 'Linked to another workspace'; this.linkLabel = options['linkLabel'] || 'Link Workspace'; this.linkProblem = options['linkProblem'] || 'There was a problem checking the link'; } registerEvents() { this.fieldMap['Login'].field['userLogin'].subscribe(this.bindUser.bind(this)); //this.fieldMap['Link'].field['linkItem'].subscribe(this.listWorkspaces.bind(this)); } bindUser(labUser: any) { console.log(labUser); if (labUser && labUser.notebooks) { this.labUser = labUser; this.listWorkspaces(); } } listWorkspaces() { if (this.labUser && this.labUser.notebooks) { this.workspaces = this.labUser.notebooks.map((nb) => { const isDefault = nb['isDefault'] ? 'default' : ''; return { id: nb['id'], name: nb['name'], isDefault: isDefault ? 'Default Notebook' : '', rdmp: {info: ''} } }); this.checkLinks(); } else { this.workspaces = []; } } linkWorkspace(item: any) { this.link.emit(item); } checkLinks() { //this.workspaces[index]['linkedState'] == 'check'; // Possible values: linked, another, link this.workspaces.map((w, index) => { this.labarchivesService.checkLink(this.rdmp, w['id']) .then((response) => { if (!response.status) { throw new Error('Error checking workspace'); } else { const check = response['check']; console.log(check); if (check['link'] === 'linked') { this.workspaces[index]['linkedState'] = 'linked'; } else { this.workspaces[index]['linkedState'] = 'link'; } } }) .catch((error) => { console.log(error); this.workspaces[index]['linkedState'] = 'problem'; }); }); } } @Component({ selector: 'ws-labarchiveslist', template: `
{{ header.label }} {{ field.rdmpLinkLabel }}
{{ item[column.property]}} {{ item[column.property] }}

There were {{ field.failedObjects.length }} records that failed to load

There were {{ field.accessDeniedObjects.length }} records that you do not have access to

` }) export class LabarchivesListComponent extends SimpleComponent { field: LabarchivesListField; ngOnInit() { this.field.registerEvents(); } ngAfterContentInit() { this.field.listWorkspaces(); } }