Project

General

Profile

1
import { Component, ViewChild, OnInit, ElementRef } from '@angular/core';
2
import { ActivatedRoute, Router } from "@angular/router";
3
import { HelpContentService } from "../../services/help-content.service";
4
import { FormGroup } from "@angular/forms";
5
import { DivIdFormComponent } from "./divId-form.component";
6
import { CheckDivId, DivId } from "../../domain/divId";
7
//import { Community } from "../../domain/community";
8
import { Page } from "../../domain/page";
9
import { EnvProperties } from '../../openaireLibrary/utils/properties/env-properties';
10

    
11
import {Session} from '../../openaireLibrary/login/utils/helper.class';
12
import {LoginErrorCodes} from '../../openaireLibrary/login/utils/guardHelper.class';
13
import {HelperFunctions} from "../../openaireLibrary/utils/HelperFunctions.class";
14
import {Title} from '@angular/platform-browser';
15

    
16
@Component({
17
    selector: 'divIds',
18
    templateUrl: './divIds.component.html',
19
})
20

    
21
export class DivIdsComponent implements OnInit {
22

    
23
    // @ViewChild(ModalFormComponent)
24
    // @ViewChild('saveModal')
25
    // public modal:ModalFormComponent;
26
    //
27
    // @ViewChild('updateModal')
28
    // public updateModal:ModalFormComponent;
29

    
30
    // @ViewChild('deleteConfirmationModal')
31
    // public deleteConfirmationModal : DeleteConfirmationDialogComponent;
32
    @ViewChild('AlertModalSaveDivId') alertModalSaveDivId;
33
    @ViewChild('AlertModalUpdateDivId') alertModalUpdateDivId;
34
    @ViewChild('AlertModalDeleteDivIds') alertModalDeleteDivIds;
35
    private selectedDivIds: string[] = [];
36

    
37
    @ViewChild(DivIdFormComponent)
38
    public formComponent : DivIdFormComponent;
39

    
40
    public checkboxes : CheckDivId[] = [];
41

    
42
    public divIds : DivId[] = [];
43

    
44
    //public errorMessage: string;
45

    
46
    public formGroup : FormGroup;
47

    
48
    private searchText : RegExp = new RegExp('');
49
    public  keyword: string = "";
50

    
51
    public pages: Page[] = [];
52
    public properties:EnvProperties = null;
53
    public formPages: Page[] = [];
54

    
55
    public showLoading: boolean = true;
56
    public errorMessage: string = '';
57
    public updateErrorMessage: string = '';
58
    public modalErrorMessage: string = '';
59

    
60
    ngOnInit() {
61
      this.route.data
62
        .subscribe((data: { envSpecific: EnvProperties }) => {
63
          HelperFunctions.scroll();
64
           this.title.setTitle('Administration Dashboard | Classes');
65
           this.properties = data.envSpecific;
66
           this.formGroup = this.formComponent.form;
67

    
68
           this.getDivIds();
69
      });
70
    }
71

    
72
    constructor(private element: ElementRef, private route: ActivatedRoute,
73
                private _router: Router, private title: Title,
74
                private _helpContentService: HelpContentService) {}
75

    
76
    getDivIds() {
77
      if(!Session.isLoggedIn()){
78
        this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this._router.url} });
79
      } else {
80
        this.showLoading = true;
81
        this.updateErrorMessage = "";
82
        this.errorMessage = "";
83

    
84
        this._helpContentService.getDivIdsFull(null, this.properties.adminToolsAPIURL).subscribe(
85
            divIds => {
86
                this.divIds = divIds;
87
                this.checkboxes = [];
88

    
89
                let self = this;
90
                divIds.forEach(_ => {
91
                    self.checkboxes.push(<CheckDivId>{divId : _, checked : false});
92
                });
93

    
94
                this.showLoading = false;
95
        },
96
        error => this.handleError('System error retrieving classes', error));
97
      }
98
    }
99

    
100
    // public showModal():void {
101
    //     this.modal.showModal();
102
    // }
103

    
104
    public toggleCheckBoxes(event) {
105
        this.checkboxes.forEach(_ => _.checked = event.target.checked);
106
    }
107

    
108
    public applyCheck(flag : boolean) {
109
        this.checkboxes.forEach(_ => _.checked = flag);
110
    }
111

    
112
    public getSelectedDivIds() : string[] {
113
        return this.checkboxes.filter(divId => divId.checked == true).map(checkedDivId => checkedDivId.divId).map(res => res._id);
114
    }
115

    
116
    private deleteDivIdsFromArray(ids : string[]) : void {
117
        for(let id of ids) {
118
            let i = this.checkboxes.findIndex(_ => _.divId._id == id);
119
            this.checkboxes.splice(i, 1);
120
        }
121
    }
122

    
123
    public confirmDeleteDivId(id : string) {
124
        //this.deleteConfirmationModal.ids = [id];
125
        //this.deleteConfirmationModal.showModal();
126
        this.selectedDivIds = [id];
127
        this.confirmModalOpen();
128
    }
129

    
130
    public confirmDeleteSelectedDivIds() {
131
        //this.deleteConfirmationModal.ids = this.getSelectedDivIds();
132
        //this.deleteConfirmationModal.showModal();
133
        this.selectedDivIds = this.getSelectedDivIds();
134
        this.confirmModalOpen();
135
    }
136

    
137
    private confirmModalOpen() {
138
      if(!Session.isLoggedIn()){
139
        this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this._router.url} });
