Project

General

Profile

1
import {Component, ViewChild, OnInit, ElementRef} from '@angular/core';
2
import {FormBuilder, FormControl, FormGroup} from "@angular/forms";
3
import {ActivatedRoute, Router} from "@angular/router";
4
import {HelpContentService} from "../../services/help-content.service";
5
import {
6
  PageHelpContent,
7
  CheckPageHelpContent,
8
  PageHelpContentFilterOptions
9
} from "../../utils/entities/adminTool/page-help-content";
10
import {Page} from "../../utils/entities/adminTool/page";
11
import {Portal} from "../../utils/entities/adminTool/portal";
12
import {EnvProperties} from '../../utils/properties/env-properties';
13
import {Session} from '../../login/utils/helper.class';
14
import {LoginErrorCodes} from '../../login/utils/guardHelper.class';
15
import {HelperFunctions} from "../../utils/HelperFunctions.class";
16
import {Subscriber} from "rxjs";
17
import {properties} from "../../../../environments/environment";
18

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

    
24
export class PageHelpContentsComponent implements OnInit {
25

    
26
  @ViewChild('AlertModalDeletePageHelpContents') alertModalDeletePageHelpContents;
27
  private selectedPageContents: string[] = [];
28

    
29
  public checkboxes: CheckPageHelpContent[] = [];
30

    
31
  public pageHelpContents: PageHelpContent[] = [];
32

    
33
  public formGroup: FormGroup;
34

    
35
  public pages: Page[];
36

    
37
  public checkboxAll: boolean = false;
38

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

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

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

    
46
  public selectedCommunityPid: string;
47

    
48
  public selectedPageId: string;
49

    
50
  public community: Portal;
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
  public filterForm: FormControl;
59
  public selectForm: FormControl;
60
  public selectOptions = [];
61
  private subscriptions: any[] = [];
62

    
63
  ngOnInit() {
64
    this.filterForm = this._fb.control('');
65
    this.selectForm = this._fb.control('');
66
    this.subscriptions.push(this.filterForm.valueChanges.subscribe(value => {
67
      this.filterBySearch(value);
68
    }));
69
    this.subscriptions.push(this.selectForm.valueChanges.subscribe(value => {
70
      this.filterByPage(value);
71
    }));
72
    this.route.data
73
      .subscribe((data: { envSpecific: EnvProperties }) => {
74
        this.properties = data.envSpecific;
75
        this.route.queryParams.subscribe(params => {
76
          HelperFunctions.scroll();
77

    
78
          this.selectedCommunityPid = params['communityId'];
79
          this.selectedPageId = params['pageId'];
80

    
81
          if (this.selectedCommunityPid && this.selectedPageId) {
82
            this.getPage(this.selectedPageId);
83
          } else if (this.selectedCommunityPid) {
84
            this.selectedPageId = "";
85
            this.getPages(this.selectedCommunityPid);
86
          }
87
        });
88
      });
89
    // this.myForm = this.formComponent.form;
90
  }
91

    
92
  constructor(private element: ElementRef, private route: ActivatedRoute, private router: Router, private _helpService: HelpContentService, private _fb: FormBuilder) {
93
  }
94
  ngOnDestroy(): void {
95
    this.subscriptions.forEach(value => {
96
      if (value instanceof Subscriber) {
97
        value.unsubscribe();
98
      } else if (value instanceof Function) {
99
        value();
100
      }
101
    });
102
  }
103
  init(){
104

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

    
119
      this._helpService.getPageByPortal(pageId, this.properties.adminToolsAPIURL, this.selectedCommunityPid).subscribe(
120
        page => {
121
          if (this.properties.adminToolsPortalType != page.portalType) {
122
            this.router.navigate(['./pageContents'], {queryParams: {"communityId": this.selectedCommunityPid}});
123
          } else {
124
            this.page = page;
125
            this.getPageHelpContents(this.selectedCommunityPid);
126
          }
127
        },
128
        error => this.handleError('System error retrieving page', error));
129
    }
130
  }
