Project

General

Profile

1
import { Component, ViewChild, OnInit, ElementRef } from '@angular/core';
2
import { Router, ActivatedRoute } from "@angular/router";
3
import { FormGroup } from "@angular/forms";
4
import { HelpContentService } from "../../services/help-content.service";
5
import { DivHelpContent, CheckDivHelpContent, DivHelpContentFilterOptions } from "../../domain/div-help-content";
6
import { Page } from "../../domain/page";
7
import { Community } from "../../domain/community";
8
import { DivId } from "../../domain/divId";
9
import { EnvProperties } from '../../openaireLibrary/utils/properties/env-properties';
10
import {SafeHtmlPipe} from '../../openaireLibrary/utils/pipes/safeHTML.pipe';
11

    
12
import {Session} from '../../openaireLibrary/login/utils/helper.class';
13
import {LoginErrorCodes} from '../../openaireLibrary/login/utils/guardHelper.class';
14
import {HelperFunctions} from "../../openaireLibrary/utils/HelperFunctions.class";
15

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

    
21
export class DivHelpContentsComponent implements OnInit {
22
    // @ViewChild('deleteConfirmationModal')
23
    // public deleteConfirmationModal : DeleteConfirmationDialogComponent;
24
    @ViewChild('AlertModalDeleteDivHelpContents') alertModalDeleteDivHelpContents;
25
    private selectedDivContents: string[] = [];
26

    
27
    public checkboxes : CheckDivHelpContent[] = [];
28

    
29
    public divHelpContents : DivHelpContent[] = [];
30

    
31
    //public errorMessage: string;
32

    
33
    public formGroup : FormGroup;
34

    
35
    public pages: Page[];
36

    
37
    public checkboxAll : boolean = false;
38

    
39
    public filters : DivHelpContentFilterOptions = {id : '', active : null, text : new RegExp('')};
40
    public keyword: string = "";
41

    
42
    public counter = {all : 0, active : 0, inactive : 0};
43

    
44
    public communities: Community[] = [];
45

    
46
    public selectedCommunityPid: string;
47

    
48
    public selectedPageId: string;
49

    
50
    public community: Community;
51

    
52
    public page: Page;
53
    public properties:EnvProperties = null;
54

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

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

    
64
           this.route.queryParams.subscribe(params => {
65
             HelperFunctions.scroll();
66

    
67
             this.selectedCommunityPid = params['communityId'];
68
             this.selectedPageId = params['pageId'];
69

    
70
             if(this.selectedCommunityPid && this.selectedPageId) {
71
               this.getPage(this.selectedPageId);
72
             } else if(this.selectedCommunityPid){
73
               this.selectedPageId = "";
74
               this.getPages(this.selectedCommunityPid);
75
             }
76
           });
77
        });
78
    }
79

    
80
    constructor(private element: ElementRef, private route: ActivatedRoute, private _helpService: HelpContentService, private router : Router) {}
81

    
82
    getPage(pageId: string) {
83
      if(!Session.isLoggedIn()){
84
        this.router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this.router.url} });
85
      } else {
86
        this.showLoading = true;
87
        this.updateErrorMessage = "";
88
        this.errorMessage = "";
89

    
90
        this._helpService.getPage(pageId, this.properties.adminToolsAPIURL).subscribe(
91
            page => {
92
              if( (this.selectedCommunityPid == 'openaire' && !page.openaire)
93
                || (this.selectedCommunityPid == 'connect' && !page.connect)
94
                || (this.selectedCommunityPid != 'openaire' && this.selectedCommunityPid != 'connect' && !page.communities)) {
95
                this.router.navigate(['/classContents'], { queryParams: { "communityId": this.selectedCommunityPid} });
96
              } else {
97
                this.page = page;
98
                this.getDivHelpContents(this.selectedCommunityPid);
99
              }
100
            },
101
            error => this.handleError('System error retrieving page', error));
102
      }
103
    }
104

    
105
    getPages(community_pid: string) {
106
      if(!Session.isLoggedIn()){
107
        this.router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this.router.url} });
108
      } else {
109
        this.showLoading = true;
110
        this.updateErrorMessage = "";
111
        this.errorMessage = "";
112

    
113
        this._helpService.getCommunityPagesWithDivId(community_pid, this.properties.adminToolsAPIURL).subscribe(
114
        //this._helpService.getPagesWithDivIds(community_pid, this.properties.adminToolsAPIURL).subscribe(
115
            pages => {
116
              this.pages = pages;
117
              this.getDivHelpContents(this.selectedCommunityPid);
118
            },
119
            error => this.handleError('System error retrieving pages', error));
120
      }
121
    }
122

    
123
    public countDivHelpContents() {
124
        this.counter = {all : 0, active : 0, inactive : 0};
125
        let filter = Object.assign({},this.filters);
126
        filter.active = null;
127
        this.divHelpContents.forEach(_ => {
128
            if(this.filterDivHelpContent(_,filter)){
129
                if (_.isActive==true) this.counter.active++;
130
                else this.counter.inactive++
131
            }
132
        });
133
        this.counter.all = this.counter.active + this.counter.inactive;
134
    }
