Project

General

Profile

1 53600 argiro.kok
/**
2
 * Created by stefania on 7/13/17.
3
 */
4 54074 konstantin
import { Component, ViewChild, OnInit, ElementRef } from '@angular/core';
5 53600 argiro.kok
import { FormGroup } from "@angular/forms";
6 53735 konstantin
import { ActivatedRoute, Router } from "@angular/router";
7 53600 argiro.kok
import { HelpContentService } from "../../services/help-content.service";
8
import { PageHelpContent, CheckPageHelpContent, PageHelpContentFilterOptions } from "../../domain/page-help-content";
9
import { Page } from "../../domain/page";
10
import { Community } from "../../domain/community";
11
import { EnvProperties } from '../../openaireLibrary/utils/properties/env-properties';
12
import {SafeHtmlPipe} from '../../openaireLibrary/utils/pipes/safeHTML.pipe';
13
14 53735 konstantin
import {Session} from '../../openaireLibrary/login/utils/helper.class';
15
import {LoginErrorCodes} from '../../openaireLibrary/login/utils/guardHelper.class';
16 55416 konstantin
import {HelperFunctions} from "../../openaireLibrary/utils/HelperFunctions.class";
17 58371 k.triantaf
import {Title} from '@angular/platform-browser';
18 53735 konstantin
19 53600 argiro.kok
@Component({
20
    selector: 'page-help-contents',
21
    templateUrl: './page-help-contents.component.html',
22
})
23
24
export class PageHelpContentsComponent implements OnInit {
25
26
    // @ViewChild(ModalFormComponent)
27
    // @ViewChild('saveModal')
28
    // public modal:ModalFormComponent;
29
    //
30
    // @ViewChild('updateModal')
31
    // public updateModal:ModalFormComponent;
32
    //
33
    // @ViewChild(PageHelpContentsFormComponent)
34
    // public formComponent : PageHelpContentsFormComponent;
35
36
    // @ViewChild('deleteConfirmationModal')
37
    // public deleteConfirmationModal : DeleteConfirmationDialogComponent;
38
    @ViewChild('AlertModalDeletePageHelpContents') alertModalDeletePageHelpContents;
39
    private selectedPageContents: string[] = [];
40
41
    public checkboxes : CheckPageHelpContent[] = [];
42
43
    public pageHelpContents : PageHelpContent[] = [];
44
45
    //public errorMessage: string;
46
47
    public formGroup : FormGroup;
48
49
    public pages: Page[];
50
51
    public checkboxAll : boolean = false;
52
53
    public filters : PageHelpContentFilterOptions = {id : '', active : null, text : new RegExp('')};
54 53984 konstantin
    public keyword: string = "";
55 53600 argiro.kok
56
    public counter = {all : 0, active : 0, inactive : 0};
57
58
    public communities: Community[] = [];
59
60
    public selectedCommunityPid: string;
61
62
    public selectedPageId: string;
63
64
    public community: Community;
65
66
    public page: Page;
67
    public properties:EnvProperties = null;
68
69
    public showLoading: boolean = true;
70
    public errorMessage: string = '';
71
    public updateErrorMessage: string = '';
72
73
    ngOnInit() {
74
      this.route.data
75
        .subscribe((data: { envSpecific: EnvProperties }) => {
76
           this.properties = data.envSpecific;
77
           this.route.queryParams.subscribe(params => {
78 55416 konstantin
             HelperFunctions.scroll();
79 58371 k.triantaf
             this.title.setTitle('Administration Dashboard | Page Help Texts');
80 53600 argiro.kok
             this.selectedCommunityPid = params['communityId'];
81
             this.selectedPageId = params['pageId'];
82
83
             if(this.selectedCommunityPid && this.selectedPageId) {
84
               this.getPage(this.selectedPageId);
85
             } else if(this.selectedCommunityPid) {
86
               this.selectedPageId = "";
87
               this.getPages(this.selectedCommunityPid);
88
             }
89
           });
90
      });
91
        // this.formGroup = this.formComponent.form;
92
    }
93
94 58371 k.triantaf
    constructor(private element: ElementRef, private route: ActivatedRoute,
95
                private title: Title,
96
                private router : Router, private _helpService: HelpContentService) {}
97 53600 argiro.kok
98
    getPage(pageId: string) {
99 53735 konstantin
      if(!Session.isLoggedIn()){
100
        this.router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this.router.url} });