140
      } else {
141
        this.alertModalDeleteDivIds.cancelButton = true;
142
        this.alertModalDeleteDivIds.okButton = true;
143
        this.alertModalDeleteDivIds.alertTitle = "Delete Confirmation";
144
        this.alertModalDeleteDivIds.message = "Are you sure you want to delete the selected class(es)?";
145
        this.alertModalDeleteDivIds.okButtonText = "Yes";
146
        this.alertModalDeleteDivIds.open();
147
      }
148
    }
149

    
150
    public confirmedDeleteDivIds(data: any) {
151
      if(!Session.isLoggedIn()){
152
        this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this._router.url} });
153
      } else {
154
        this.showLoading = true;
155
        this.updateErrorMessage = "";
156

    
157
        this._helpContentService.deleteDivIds(this.selectedDivIds, this.properties.adminToolsAPIURL).subscribe(
158
           _ => {
159
             this.deleteDivIdsFromArray(this.selectedDivIds);
160
             this.showLoading = false;
161
           },
162
           error => this.handleUpdateError('System error deleting the selected classes', error)
163
        );
164
      }
165
    }
166

    
167
    public editDivId(i : number) {
168
        let divId : DivId = this.checkboxes[i].divId;
169
        this.formPages = <Page[]>divId.pages;
170

    
171
        /*let pageIds: string[] = [];
172
        let index = 0;
173
        for(let page of <Page[]>divId.pages) {
174
          pageIds[index] = page._id;
175
          index++;
176
        }*/
177

    
178
        this.formGroup.patchValue(divId);
179
        this.formComponent.setPages(divId.pages as Page[]);//pageIds);
180
        this.formGroup.controls['portalType'].disable();
181

    
182
        //this.updateModal.showModal();
183
        this.divIdsModalOpen(this.alertModalUpdateDivId, "Update", "Update Class");
184
    }
185

    
186
    public newDivId() {
187
        this.formGroup.controls['portalType'].enable();
188
        this.formComponent.reset();
189
        this.modalErrorMessage = "";
190
        this.divIdsModalOpen(this.alertModalSaveDivId, "Save", "Add a new Class");
191
    }
192

    
193
    private divIdsModalOpen(modal: any, title: string, yesBtn: string) {
194
      if(!Session.isLoggedIn()){
195
        this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this._router.url} });
196
      } else {
197
        modal.cancelButton = true;
198
        modal.okButton = true;
199
        modal.alertTitle = title;
200
        modal.okButtonText = yesBtn;
201
        modal.open();
202
      }
203
    }
204

    
205
    public divIdSaveConfirmed(data: any) {
206
      if(!Session.isLoggedIn()){
207
        this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this._router.url} });
208
      } else {
209
        if(!this.formGroup.valid) {
210
          this.divIdsModalOpen(this.alertModalSaveDivId, "Save", "Add a new Class");
211
          this.modalErrorMessage = "Please fill in all required fields marked with *";
212
        } else {
213
          this.modalErrorMessage = "";
214
          this._helpContentService.saveDivId(<DivId> this.formGroup.value, this.properties.adminToolsAPIURL).subscribe(
215
              divId => {
216
                this.divIdSavedSuccessfully(divId);
217
              },
218
              error => this.handleUpdateError("System error creating class", error)
219
          );
220
        }
221
      }
222
    }
223

    
224
    public divIdUpdateConfirmed(data: any) {
225
      if(!Session.isLoggedIn()){
226
        this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this._router.url} });
227
      } else {
228
        if(!this.formGroup.valid) {
229
          this.divIdsModalOpen(this.alertModalUpdateDivId, "Update", "Update Class");
230
          this.modalErrorMessage = "Please fill in all required fields marked with *";
231
        } else {
232
          this.formGroup.controls['portalType'].enable();
233
          this._helpContentService.updateDivId(<DivId> this.formGroup.value, this.properties.adminToolsAPIURL).subscribe(
234
              divId => {
235
                this.divIdUpdatedSuccessfully(divId);
236
              },
237
              error => this.handleUpdateError("System error updating class", error)
238
          );
239
        }
240
      }
241
    }
242

    
243
    public divIdSavedSuccessfully(divId: DivId) {
244
        this.checkboxes.push(<CheckDivId>{divId : divId, checked : false});
245
        this.applyCheck(false);
246
    }
247

    
248
    public divIdUpdatedSuccessfully(divId : DivId) {
249
        this.checkboxes.find(checkItem => checkItem.divId._id==divId._id).divId = divId;
250
        this.applyCheck(false);
251
    }
252

    
253
    public filterBySearch(text : string) {
254
        this.searchText = new RegExp(text,'i');
255
        this.applyFilter();
256
    }
257

    
258
    public applyFilter() {
259
        this.checkboxes = [];
260
        this.divIds.filter(item => this.filterDivIds(item)).forEach(
261
            _ => this.checkboxes.push(<CheckDivId>{divId: _, checked: false})
262
        );
263
    }
264

    
265
    public filterDivIds(divId : DivId) : boolean {
266
        let textFlag = this.searchText.toString() == '' || (divId.name + ' ' + divId.portalType).match(this.searchText) != null;
267
        return textFlag;
268
    }
269

    
270
    handleUpdateError(message: string, error) {
271
        if(error == null) {
272
            this.formComponent.reset();
273
        } else {
274
          this.updateErrorMessage = message;
275
          console.log('Server responded: ' +error);
276
        }
277

    
278
        this.showLoading = false;
279
    }
280

    
281
    handleError(message: string, error) {
282
        this.errorMessage = message;
283
        console.log('Server responded: ' + error);
284

    
285
        this.showLoading = false;
286
    }
287
}
(5-5/6)