sector.courses.select.ts 8.38 KB
Newer Older
1
import { Component, OnInit, OnDestroy } from '@angular/core';
2
import { Router } from '@angular/router';
3
import { BehaviorSubject, Subscription } from 'rxjs/Rx';
4 5 6
import { Injectable } from "@angular/core";
import { SectorCoursesActions } from '../../actions/sectorcourses.actions';
import { ISectors } from '../../store/sectorcourses/sectorcourses.types';
7
import { SECTOR_COURSES_INITIAL_STATE } from '../../store/sectorcourses/sectorcourses.initial-state';
8
import { NgRedux, select } from 'ng2-redux';
9
import { IAppState } from '../../store/store';
Open Source Developer's avatar
UI  
Open Source Developer committed
10
import {RemoveSpaces} from '../../pipes/removespaces';
11

12
import { RegionSchoolsActions } from '../../actions/regionschools.actions';
13

14 15 16 17 18 19 20 21 22 23 24
import {
    FormBuilder,
    FormGroup,
    FormControl,
    FormArray
} from '@angular/forms';
import {AppSettings} from '../../app.settings';

@Component({
    selector: 'sectorcourses-fields-select',
    template: `
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
    <div id="sectorCourseNotice" (onHidden)="onHidden()" class="modal" tabindex="-1" role="dialog" aria-hidden="true" data-backdrop="static" data-keyboard="false">
      <div class="modal-dialog modal-lg">
        <div class="modal-content">
          <div class="modal-header {{modalHeader | async}}">
              <h3 class="modal-title pull-left"><i class="fa fa-check-square-o"></i>&nbsp;&nbsp;{{ modalTitle | async }}</h3>
            <button type="button" class="close pull-right" aria-label="Close" (click)="hideModal()">
              <span aria-hidden="true"><i class="fa fa-times"></i></span>
            </button>
          </div>
          <div class="modal-body">
              <p>{{ modalText | async }}</p>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default pull-left" data-dismiss="modal" (click)="hideModal()">Κλείσιμο</button>
          </div>
        </div>
      </div>
    </div>
Open Source Developer's avatar
commit  
Open Source Developer committed
43
    <div class="row">
44
             <breadcrumbs></breadcrumbs>
Open Source Developer's avatar
commit  
Open Source Developer committed
45
    </div>
46
    <div class = "loading" *ngIf="(sectors$ | async).size === 0">
47
   </div>
Open Source Developer's avatar
trans  
Open Source Developer committed
48
       <h4> Επιλογή Ειδικότητας</h4>
49 50
      <form [formGroup]="formGroup">
        <div formArrayName="formArray">
51
        <p style="margin-top: 20px; line-height: 2em;"> Παρακαλώ επιλέξτε την ειδικότητα στην οποία θα φοιτήσει ο μαθητής στην Επαγγελματική Εκπαίδευση. Έπειτα επιλέξτε <i>Συνέχεια</i>.</p>
52
            <ul class="list-group">
53 54
            <div *ngFor="let sector$ of sectors$ | async; let i=index; let isOdd=odd; let isEven=even">
                <li class="list-group-item isclickable" (click)="setActiveSector(i)"  [class.oddout]="isOdd" [class.evenout]="isEven" [class.selectedout]="sectorActive === i">
55 56
                    <h5>{{sector$.sector_name}}</h5>
                </li>
57
                <div *ngFor="let course$ of sector$.courses; let j=index; let isOdd2=odd; let isEven2=even" [class.oddin]="isOdd2" [class.evenin]="isEven2" [hidden]="i !== sectorActive">
Open Source Developer's avatar
Open Source Developer committed
58
                          <div class="row">
59
                           <div class="col-md-2 col-md-offset-1">
60 61
                                <input #cb type="checkbox" formControlName="{{ course$.globalIndex }}"
                                (change)="updateCheckedOptions(course$.globalIndex, cb)"
62 63
                                [checked] = " course$.globalIndex === idx "
                                >
64
                            </div>
65
                            <div class="col-md-8  col-md-offset-1 isclickable">
Open Source Developer's avatar
UI  
Open Source Developer committed
66
                                {{course$.course_name | removeSpaces}}
67
                            </div>
Open Source Developer's avatar
Open Source Developer committed
68 69
                            </div>
                    </div>
70 71 72 73
              </div>
            </ul>
        </div>

74
        <div class="row" style="margin-top: 20px; margin-bottom: 20px;" *ngIf="(sectors$ | async).size > 0">
75
        <div class="col-md-6">
76
            <button type="button" class="btn-primary btn-lg pull-left" (click)="router.navigate(['/epal-class-select']);" >
77 78 79 80
          <i class="fa fa-backward"></i>
            </button>
        </div>
        <div class="col-md-6">
81
            <button type="button" class="btn-primary btn-lg pull-right isclickable" style="width: 9em;" (click)="navigateToSchools()" >
82
                <span style="font-size: 0.9em; font-weight: bold;">Συνέχεια&nbsp;&nbsp;&nbsp;</span><i class="fa fa-forward"></i>
83 84 85 86 87 88
            </button>
        </div>
        </div>
     </form>
  `
})
89 90 91
@Injectable() export default class SectorCoursesSelect implements OnInit, OnDestroy {
    private sectors$: BehaviorSubject<ISectors>;
    private sectorsSub: Subscription;
92 93 94 95
    private formGroup: FormGroup;
    private rss = new FormArray([]);
    private sectorActive = <number>-1;
    private idx = <number>-1;
96
    private sectorsList: Array<boolean> = new Array();
97 98 99 100
    private modalTitle: BehaviorSubject<string>;
    private modalText: BehaviorSubject<string>;
    private modalHeader: BehaviorSubject<string>;
    public isModalShown: BehaviorSubject<boolean>;
101 102

