labarchives-link.component.ts 4.74 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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;

  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;
    this.workspaceDetailsTitle = 'Workspace Title';
    this.workspaceDefinition = '';
    this.currentWorkspace = {};
    this.processingLabel = 'Processing...';
    this.processingMessage = 'processing message';
    this.checks = new Checks();
    this.processingSuccess = 'Success';
    this.processingFail = 'Failed';
    this.processingStatus = '';
  }

  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 {
        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: `
    <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>
            <h5>{{ field.processingLabel }}</h5>
Moises Sacal's avatar
Moises Sacal committed
127
            <p>{{ field.processingMessage }}&nbsp;</p>
Moises Sacal's avatar
Moises Sacal committed
128
129
130
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
            <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();
  }
}