131

    
132
  getPages(community_pid: string) {
133
    if (!Session.isLoggedIn()) {
134
      this.router.navigate(['/user-info'], {
135
        queryParams: {
136
          "errorCode": LoginErrorCodes.NOT_VALID,
137
          "redirectUrl": this.router.url
138
        }
139
      });
140
    } else {
141
      this.showLoading = true;
142
      this.updateErrorMessage = "";
143
      this.errorMessage = "";
144

    
145
      //this._helpService.getCommunityPages(community_pid, "", this.properties.adminToolsAPIURL).subscribe(
146
      this._helpService.getCommunityPagesWithPositions(community_pid,this.properties.adminToolsAPIURL).subscribe(
147
        pages => {
148
          this.pages = pages;
149
          this.getPageHelpContents(this.selectedCommunityPid);
150
          this.selectOptions = [{label:'All pages', value: ''}];
151
          for (let page of this.pages) {
152
            this.selectOptions.push({label:page.name, value: page._id})
153
          }
154
        },
155
        error => this.handleError('System error retrieving pages', error));
156
    }
157
  }
158

    
159
  public countPageHelpContents() {
160
    this.counter = {all: 0, active: 0, inactive: 0};
161
    let filter = Object.assign({}, this.filters);
162
    filter.active = null;
163
    this.pageHelpContents.forEach(_ => {
164
      if (this.filterPageHelpContent(_, filter)) {
165
        if (_.isActive == true) this.counter.active++;
166
        else this.counter.inactive++
167
      }
168
    });
169
    this.counter.all = this.counter.active + this.counter.inactive;
170
  }
171

    
172
  getPageHelpContents(community_pid: string) {
173
    if (!Session.isLoggedIn()) {
174
      this.router.navigate(['/user-info'], {
175
        queryParams: {
176
          "errorCode": LoginErrorCodes.NOT_VALID,
177
          "redirectUrl": this.router.url
178
        }
179
      });
180
    } else {
181
      this._helpService.getCommunityPageHelpContents(community_pid, this.properties.adminToolsAPIURL).subscribe(
182
        pageHelpContents => {
183
          this.pageHelpContents = pageHelpContents as Array<PageHelpContent>;
184
          this.counter.all = this.pageHelpContents.length;
185
          this.checkboxes = [];
186

    
187
          for (let i = this.pageHelpContents.length - 1; i >= 0; i -= 1) {
188
            //for (let i = 0; i < this.pageHelpContents.length; i++) {
189
            let page: Page = this.pageHelpContents[i].page as Page;
190
            if (!this.selectedPageId || (page._id == this.selectedPageId)) {
191
              this.cutContent(this.pageHelpContents[i]);
192
              this.checkboxes.unshift(<CheckPageHelpContent>{
193
                pageHelpContent: this.pageHelpContents[i],
194
                checked: false
195
              });
196
            } else {
197
              this.pageHelpContents.splice(i, 1);
198
            }
199
          }
200

    
201
          this.countPageHelpContents();
202

    
203
          this.showLoading = false;
204
        },
205
        error => this.handleError('System error retrieving page contents', error));
206
    }
207
  }
208

    
209
  // public showModal():void {
210
  //     this.modal.showModal();
211
  // }
212

    
213
  public toggleCheckBoxes(event) {
214
    this.checkboxes.forEach(_ => _.checked = event.target.checked);
215
    this.checkboxAll = event.target.checked;
216
  }
217

    
218
  public applyCheck(flag: boolean) {
219
    this.checkboxes.forEach(_ => _.checked = flag);
220
    this.checkboxAll = false;
221
  }
222

    
223
  public getSelectedPageHelpContents(): string[] {
224
    return this.checkboxes.filter(pageHelpContent => pageHelpContent.checked == true)
225
      .map(checkedPageHelpContent => checkedPageHelpContent.pageHelpContent).map(res => res._id);
226
  }
227

    
228
  public confirmDeletePageHelpContent(id: string) {
229
    //this.deleteConfirmationModal.ids = [id];
230
    //this.deleteConfirmationModal.showModal();
231
    this.selectedPageContents = [id];
232
    this.confirmModalOpen();
233
  }
