Project

General

Profile

1
/**
2
 * Created by stefania on 7/13/17.
3
 */
4
import { Component, ViewChild, OnInit } from '@angular/core';
5
import { ActivatedRoute } from "@angular/router";
6
import { HelpContentService } from "../../services/help-content.service";
7
import { FormGroup } from "@angular/forms";
8
import { ModalFormComponent } from "../modal-form.component";
9
import { DeleteConfirmationDialogComponent } from "../delete-confirmation-dialog.component";
10
import { PageFormComponent } from "./page-form.component";
11
import { CheckPage, Page } from "../../domain/page";
12
import { Community } from "../../domain/community";
13
import { Entity } from "../../domain/entity";
14
import { EnvProperties } from '../../openaireLibrary/utils/properties/env-properties';
15

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

    
21
export class PagesComponent 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

    
33
    @ViewChild(PageFormComponent)
34
    public formComponent : PageFormComponent;
35

    
36
    public checkboxes : CheckPage[] = [];
37

    
38
    public pages : Page[] = [];
39
    public pageWithDivIds: string[] = [];
40

    
41
    //public errorMessage: string;
42

    
43
    public formGroup : FormGroup;
44

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

    
47
    public communities: Community[] = [];
48

    
49
    public selectedCommunityPid: string;
50

    
51
    public pagesType: string;
52
    public properties:EnvProperties = null;
53

    
54
    public showLoading: boolean = true;
55
    public errorMessage: string = '';
56
    public updateErrorMessage: string = '';
57

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

    
64
             this.route.queryParams.subscribe(params => {
65
               this.pagesType = "";
66
               if(params['type']) {
67
                 this.pagesType = params['type'];
68
               }
69

    
70
               this.selectedCommunityPid = params['communityId'];
71
               this.applyCommunityFilter(this.selectedCommunityPid);
72

    
73
               //this.getCommunities();
74
             });
75
        });
76
    }
77

    
78
    constructor(private route: ActivatedRoute, private _helpContentService: HelpContentService) {}
79

    
80
    getPages(community_pid: string) {
81
        this.showLoading = true;
82
        this.updateErrorMessage = "";
83
        this.errorMessage = "";
84

    
85
        let parameters = "";
86
        if(this.pagesType) {
87
          parameters = "?page_type="+this.pagesType;
88
        }
89
        this._helpContentService.getCommunityPages(community_pid, parameters, this.properties.adminToolsAPIURL).subscribe(
90
            pages => {
91
              this.pagesReturned(pages);
92
              this.getPagesWithDivIds(community_pid);
93
            },
94
            error => this.handleError('System error retrieving pages', error)
95
        );
96
    }
97

    
98
    getPagesWithDivIds(community_pid: string) {
99
      this._helpContentService.getPagesWithDivIds(community_pid, this.properties.adminToolsAPIURL).subscribe(
100
          pages => {
101
            this.pageWithDivIds = pages;
102
            this.showLoading = false;
103
          },
104
          error => this.handleError('System error retrieving information about pages\' classes', error));
105
    }
106

    
107
    pagesReturned(pages: Page[]) {
108
      this.pages = pages;
109
      this.checkboxes = [];
110

    
111
      if(pages) {
112
        pages.forEach(_ => {
113
          this.checkboxes.push(<CheckPage>{page : _, checked : false});
114
        });
115
      }
116
    }
117
/*
118
    getCommunities() {
119
        this._helpContentService.getCommunities(this.properties.adminToolsAPIURL).subscribe(
120
            communities => {
121
                this.communities = communities;
122
                this.selectedCommunityPid = this.communities[0].pid;
123
                this.getPages(this.selectedCommunityPid);
124
                this.getPagesWithDivIds(this.selectedCommunityPid);
125
        },
126
        error => this.handleError('System error retrieving communities', error));
127
    }
128
*/
129
    public showModal():void {
130
        this.modal.showModal();
131
    }
132

    
133
    public toggleCheckBoxes(event) {
134
        this.checkboxes.forEach(_ => _.checked = event.target.checked);
135
    }
136

    
137
    public applyCheck(flag : boolean) {
138
      console.info("applyCheck "+flag);
139
        this.checkboxes.forEach(_ => _.checked = flag);
140
    }
