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 { Portal } from "../../domain/portal";
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
import {PageHelpContent} from "../../domain/page-help-content";
16
import {Title} from '@angular/platform-browser';
17
import {ClearCacheService} from "../../openaireLibrary/services/clear-cache.service";
18

    
19
@Component({
20
    selector: 'div-help-contents',
21
    templateUrl: './div-help-contents.component.html',
22
})
23

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

    
30
    public checkboxes : CheckDivHelpContent[] = [];
31

    
32
    public divHelpContents : DivHelpContent[] = [];
33

    
34
    //public errorMessage: string;
35

    
36
    public formGroup : FormGroup;
37

    
38
    public pages: Page[];
39

    
40
    public checkboxAll : boolean = false;
41

    
42
    public filters : DivHelpContentFilterOptions = {id : '', active : null, text : new RegExp('')};
43
    public keyword: string = "";
44

    
45
    public counter = {all : 0, active : 0, inactive : 0};
46

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

    
49
    public selectedCommunityPid: string;
50

    
51
    public selectedPageId: string;
52

    
53
    public community: Portal;
54

    
55
    public page: Page;
56
    public properties:EnvProperties = null;
57

    
58
    public showLoading: boolean = true;
59
    public errorMessage: string = '';
60
    public updateErrorMessage: string = '';
61

    
62
    ngOnInit() {
63
      this.route.data
64
        .subscribe((data: { envSpecific: EnvProperties }) => {
65
           this.properties = data.envSpecific;
66

    
67
           this.route.queryParams.subscribe(params => {
68
             HelperFunctions.scroll();
69
             this.title.setTitle('Administration Dashboard | Class Help Texts');
70
             this.selectedCommunityPid = params['communityId'];
71
             this.selectedPageId = params['pageId'];
72

    
73
             if(this.selectedCommunityPid && this.selectedPageId) {
74
               this.getPage(this.selectedPageId);
75
             } else if(this.selectedCommunityPid){
76
               this.selectedPageId = "";
77
               this.getPages(this.selectedCommunityPid);
78
             }
79
           });
80
        });
81
    }
82

    
83
    constructor(private element: ElementRef, private route: ActivatedRoute,
84
                private title: Title,
85
                private _helpService: HelpContentService, private router : Router,
86
                private _clearCacheService: ClearCacheService) {}
87

    
88
    getPage(pageId: string) {
89
      if(!Session.isLoggedIn()){
90
        this.router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this.router.url} });
91
      } else {
92
        this.showLoading = true;
93
        this.updateErrorMessage = "";
94
        this.errorMessage = "";
95
        this._helpService.getPageByPortal(pageId, this.properties.adminToolsAPIURL, this.selectedCommunityPid).subscribe(
96
            page => {
97
              // if( (this.selectedCommunityPid == 'openaire' && !page.openaire)
98
              //   || (this.selectedCommunityPid == 'connect' && !page.connect)
99
              //   || (this.selectedCommunityPid != 'openaire' && this.selectedCommunityPid != 'connect' && !page.communities)) {
100
              if(this.properties.adminToolsPortalType != page.portalType) {
101
                this.router.navigate(['/classContents'], { queryParams: { "communityId": this.selectedCommunityPid} });
102
              } else {
103
                this.page = page;
104
                this.getDivHelpContents(this.selectedCommunityPid);
105
              }
106
            },
107
            error => this.handleError('System error retrieving page', error));
108
      }
109
    }
110

    
111
    getPages(community_pid: string) {
112
      if(!Session.isLoggedIn()){
113
        this.router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this.router.url} });
114
      } else {
115
        this.showLoading = true;
116
        this.updateErrorMessage = "";
117
        this.errorMessage = "";
118

    
119
        this._helpService.getCommunityPagesWithDivId(community_pid, this.properties.adminToolsAPIURL).subscribe(
120
        //this._helpService.getPagesWithDivIds(community_pid, this.properties.adminToolsAPIURL).subscribe(
121
            pages => {
122
              this.pages = pages;
123
              this.getDivHelpContents(this.selectedCommunityPid);
124
            },
125
            error => this.handleError('System error retrieving pages', error));
126
      }