234

    
235
  public confirmDeleteSelectedPageHelpContents() {
236
    //this.deleteConfirmationModal.ids = this.getSelectedPageHelpContents();
237
    //this.deleteConfirmationModal.showModal();
238
    this.selectedPageContents = this.getSelectedPageHelpContents();
239
    this.confirmModalOpen();
240
  }
241

    
242
  private confirmModalOpen() {
243
    if (!Session.isLoggedIn()) {
244
      this.router.navigate(['/user-info'], {
245
        queryParams: {
246
          "errorCode": LoginErrorCodes.NOT_VALID,
247
          "redirectUrl": this.router.url
248
        }
249
      });
250
    } else {
251
      this.alertModalDeletePageHelpContents.cancelButton = true;
252
      this.alertModalDeletePageHelpContents.okButton = true;
253
      this.alertModalDeletePageHelpContents.alertTitle = "Delete Confirmation";
254
      this.alertModalDeletePageHelpContents.message = "Are you sure you want to delete the selected page content(s)?";
255
      this.alertModalDeletePageHelpContents.okButtonText = "Yes";
256
      this.alertModalDeletePageHelpContents.open();
257
    }
258
  }
259

    
260
  public confirmedDeletePageHelpContents(data: any) {
261
    if (!Session.isLoggedIn()) {
262
      this.router.navigate(['/user-info'], {
263
        queryParams: {
264
          "errorCode": LoginErrorCodes.NOT_VALID,
265
          "redirectUrl": this.router.url
266
        }
267
      });
268
    } else {
269
      this.showLoading = true;
270
      this.updateErrorMessage = "";
271

    
272
      this._helpService.deletePageHelpContents(this.selectedPageContents, this.properties.adminToolsAPIURL, this.selectedCommunityPid).subscribe(
273
        _ => {
274
          this.deletePageHelpContentsFromArray(this.selectedPageContents);
275
          this.showLoading = false;
276
        },
277
        error => this.handleUpdateError('System error deleting the selected page content(s)', error)
278
      );
279
    }
280
  }
281

    
282
  private deletePageHelpContentsFromArray(ids: string[]): void {
283
    for (let id of ids) {
284
      let iqc = this.checkboxes.findIndex(_ => _.pageHelpContent._id == id);
285
      let iq = this.pageHelpContents.findIndex(_ => _._id == id);
286
      this.checkboxes.splice(iqc, 1);
287
      this.pageHelpContents.splice(iqc, 1);
288
    }
289
    this.countPageHelpContents();
290
  }
291

    
292
  public editPageHelpContent(id: string) {
293
    //this.router.navigate(['/pageContents/edit/', _id]);
294
    if (this.selectedPageId) {
295
      this.router.navigate(['edit/'], {
296
        queryParams: {
297
          "pageContentId": id,
298
          "communityId": this.selectedCommunityPid,
299
          "pageId": this.selectedPageId
300
        }, relativeTo: this.route
301
      });
302
    } else {
303
      this.router.navigate(['edit/'], {
304
        queryParams: {
305
          "pageContentId": id,
306
          "communityId": this.selectedCommunityPid
307
        }, relativeTo: this.route
308
      });
309
    }
310
  }
311

    
312
  public togglePageHelpContents(status: boolean, ids: string[]) {
313
    if (!Session.isLoggedIn()) {
314
      this.router.navigate(['/user-info'], {
315
        queryParams: {
316
          "errorCode": LoginErrorCodes.NOT_VALID,
317
          "redirectUrl": this.router.url
318
        }
319
      });
320
    } else {
321
      this.updateErrorMessage = "";
322

    
323
      this._helpService.togglePageHelpContents(ids, status, this.properties.adminToolsAPIURL, this.selectedCommunityPid).subscribe(
324
        () => {
325
          for (let id of ids) {
326
            let i = this.checkboxes.findIndex(_ => _.pageHelpContent._id == id);
327
            this.checkboxes[i].pageHelpContent.isActive = status;
328
          }
329
          this.countPageHelpContents();
330
          this.applyCheck(false);
331
        },
332
        error => this.handleUpdateError('System error changing the status of the selected page content(s)', error)
333
      );
334
    }
335
  }
