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
|
@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
|
constructor(injector : Injector) {
|
51
|
this._fb = injector.get(FormBuilder);
|
52
|
this.patchData.subscribe(_ => {
|
53
|
if(typeof _ != 'undefined') {
|
54
|
setTimeout( () => {
|
55
|
(this.group as FormGroup).patchValue(_);
|
56
|
},500);
|
57
|
}
|
58
|
});
|
59
|
}
|
60
|
|
61
|
protected get isArray() {
|
62
|
return this.index != -1;
|
63
|
}
|
64
|
|
65
|
public generate() : FormGroup {
|
66
|
let ret = this._fb.group(this.groupDefinition);
|
67
|
if (this.patchData) {
|
68
|
// console.log(this.patchData);
|
69
|
}
|
70
|
if (!this.required)
|
71
|
Object.keys(ret.controls).forEach(item => ret.controls[item].clearValidators());
|
72
|
return ret;
|
73
|
}
|
74
|
|
75
|
public getMyControl(name : string) : AbstractControl {
|
76
|
if (this.isArray) {
|
77
|
return this.group.get(<string>name);
|
78
|
}
|
79
|
else {
|
80
|
return this.group.get(name as string);
|
81
|
}
|
82
|
}
|
83
|
|
84
|
ngOnInit(): void {
|
85
|
if(this.index == -1) {
|
86
|
if(<string>this.name == '' || (<FormGroup>this.parentGroup).contains(<string>this.name)) {
|
87
|
let obj = this.generate();
|
88
|
Object.keys(obj.controls).forEach(c => {
|
89
|
(<FormGroup>this.parentGroup.get(<string>this.name)).addControl(c,obj.controls[c]);
|
90
|
});
|
91
|
this.group = this.parentGroup.get(this.name as string) as FormGroup;
|
92
|
} else {
|
93
|
(<FormGroup>this.parentGroup).addControl(<string>this.name, this.generate());
|
94
|
this.group = this.parentGroup.get(this.name as string) as FormGroup;
|
95
|
}
|
96
|
} else {
|
97
|
this.name = this.index;
|
98
|
this.group = this.parentGroup as FormGroup;
|
99
|
}
|
100
|
}
|
101
|
|
102
|
ngAfterContentInit(): void {
|
103
|
this.createdEvent.emit(this.name);
|
104
|
// setTimeout(() => {
|
105
|
// if(this.patchData != null) {
|
106
|
// (this.group as FormGroup).patchValue(this.patchData);
|
107
|
// }
|
108
|
// },1000);
|
109
|
// setTimeout(() => {
|
110
|
// console.log(this.group,this.parentGroup);
|
111
|
// (this.group as FormGroup).updateValueAndValidity();
|
112
|
// },2000);
|
113
|
}
|
114
|
|
115
|
public get valid() {
|
116
|
return this.group.valid;
|
117
|
}
|
118
|
|
119
|
}
|
120
|
|
121
|
@Component({
|
122
|
selector : 'form-inline',
|
123
|
template : `
|
124
|
<div class="form-group">
|
125
|
<label class="control-label" *ngIf="description.label != ''" [ngClass]="{'required' : description.mandatory==true}" title="{{ description.desc }}">
|
126
|
{{ description.label }}
|
127
|
</label>
|
128
|
<ng-content></ng-content>
|
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
|
}
|