labarchives-form.component.ts 5.23 KB
Newer Older
1
2
3
4
5
6
7
8
9
import {Component, Inject, Input, ElementRef, Output, EventEmitter} from '@angular/core';
import {Location, LocationStrategy, PathLocationStrategy} from '@angular/common';
import {FormGroup, FormControl, Validators, NgForm} from '@angular/forms';
import {RecordsService} from './shared/form/records.service';
import {LoadableComponent} from './shared/loadable.component';
import {FieldControlService} from './shared/form/field-control.service';
import {Observable} from 'rxjs/Observable';
import * as _ from "lodash-es";
import {TranslationService} from './shared/translation-service';
Moises Sacal's avatar
Moises Sacal committed
10

11
12
import {LabarchivesLoginComponent, LabarchivesLoginField} from './components/labarchives-login.component';
import {LabarchivesListComponent, LabarchivesListField} from './components/labarchives-list.component';
Moises Sacal's avatar
Moises Sacal committed
13
import {LabarchivesLinkField, LabarchivesLinkComponent} from './components/labarchives-link.component';
14
import {LabarchivesCreateField, LabarchivesCreateComponent} from './components/labarchives-create.component';
15
16
17
18
19
20
21
22
23
24

import * as jQuery from 'jquery';


/**
 * Main LabArchives Edit component
 *
 * @author <a target='_' href='https://github.com/moisbo'>moisbo</a>
 *
 */
Moises Sacal's avatar
Moises Sacal committed
25
@Component({
26
27
  moduleId: module.id,
  selector: 'labarchives-form',
Moises Sacal's avatar
Moises Sacal committed
28
  templateUrl: './labarchives-form.component.html',
29
30
  styleUrls: ['./labarchives-form.component.css'],
  providers: [Location, {provide: LocationStrategy, useClass: PathLocationStrategy}]
Moises Sacal's avatar
Moises Sacal committed
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
export class LabarchivesFormComponent extends LoadableComponent {
  /**
   * The OID for this Form.
   *
   */
  @Input() oid: string;
  /**
   * Edit mode
   *
   */
  @Input() editMode: boolean;
  /**
   * The Record type
   *
   */
  @Input() recordType: string;
  /**
   * Fields for the form
   */
  fields: any[] = [];
  /**
   * Form group
   */
  form: FormGroup;
  /**
   * Initialization subscription
   */
  initSubs: any;
  /**
   * Field map
   */
  fieldMap: any;
  /**
   * Form JSON string
   */
  payLoad: any;
  /**
   * Form status
   */
  status: any;
  /**
   * Critical error message
   */
  criticalError: any;
  /**
   * Form definition
   */
  formDef: any;
  /**
   * CSS classes for this form
   */
  cssClasses: any;
  /**
   * Flag when form needs saving.
   *
   */
  needsSave: boolean;
  /**
   * Links to tabs
   */
  failedValidationLinks: any[];
  /**
   * Expects a number of DI'ed elements.
   */

  login: any;
  loading: boolean = false;
  loggedIn: any;
  rdmp: string;
Moises Sacal's avatar
Moises Sacal committed
101
102
  user: any;

103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
  constructor(
    elm: ElementRef,
    @Inject(RecordsService) protected RecordsService: RecordsService,
    @Inject(FieldControlService) protected fcs: FieldControlService,
    @Inject(Location) protected LocationService: Location,
    public translationService: TranslationService
  ) {
    super();
    this.oid = elm.nativeElement.getAttribute('oid');
    this.editMode = elm.nativeElement.getAttribute('editMode') == "true";
    this.recordType = elm.nativeElement.getAttribute('recordType');
    this.rdmp = elm.nativeElement.getAttribute('rdmp');

    //TODO: Find out what is this next line!
    this.fieldMap = {_rootComp: this};
    this.user = {};

    this.initSubs = RecordsService.waitForInit((initStat: boolean) => {
      this.initSubs.unsubscribe();
      this.loadForm();
    });

Moises Sacal's avatar
Moises Sacal committed
125
126
  }

127
  loginUser(user) {
Moises Sacal's avatar
Moises Sacal committed
128
129
    this.user = user;
  }
130
131
132
133
134
135
136
137

  registerEvents() {
    //this.fieldMap['ListWorkspaces'].field['setWorkspaceUser'].subscribe(this.setWorkspaceUser.bind(this));
  }

  loadForm() {
    this.fcs.addComponentClasses({
      'LabarchivesLoginField': {'meta': LabarchivesLoginField, 'comp': LabarchivesLoginComponent},
Moises Sacal's avatar
Moises Sacal committed
138
      'LabarchivesListField': {'meta': LabarchivesListField, 'comp': LabarchivesListComponent},
139
140
      'LabarchivesLinkField': {'meta': LabarchivesLinkField, 'comp': LabarchivesLinkComponent},
      'LabarchivesCreateField': {'meta': LabarchivesCreateField, 'comp': LabarchivesCreateComponent}
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
    });

    this.RecordsService.getForm(this.oid, this.recordType, this.editMode).then((obs: any) => {
      obs.subscribe((form: any) => {
        this.formDef = form;
        if (this.editMode) {
          this.cssClasses = this.formDef.editCssClasses;
        } else {
          this.cssClasses = this.formDef.viewCssClasses;
        }
        this.loggedIn = false;
        if (form.fieldsMeta) {
          this.fields = form.fieldsMeta;
          this.rebuildForm();
          this.watchForChanges();
          this.registerEvents();
          const focusTab = '?focusTabId=workspaces';
          this.fieldMap['BackToPlan'].field.value = this.fieldMap['BackToPlan'].field.value + this.rdmp + focusTab;
        }
      });
    }).catch((err: any) => {
      console.log("Error loading form...");
      console.log(err);
      if (err.status == false) {
        this.criticalError = err.message;
      }
      this.setLoading(false);
    });
  }

  /**
   * Rebuild the form message.
   *
   * @return {[type]}
   */
  rebuildForm() {
    this.form = this.fcs.toFormGroup(this.fields, this.fieldMap);
  }

  /**
   * Enable form change monitor.
   *
   * @return {[type]}
   */
  watchForChanges() {
    this.setLoading(false);
    if (this.editMode) {
      this.form.valueChanges.subscribe((data: any) => {
        //this.needsSave = true;
      });
    }
  }

  // setWorkspaceUser(workspaceUser: WorkspaceUser) {
  //   this.workspaceUser = workspaceUser;
  // }

Moises Sacal's avatar
Moises Sacal committed
198
}
199