135

    
136
    getDivHelpContents(community_pid: string) {
137
      if(!Session.isLoggedIn()){
138
        this.router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this.router.url} });
139
      } else {
140
        this._helpService.getCommunityDivHelpContents(community_pid, this.properties.adminToolsAPIURL).subscribe(
141
            divHelpContents => {
142
                this.divHelpContents = divHelpContents as Array<DivHelpContent>;
143
                this.counter.all = this.divHelpContents.length;
144
                this.checkboxes = [];
145

    
146
                //for (let i = this.divHelpContents.length - 1; i >= 0; i -= 1) {
147
                for (let i = 0; i < this.divHelpContents.length; i++) {
148
                  let divId: DivId = this.divHelpContents[i].divId as DivId;
149
                  let pages: Page[] =  divId.pages as Page[];
150
                  const pageIds = pages.map(x => x._id);
151

    
152
                  if(!this.selectedPageId || pageIds.includes(this.selectedPageId)) {
153
                    this.divHelpContents[i].content = this.divHelpContents[i].content.replace(/<[^>]*>/g, '');
154
                    if(this.divHelpContents[i].content.length > 200) {
155
                      this.divHelpContents[i].content = this.divHelpContents[i].content.substr(0, 200) + "...";
156
                    }
157
                    this.checkboxes.push(<CheckDivHelpContent>{divHelpContent : this.divHelpContents[i], checked : false});
158
                  } else {
159
                    this.divHelpContents.splice(i, 1);
160
                  }
161
                }
162

    
163
                this.countDivHelpContents();
164

    
165
                this.showLoading = false;
166
            },
167
            error => this.handleError('System error retrieving page contents', error));
168
      }
169
    }
170

    
171
    public toggleCheckBoxes(event) {
172
        this.checkboxes.forEach(_ => _.checked = event.target.checked);
173
        this.checkboxAll = event.target.checked;
174
    }
175

    
176
    public applyCheck(flag : boolean) {
177
        this.checkboxes.forEach(_ => _.checked = flag);
178
        this.checkboxAll = false;
179
    }
180

    
181
    public getSelectedDivHelpContents() : string[] {
182
        return this.checkboxes.filter(divHelpContent => divHelpContent.checked == true)
183
            .map(checkedDivHelpContent => checkedDivHelpContent.divHelpContent).map(res => res._id);
184
    }
185

    
186
    public confirmDeleteDivHelpContent(id : string) {
187
        //this.deleteConfirmationModal.ids = [id];
188
        //this.deleteConfirmationModal.showModal();
189
        this.selectedDivContents = [id];
190
        this.confirmModalOpen();
191
    }
192

    
193
    public confirmDeleteSelectedDivHelpContents() {
194
        //this.deleteConfirmationModal.ids = this.getSelectedDivHelpContents();
195
        //this.deleteConfirmationModal.showModal();
196
        this.selectedDivContents = this.getSelectedDivHelpContents();
197
        this.confirmModalOpen();
198
    }
199

    
200
    private confirmModalOpen() {
201
      if(!Session.isLoggedIn()){
202
        this.router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this.router.url} });
203
      } else {
204
        this.alertModalDeleteDivHelpContents.cancelButton = true;
205
        this.alertModalDeleteDivHelpContents.okButton = true;
206
        this.alertModalDeleteDivHelpContents.alertTitle = "Delete Confirmation";
207
        this.alertModalDeleteDivHelpContents.message = "Are you sure you want to delete the help text(s)?";
208
        this.alertModalDeleteDivHelpContents.okButtonText = "Yes";
209
        this.alertModalDeleteDivHelpContents.open();
210
      }
211
    }
212

    
213
    public confirmedDeleteDivHelpContents(data: any) {
214
      if(!Session.isLoggedIn()){
215
        this.router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this.router.url} });
216
      } else {
217
        this.showLoading = true;
218
        this.updateErrorMessage = "";
219

    
220
        this._helpService.deleteDivHelpContents(this.selectedDivContents, this.properties.adminToolsAPIURL).subscribe(
221
            _ => {
222
              this.deleteDivHelpContentsFromArray(this.selectedDivContents);
223
              this.showLoading = false;
224
            },
225
            error => this.handleUpdateError('System error deleting the selected class content(s)', error)
226
        );
227
      }
228
    }
229

    
230
    private deleteDivHelpContentsFromArray(ids : string[]) : void {
231
        for(let id of ids) {
232
            let iqc = this.checkboxes.findIndex(_ => _.divHelpContent._id == id);
233
            let iq = this.divHelpContents.findIndex(_ => _._id == id);
234
            this.checkboxes.splice(iqc, 1);
235
            this.divHelpContents.splice(iqc, 1);
236
        }
237
        this.countDivHelpContents();
238
    }