101
      } else {
102
        this.showLoading = true;
103
        this.updateErrorMessage = "";
104
        this.errorMessage = "";
105 53600 argiro.kok
106 53735 konstantin
        this._helpService.getPage(pageId, this.properties.adminToolsAPIURL).subscribe(
107
            page => {
108 59297 konstantin
                // if( (this.selectedCommunityPid == 'openaire' && !page.openaire)
109
                //     || (this.selectedCommunityPid == 'connect' && !page.connect)
110
                //     || (this.selectedCommunityPid != 'openaire' && this.selectedCommunityPid != 'connect' && !page.communities)) {
111
                if(this.properties.adminToolsPortalType != page.portalType) {
112 56740 konstantin
                  this.router.navigate(['/pageContents'], { queryParams: { "communityId": this.selectedCommunityPid} });
113
                } else {
114
                  this.page = page;
115
                  this.getPageHelpContents(this.selectedCommunityPid);
116
                }
117 53735 konstantin
              },
118
              error => this.handleError('System error retrieving page', error));
119
      }
120 53600 argiro.kok
    }
121
122
    getPages(community_pid: string) {
123 53735 konstantin
      if(!Session.isLoggedIn()){
124
        this.router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this.router.url} });
125
      } else {
126
        this.showLoading = true;
127
        this.updateErrorMessage = "";
128
        this.errorMessage = "";
129 53600 argiro.kok
130 56740 konstantin
        //this._helpService.getCommunityPages(community_pid, "", this.properties.adminToolsAPIURL).subscribe(
131
        this._helpService.getPages(this.properties.adminToolsAPIURL, community_pid, true).subscribe(
132
          pages => {
133 53735 konstantin
              this.pages = pages;
134
              this.getPageHelpContents(this.selectedCommunityPid);
135
            },
136
            error => this.handleError('System error retrieving pages', error));
137
      }
138 53600 argiro.kok
    }
139
140
    public countPageHelpContents() {
141
        this.counter = {all : 0, active : 0, inactive : 0};
142
        let filter = Object.assign({},this.filters);
143
        filter.active = null;
144
        this.pageHelpContents.forEach(_ => {
145
            if(this.filterPageHelpContent(_,filter)){
146
                if (_.isActive==true) this.counter.active++;
147
                else this.counter.inactive++
148
            }
149
        });
150
        this.counter.all = this.counter.active + this.counter.inactive;
151
    }
152
153
    getPageHelpContents(community_pid: string) {
154 53735 konstantin
      if(!Session.isLoggedIn()){
155
        this.router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this.router.url} });
156
      } else {
157 53600 argiro.kok
        this._helpService.getCommunityPageHelpContents(community_pid, this.properties.adminToolsAPIURL).subscribe(
158
            pageHelpContents => {
159
                this.pageHelpContents = pageHelpContents as Array<PageHelpContent>;
160
                this.counter.all = this.pageHelpContents.length;
161
                this.checkboxes = [];
162
163 56780 konstantin
                for (let i = this.pageHelpContents.length - 1; i >= 0; i -= 1) {
164
                //for (let i = 0; i < this.pageHelpContents.length; i++) {
165 53600 argiro.kok
                  let page: Page =  this.pageHelpContents[i].page as Page;
166
                  if(!this.selectedPageId || (page._id == this.selectedPageId)) {
167 56775 konstantin
                    this.cutContent(this.pageHelpContents[i]);
168 56780 konstantin
                    this.checkboxes.unshift(<CheckPageHelpContent>{pageHelpContent : this.pageHelpContents[i], checked : false});
169 53600 argiro.kok
                  } else {
170
                    this.pageHelpContents.splice(i, 1);
171
                  }
172
                }
173
174
                this.countPageHelpContents();
175
176
                this.showLoading = false;
177
            },
178
            error => this.handleError('System error retrieving page contents', error));
179 53735 konstantin
        }
180 53600 argiro.kok
    }
181
182
    // public showModal():void {
183
    //     this.modal.showModal();
184
    // }
185
186
    public toggleCheckBoxes(event) {
187
        this.checkboxes.forEach(_ => _.checked = event.target.checked);
188
        this.checkboxAll = event.target.checked;
189
    }
