Project

General

Profile

1
import { Component, ViewChild, OnInit } from '@angular/core';
2
import { ActivatedRoute } 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
@Component({
14
    selector: 'divIds',
15
    templateUrl: './divIds.component.html',
16
})
17

    
18
export class DivIdsComponent implements OnInit {
19

    
20
    // @ViewChild(ModalFormComponent)
21
    // @ViewChild('saveModal')
22
    // public modal:ModalFormComponent;
23
    //
24
    // @ViewChild('updateModal')
25
    // public updateModal:ModalFormComponent;
26

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

    
34
    @ViewChild(DivIdFormComponent)
35
    public formComponent : DivIdFormComponent;
36

    
37
    public checkboxes : CheckDivId[] = [];
38

    
39
    public divIds : DivId[] = [];
40

    
41
    //public errorMessage: string;
42

    
43
    public formGroup : FormGroup;
44

    
45
    private searchText : RegExp = new RegExp('');
46

    
47
    public pages: Page[] = [];
48
    public properties:EnvProperties = null;
49
    public formPages: Page[] = [];
50

    
51
    public showLoading: boolean = true;
52
    public errorMessage: string = '';
53
    public updateErrorMessage: string = '';
54
    public modalErrorMessage: string = '';
55

    
56
    ngOnInit() {
57
      this.route.data
58
        .subscribe((data: { envSpecific: EnvProperties }) => {
59
           this.properties = data.envSpecific;
60
           this.formGroup = this.formComponent.form;
61

    
62
           this.getDivIds();
63
      });
64
    }
65

    
66
    constructor(private route: ActivatedRoute, private _helpContentService: HelpContentService) {}
67

    
68
    getDivIds() {
69
        this.showLoading = true;
70
        this.updateErrorMessage = "";
71
        this.errorMessage = "";
72

    
73
        this._helpContentService.getDivIdsFull(null, this.properties.adminToolsAPIURL).subscribe(
74
            divIds => {
75
                this.divIds = divIds;
76
                this.checkboxes = [];
77

    
78
                let self = this;
79
                divIds.forEach(_ => {
80
                    self.checkboxes.push(<CheckDivId>{divId : _, checked : false});
81
                });
82

    
83
                this.showLoading = false;
84
        },
85
        error => this.handleError('System error retrieving classes', error));
86
    }
87

    
88
    // public showModal():void {
89
    //     this.modal.showModal();
90
    // }
91

    
92
    public toggleCheckBoxes(event) {
93
        this.checkboxes.forEach(_ => _.checked = event.target.checked);
94
    }
95

    
96
    public applyCheck(flag : boolean) {
97
      console.info("applyCheck "+flag);
98
        this.checkboxes.forEach(_ => _.checked = flag);
99
    }
100

    
101
    public getSelectedDivIds() : string[] {
102
        return this.checkboxes.filter(divId => divId.checked == true).map(checkedDivId => checkedDivId.divId).map(res => res._id);
103
    }
104

    
105
    private deleteDivIdsFromArray(ids : string[]) : void {
106
        for(let id of ids) {
107
            let i = this.checkboxes.findIndex(_ => _.divId._id == id);
108
            this.checkboxes.splice(i, 1);
109
        }
110
    }
111

    
112
    public confirmDeleteDivId(id : string) {
113
        //this.deleteConfirmationModal.ids = [id];
114
        //this.deleteConfirmationModal.showModal();
115
        this.selectedDivIds = [id];
116
        this.confirmModalOpen();
117
    }
118

    
119
    public confirmDeleteSelectedDivIds() {
120
        //this.deleteConfirmationModal.ids = this.getSelectedDivIds();
121
        //this.deleteConfirmationModal.showModal();
122
        this.selectedDivIds = this.getSelectedDivIds();
123
        this.confirmModalOpen();
124
    }
125

    
126
    private confirmModalOpen() {
127
      this.alertModalDeleteDivIds.cancelButton = true;
128
      this.alertModalDeleteDivIds.okButton = true;
129
      this.alertModalDeleteDivIds.alertTitle = "Delete Confirmation";
130
      this.alertModalDeleteDivIds.message = "Are you sure you want to delete the selected class(es)?";
131
      this.alertModalDeleteDivIds.okButtonText = "Yes";
132
      this.alertModalDeleteDivIds.open();
133
    }
134

    
135
    public confirmedDeleteDivIds(data: any) {
136
        this.showLoading = true;
137
        this.updateErrorMessage = "";
138

    
139
        this._helpContentService.deleteDivIds(this.selectedDivIds, this.properties.adminToolsAPIURL).subscribe(
140
           _ => {
141
             this.deleteDivIdsFromArray(this.selectedDivIds);
142
             this.showLoading = false;
143
           },
144
           error => this.handleUpdateError('System error deleting the selected classes', error)
145
        );
146
    }
147

    
148
    public editDivId(i : number) {
149
        let divId : DivId = this.checkboxes[i].divId;
150
        this.formPages = <Page[]>divId.pages;
151

    
152
        /*let pageIds: string[] = [];
153
        let index = 0;
154
        for(let page of <Page[]>divId.pages) {
155
          pageIds[index] = page._id;
156
          index++;
157
        }*/
158

    
159
        this.formGroup.patchValue(divId);
160
        this.formComponent.setPages(divId.pages as Page[]);//pageIds);
161

    
162
        //this.updateModal.showModal();
163
        this.divIdsModalOpen(this.alertModalUpdateDivId, "Update", "Update Class");
164
    }
165

    
166
    public newDivId() {
167
        this.formComponent.reset();
168
        this.modalErrorMessage = "";
169
        this.divIdsModalOpen(this.alertModalSaveDivId, "Save", "Add a new Class");
170
    }
171

    
172
    private divIdsModalOpen(modal: any, title: string, yesBtn: string) {
173
      modal.cancelButton = true;
174
      modal.okButton = true;
175
      modal.alertTitle = title;
176
      modal.okButtonText = yesBtn;
177
      modal.open();
178
    }
179

    
180
    public divIdSaveConfirmed(data: any) {
181
      if(!this.formGroup.valid) {
182
        this.divIdsModalOpen(this.alertModalSaveDivId, "Save", "Add a new Class");
183
        this.modalErrorMessage = "Please fill in all required fields marked with *";
184
      } else {
185
        this.modalErrorMessage = "";
186
        this._helpContentService.saveDivId(<DivId> this.formGroup.value, this.properties.adminToolsAPIURL).subscribe(
187
            divId => {
188
              this.divIdSavedSuccessfully(divId);
189
            },
190
            error => this.handleUpdateError("System error creating class", error)
191
        );
192
      }
193
    }
194

    
195
    public divIdUpdateConfirmed(data: any) {
196
      if(!this.formGroup.valid) {
197
        this.divIdsModalOpen(this.alertModalUpdateDivId, "Update", "Update Class");
198
        this.modalErrorMessage = "Please fill in all required fields marked with *";
199
      } else {
200
        this._helpContentService.updateDivId(<DivId> this.formGroup.value, this.properties.adminToolsAPIURL).subscribe(
201
            divId => {
202
              this.divIdUpdatedSuccessfully(divId);
203
            },
204
            error => this.handleUpdateError("System error updating class", error)
205
        );
206
      }
207
    }
208

    
209
    public divIdSavedSuccessfully(divId: DivId) {
210
        this.checkboxes.push(<CheckDivId>{divId : divId, checked : false});
211
        console.info("checkboxes length: "+this.checkboxes.length);
212
        this.applyCheck(false);
213
    }
214

    
215
    public divIdUpdatedSuccessfully(divId : DivId) {
216
        this.checkboxes.find(checkItem => checkItem.divId._id==divId._id).divId = divId;
217
        this.applyCheck(false);
218
    }
219

    
220
    public filterBySearch(text : string) {
221
        this.searchText = new RegExp(text,'i');
222
        this.applyFilter();
223
    }
224

    
225
    public applyFilter() {
226
        this.checkboxes = [];
227
        this.divIds.filter(item => this.filterDivIds(item)).forEach(
228
            _ => this.checkboxes.push(<CheckDivId>{divId: _, checked: false})
229
        );
230
    }
231

    
232
    public filterDivIds(divId : DivId) : boolean {
233
        let textFlag = this.searchText.toString() == '' || (divId.name).match(this.searchText) != null;
234
        return textFlag;
235
    }
236

    
237
    handleUpdateError(message: string, error) {
238
        if(error == null) {
239
            this.formComponent.reset();
240
        } else {
241
          this.updateErrorMessage = message;
242
          console.log('Server responded: ' +error);
243
        }
244

    
245
        this.showLoading = false;
246
    }
247

    
248
    handleError(message: string, error) {
249
        this.errorMessage = message;
250
        console.log('Server responded: ' + error);
251

    
252
        this.showLoading = false;
253
    }
254
}
(4-4/4)