336

    
337
  public pageHelpContentSavedSuccessfully(pageHelpContent: PageHelpContent) {
338
    this.cutContent(pageHelpContent);
339
    this.checkboxes.push(<CheckPageHelpContent>{pageHelpContent: pageHelpContent, checked: false});
340
    this.pageHelpContents.push(pageHelpContent);
341
    this.applyCheck(false);
342
    this.countPageHelpContents();
343
  }
344

    
345
  public pageHelpContentUpdatedSuccessfully(pageHelpContent: PageHelpContent) {
346
    this.checkboxes.find(checkItem => checkItem.pageHelpContent._id == pageHelpContent._id).pageHelpContent = pageHelpContent;
347
    let index = this.pageHelpContents.findIndex(checkItem => checkItem._id == pageHelpContent._id);
348
    this.pageHelpContents[index] = pageHelpContent;
349
    this.applyCheck(false);
350
    this.countPageHelpContents();
351
  }
352

    
353

    
354
  public filterPageHelpContent(pageHelpContent: PageHelpContent, filters: PageHelpContentFilterOptions): boolean {
355
    let idFlag = filters.id == '' || (<Page>pageHelpContent.page)._id == filters.id;
356
    let activeFlag = filters.active == null || pageHelpContent.isActive == filters.active;
357
    let textFlag = filters.text.toString() == '' || (pageHelpContent.content).match(filters.text) != null
358
      || ((<Page>pageHelpContent.page).name).match(filters.text) != null;
359
    return idFlag && activeFlag && textFlag;
360
  }
361

    
362
  public cutContent(pageHelpContent: PageHelpContent) {
363
    pageHelpContent.content = pageHelpContent.content.replace(/<[^>]*>/g, '');
364
    pageHelpContent.content = pageHelpContent.content.replace(/(\r\n|\n|\r| +(?= ))|\s\s+/gm, " ");
365

    
366
    if (pageHelpContent.content.length > 200) {
367
      pageHelpContent.content = pageHelpContent.content.substr(0, 200) + "...";
368
    }
369
  }
370

    
371
  public applyFilter() {
372
    this.checkboxes = [];
373
    this.pageHelpContents.filter(item => this.filterPageHelpContent(item, this.filters)).forEach(
374
      _ => {
375
        this.cutContent(_);
376
        this.checkboxes.push(<CheckPageHelpContent>{pageHelpContent: _, checked: false})
377
      }
378
    );
379
    this.countPageHelpContents();
380
  }
381

    
382
  public filterByPage(event: any) {
383
    if(event.target && event.target.value) {
384
      this.filters.id = event.target.value;
385
      this.applyFilter();
386
    }
387
  }
388

    
389
  public displayAllPageHelpContents() {
390
    this.filters.active = null;
391
    this.applyFilter();
392
  }
393

    
394
  public displayActivePageHelpContents() {
395
    this.filters.active = true;
396
    this.applyFilter();
397
  }
398

    
399
  public filterBySearch(text: string) {
400
    this.filters.text = new RegExp(text, "i");
401
    this.applyFilter();
402
  }
403

    
404
  public displayInactivePageHelpContents() {
405
    this.filters.active = false;
406
    this.applyFilter();
407
  }
408

    
409
  handleError(message: string, error) {
410
    this.errorMessage = message;
411
    console.log('Server responded: ' + error);
412

    
413
    this.showLoading = false;
414
  }
415

    
416
  handleUpdateError(message: string, error) {
417
    this.updateErrorMessage = message;
418
    console.log('Server responded: ' + error);
419

    
420
    this.showLoading = false;
421
  }
422

    
423
  public newPageContent() {
424
    if (this.selectedPageId) {
425
      this.router.navigate(['new'], {
426
        queryParams: {
427
          communityId: this.selectedCommunityPid,
428
          pageId: this.selectedPageId
429
        }, relativeTo: this.route
430
      });
431
    } else {
432
      this.router.navigate(['new'], {queryParams: {communityId: this.selectedCommunityPid}, relativeTo: this.route});
433
    }
434
  }
435
}
(14-14/15)