127
    }
128

    
129
    public countDivHelpContents() {
130
        this.counter = {all : 0, active : 0, inactive : 0};
131
        let filter = Object.assign({},this.filters);
132
        filter.active = null;
133
        this.divHelpContents.forEach(_ => {
134
            if(this.filterDivHelpContent(_,filter)){
135
                if (_.isActive==true) this.counter.active++;
136
                else this.counter.inactive++
137
            }
138
        });
139
        this.counter.all = this.counter.active + this.counter.inactive;
140
    }
141

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

    
152
                for (let i = this.divHelpContents.length - 1; i >= 0; i -= 1) {
153
                //for (let i = 0; i < this.divHelpContents.length; i++) {
154
                  let divId: DivId = this.divHelpContents[i].divId as DivId;
155
                  let pages: Page[] =  divId.pages as Page[];
156
                  const pageIds = pages.map(x => x._id);
157

    
158
                  if(!this.selectedPageId || pageIds.includes(this.selectedPageId)) {
159
                    this.cutContent(this.divHelpContents[i]);
160
                    this.checkboxes.unshift(<CheckDivHelpContent>{divHelpContent : this.divHelpContents[i], checked : false});
161
                  } else {
162
                    this.divHelpContents.splice(i, 1);
163
                  }
164
                }
165

    
166
                this.countDivHelpContents();
167

    
168
                this.showLoading = false;
169
            },
170
            error => this.handleError('System error retrieving page contents', error));
171
      }
172
    }
173

    
174
    public toggleCheckBoxes(event) {
175
        this.checkboxes.forEach(_ => _.checked = event.target.checked);
176
        this.checkboxAll = event.target.checked;
177
    }
178

    
179
    public applyCheck(flag : boolean) {
180
        this.checkboxes.forEach(_ => _.checked = flag);
181
        this.checkboxAll = false;
182
    }
183

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

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

    
196
    public confirmDeleteSelectedDivHelpContents() {
197
        //this.deleteConfirmationModal.ids = this.getSelectedDivHelpContents();
198
        //this.deleteConfirmationModal.showModal();
199
        this.selectedDivContents = this.getSelectedDivHelpContents();
200
        this.confirmModalOpen();
201
    }
202

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

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

    
223
        this._helpService.deleteDivHelpContents(this.selectedDivContents, this.properties.adminToolsAPIURL, this.selectedCommunityPid).subscribe(
224
            _ => {
225
              this.deleteDivHelpContentsFromArray(this.selectedDivContents);
226
              this.showLoading = false;
227
              this._clearCacheService.clearCache("class help contents deleted");
228
            },
229
            error => this.handleUpdateError('System error deleting the selected class content(s)', error)
230
        );
231
      }
232
    }
233

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

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

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

    
259
        this._helpService.toggleDivHelpContents(ids,status, this.properties.adminToolsAPIURL, this.selectedCommunityPid).subscribe(
260
            () => {
261
                for(let id of ids) {
262
                    let i = this.checkboxes.findIndex(_ => _.divHelpContent._id == id);
263
                    this.checkboxes[i].divHelpContent.isActive=status;
264
                }
265
                this.countDivHelpContents();
266
                this.applyCheck(false);
267
                this._clearCacheService.clearCache("class help contents toggled (status: "+status+")");
268
            },
269
            error => this.handleUpdateError('System error changing the status of the selected page content(s)', error)
270
        );
271
      }
272
    }
273

    
274
    public divHelpContentSavedSuccessfully(divHelpContent: DivHelpContent) {
275
        this.cutContent(divHelpContent);
276
        this.checkboxes.push(<CheckDivHelpContent>{divHelpContent : divHelpContent, checked : false});
277
        this.divHelpContents.push(divHelpContent);
278
        this.applyCheck(false);
279
        this.countDivHelpContents();
280
    }
