1
|
/**
|
2
|
* Created by stefanos on 15/5/2017.
|
3
|
*/
|
4
|
import { FormArray, FormGroup } from "@angular/forms";
|
5
|
import { Component, ComponentFactoryResolver, Injector, Input, Type, ViewChild, ViewContainerRef } from "@angular/core";
|
6
|
import { MyFormDirective } from "./my-form.directive";
|
7
|
import { MyGroup } from "./my-group.interface";
|
8
|
import { MyWrapper } from "./my-wrapper.interface";
|
9
|
import { Description } from '../../../domain/oa-description';
|
10
|
import { Subject } from "rxjs/Subject";
|
11
|
import {ConfirmationDialogComponent} from "../confirmation-dialog.component";
|
12
|
|
13
|
|
14
|
@Component({
|
15
|
selector : 'form-repeat',
|
16
|
template : `
|
17
|
<div [formGroup]="parentGroup" class="interfacesForm uk-margin uk-grid-match uk-child-width-1-1 uk-child-width-1-2@m uk-grid-small uk-grid uk-scrollspy-inview uk-animation-fade">
|
18
|
<!--<div formArrayName="{{name}}">-->
|
19
|
<ng-template my-form></ng-template>
|
20
|
<!--</div>-->
|
21
|
<div class="el-item uk-card uk-card-default uk-card-body uk-scrollspy-inview uk-animation-fade">
|
22
|
<div class="interface-box new" style="text-align: center">
|
23
|
<a class="add-new-element add-new-group" (click)="push()"><i class="fa fa-plus-square-o" aria-hidden="true"></i>
|
24
|
<span class="info">Add New {{ description.label }}</span></a>
|
25
|
</div>
|
26
|
</div>
|
27
|
</div>
|
28
|
|
29
|
|
30
|
<confirmation-dialog #confirmDelete [title]="'Delete Interface'" [isModalShown]="isModalShown"
|
31
|
[confirmActionButton]="'Yes, delete it'" (emitObject)="confirmedRemove($event)">
|
32
|
Are you sure you want to delete this interface?
|
33
|
</confirmation-dialog>
|
34
|
|
35
|
`
|
36
|
|
37
|
})
|
38
|
export class MyArray extends MyGroup {
|
39
|
|
40
|
@Input() public component : Type<MyGroup>;
|
41
|
|
42
|
@Input() public wrapper : Type<MyWrapper> = MyArrayWrapper;
|
43
|
|
44
|
@Input() public initEmpty : boolean = false;
|
45
|
|
46
|
@ViewChild(MyFormDirective) protected formComponents: MyFormDirective;
|
47
|
|
48
|
protected _cfr : ComponentFactoryResolver;
|
49
|
|
50
|
protected viewContainerRef : ViewContainerRef;
|
51
|
|
52
|
arrayData_ : Subject<any>[] = [];
|
53
|
|
54
|
push() {
|
55
|
this.createView();
|
56
|
}
|
57
|
|
58
|
constructor(injector : Injector) {
|
59
|
super(injector);
|
60
|
this._cfr = injector.get(ComponentFactoryResolver);
|
61
|
}
|
62
|
|
63
|
protected createView() : void {
|
64
|
let componentFactory = this._cfr.resolveComponentFactory(this.component);
|
65
|
let wrapperFactory = this._cfr.resolveComponentFactory(this.wrapper);
|
66
|
let wrapperView = wrapperFactory.create(this.viewContainerRef.injector);
|
67
|
let componentView = componentFactory.create(this.viewContainerRef.injector);
|
68
|
|
69
|
(<MyGroup>componentView.instance).index = this.viewContainerRef.length;
|
70
|
(<MyGroup>componentView.instance).required = this.required;
|
71
|
(<MyGroup>componentView.instance).data = this.data;
|
72
|
(<MyGroup>componentView.instance).otherData = this.otherData;
|
73
|
this.arrayData_.push((<MyGroup>componentView.instance).patchData);
|
74
|
(<MyGroup>componentView.instance).description = this.description;
|
75
|
let arrayGroup = (<MyGroup>componentView.instance).generate();
|
76
|
(<MyGroup>componentView.instance).parentGroup = arrayGroup as FormGroup;
|
77
|
(<MyWrapper>wrapperView.instance).component = componentView.hostView;
|
78
|
(<MyWrapper>wrapperView.instance).viewRef = wrapperView.hostView;
|
79
|
(<MyWrapper>wrapperView.instance).description = this.description;
|
80
|
|
81
|
(<MyWrapper>wrapperView.instance).first = this.viewContainerRef.length == 0;
|
82
|
(<MyWrapper>wrapperView.instance).deleteNotifier.subscribe($event => {
|
83
|
let index = this.viewContainerRef.indexOf($event);
|
84
|
console.log(index);
|
85
|
if( this.viewContainerRef.length == 1 && this.description.mandatory==true) {
|
86
|
console.log(this.viewContainerRef.get(0));
|
87
|
((this.parentGroup as FormArray).controls[this.name].at(0).corpus((<MyGroup>componentView.instance).generate().value));
|
88
|
} else {
|
89
|
if (index>0){
|
90
|
this.curIntrf = <MyGroup>componentView.instance;
|
91
|
this.curIndex = index;
|
92
|
this.confirmRemoveInterface();
|
93
|
} else { (<MyGroup>componentView.instance).groupErrorMessage = 'At least one interface must be present'; }
|
94
|
/*(<MyGroup>componentView.instance).toBeDeleted = true;
|
95
|
this.remove(index);
|
96
|
(this.parentGroup as FormArray).controls[this.name].removeAt(index-1);
|
97
|
this.arrayData_.splice(index-1,1);*/
|
98
|
}
|
99
|
});
|
100
|
|
101
|
((this.parentGroup as FormArray).controls[this.name]).push(arrayGroup);
|
102
|
|
103
|
this.viewContainerRef.insert(wrapperView.hostView);
|
104
|
console.log("ADDED NEW GROUP IN CREATEVIEW");
|
105
|
}
|
106
|
|
107
|
isModalShown: boolean = false;
|
108
|
curIntrf: any;
|
109
|
curIndex: number;
|
110
|
|
111
|
@ViewChild('confirmDelete')
|
112
|
public confirmDelete: ConfirmationDialogComponent;
|
113
|
|
114
|
confirmRemoveInterface(){
|
115
|
this.confirmDelete.showModal();
|
116
|
}
|
117
|
|
118
|
confirmedRemove(event: any){
|
119
|
if (this.curIndex!=0) {
|
120
|
this.curIntrf.toBeDeleted = true;
|
121
|
this.remove(this.curIndex);
|
122
|
(this.parentGroup as FormArray).controls[this.name].removeAt(this.curIndex - 1);
|
123
|
this.arrayData_.splice(this.curIndex - 1, 1);
|
124
|
}
|
125
|
}
|
126
|
|
127
|
remove(i : number) : void {
|
128
|
this.viewContainerRef.remove(i);
|
129
|
}
|
130
|
|
131
|
ngOnInit(): void {
|
132
|
// super.ngOnInit();
|
133
|
this.viewContainerRef = this.formComponents.viewContainerRef;
|
134
|
(<FormGroup>this.parentGroup).addControl(<string>this.name, this._fb.array([]));
|
135
|
/* (<FormGroup>this.parentGroup).addControl(<string>this.name, this._fb.array([]));
|
136
|
!this.initEmpty && this.createView();
|
137
|
this.parentGroup.get(this.name as string).patchValue = this.patchValue();*/
|
138
|
if (this.data && this.data.length) {
|
139
|
for (let i=0; i<this.data.length; i++ ) {
|
140
|
!this.initEmpty && this.createView();
|
141
|
this.parentGroup.get(this.name as string).patchValue = this.patchValue();
|
142
|
}
|
143
|
} else {
|
144
|
!this.initEmpty && this.createView();
|
145
|
this.parentGroup.get(this.name as string).patchValue = this.patchValue();
|
146
|
}
|
147
|
|
148
|
}
|
149
|
|
150
|
get valid() {
|
151
|
return this.parentGroup.valid;//true;//this.registerGroup_.valid;
|
152
|
}
|
153
|
|
154
|
|
155
|
protected patchValue() {
|
156
|
let self = this;
|
157
|
return (value: {[key: string]: any}, {onlySelf, emitEvent}: {onlySelf?: boolean, emitEvent?: boolean} = {}) => {
|
158
|
for (let i = (<FormArray>self.parentGroup.get(this.name as string)).length; i < Object.keys(value).length; i++) {
|
159
|
self.createView();
|
160
|
console.log("ADDED NEW GROUP");
|
161
|
}
|
162
|
for (let i = 0; i < Object.keys(value).length; i++) {
|
163
|
self.arrayData_[i].next(value[i]);
|
164
|
}
|
165
|
};
|
166
|
}
|
167
|
}
|
168
|
|
169
|
@Component({
|
170
|
selector : 'form-repeat-inline',
|
171
|
template : `
|
172
|
<form-inline [description]="description" [valid]="valid">
|
173
|
<ng-template my-form></ng-template>
|
174
|
<a class="add-new-element" (click)="push()">
|
175
|
<i class="fa fa-plus" aria-hidden="true"></i> Add another
|
176
|
</a>
|
177
|
</form-inline>
|
178
|
`
|
179
|
|
180
|
})
|
181
|
export class MyArrayInline extends MyArray {
|
182
|
@Input()
|
183
|
public wrapper : Type<MyWrapper> = MyInlineArrayWrapper;
|
184
|
|
185
|
@Input()
|
186
|
public description : Description;
|
187
|
}
|
188
|
|
189
|
|
190
|
@Component({
|
191
|
selector : 'form-repeat-wrapper',
|
192
|
template : `
|
193
|
<div class="el-item uk-card uk-card-default uk-card-body uk-scrollspy-inview uk-animation-fade">
|
194
|
<div class="interfaceActionsPanel" style="margin-left: 5px;">
|
195
|
<a (click)="remove()"><i class="fa fa-remove fa-lg"></i></a>
|
196
|
</div>
|
197
|
<ng-template my-form></ng-template>
|
198
|
</div>
|
199
|
|
200
|
`
|
201
|
|
202
|
})
|
203
|
export class MyArrayWrapper extends MyWrapper{
|
204
|
|
205
|
}
|
206
|
|
207
|
@Component({
|
208
|
selector : 'form-inline-repeat-wrapper',
|
209
|
template : `
|
210
|
<div class="uk-grid uk-margin">
|
211
|
<div class="uk-width-5-6">
|
212
|
<ng-template my-form></ng-template>
|
213
|
</div>
|
214
|
<a *ngIf="canDelete" class="remove-element uk-width-1-6" (click)="remove()"><i
|
215
|
class="fa fa-times" aria-hidden="true"></i></a>
|
216
|
</div>
|
217
|
`
|
218
|
|
219
|
})
|
220
|
export class MyInlineArrayWrapper extends MyWrapper {
|
221
|
}
|