141

    
142
    public getSelectedPages() : string[] {
143
        return this.checkboxes.filter(page => page.checked == true).map(checkedPage => checkedPage.page).map(res => res._id);
144
    }
145

    
146
    private deletePagesFromArray(ids : string[]) : void {
147
        for(let id of ids) {
148
            let i = this.checkboxes.findIndex(_ => _.page._id == id);
149
            this.checkboxes.splice(i, 1);
150
        }
151
    }
152

    
153
    public confirmDeletePage(id : string) {
154
        this.deleteConfirmationModal.ids = [id];
155
        this.deleteConfirmationModal.showModal();
156
    }
157

    
158
    public confirmDeleteSelectedPages() {
159
        this.deleteConfirmationModal.ids = this.getSelectedPages();
160
        this.deleteConfirmationModal.showModal();
161
    }
162

    
163
    public confirmedDeletePages(ids : string[]) {
164
        this.showLoading = true;
165
        this.updateErrorMessage = "";
166

    
167
        this._helpContentService.deletePages(ids, this.properties.adminToolsAPIURL).subscribe(
168
            _ => {
169
              this.deletePagesFromArray(ids);
170
              this.showLoading = false;
171
            },
172
            error => this.handleUpdateError('System error deleting the selected pages', error)
173
        );
174
    }
175

    
176
    public editPage(i : number) {
177
        let page : Page = this.checkboxes[i].page;
178
        this.formGroup.patchValue(page);
179
        this.formComponent.setEntities(page.entities as Entity[]);
180

    
181
        console.info(this.formGroup.value);
182
        this.updateModal.showModal();
183
    }
184

    
185
    public pageSavedSuccessfully(page: Page) {
186
        this.checkboxes.push(<CheckPage>{page : page, checked : false});
187
        console.info("checkboxes length: "+this.checkboxes.length);
188
        this.applyCheck(false);
189
    }
190

    
191
    public pageUpdatedSuccessfully(page : Page) {
192
      console.info(page._id);
193
      console.info(this.checkboxes.find(checkItem => (checkItem.page._id == page._id)));
194
      console.info(page.entities);
195
        this.checkboxes.find(checkItem => checkItem.page._id==page._id).page = page;
196
        this.applyCheck(false);
197
    }
198

    
199
    public filterBySearch(text : string) {
200
        this.searchText = new RegExp(text,'i');
201
        this.applyFilter();
202
    }
203

    
204
    public applyFilter() {
205
        this.checkboxes = [];
206
        this.pages.filter(item => this.filterPages(item)).forEach(
207
            _ => this.checkboxes.push(<CheckPage>{page: _, checked: false})
208
        );
209
    }
210

    
211
    public filterPages(page : Page) : boolean {
212
        let textFlag = this.searchText.toString() == '' || (page.route + ' ' +page.name).match(this.searchText) != null;
213
        return textFlag;
214
    }
215

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

    
224
        this.showLoading = false;
225
    }
226

    
227
    handleUpdateError(message: string, error) {
228
        if(error == null) {
229
            this.formComponent.reset();
230
        } else {
231
          this.updateErrorMessage = message;// + ' (Server responded: ' + error + ')';
232
          console.log('Server responded: ' +error);
233
        }
234

    
235
        this.showLoading = false;
236
    }
237

    
238
    // public filterByCommunity(event: any) {
239
    //     this.selectedCommunityPid = event.target.value;
240
    //     this.applyCommunityFilter(this.selectedCommunityPid);
241
    // }
242

    
243
    public applyCommunityFilter(community_pid: string) {
244
        this.getPages(community_pid);
245
    }
246

    
247
    public togglePages(status : boolean, ids : string[]) {
248
        this.showLoading = true;
249
        this.updateErrorMessage = "";
250

    
251
        this._helpContentService.togglePages(this.selectedCommunityPid,ids,status, this.properties.adminToolsAPIURL).subscribe(
252
            () => {
253
                for(let id of ids) {
254
                  let i = this.checkboxes.findIndex(_ => _.page._id == id);
255
                  this.checkboxes[i].page.isEnabled=status;
256
                }
257
                this.applyCheck(false);
258
                this.showLoading = false;
259
            },
260
            error => this.handleUpdateError('System error changing the status of the selected page(s)', error)
261
        );
262
    }
263

    
264

    
265
}
(4-4/4)