labarchives-list.component.ts 6.24 KB
Newer Older
1
2
3
4
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';
Moises Sacal's avatar
Moises Sacal committed
5
import {LabarchivesService} from "../labarchives.service";
6
7
8
9
10
11
12
13
14
15

export class LabarchivesListField extends FieldBase<any> {

  columns: any;
  syncLabel: any;
  accessDeniedObjects: any = [];
  failedObjects: any = [];
  rdmpLinkLabel: string;

  loading: boolean;
Moises Sacal's avatar
Moises Sacal committed
16
  labUser: any = {};
17
18
  rdmp: string;
  workspaces: any;
Moises Sacal's avatar
Moises Sacal committed
19
20
21
22
  linkedLabel: string;
  linkedAnotherLabel: string;
  linkLabel: string;
  linkProblem: string;
23
24

  @Input() user: any;
Moises Sacal's avatar
Moises Sacal committed
25
  @Output() link: EventEmitter<any> = new EventEmitter<any>();
26

Moises Sacal's avatar
Moises Sacal committed
27
28
  labarchivesService: LabarchivesService;

29
30
  constructor(options: any, injector: any) {
    super(options, injector);
Moises Sacal's avatar
Moises Sacal committed
31
    this.labarchivesService = this.getFromInjector(LabarchivesService);
32
33
    this.columns = [
      {'label': 'Name', 'property': 'name'},
Moises Sacal's avatar
Moises Sacal committed
34
      {'label': 'Default', 'property': 'isDefault'}
35
36
37
    ];
    this.rdmpLinkLabel = 'Plan';
    this.syncLabel = 'Sync';
Moises Sacal's avatar
Moises Sacal committed
38
39
40
41
    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';
42
43
  }

44
  registerEvents() {
Moises Sacal's avatar
Moises Sacal committed
45
    this.fieldMap['Login'].field['userLogin'].subscribe(this.bindUser.bind(this));
Moises Sacal's avatar
Moises Sacal committed
46
    //this.fieldMap['Link'].field['linkItem'].subscribe(this.listWorkspaces.bind(this));
47
48
  }

Moises Sacal's avatar
Moises Sacal committed
49
50
  bindUser(labUser: any) {
    console.log(labUser);
Moises Sacal's avatar
Moises Sacal committed
51
    if (labUser && labUser.notebooks) {
Moises Sacal's avatar
Moises Sacal committed
52
53
54
55
56
57
58
59
      this.labUser = labUser;
      this.listWorkspaces();
    }
  }

  listWorkspaces() {
    if (this.labUser && this.labUser.notebooks) {
      this.workspaces = this.labUser.notebooks.map((nb) => {
Moises Sacal's avatar
Moises Sacal committed
60
        const isDefault = nb['isDefault'] ? 'default' : '';
61
        return {
Moises Sacal's avatar
Moises Sacal committed
62
          id: nb['id'],
63
          name: nb['name'],
Moises Sacal's avatar
Moises Sacal committed
64
          isDefault: isDefault ? 'Default Notebook' : '',
Moises Sacal's avatar
Moises Sacal committed
65
          rdmp: {info: ''}
66
67
        }
      });
Moises Sacal's avatar
Moises Sacal committed
68
      this.checkLinks();
69
70
71
    } else {
      this.workspaces = [];
    }
72
73
  }

74
  linkWorkspace(item: any) {
Moises Sacal's avatar
Moises Sacal committed
75
    this.link.emit(item);
76
77
  }

Moises Sacal's avatar
Moises Sacal committed
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
  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';
        });
    });
  }
Moises Sacal's avatar
Moises Sacal committed
101

102
}
Moises Sacal's avatar
Moises Sacal committed
103
104
105


@Component({
106
  selector: 'ws-labarchiveslist',
Moises Sacal's avatar
Moises Sacal committed
107
  template: `
108
    <div class="row">
Moises Sacal's avatar
Moises Sacal committed
109
      <div *ngIf="field.workspaces" class="col-lg-12">
110
111
112
113
114
115
116
117
118
119
120
121
122
123
        <div class="">
          <table class="table">
            <thead>
            <tr>
              <ng-container *ngFor="let header of field.columns">
                <th>{{ header.label }}</th>
              </ng-container>
              <th>{{ field.rdmpLinkLabel }}</th>
            </tr>
            </thead>
            <tbody>
            <tr *ngFor="let item of field.workspaces">
              <ng-container *ngFor="let column of field.columns">
                <td *ngIf="column.show != false">
Moises Sacal's avatar
Moises Sacal committed
124
125
126
127
                  <span *ngIf="column.link; else noProcessing ">
                    <a target="_blank" rel="noopener noreferrer"
                       href="{{ item[column.property] }}">{{ item[column.property]}}</a>
                  </span>
128
129
130
131
132
                  <ng-template #multivalue></ng-template>
                  <ng-template #noProcessing><span>{{ item[column.property] }}</span></ng-template>
                </td>
              </ng-container>
              <td>
Moises Sacal's avatar
Moises Sacal committed
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
                <span *ngIf="!item['linkedState']">
                  <button type="button" disabled class="btn btn-info btn-block" [name]="item['@id']">
                    <span><i class="fa fa-spinner fa-spin"></i></span></button>
                </span>
                <span *ngIf="item['linkedState'] === 'linked'">
                  <button type="button" disabled class="btn btn-success btn-block"
                          [name]="item['@id']">{{ field.linkedLabel }}</button>
                </span>
                <span *ngIf="item['linkedState'] === 'another'">
                  <button type="button" disabled class="btn btn-info btn-block"
                          [name]="item['@id']">{{ field.linkedAnotherLabel }}</button>
                </span>
                <span *ngIf="item['linkedState'] === 'link'">
                  <button type="button" class="btn btn-info btn-block" [name]="item['@id']"
                          (click)="field.linkWorkspace(item)">{{ field.linkLabel }}</button>
                </span>
                <span *ngIf="item['linkedState'] === 'problem'">
                  <button type="button" disabled class="btn btn-warning btn-block"
                          [name]="item['@id']">{{ field.linkProblem }}</button>
                </span>
153
154
155
156
157
158
159
160
              </td>
            </tr>
            </tbody>
          </table>
          <div *ngIf="field.loading" class="">
            <img class="center-block" src="/images/loading.svg">
          </div>
          <div>
Moises Sacal's avatar
Moises Sacal committed
161
162
163
164
165
            <p *ngIf="field.failedObjects.length > 0">There were {{ field.failedObjects.length }} records that failed to
              load</p>
            <p *ngIf="field.accessDeniedObjects.length > 0">There were {{ field.accessDeniedObjects.length }} records
              that
              you do not have access to</p>
166
167
          </div>
        </div>
168
      </div>
Moises Sacal's avatar
Moises Sacal committed
169
170
171
    </div>
  `
})
172
173
export class LabarchivesListComponent extends SimpleComponent {
  field: LabarchivesListField;
Moises Sacal's avatar
Moises Sacal committed
174
175

  ngOnInit() {
176
    this.field.registerEvents();
Moises Sacal's avatar
Moises Sacal committed
177
  }
Moises Sacal's avatar
Moises Sacal committed
178
179
180
181
182

  ngAfterContentInit() {
    this.field.listWorkspaces();
  }

Moises Sacal's avatar
Moises Sacal committed
183
}