Project

General

Profile

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

    
12

    
13
@Component({
14
  selector : 'form-repeat',
15
  template : `
16
    <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">
17
      <!--<div formArrayName="{{name}}">-->
18
      <ng-template my-form></ng-template>
19
      <!--</div>-->
20
      <div class="el-item uk-card uk-card-default uk-card-body uk-scrollspy-inview uk-animation-fade">
21
        <div class="interface-box new" style="text-align: center">
22
          <a class="add-new-element add-new-group" (click)="push()"><i class="fa fa-plus-square-o" aria-hidden="true"></i>
23
            <span class="info">Add New {{ description.label }}</span></a>
24
        </div>
25
      </div>
26
    </div>
27
  `
28

    
29
})
30
export class MyArray extends MyGroup {
31

    
32
  @Input() public component : Type<MyGroup>;
33

    
34
  @Input() public wrapper : Type<MyWrapper> = MyArrayWrapper;
35

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

    
38
  @ViewChild(MyFormDirective) protected formComponents: MyFormDirective;
39

    
40
  protected _cfr : ComponentFactoryResolver;
41

    
42
  protected viewContainerRef : ViewContainerRef;
43

    
44
  arrayData_ : Subject<any>[] = [];
45

    
46
  push() {
47
    this.createView();
48
  }
49

    
50
  constructor(injector : Injector) {
51
    super(injector);
52
    this._cfr = injector.get(ComponentFactoryResolver);
53
  }
54

    
55
  protected createView() : void {
56
    let componentFactory = this._cfr.resolveComponentFactory(this.component);
57
    let wrapperFactory = this._cfr.resolveComponentFactory(this.wrapper);
58
    let wrapperView = wrapperFactory.create(this.viewContainerRef.injector);
59
    let componentView = componentFactory.create(this.viewContainerRef.injector);
60

    
61
    (<MyGroup>componentView.instance).index = this.viewContainerRef.length;
62
    (<MyGroup>componentView.instance).required = this.required;
63
    (<MyGroup>componentView.instance).data = this.data;
64
    (<MyGroup>componentView.instance).otherData = this.otherData;
65
    this.arrayData_.push((<MyGroup>componentView.instance).patchData);
66
    (<MyGroup>componentView.instance).description = this.description;
67
    let arrayGroup = (<MyGroup>componentView.instance).generate();
68
    (<MyGroup>componentView.instance).parentGroup = arrayGroup as FormGroup;
69
    (<MyWrapper>wrapperView.instance).component = componentView.hostView;
70
    (<MyWrapper>wrapperView.instance).viewRef = wrapperView.hostView;
71
    (<MyWrapper>wrapperView.instance).description = this.description;
72

    
73
    (<MyWrapper>wrapperView.instance).first = this.viewContainerRef.length == 0;
74
    (<MyWrapper>wrapperView.instance).deleteNotifier.subscribe($event => {
75
      let index = this.viewContainerRef.indexOf($event);
76
      console.log(index);
77
      if( this.viewContainerRef.length == 1 && this.description.mandatory==true) {
78
        console.log(this.viewContainerRef.get(0));
79
        ((this.parentGroup as FormArray).controls[this.name].at(0).corpus((<MyGroup>componentView.instance).generate().value));
80
      } else {
81
        this.remove(index);
82
        (this.parentGroup as FormArray).controls[this.name].removeAt(index-1);
83
        this.arrayData_.splice(index-1,1);
84
      }
85
    });
86

    
87
    ((this.parentGroup as FormArray).controls[this.name]).push(arrayGroup);
88

    
89
    this.viewContainerRef.insert(wrapperView.hostView);
90
    console.log("ADDED NEW GROUP IN CREATEVIEW");
91
  }
92

    
93
  remove(i : number) : void {
94
    this.viewContainerRef.remove(i);
95
  }
96

    
97
  ngOnInit(): void {
98
    // super.ngOnInit();
99
    this.viewContainerRef = this.formComponents.viewContainerRef;
100
    (<FormGroup>this.parentGroup).addControl(<string>this.name, this._fb.array([]));
101
/*    (<FormGroup>this.parentGroup).addControl(<string>this.name, this._fb.array([]));
102
    !this.initEmpty && this.createView();
103
    this.parentGroup.get(this.name as string).patchValue = this.patchValue();*/
104
    if (this.data && this.data.length) {
105
      for (let i=0; i<this.data.length; i++ ) {
106
        !this.initEmpty && this.createView();
107
        this.parentGroup.get(this.name as string).patchValue = this.patchValue();
108
      }
109
    } else {
110
      !this.initEmpty && this.createView();
111
      this.parentGroup.get(this.name as string).patchValue = this.patchValue();
112
    }
113

    
114
  }
115

    
116
  get valid() {
117
    return this.parentGroup.valid;//true;//this.registerGroup_.valid;
118
  }
119

    
120

    
121
  protected patchValue() {
122
    let self = this;
123
    return (value: {[key: string]: any}, {onlySelf, emitEvent}: {onlySelf?: boolean, emitEvent?: boolean} = {}) => {
124
      for (let i = (<FormArray>self.parentGroup.get(this.name as string)).length; i < Object.keys(value).length; i++) {
125
        self.createView();
126
        console.log("ADDED NEW GROUP");
127
      }
128
      for (let i = 0; i < Object.keys(value).length; i++) {
129
        self.arrayData_[i].next(value[i]);
130
      }
131
    };
132
  }
133
}
134

    
135
@Component({
136
  selector : 'form-repeat-inline',
137
  template : `
138
    <form-inline [description]="description" [valid]="valid">
139
      <ng-template my-form></ng-template>
140
      <a class="add-new-element" (click)="push()">
141
        <i class="fa fa-plus" aria-hidden="true"></i> Add another
142
      </a>
143
    </form-inline>
144
  `
145

    
146
})
147
export class MyArrayInline extends MyArray {
148
  @Input()
149
  public wrapper : Type<MyWrapper> = MyInlineArrayWrapper;
150

    
151
  @Input()
152
  public description : Description;
153
}
154

    
155

    
156
@Component({
157
  selector : 'form-repeat-wrapper',
158
  template : `
159
    <div class="el-item uk-card uk-card-default uk-card-body uk-scrollspy-inview uk-animation-fade">
160
      <div class="interfaceActionsPanel" style="margin-left: 5px;">
161
        <a (click)="remove()"><i class="fa fa-remove fa-lg"></i></a>
162
      </div>
163
      <ng-template my-form></ng-template>
164
    </div>
165
  `
166

    
167
})
168
export class MyArrayWrapper extends MyWrapper{
169
}
170

    
171
@Component({
172
  selector : 'form-inline-repeat-wrapper',
173
  template : `
174
    <div class="uk-grid uk-margin">
175
      <div class="uk-width-5-6">
176
        <ng-template my-form></ng-template>
177
      </div>
178
      <a *ngIf="canDelete" class="remove-element uk-width-1-6" (click)="remove()"><i
179
        class="fa fa-times" aria-hidden="true"></i></a>
180
    </div>
181
  `
182

    
183
})
184
export class MyInlineArrayWrapper extends MyWrapper {
185
}
(1-1/5)