239

    
240
    public editDivHelpContent(id : string) {
241
        //this.router.navigate(['/pageContents/edit/', _id]);
242
        if(this.selectedPageId) {
243
          this.router.navigate( ['/classContents/edit/'], { queryParams: { "classContentId": id, "communityId": this.selectedCommunityPid, "pageId": this.selectedPageId  } } );
244
        } else {
245
          this.router.navigate( ['/classContents/edit/'], { queryParams: { "classContentId": id, "communityId": this.selectedCommunityPid } } );
246
        }
247
    }
248

    
249
    public toggleDivHelpContents(status : boolean, ids : string[]) {
250
      if(!Session.isLoggedIn()){
251
        this.router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this.router.url} });
252
      } else {
253
        this.updateErrorMessage = "";
254

    
255
        this._helpService.toggleDivHelpContents(ids,status, this.properties.adminToolsAPIURL).subscribe(
256
            () => {
257
                for(let id of ids) {
258
                    let i = this.checkboxes.findIndex(_ => _.divHelpContent._id == id);
259
                    this.checkboxes[i].divHelpContent.isActive=status;
260
                }
261
                this.countDivHelpContents();
262
                this.applyCheck(false);
263
            },
264
            error => this.handleUpdateError('System error changing the status of the selected page content(s)', error)
265
        );
266
      }
267
    }
268

    
269
    public divHelpContentSavedSuccessfully(divHelpContent: DivHelpContent) {
270
        this.checkboxes.push(<CheckDivHelpContent>{divHelpContent : divHelpContent, checked : false});
271
        this.divHelpContents.push(divHelpContent);
272
        this.applyCheck(false);
273
        this.countDivHelpContents();
274
    }
275

    
276
    public divHelpContentUpdatedSuccessfully(divHelpContent : DivHelpContent) {
277
        this.checkboxes.find(checkItem => checkItem.divHelpContent._id==divHelpContent._id).divHelpContent = divHelpContent;
278
        let index = this.divHelpContents.findIndex(checkItem => checkItem._id==divHelpContent._id);
279
        this.divHelpContents[index] = divHelpContent;
280
        this.applyCheck(false);
281
        this.countDivHelpContents();
282
    }
283

    
284

    
285
    public filterDivHelpContent(divHelpContent : DivHelpContent, filters : DivHelpContentFilterOptions) : boolean {
286
        let divId: DivId = divHelpContent.divId as DivId;
287
        let pages: Page[] = <Page[]>divId.pages;
288
        let pageIds: string[] = pages.map(x => x._id);
289

    
290
        let idFlag = filters.id == '' || /*(<Page[]>divId.pages)._id == filters.id*/ pageIds.includes(filters.id);
291
        let activeFlag = filters.active == null || divHelpContent.isActive == filters.active;
292
        let textFlag = filters.text.toString() == '' || (divHelpContent.content).match(filters.text) != null;
293
        return idFlag && activeFlag && textFlag;
294
    }
295

    
296
    public applyFilter() {
297
        this.checkboxes = [];
298
        this.divHelpContents.filter(item => this.filterDivHelpContent(item,this.filters)).forEach(
299
            _ => this.checkboxes.push(<CheckDivHelpContent>{divHelpContent: _, checked: false})
300
        );
301
        this.countDivHelpContents();
302
    }
303

    
304
    public filterByPage(event: any) {
305
        this.filters.id = event.target.value;
306
        this.applyFilter();
307
    }
308

    
309
    public displayAllDivHelpContents() {
310
        this.filters.active = null;
311
        this.applyFilter();
312
    }
313

    
314
    public displayActiveDivHelpContents() {
315
        this.filters.active = true;
316
        this.applyFilter();
317
    }
318

    
319
    public filterBySearch(text : string) {
320
        this.filters.text = new RegExp(text, "i");
321
        this.applyFilter();
322
    }
323

    
324
    public displayInactiveDivHelpContents() {
325
        this.filters.active = false;
326
        this.applyFilter();
327
    }
328

    
329
    handleError(message: string, error) {
330
        this.errorMessage = message;
331
        console.log('Server responded: ' + error);
332

    
333
        this.showLoading = false;
334
    }
335

    
336
    handleUpdateError(message: string, error) {
337
        this.updateErrorMessage = message;
338
        console.log('Server responded: ' +error);
339

    
340
        this.showLoading = false;
341
    }
342

    
343
    public newClassContent() {
344
      if(this.selectedPageId) {
345
        this.router.navigate( ['/classContents/new'], { queryParams: {communityId: this.selectedCommunityPid, pageId: this.selectedPageId} } );
346
      } else {
347
        this.router.navigate( ['/classContents/new'], { queryParams: {communityId: this.selectedCommunityPid} } );
348
      }
349
    }
350
}
(6-6/15)