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, Output,
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
  @Input() public toBeDeleted: boolean;
33

    
34
  public patchData : Subject<any> = new Subject();
35

    
36
  @Input() public required : boolean = false;
37

    
38
  @Input() public description : Description = null;
39

    
40
  @Input() public index : number = -1;
41

    
42
  protected _fb : FormBuilder;
43

    
44
  protected groupDefinition : { [key:string]:any };
45

    
46
  public group : AbstractControl;
47

    
48
  public createdEvent : EventEmitter<any> = new EventEmitter();
49

    
50
  public groupErrorMessage: string;
51

    
52
  public wasSaved: boolean = false;
53

    
54
  constructor(injector : Injector) {
55
    this._fb = injector.get(FormBuilder);
56
    this.patchData.subscribe(_ => {
57
      if(typeof _ != 'undefined') {
58
        setTimeout( () => {
59
          (this.group as FormGroup).patchValue(_);
60
        },500);
61
      }
62
    });
63
  }
64

    
65
  protected get isArray() {
66
    return this.index != -1;
67
  }
68

    
69
  public generate() : FormGroup {
70
    let ret = this._fb.group(this.groupDefinition);
71
    if (this.patchData) {
72
      // console.log(this.patchData);
73
    }
74
    if (!this.required)
75
      Object.keys(ret.controls).forEach(item => ret.controls[item].clearValidators());
76
    return ret;
77
  }
78

    
79
  public getMyControl(name : string) : AbstractControl {
80
    if (this.isArray) {
81
      return this.group.get(<string>name);
82
    }
83
    else {
84
      return this.group.get(name as string);
85
    }
86
  }
87

    
88
  ngOnInit(): void {
89
    if(this.index == -1) {
90
      if(<string>this.name == '' || (<FormGroup>this.parentGroup).contains(<string>this.name)) {
91
        let obj = this.generate();
92
        Object.keys(obj.controls).forEach(c => {
93
          (<FormGroup>this.parentGroup.get(<string>this.name)).addControl(c,obj.controls[c]);
94
        });
95
        this.group = this.parentGroup.get(this.name as string) as FormGroup;
96
      } else {
97
        (<FormGroup>this.parentGroup).addControl(<string>this.name, this.generate());
98
        this.group = this.parentGroup.get(this.name as string) as FormGroup;
99
      }
100
    } else {
101
      this.name = this.index;
102
      this.group = this.parentGroup as FormGroup;
103
    }
104
  }
105

    
106
  ngAfterContentInit(): void {
107
    this.createdEvent.emit(this.name);
108
    // setTimeout(() => {
109
    //     if(this.patchData != null) {
110
    //         (this.group as FormGroup).patchValue(this.patchData);
111
    //     }
112
    // },1000);
113
    // setTimeout(() => {
114
    //     console.log(this.group,this.parentGroup);
115
    //     (this.group as FormGroup).updateValueAndValidity();
116
    // },2000);
117
  }
118

    
119
  public get valid() {
120
    return this.group.valid;
121
  }
122

    
123
}
124

    
125
@Component({
126
  selector : 'form-inline',
127
  template : `
128
    <div class="form-group">
129
      <label class="control-label" *ngIf="description.label != ''" [ngClass]="{'required' : description.mandatory==true}" title="{{ description.desc }}">
130
        {{ description.label }}
131
      </label>
132
      <ng-content></ng-content>
133
    </div>
134
  `
135

    
136
})
137
export class InlineFormWrapper implements OnChanges {
138

    
139
  @Input() public description: Description = null;
140

    
141
  @Input() public params: string = 'inline';
142

    
143
  @Input() public width: number = 9;
144

    
145
  @Input() public valid: boolean = true;
146

    
147
  ngOnChanges(changes: SimpleChanges): void {
148
    if (changes && changes.valid)
149
      this.valid = <boolean>changes.valid.currentValue;
150
  }
151
}
(3-3/4)