    constructor(private fb: FormBuilder,
103
                private _sca: SectorCoursesActions,
104
                private _rsa: RegionSchoolsActions,
105 106 107
                private _ngRedux: NgRedux<IAppState>,
                private router: Router
            ) {
108 109

        this.sectors$ = new BehaviorSubject(SECTOR_COURSES_INITIAL_STATE);
110 111 112
        this.formGroup = this.fb.group({
            formArray: this.rss
        });
113 114 115 116
        this.modalTitle = new BehaviorSubject("");
        this.modalText = new BehaviorSubject("");
        this.modalHeader = new BehaviorSubject("");
        this.isModalShown = new BehaviorSubject(false);
117 118 119
    };

    ngOnInit() {
120
        (<any>$('#sectorCourseNotice')).appendTo("body");
121
        this._sca.getSectorCourses(false);
122
        let ids = 0;
123
        this.sectorsSub = this._ngRedux.select(state => {
124
            state.sectors.reduce((prevSector, sector) =>{
125 126 127
                this.sectorsList[ids] = sector.sector_selected;
                ids++;
                //In case we want to preserve last checked option when we revisit the form
128 129
                if (sector.sector_selected === true)
                    this.sectorActive = ids-1;
130 131
                sector.courses.reduce((prevCourse, course) =>{
                    this.rss.push( new FormControl(course.selected, []));
132 133 134
                    //this.retrieveCheck();
                    if (course.selected === true) {
                      //In case we want to preserve last checked option when we revisit the form
135
                      this.idx = course.globalIndex;
136
                    }
137 138 139 140
                    return course;
                }, {});
                return sector;
            }, {});
141
            ids = 0;
142
            return state.sectors;
143 144 145
        }).subscribe(this.sectors$);

    }
146

147
    ngOnDestroy() {
148
        (<any>$('#sectorCourseNotice')).remove();
149
        if (this.sectorsSub) this.sectorsSub.unsubscribe();
150
        this.sectors$.unsubscribe();
151 152
    }

153 154 155 156 157 158 159 160 161 162 163 164 165
    public showModal(): void {
        (<any>$('#sectorCourseNotice')).modal('show');
    }

    public hideModal(): void {
        (<any>$('#sectorCourseNotice')).modal('hide');

    }

    public onHidden(): void {
        this.isModalShown.next(false);
    }

166
    setActiveSector(ind) {
167 168 169
      if (ind === this.sectorActive)
        ind = -1;
      this.sectorActive = ind;
170 171 172
    }

    saveSelected() {
173
        this._sca.saveSectorCoursesSelected(this.formGroup.value.formArray, this.sectorsList);
174

175
        this._rsa.initRegionSchools();
176 177 178
    }

    navigateToSchools() {
179

180 181 182 183 184 185 186 187
        if (this.idx === -1) {
            this.modalTitle.next("Δεν επιλέχθηκε ειδικότητα");
            this.modalText.next("Παρακαλούμε να επιλέξετε πρώτα ειδικότητα φοίτησης του μαθητή για το νέο σχολικό έτος");
            this.modalHeader.next("modal-header-danger");
            this.showModal();
        } else {
            this.router.navigate(['/region-schools-select']);
        }
188 189 190 191 192 193 194 195 196
    }

    updateCheckedOptions(globalIndex, cb){
      this.idx = globalIndex;
      for (let i = 0; i < this.formGroup.value.formArray.length; i++)
        this.formGroup.value.formArray[i] = false;
      this.formGroup.value.formArray[globalIndex] = cb.checked;
      if (cb.checked === false)
        this.idx = -1;
197 198 199 200 201 202

      for (let i = 0; i < this.sectorsList.length; i++)
          this.sectorsList[i] = false;
      this.sectorsList[this.sectorActive] = true;

      this.saveSelected();
203
  }
204 205

}