sector.fields.select.ts 4.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { Observable } from 'rxjs/Rx';
import { Injectable } from "@angular/core";
import { SectorFieldsActions } from '../../actions/sectorfields.actions';
import { NgRedux, select } from 'ng2-redux';
import { ISectorFields } from '../../store/sectorfields/sectorfields.types';
import { IAppState } from '../../store/store';

//import ApplicationPreview from './application.preview';

import {
    FormBuilder,
    FormGroup,
    FormControl,
    FormArray
} from '@angular/forms';
import {AppSettings} from '../../app.settings';

@Component({
    selector: 'sector-fields-select',
    template: `
23

24
    <div class="row equal">
25

26
27
28
     <div class="col-md-8">
       <form [formGroup]="formGroup">
        <div formArrayName="formArray">
29

30
            <div *ngFor="let sectorField$ of sectorFields$ | async; let i=index">
31
                <li class="list-group-item" (click)="setActiveSectorAndSave(i)" [style.background-color]="toggleBackgroundColor(i)">
32
33
34
                    <h5>{{sectorField$.name}}</h5>
                </li>
            </div>
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52


            <!--CHECK BOXES USING BOTSTRAP
            <div *ngFor="let sectorField$ of sectorFields$ | async; let i=index">
                <div class="[ form-group ]">
                  <input type="checkbox" name="{{i}}" id="{{i}}" autocomplete="off" />
                  <div class="[ btn-group ]">
                      <label for="{{i}}" class="[ btn btn-primary ]">
                          <span class="[ glyphicon glyphicon-ok ]"></span>
                          <span> </span>
                      </label>
                      <label for="{{i}}" class="[ btn btn-default active ]">
                          {{sectorField$.name}}
                      </label>
                  </div>
                </div>
            </div>
            -->
53
        </div>
54

55
        <!--[disabled]="sectorActive === -1-->
56
57
        <div class="row">
        <div class="col-md-2 col-md-offset-5">
58
            <button type="button" class="btn-primary btn-lg pull-center" (click)="navigateToSchools()" [disabled]="true"	>
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
            Συνέχεια<span class="glyphicon glyphicon-menu-right"></span>
            </button>
        </div>
        </div>
      </form>
    </div>

   <div class="col-md-4">
     <application-preview-select></application-preview-select>
   </div>
  </div>
  `

})
@Injectable() export default class SectorFieldsSelect implements OnInit {
    private sectorFields$: Observable<ISectorFields>;

    public formGroup: FormGroup;
    public cfs = new FormArray([]);

    private sectorActive = <number>-1;

    constructor(private fb: FormBuilder,
                private _cfa: SectorFieldsActions,
                private _ngRedux: NgRedux<IAppState>,
                private router: Router) {
        this.formGroup = this.fb.group({
            formArray: this.cfs
        });
    };

    ngOnInit() {
        this._cfa.getSectorFields();

        this.sectorFields$ = this._ngRedux.select(state => {
            state.sectorFields.reduce(({}, sectorField) =>{
                this.cfs.push(new FormControl(sectorField.selected, []));
96
97
98
                if (sectorField.selected === true) {
                  this.sectorActive = sectorField.id - 1;
                }
99
100
101
102
103
104
105
                return sectorField;
            }, {});
            return state.sectorFields;
        });

    }

106
107
    navigateToSchools() {
        //this.saveSelected();
108
109
110
        this.router.navigate(['/region-schools-select']);
    }

111
112
113
114
115
116
117
118
119
120
121
122
    saveSelected() {
      for (let i = 0; i < this.formGroup.value.formArray.length; i++)
        this.formGroup.value.formArray[i] = false;
      if (this.sectorActive != -1)
        this.formGroup.value.formArray[this.sectorActive] = true;

      this._cfa.saveSectorFieldsSelected(this.formGroup.value.formArray);
    }

    setActiveSectorAndSave(ind) {
        if (ind === this.sectorActive)
          ind = -1;
123
        this.sectorActive = ind;
124
        this.saveSelected();
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
    }

    toggleBackgroundColor(ind) {
        return ((this.sectorActive === ind) ? "cyan" : "#eeeeee");
    }

    retrieveCheck()  {
      for (let i = 0; i < this.formGroup.value.formArray.length; i++)
        if (this.formGroup.value.formArray[i] === true) {
          this.sectorActive = i;
          return i;
        }
        return -1;
    }

}