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 { ModalFormComponent } from "../modal-form.component";
6
import { DeleteConfirmationDialogComponent } from "../delete-confirmation-dialog.component";
7
import { DivIdFormComponent } from "./divId-form.component";
8
import { CheckDivId, DivId } from "../../domain/divId";
9
//import { Community } from "../../domain/community";
10
import { Page } from "../../domain/page";
11
import { EnvProperties } from '../../openaireLibrary/utils/properties/env-properties';
12

    
13
import {Session} from '../../openaireLibrary/login/utils/helper.class';
14
import {LoginErrorCodes} from '../../openaireLibrary/login/utils/guardHelper.class';
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
            this.scroll();
64

    
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, private _router: Router, private _helpContentService: HelpContentService) {}
73

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

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

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

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

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

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

    
107
    public applyCheck(flag : boolean) {
108
      console.info("applyCheck "+flag);
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
        console.info(this._router.url);
140
        this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this._router.url} });
141
      } else {
142
        this.alertModalDeleteDivIds.cancelButton = true;
143
        this.alertModalDeleteDivIds.okButton = true;
144
        this.alertModalDeleteDivIds.alertTitle = "Delete Confirmation";
145
        this.alertModalDeleteDivIds.message = "Are you sure you want to delete the selected class(es)?";
146
        this.alertModalDeleteDivIds.okButtonText = "Yes";
147
        this.alertModalDeleteDivIds.open();
148
      }
149
    }
150

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

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

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

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

    
180
        this.formGroup.patchValue(divId);
181
        this.formComponent.setPages(divId.pages as Page[]);//pageIds);
182

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

    
187
    public newDivId() {
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
        console.info(this._router.url);
196
        this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this._router.url} });
197
      } else {
198
        modal.cancelButton = true;
199
        modal.okButton = true;
200
        modal.alertTitle = title;
201
        modal.okButtonText = yesBtn;
202
        modal.open();
203
      }
204
    }
205

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

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

    
245
    public divIdSavedSuccessfully(divId: DivId) {
246
        this.checkboxes.push(<CheckDivId>{divId : divId, checked : false});
247
        console.info("checkboxes length: "+this.checkboxes.length);
248
        this.applyCheck(false);
249
    }
250

    
251
    public divIdUpdatedSuccessfully(divId : DivId) {
252
        this.checkboxes.find(checkItem => checkItem.divId._id==divId._id).divId = divId;
253
        this.applyCheck(false);
254
    }
255

    
256
    public filterBySearch(text : string) {
257
        this.searchText = new RegExp(text,'i');
258
        this.applyFilter();
259
    }
260

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

    
268
    public filterDivIds(divId : DivId) : boolean {
269
        let textFlag = this.searchText.toString() == '' || (divId.name).match(this.searchText) != null;
270
        return textFlag;
271
    }
272

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

    
281
        this.showLoading = false;
282
    }
283

    
284
    handleError(message: string, error) {
285
        this.errorMessage = message;
286
        console.log('Server responded: ' + error);
287

    
288
        this.showLoading = false;
289
    }
290

    
291
    public scroll() {
292
      console.info("scroll into view");
293
      if (typeof document !== 'undefined') {
294
         this.element.nativeElement.scrollIntoView();
295
      }
296
    }
297
}
(4-4/4)