Project

General

Profile

1
import { AbstractControl, FormBuilder, FormGroup } from "@angular/forms";
2
import {
3
  AfterContentInit,
4
  Component,
5
  EventEmitter,
6
  Injector,
7
  Input,
8
  OnChanges,
9
  OnInit,
10
  SimpleChanges
11
} from "@angular/core";
12
import { Description } from '../../../domain/oa-description';
13
import { Subject } from "rxjs/Subject";
14

    
15
/**
16
 * Created by stefanos on 15/5/2017.
17
 */
18

    
19
@Component({
20
  template:``
21
})
22
export class MyGroup implements OnInit, AfterContentInit {
23

    
24
  @Input() public parentGroup: AbstractControl;
25

    
26
  @Input() public name : string | number;
27

    
28
  @Input() public data : any = null;
29

    
30
  public patchData : Subject<any> = new Subject();
31

    
32
  @Input() public required : boolean = false;
33

    
34
  @Input() public description : Description = null;
35

    
36
  @Input() public index : number = -1;
37

    
38
  protected _fb : FormBuilder;
39

    
40
  protected groupDefinition : { [key:string]:any };
41

    
42
  public group : AbstractControl;
43

    
44
  public createdEvent : EventEmitter<any> = new EventEmitter();
45

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

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

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

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

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

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

    
111
  public get valid() {
112
    return this.group.valid;
113
  }
114

    
115
}
116

    
117
@Component({
118
  selector : 'form-inline',
119
  template : `
120
    <div class="form-group">
121
      <label class="control-label" *ngIf="description.label != ''" [ngClass]="{'required' : description.mandatory==true}">
122
        {{description.label}}
123
      </label>
124
      <ng-content></ng-content>
125
    </div>
126
  `
127

    
128
})
129
export class InlineFormWrapper implements OnChanges {
130

    
131
  @Input() public description: Description = null;
132

    
133
  @Input() public params: string = 'inline';
134

    
135
  @Input() public width: number = 9;
136

    
137
  @Input() public valid: boolean = true;
138

    
139
  ngOnChanges(changes: SimpleChanges): void {
140
    if (changes && changes.valid)
141
      this.valid = <boolean>changes.valid.currentValue;
142
  }
143
}
(4-4/5)