labarchives-link.component.ts 5.16 KB
Newer Older
Moises Sacal's avatar
Moises Sacal committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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 <a target='_' href='https://github.com/moisbo'>moisbo</a>
 *
 */
export class LabarchivesLinkField extends FieldBase<any> {

  closeLabel: string;
  processing: boolean;
  workspaceDetailsTitle: string;
  workspaceDefinition: string;
  currentWorkspace: any;
  processingLabel: string;
  processingMessage: string;
  processingSuccess: string;
  processingFail: string;
  processingStatus: string;
31
  processingNoPermission: string;
Moises Sacal's avatar
Moises Sacal committed
32
33
34
35
36
37
38
39
40
41
42
43
44

  checks: Checks;
  rdmp: string;

  @Input() LinkItem: EventEmitter<any> = new EventEmitter<any>();

  labarchivesService: LabarchivesService;

  constructor(options: any, injector: any) {
    super(options, injector);
    this.labarchivesService = this.getFromInjector(LabarchivesService);
    this.closeLabel = 'Close';
    this.processing = false;
45
    this.workspaceDetailsTitle = 'Workspace';
Moises Sacal's avatar
Moises Sacal committed
46
47
    this.workspaceDefinition = '';
    this.currentWorkspace = {};
48
49
    this.processingLabel = options['processingLabel'] || 'Processing...';
    this.processingMessage = '';
Moises Sacal's avatar
Moises Sacal committed
50
    this.checks = new Checks();
51
52
    this.processingSuccess = options['processingSuccess'] || 'Success';
    this.processingFail = options['processingFail'] || 'There was a problem linking your workspace, please try again later';
Moises Sacal's avatar
Moises Sacal committed
53
    this.processingStatus = '';
54
    this.processingNoPermission = options['processingNoPermission'] || 'You are not allowed to modify this item'
Moises Sacal's avatar
Moises Sacal committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
  }

  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;
102
103
      } else if(link.message === 'cannot insert node'){
        this.processingFail = this.processingNoPermission;
Moises Sacal's avatar
Moises Sacal committed
104
105
106
107
        this.checks.linkWithOther = true
      }
    } catch (e) {
      this.processing = false;
108
      this.checks.linkWithOther = true;
Moises Sacal's avatar
Moises Sacal committed
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
    }
  }
}

/**
 * Component that Links Workspaces to Workspace Records in Stash
 */
@Component({
  selector: 'ws-labarchiveslink',
  template: `
    <div id="linkModal" class="modal fade" data-keyboard="false">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <h4 class="modal-title">Link Workspace</h4>
          </div>
          <div class="modal-body">
            <h5>{{ field.workspaceDetailsTitle }}</h5>
            <p *ngFor="let item of field.workspaceDefinition">{{ item.label }} :
              {{ field.currentWorkspace[item.name]}}</p>
129
130
            <h5 *ngIf="field.processing">{{ field.processingLabel }}</h5>
            <p *ngIf="field.processing">{{ field.processingMessage }}&nbsp;</p>
Moises Sacal's avatar
Moises Sacal committed
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
            <p class="alert alert-success" *ngIf="field.checks.linkCreated">{{ field.processingSuccess }}</p>
            <p class="alert alert-danger" *ngIf="field.processingStatus === 'done' && field.processingFail">
              {{ field.processingFail }}</p>
            <ng-template #isDone>
              <i class="fa fa-check-circle"></i>
            </ng-template>
            <ng-template #isSpinning>
              <i class="fa fa-spinner fa-spin"></i>
            </ng-template>
          </div>
          <div class="modal-footer">
            <span *ngIf="field.processing; then waitForProcessing; else finishProcessing"></span>
            <ng-template #finishProcessing>
              <button type="button" class="btn btn-secondary" data-dismiss="modal">{{ field.closeLabel }}</button>
            </ng-template>
            <ng-template #waitForProcessing>
              <button type="button" class="btn btn-secondary disabled" data-dismiss="modal">{{ field.closeLabel }}
              </button>
            </ng-template>
          </div>
        </div>
      </div>
    </div>
  `
})
export class LabarchivesLinkComponent extends SimpleComponent {
  field: LabarchivesLinkField;

  ngOnInit() {
    this.field.init();
    this.field.registerEvents();
  }
}