281

    
282
    public divHelpContentUpdatedSuccessfully(divHelpContent : DivHelpContent) {
283
        this.checkboxes.find(checkItem => checkItem.divHelpContent._id==divHelpContent._id).divHelpContent = divHelpContent;
284
        let index = this.divHelpContents.findIndex(checkItem => checkItem._id==divHelpContent._id);
285
        this.divHelpContents[index] = divHelpContent;
286
        this.applyCheck(false);
287
        this.countDivHelpContents();
288
    }
289

    
290

    
291
    public filterDivHelpContent(divHelpContent : DivHelpContent, filters : DivHelpContentFilterOptions) : boolean {
292
        let divId: DivId = divHelpContent.divId as DivId;
293
        let pages: Page[] = <Page[]>divId.pages;
294
        let pageIds: string[] = pages.map(x => x._id);
295

    
296
        let idFlag = filters.id == '' || /*(<Page[]>divId.pages)._id == filters.id*/ pageIds.includes(filters.id);
297
        let activeFlag = filters.active == null || divHelpContent.isActive == filters.active;
298
        let textFlag = filters.text.toString() == '' || (divHelpContent.content).match(filters.text) != null
299
          || ((<DivId>divHelpContent.divId).name).match(filters.text) != null;
300
        return idFlag && activeFlag && textFlag;
301
    }
302

    
303
    public cutContent(divHelpContent: DivHelpContent) {
304
      divHelpContent.content = divHelpContent.content.replace(/<[^>]*>/g, '');
305
      divHelpContent.content = divHelpContent.content.replace(/(\r\n|\n|\r| +(?= ))|\s\s+/gm," ");
306
      if(divHelpContent.content.length > 200) {
307
        divHelpContent.content = divHelpContent.content.substr(0, 200) + "...";
308
      }
309
    }
310

    
311
    public applyFilter() {
312
        this.checkboxes = [];
313
        this.divHelpContents.filter(item => this.filterDivHelpContent(item,this.filters)).forEach(
314
            _ => {
315
              this.cutContent(_);
316
              this.checkboxes.push(<CheckDivHelpContent>{divHelpContent: _, checked: false})
317
            }
318
        );
319
        this.countDivHelpContents();
320
    }
321

    
322
    public filterByPage(event: any) {
323
        this.filters.id = event.target.value;
324
        this.applyFilter();
325
    }
326

    
327
    public displayAllDivHelpContents() {
328
        this.filters.active = null;
329
        this.applyFilter();
330
    }
331

    
332
    public displayActiveDivHelpContents() {
333
        this.filters.active = true;
334
        this.applyFilter();
335
    }
336

    
337
    public filterBySearch(text : string) {
338
        this.filters.text = new RegExp(text, "i");
339
        this.applyFilter();
340
    }
341

    
342
    public displayInactiveDivHelpContents() {
343
        this.filters.active = false;
344
        this.applyFilter();
345
    }
346

    
347
    handleError(message: string, error) {
348
        this.errorMessage = message;
349
        console.log('Server responded: ' + error);
350

    
351
        this.showLoading = false;
352
    }
353

    
354
    handleUpdateError(message: string, error) {
355
        this.updateErrorMessage = message;
356
        console.log('Server responded: ' +error);
357

    
358
        this.showLoading = false;
359
    }
360

    
361
    public newClassContent() {
362
      if(this.selectedPageId) {
363
        this.router.navigate( ['/classContents/new'], { queryParams: {communityId: this.selectedCommunityPid, pageId: this.selectedPageId} } );
364
      } else {
365
        this.router.navigate( ['/classContents/new'], { queryParams: {communityId: this.selectedCommunityPid} } );
366
      }
367
    }
368
}
(6-6/15)