Project

General

Profile

1
/**
2
 * Created by stefanos on 15/5/2017.
3
 */
4
import { AfterContentInit, Component, EventEmitter, Injector, Input, OnChanges, OnInit, SimpleChanges } from '@angular/core';
5
import { AbstractControl, FormBuilder, FormGroup } from '@angular/forms';
6
import { Subject } from 'rxjs';
7
import { Description } from '../../../domain/oa-description';
8

    
9

    
10
@Component({
11
  template: ``
12
})
13
export class MyGroup implements OnInit, AfterContentInit {
14

    
15
  @Input() public parentGroup: AbstractControl;
16

    
17
  @Input() public name : string | number;
18

    
19
  @Input() public data : any = null;
20

    
21
  @Input() public otherData: any;
22

    
23
  @Input() public toBeDeleted: boolean;
24

    
25
  public patchData: Subject<any> = new Subject();
26

    
27
  @Input() public required: boolean = false;
28

    
29
  @Input() public description : Description = null;
30

    
31
  @Input() public index: number = -1;
32

    
33
  protected _fb: FormBuilder;
34

    
35
  protected groupDefinition: { [key:string]: any };
36

    
37
  public group: AbstractControl;
38

    
39
  public createdEvent: EventEmitter<any> = new EventEmitter();
40

    
41
  public groupErrorMessage: string;
42

    
43
  public wasSaved: boolean = false;
44

    
45
  public exportedData: any = null;
46
  @Input() public inRegister: boolean = false;
47

    
48
  constructor(injector: Injector) {
49
    this._fb = injector.get(FormBuilder);
50
    this.patchData.subscribe(_ => {
51
      if (typeof _ !== 'undefined') {
52
        setTimeout( () => {
53
          (this.group as FormGroup).patchValue(_);
54
        }, 500);
55
      }
56
    });
57
  }
58

    
59
  protected get isArray() {
60
    return this.index !== -1;
61
  }
62

    
63
  public generate(): FormGroup {
64
    const ret = this._fb.group(this.groupDefinition);
65
    if (this.patchData) {
66
      // console.log(this.patchData);
67
    }
68
    if (!this.required) {
69
      Object.keys(ret.controls).forEach(item => ret.controls[item].clearValidators());
70
    }
71
    return ret;
72
  }
73

    
74
  public getMyControl(name: string): AbstractControl {
75
    if (this.isArray) {
76
      return this.group.get(<string>name);
77
    } else {
78
      return this.group.get(name as string);
79
    }
80
  }
81

    
82
  ngOnInit(): void {
83
    if (this.index === -1) {
84
      if (<string>this.name === '' || (<FormGroup>this.parentGroup).contains(<string>this.name)) {
85
        const obj = this.generate();
86
        Object.keys(obj.controls).forEach(c => {
87
          (<FormGroup>this.parentGroup.get(<string>this.name)).addControl(c, obj.controls[c]);
88
        });
89
        this.group = this.parentGroup.get(this.name as string) as FormGroup;
90
      } else {
91
        (<FormGroup>this.parentGroup).addControl(<string>this.name, this.generate());
92
        this.group = this.parentGroup.get(this.name as string) as FormGroup;
93
      }
94
    } else {
95
      this.name = this.index;
96
      this.group = this.parentGroup as FormGroup;
97
    }
98
  }
99

    
100
  ngAfterContentInit(): void {
101
    this.createdEvent.emit(this.name);
102
    // setTimeout(() => {
103
    //     if(this.patchData != null) {
104
    //         (this.group as FormGroup).patchValue(this.patchData);
105
    //     }
106
    // },1000);
107
    // setTimeout(() => {
108
    //     console.log(this.group,this.parentGroup);
109
    //     (this.group as FormGroup).updateValueAndValidity();
110
    // },2000);
111
  }
112

    
113
  public get valid() {
114
    return this.group.valid;
115
  }
116

    
117
}
118

    
119
@Component({
120
  selector : 'form-inline',
121
  template : `
122
    <div class="">
123
      <label class="" *ngIf="description.label != ''"
124
             [ngClass]="{'required' : description.mandatory==true}" title="{{ description.desc }}">
125
        {{ description.label }}
126
      </label>
127
      <ng-content></ng-content>
128
      <span class="md-input-bar"></span>
129
    </div>
130
  `
131

    
132
})
133
export class InlineFormWrapper implements OnChanges {
134

    
135
  @Input() public description: Description = null;
136

    
137
  @Input() public params: string = 'inline';
138

    
139
  @Input() public width: number = 9;
140

    
141
  @Input() public valid: boolean = true;
142

    
143
  ngOnChanges(changes: SimpleChanges): void {
144
    if (changes && changes.valid) {
145
      this.valid = <boolean>changes.valid.currentValue;
146
    }
147
  }
148
}
(3-3/4)