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

    
13
import { Subject } from "rxjs/Subject";
14
import { Description } from '../../../domain/oa-description';
15

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

    
20

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

    
26
  @Input() public parentGroup: AbstractControl;
27

    
28
  @Input() public name : string | number;
29

    
30
  @Input() public data : any = null;
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
        },1000)exit;
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
  <ng-template #descTemplate>{{description.desc}}</ng-template>
123
  <div class="uk-grid uk-form-horizontal">
124
      <!--<label class="uk-width-1-5 uk-form-label" *ngIf="description.label!=null" [ngClass]="{'required' : description.mandatory==true}">-->
125
      <label class="uk-width-1-5" *ngIf="description.label!=null" [ngClass]="{'required' : description.mandatory==true}">
126
          <!--<span *ngIf="description.mandatory==true && !valid"><i class="fa fa-star" style="color : red"></i></span>-->
127
          <!--<span *ngIf="description.recommended==true"><i class="fa fa-star" style="color : green"></i></span>-->
128
          {{description.label}}
129
          <span *ngIf="params==='tooltip'"><i class="fa fa-info-circle" [tooltip]="descTemplate" container="body"></i></span>
130
      </label>
131
      <!--<div class="uk-width-expand@m uk-form-controls" [ngClass]="{'has-error': !valid}">-->
132
      <div class="uk-width-expand\@m" [ngClass]="{'has-error': !valid}">
133
          <ng-content></ng-content>
134
          <div *ngIf="params==='inline'">
135
              <i><small>{{description.desc}}</small></i>
136
          </div>
137
      </div>
138
  </div>
139
  `
140
})
141
export class InlineFormWrapper implements OnChanges {
142

    
143
  @Input() public description : Description = null;
144

    
145
  @Input() public params : string = 'inline';
146

    
147
  @Input() public width : number = 9;
148

    
149
  @Input() public valid : boolean = true;
150

    
151
  ngOnChanges(changes: SimpleChanges): void {
152
    if (changes && changes.valid)
153
      this.valid = <boolean>changes.valid.currentValue;
154
  }
155

    
156
}
(4-4/5)