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
    ngOnInit() {
55
        this.formGroup = this.formComponent.form;
56
        this.route.data
57
          .subscribe((data: { envSpecific: EnvProperties }) => {
58
             this.properties = data.envSpecific;
59
             //        this.getCommunities();
60

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

    
71
    constructor(private route: ActivatedRoute, private _helpContentService: HelpContentService) {}
72

    
73
    getPages(community_pid: string) {
74
        let self = this;
75

    
76
        if(this.pagesType) {
77
          this._helpContentService.getCommunityPagesByType(community_pid, "?page_type="+this.pagesType, this.properties.adminToolsAPIURL).subscribe(
78
              pages => {
79
                self.pagesReturned(pages);
80
              },
81
              error => this.handleError('System error retrieving pages', error));
82
        } else {
83
          this._helpContentService.getCommunityPages(community_pid, this.properties.adminToolsAPIURL).subscribe(
84
              pages => {
85
                self.pagesReturned(pages);
86
              },
87
              error => this.handleError('System error retrieving pages', error));
88
        }
89
    }
90

    
91
    getPagesWithDivIds(community_pid: string) {
92
      let self = this;
93
      this._helpContentService.getPagesWithDivIds(community_pid, this.properties.adminToolsAPIURL).subscribe(
94
          pages => {
95
            self.pageWithDivIds = pages[community_pid];
96
          },
97
          error => this.handleError('System error retrieving pages', error));
98
    }
99

    
100
    pagesReturned(pages: Page[]) {
101
      this.pages = pages;
102
      this.checkboxes = [];
103

    
104
      pages.forEach(_ => {
105
          this.checkboxes.push(<CheckPage>{page : _, checked : false});
106
      });
107
    }
108

    
109
    getCommunities() {
110
        let self = this;
111
        this._helpContentService.getCommunities(this.properties.adminToolsAPIURL).subscribe(
112
            communities => {
113
                self.communities = communities;
114
                self.selectedCommunityPid = self.communities[0].pid;
115
                self.getPages(self.selectedCommunityPid);
116
                self.getPagesWithDivIds(self.selectedCommunityPid);
117
        },
118
        error => this.handleError('System error retrieving communities', error));
119
    }
120

    
121
    public showModal():void {
122
        this.modal.showModal();
123
    }
124

    
125
    public toggleCheckBoxes(event) {
126
        this.checkboxes.forEach(_ => _.checked = event.target.checked);
127
    }
128

    
129
    public applyCheck(flag : boolean) {
130
      console.info("applyCheck "+flag);
131
        this.checkboxes.forEach(_ => _.checked = flag);
132
    }
133

    
134
    public getSelectedPages() : string[] {
135
        return this.checkboxes.filter(page => page.checked == true).map(checkedPage => checkedPage.page).map(res => res._id);
136
    }
137

    
138
    private deletePagesFromArray(ids : string[]) : void {
139
        for(let id of ids) {
140
            let i = this.checkboxes.findIndex(_ => _.page._id == id);
141
            this.checkboxes.splice(i, 1);
142
        }
143
    }
144

    
145
    public confirmDeletePage(id : string) {
146
        this.deleteConfirmationModal.ids = [id];
147
        this.deleteConfirmationModal.showModal();
148
    }
149

    
150
    public confirmDeleteSelectedPages() {
151
        this.deleteConfirmationModal.ids = this.getSelectedPages();
152
        this.deleteConfirmationModal.showModal();
153
    }
154

    
155
    public confirmedDeletePages(ids : string[]) {
156
        this._helpContentService.deletePages(ids, this.properties.adminToolsAPIURL).subscribe(
157
            _ => this.deletePagesFromArray(ids),
158
            error => this.handleError('System error deleting the selected pages', error)
159
        );
160
    }
161

    
162
    public editPage(i : number) {
163
        let page : Page = this.checkboxes[i].page;
164
        this.formGroup.patchValue(page);
165
        const entityFGs = (page.entities as Entity[]).map(entity => this.formComponent._fb.group(entity));
166
        const entityFormArray = this.formComponent._fb.array(entityFGs);
167
        this.formGroup.setControl('entities', entityFormArray);
168
        console.info(this.formGroup.value);
169
        this.updateModal.showModal();
170
    }
171

    
172
    public pageSavedSuccessfully(page: Page) {
173
        this.checkboxes.push(<CheckPage>{page : page, checked : false});
174
        console.info("checkboxes length: "+this.checkboxes.length);
175
        this.applyCheck(false);
176
    }
177

    
178
    public pageUpdatedSuccessfully(page : Page) {
179
      console.info(page._id);
180
      console.info(this.checkboxes.find(checkItem => (checkItem.page._id == page._id)));
181
      console.info(page.entities);
182
        this.checkboxes.find(checkItem => checkItem.page._id==page._id).page = page;
183
        this.applyCheck(false);
184
    }
185

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

    
191
    public applyFilter() {
192
        this.checkboxes = [];
193
        this.pages.filter(item => this.filterPages(item)).forEach(
194
            _ => this.checkboxes.push(<CheckPage>{page: _, checked: false})
195
        );
196
    }
197

    
198
    public filterPages(page : Page) : boolean {
199
        let textFlag = this.searchText.toString() == '' || (page.route + ' ' +page.name).match(this.searchText) != null;
200
        return textFlag;
201
    }
202

    
203
    handleError(message: string, error) {
204
        if(error == null) {
205
            this.formComponent.reset();
206
        }
207
        this.errorMessage = message + ' (Server responded: ' + error + ')';
208
    }
209

    
210

    
211

    
212
    public filterByCommunity(event: any) {
213
        this.selectedCommunityPid = event.target.value;
214
        this.applyCommunityFilter(this.selectedCommunityPid);
215
    }
216

    
217
    public applyCommunityFilter(community_pid: string) {
218
        this.getPages(community_pid);
219
        this.getPagesWithDivIds(community_pid);
220
    }
221

    
222
    public togglePages(status : boolean, ids : string[]) {
223
        this._helpContentService.togglePages(this.selectedCommunityPid,ids,status, this.properties.adminToolsAPIURL).subscribe(
224
            () => {
225
                for(let id of ids) {
226
                //     let i = this.checkboxes.findIndex(_ => _.page._id == id);
227
                //     this.checkboxes[i].page.isEnabled=status;
228
                // }
229
                  let i = this.checkboxes.findIndex(_ => _.page._id == id);
230
                  this.checkboxes[i].page.isEnabled=status;
231
                }
232
                this.applyCheck(false);
233
            },
234
            error => this.handleError('System error changing the status of the selected page(s)', error)
235
        );
236
    }
237

    
238

    
239
}
(4-4/4)