190
191
    public applyCheck(flag : boolean) {
192
        this.checkboxes.forEach(_ => _.checked = flag);
193
        this.checkboxAll = false;
194
    }
195
196
    public getSelectedPageHelpContents() : string[] {
197
        return this.checkboxes.filter(pageHelpContent => pageHelpContent.checked == true)
198
            .map(checkedPageHelpContent => checkedPageHelpContent.pageHelpContent).map(res => res._id);
199
    }
200
201
    public confirmDeletePageHelpContent(id : string) {
202
        //this.deleteConfirmationModal.ids = [id];
203
        //this.deleteConfirmationModal.showModal();
204
        this.selectedPageContents = [id];
205
        this.confirmModalOpen();
206
    }
207
208
    public confirmDeleteSelectedPageHelpContents() {
209
        //this.deleteConfirmationModal.ids = this.getSelectedPageHelpContents();
210
        //this.deleteConfirmationModal.showModal();
211
        this.selectedPageContents = this.getSelectedPageHelpContents();
212
        this.confirmModalOpen();
213
    }
214
215
    private confirmModalOpen() {
216 53735 konstantin
      if(!Session.isLoggedIn()){
217
        this.router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this.router.url} });
218
      } else {
219
        this.alertModalDeletePageHelpContents.cancelButton = true;
220
        this.alertModalDeletePageHelpContents.okButton = true;
221
        this.alertModalDeletePageHelpContents.alertTitle = "Delete Confirmation";
222
        this.alertModalDeletePageHelpContents.message = "Are you sure you want to delete the selected page content(s)?";
223
        this.alertModalDeletePageHelpContents.okButtonText = "Yes";
224
        this.alertModalDeletePageHelpContents.open();
225
      }
226 53600 argiro.kok
    }
227
228
    public confirmedDeletePageHelpContents(data: any) {
229 53735 konstantin
      if(!Session.isLoggedIn()){
230
        this.router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this.router.url} });
231
      } else {
232 53600 argiro.kok
        this.showLoading = true;
233
        this.updateErrorMessage = "";
234
235
        this._helpService.deletePageHelpContents(this.selectedPageContents, this.properties.adminToolsAPIURL).subscribe(
236
            _ => {
237
              this.deletePageHelpContentsFromArray(this.selectedPageContents);
238
              this.showLoading = false;
239
            },
240
            error => this.handleUpdateError('System error deleting the selected page content(s)', error)
241
        );
242 53735 konstantin
      }
243 53600 argiro.kok
    }
244
245
    private deletePageHelpContentsFromArray(ids : string[]) : void {
246
        for(let id of ids) {
247
            let iqc = this.checkboxes.findIndex(_ => _.pageHelpContent._id == id);
248
            let iq = this.pageHelpContents.findIndex(_ => _._id == id);
249
            this.checkboxes.splice(iqc, 1);
250
            this.pageHelpContents.splice(iqc, 1);
251
        }
252
        this.countPageHelpContents();
253
    }
254
255
    public editPageHelpContent(id : string) {
256
        //this.router.navigate(['/pageContents/edit/', _id]);
257
        if(this.selectedPageId) {
258
          this.router.navigate( ['/pageContents/edit/'], { queryParams: { "pageContentId": id, "communityId": this.selectedCommunityPid, "pageId": this.selectedPageId  } } );
259
        } else {
260
          this.router.navigate( ['/pageContents/edit/'], { queryParams: { "pageContentId": id, "communityId": this.selectedCommunityPid } } );
261
        }
262
    }
263
264
    public togglePageHelpContents(status : boolean, ids : string[]) {
265 53735 konstantin
      if(!Session.isLoggedIn()){
266
        this.router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this.router.url} });
267
      } else {
268 53600 argiro.kok
        this.updateErrorMessage = "";
269
270
        this._helpService.togglePageHelpContents(ids,status, this.properties.adminToolsAPIURL).subscribe(
271
            () => {
272
                for(let id of ids) {
273
                    let i = this.checkboxes.findIndex(_ => _.pageHelpContent._id == id);
274
                    this.checkboxes[i].pageHelpContent.isActive=status;
275
                }
276
                this.countPageHelpContents();
277
                this.applyCheck(false);
278
            },
279
            error => this.handleUpdateError('System error changing the status of the selected page content(s)', error)
280
        );
281 53735 konstantin
      }
