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

    
30
    @ViewChild(DivIdFormComponent)
31
    public formComponent : DivIdFormComponent;
32

    
33
    public checkboxes : CheckDivId[] = [];
34

    
35
    public divIds : DivId[] = [];
36

    
37
    public errorMessage: string;
38

    
39
    public formGroup : FormGroup;
40

    
41
    private searchText : RegExp = new RegExp('');
42

    
43
    public selectedCommunityPid: string;
44
    public communities: Community[] = [];
45
    public pages: Page[] = [];
46
    public properties:EnvProperties = null;
47
    public formPages: Page[] = [];
48

    
49
    ngOnInit() {
50
      this.route.data
51
        .subscribe((data: { envSpecific: EnvProperties }) => {
52
           this.properties = data.envSpecific;
53
           this.formGroup = this.formComponent.form;
54
           this.route.queryParams.subscribe(params => {
55
             // if(params['community']) {
56
             //   this.getCommunity(params['community']);
57
             // } else {
58
             this.getCommunities();
59
             //}
60
           });
61
      });
62
    }
63

    
64
    constructor(private route: ActivatedRoute, private _helpContentService: HelpContentService) {}
65
/*
66
    getCommunity(community_pid: string) {
67
        let self = this;
68
        this._helpContentService.getCommunityFull(community_pid).subscribe(
69
            community => {
70
                self.communities = [community];
71
                this.checkboxes.push(<CheckCommunity>{community : community, checked : false});
72
        },
73
        error => this.handleError('System error retrieving communities', error));
74
    }
75
*/
76
    getCommunities() {
77
        let self = this;
78
        this._helpContentService.getCommunities(this.properties.adminToolsAPIURL).subscribe(
79
            communities => {
80
                self.communities = communities;
81
                self.selectedCommunityPid = self.communities[0].pid;
82
                self.getPages(self.selectedCommunityPid);
83
                self.getDivIds(self.selectedCommunityPid);
84
                self.formGroup.patchValue({
85
                  community: self.selectedCommunityPid
86
                });
87
        },
88
        error => this.handleError('System error retrieving communities', error));
89
    }
90

    
91
    getPages(community_pid: string) {
92
        this._helpContentService.getCommunityPages(community_pid, this.properties.adminToolsAPIURL).subscribe(
93
            pages => this.pages = pages,
94
            error => this.handleError('System error retrieving pages', error));
95
    }
96

    
97
    getDivIds(community_pid: string) {
98
        let self = this;
99
        this._helpContentService.getDivIdsFull(community_pid, null, this.properties.adminToolsAPIURL).subscribe(
100
            divIds => {
101
                self.divIds = divIds;
102

    
103
                self.checkboxes = [];
104
                divIds.forEach(_ => {
105
                    self.checkboxes.push(<CheckDivId>{divId : _, checked : false});
106
                });
107
        },
108
        error => this.handleError('System error retrieving classes', error));
109
    }
110

    
111
    public showModal():void {
112
        this.modal.showModal();
113
    }
114

    
115
    public toggleCheckBoxes(event) {
116
        this.checkboxes.forEach(_ => _.checked = event.target.checked);
117
    }
118

    
119
    public applyCheck(flag : boolean) {
120
      console.info("applyCheck "+flag);
121
        this.checkboxes.forEach(_ => _.checked = flag);
122
    }
123

    
124
    public getSelectedDivIds() : string[] {
125
        return this.checkboxes.filter(divId => divId.checked == true).map(checkedDivId => checkedDivId.divId).map(res => res._id);
126
    }
127

    
128
    private deleteDivIdsFromArray(ids : string[]) : void {
129
        for(let id of ids) {
130
            let i = this.checkboxes.findIndex(_ => _.divId._id == id);
131
            this.checkboxes.splice(i, 1);
132
        }
133
    }
134

    
135
    public confirmDeleteDivId(id : string) {
136
        this.deleteConfirmationModal.ids = [id];
137
        this.deleteConfirmationModal.showModal();
138
    }
139

    
140
    public confirmDeleteSelectedDivIds() {
141
        this.deleteConfirmationModal.ids = this.getSelectedDivIds();
142
        this.deleteConfirmationModal.showModal();
143
    }
144

    
145
    public confirmedDeleteDivIds(ids : string[]) {
146
        this._helpContentService.deleteDivIds(ids, this.properties.adminToolsAPIURL).subscribe(
147
           _ => this.deleteDivIdsFromArray(ids),
148
           error => this.handleError('System error deleting the selected classes', error)
149
        );
150
    }
151

    
152
    public editDivId(i : number) {
153
        let divId : DivId = this.checkboxes[i].divId;
154
        this.formPages = <Page[]>divId.pages;
155

    
156
        let pageIds: string[] = [];
157
        let index = 0;
158
        for(let page of <Page[]>divId.pages) {
159
          pageIds[index] = page._id;
160
          index++;
161
        }
162
        //divId.pages = pageIds;
163

    
164
        let community: Community = <Community>divId.community;
165
        this.formGroup.patchValue(divId);
166
        this.formGroup.patchValue({
167
          community: community._id
168
        });
169
        this.formComponent.setPages(pageIds);
170

    
171
        this.updateModal.showModal();
172
    }
173

    
174
    public divIdSavedSuccessfully(divId: DivId) {
175
        this.checkboxes.push(<CheckDivId>{divId : divId, checked : false});
176
        console.info("checkboxes length: "+this.checkboxes.length);
177
        this.applyCheck(false);
178
    }
179

    
180
    public divIdUpdatedSuccessfully(divId : DivId) {
181
        this.checkboxes.find(checkItem => checkItem.divId._id==divId._id).divId = divId;
182
        this.applyCheck(false);
183
    }
184

    
185
    public filterBySearch(text : string) {
186
        this.searchText = new RegExp(text,'i');
187
        this.applyFilter();
188
    }
189

    
190
    public applyFilter() {
191
        this.checkboxes = [];
192
        this.divIds.filter(item => this.filterDivIds(item)).forEach(
193
            _ => this.checkboxes.push(<CheckDivId>{divId: _, checked: false})
194
        );
195
    }
196

    
197
    public filterDivIds(divId : DivId) : boolean {
198
        let textFlag = this.searchText.toString() == '' || (/*community.route + ' ' +*/divId.name).match(this.searchText) != null;
199
        return textFlag;
200
    }
201

    
202
    public filterByCommunity(event: any) {
203
        this.selectedCommunityPid = event.target.value;
204
        this.formGroup.patchValue({
205
          community: this.selectedCommunityPid
206
        });
207
        this.applyCommunityFilter(this.selectedCommunityPid);
208
    }
209

    
210
    public applyCommunityFilter(community_pid: string) {
211
      this.getDivIds(community_pid);
212
      this.getPages(community_pid);
213
    }
214

    
215
    handleError(message: string, error) {
216
        if(error == null) {
217
            console.info("handleError");
218
            this.formComponent.reset();
219
        }
220
        this.errorMessage = message + ' (Server responded: ' + error + ')';
221
    }
222
}
(4-4/4)