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
  @Input() public otherData: any;
31

    
32
  public patchData : Subject<any> = new Subject();
33

    
34
  @Input() public required : boolean = false;
35

    
36
  @Input() public description : Description = null;
37

    
38
  @Input() public index : number = -1;
39

    
40
  protected _fb : FormBuilder;
41

    
42
  protected groupDefinition : { [key:string]:any };
43

    
44
  public group : AbstractControl;
45

    
46
  public createdEvent : EventEmitter<any> = new EventEmitter();
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
    let 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
    return ret;
71
  }
72

    
73
  public getMyControl(name : string) : AbstractControl {
74
    if (this.isArray) {
75
      return this.group.get(<string>name);
76
    }
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
        let 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="form-group">
123
      <label class="control-label" *ngIf="description.label != ''" [ngClass]="{'required' : description.mandatory==true}">
124
        {{description.label}}
125
      </label>
126
      <ng-content></ng-content>
127
    </div>
128
  `
129

    
130
})
131
export class InlineFormWrapper implements OnChanges {
132

    
133
  @Input() public description: Description = null;
134

    
135
  @Input() public params: string = 'inline';
136

    
137
  @Input() public width: number = 9;
138

    
139
  @Input() public valid: boolean = true;
140

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