282 53600 argiro.kok
    }
283
284
    public pageHelpContentSavedSuccessfully(pageHelpContent: PageHelpContent) {
285 56775 konstantin
        this.cutContent(pageHelpContent);
286 53600 argiro.kok
        this.checkboxes.push(<CheckPageHelpContent>{pageHelpContent : pageHelpContent, checked : false});
287
        this.pageHelpContents.push(pageHelpContent);
288
        this.applyCheck(false);
289
        this.countPageHelpContents();
290
    }
291
292
    public pageHelpContentUpdatedSuccessfully(pageHelpContent : PageHelpContent) {
293
        this.checkboxes.find(checkItem => checkItem.pageHelpContent._id==pageHelpContent._id).pageHelpContent = pageHelpContent;
294
        let index = this.pageHelpContents.findIndex(checkItem => checkItem._id==pageHelpContent._id);
295
        this.pageHelpContents[index] = pageHelpContent;
296
        this.applyCheck(false);
297
        this.countPageHelpContents();
298
    }
299
300
301
    public filterPageHelpContent(pageHelpContent : PageHelpContent, filters : PageHelpContentFilterOptions) : boolean {
302 56786 konstantin
      let idFlag = filters.id == '' || (<Page>pageHelpContent.page)._id == filters.id;
303
      let activeFlag = filters.active == null || pageHelpContent.isActive == filters.active;
304
      let textFlag = filters.text.toString() == '' || (pageHelpContent.content).match(filters.text) != null
305
        || ((<Page>pageHelpContent.page).name).match(filters.text) != null;
306
      return idFlag && activeFlag && textFlag;
307 53600 argiro.kok
    }
308
309 56775 konstantin
    public cutContent(pageHelpContent: PageHelpContent) {
310
      pageHelpContent.content = pageHelpContent.content.replace(/<[^>]*>/g, '');
311 56786 konstantin
      pageHelpContent.content = pageHelpContent.content.replace(/(\r\n|\n|\r| +(?= ))|\s\s+/gm," ");
312
313 56775 konstantin
      if(pageHelpContent.content.length > 200) {
314
        pageHelpContent.content = pageHelpContent.content.substr(0, 200) + "...";
315
      }
316
    }
317
318 53600 argiro.kok
    public applyFilter() {
319
        this.checkboxes = [];
320
        this.pageHelpContents.filter(item => this.filterPageHelpContent(item,this.filters)).forEach(
321 56775 konstantin
            _ => {
322
              this.cutContent(_);
323
              this.checkboxes.push(<CheckPageHelpContent>{pageHelpContent: _, checked: false})
324
            }
325 53600 argiro.kok
        );
326
        this.countPageHelpContents();
327
    }
328
329
    public filterByPage(event: any) {
330
        this.filters.id = event.target.value;
331
        this.applyFilter();
332
    }
333
334
    public displayAllPageHelpContents() {
335
        this.filters.active = null;
336
        this.applyFilter();
337
    }
338
339
    public displayActivePageHelpContents() {
340
        this.filters.active = true;
341
        this.applyFilter();
342
    }
343
344
    public filterBySearch(text : string) {
345
        this.filters.text = new RegExp(text, "i");
346
        this.applyFilter();
347
    }
348
349
    public displayInactivePageHelpContents() {
350
        this.filters.active = false;
351
        this.applyFilter();
352
    }
353
354
    handleError(message: string, error) {
355
        this.errorMessage = message;
356
        console.log('Server responded: ' + error);
357
358
        this.showLoading = false;
359
    }
360
361
    handleUpdateError(message: string, error) {
362
        this.updateErrorMessage = message;
363
        console.log('Server responded: ' +error);
364
365
        this.showLoading = false;
366
    }
367
368
    public newPageContent() {
369
      if(this.selectedPageId) {
370
        this.router.navigate( ['/pageContents/new'], { queryParams: {communityId: this.selectedCommunityPid, pageId: this.selectedPageId} } );
371
      } else {
372
        this.router.navigate( ['/pageContents/new'], { queryParams: {communityId: this.selectedCommunityPid} } );
373
      }
374
    }
375
}