Project

General

Profile

1
import {Component, ViewChild, OnInit, ElementRef} from '@angular/core';
2
import {Router, ActivatedRoute} from "@angular/router";
3
import {FormBuilder, FormControl, FormGroup} from "@angular/forms";
4
import {HelpContentService} from "../../services/help-content.service";
5
import {
6
  DivHelpContent,
7
  CheckDivHelpContent,
8
  DivHelpContentFilterOptions
9
} from "../../utils/entities/adminTool/div-help-content";
10
import {Page} from "../../utils/entities/adminTool/page";
11
import {Portal} from "../../utils/entities/adminTool/portal";
12
import {DivId} from "../../utils/entities/adminTool/divId";
13
import {EnvProperties} from '../../utils/properties/env-properties';
14

    
15
import {Session} from '../../login/utils/helper.class';
16
import {LoginErrorCodes} from '../../login/utils/guardHelper.class';
17
import {HelperFunctions} from "../../utils/HelperFunctions.class";
18
import {Subscriber} from "rxjs";
19
import {properties} from "../../../../environments/environment";
20

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

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

    
32
  public checkboxes: CheckDivHelpContent[] = [];
33

    
34
  public divHelpContents: DivHelpContent[] = [];
35

    
36
  //public errorMessage: string;
37

    
38
  public formGroup: FormGroup;
39

    
40
  public pages: Page[];
41

    
42
  public checkboxAll: boolean = false;
43

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

    
47
  public counter = {all: 0, active: 0, inactive: 0};
48

    
49
  public communities: Portal[] = [];
50

    
51
  public selectedCommunityPid: string;
52

    
53
  public selectedPageId: string;
54

    
55
  public community: Portal;
56

    
57
  public page: Page;
58
  public properties: EnvProperties = null;
59

    
60
  public showLoading: boolean = true;
61
  public errorMessage: string = '';
62
  public updateErrorMessage: string = '';
63
  public filterForm: FormControl;
64
  public selectForm: FormControl;
65
  public selectOptions = [];
66
  private subscriptions: any[] = [];
67

    
68
  ngOnInit() {
69
    this.filterForm = this._fb.control('');
70
    this.selectForm = this._fb.control('');
71
    this.subscriptions.push(this.filterForm.valueChanges.subscribe(value => {
72
      this.filterBySearch(value);
73
    }));
74
    this.subscriptions.push(this.selectForm.valueChanges.subscribe(value => {
75
      this.filterByPage(value);
76
    }));
77

    
78
    this.properties = properties;
79

    
80
    this.subscriptions.push(this.route.queryParams.subscribe(params => {
81
      HelperFunctions.scroll();
82

    
83
      this.selectedCommunityPid = params['communityId'];
84
      this.selectedPageId = params['pageId'];
85

    
86
      if (this.selectedCommunityPid && this.selectedPageId) {
87
        this.getPage(this.selectedPageId);
88
      } else if (this.selectedCommunityPid) {
89
        this.selectedPageId = "";
90
        this.getPages(this.selectedCommunityPid);
91
      }
92
    }));
93

    
94
  }
95

    
96
  constructor(private element: ElementRef, private route: ActivatedRoute, private _helpService: HelpContentService, private router: Router, private _fb: FormBuilder) {
97
  }
98

    
99
  ngOnDestroy(): void {
100
    this.subscriptions.forEach(value => {
101
      if (value instanceof Subscriber) {
102
        value.unsubscribe();
103
      } else if (value instanceof Function) {
104
        value();
105
      }
106
    });
107
  }
108

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

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

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

    
148
      this._helpService.getCommunityPagesWithDivId(community_pid, this.properties.adminToolsAPIURL).subscribe(
149
        //this._helpService.getPagesWithDivIds(community_pid, this.properties.adminToolsAPIURL).subscribe(
150
        pages => {
151
          this.pages = pages;
152
          this.getDivHelpContents(this.selectedCommunityPid);
153
          this.selectOptions = [{label:'All pages', value: ''}];
154
          for (let page of this.pages) {
155
            this.selectOptions.push({label:page.name, value: page._id})
156
          }
157
        },
158
        error => this.handleError('System error retrieving pages', error));
159
    }
160
  }
161

    
162
  public countDivHelpContents() {
163
    this.counter = {all: 0, active: 0, inactive: 0};
164
    let filter = Object.assign({}, this.filters);
165
    filter.active = null;
166
    this.divHelpContents.forEach(_ => {
167
      if (this.filterDivHelpContent(_, filter)) {
168
        if (_.isActive == true) this.counter.active++;
169
        else this.counter.inactive++
170
      }
171
    });
172
    this.counter.all = this.counter.active + this.counter.inactive;
173
  }
174

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

    
190
          for (let i = this.divHelpContents.length - 1; i >= 0; i -= 1) {
191
            //for (let i = 0; i < this.divHelpContents.length; i++) {
192
            let divId: DivId = this.divHelpContents[i].divId as DivId;
193
            let pages: Page[] = divId.pages as Page[];
194
            const pageIds = pages.map(x => x._id);
195

    
196
            if (!this.selectedPageId || pageIds.includes(this.selectedPageId)) {
197
              this.cutContent(this.divHelpContents[i]);
198
              this.checkboxes.unshift(<CheckDivHelpContent>{divHelpContent: this.divHelpContents[i], checked: false});
199
            } else {
200
              this.divHelpContents.splice(i, 1);
201
            }
202
          }
203

    
204
          this.countDivHelpContents();
205

    
206
          this.showLoading = false;
207
        },
208
        error => this.handleError('System error retrieving page contents', error));
209
    }
210
  }
211

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

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

    
222
  public getSelectedDivHelpContents(): string[] {
223
    return this.checkboxes.filter(divHelpContent => divHelpContent.checked == true)
224
      .map(checkedDivHelpContent => checkedDivHelpContent.divHelpContent).map(res => res._id);
225
  }
226

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

    
234
  public confirmDeleteSelectedDivHelpContents() {
235
    //this.deleteConfirmationModal.ids = this.getSelectedDivHelpContents();
236
    //this.deleteConfirmationModal.showModal();
237
    this.selectedDivContents = this.getSelectedDivHelpContents();
238
    this.confirmModalOpen();
239
  }
240

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

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

    
271
      this._helpService.deleteDivHelpContents(this.selectedDivContents, this.properties.adminToolsAPIURL, this.selectedCommunityPid).subscribe(
272
        _ => {
273
          this.deleteDivHelpContentsFromArray(this.selectedDivContents);
274
          this.showLoading = false;
275
        },
276
        error => this.handleUpdateError('System error deleting the selected class content(s)', error)
277
      );
278
    }
279
  }
280

    
281
  private deleteDivHelpContentsFromArray(ids: string[]): void {
282
    for (let id of ids) {
283
      let iqc = this.checkboxes.findIndex(_ => _.divHelpContent._id == id);
284
      let iq = this.divHelpContents.findIndex(_ => _._id == id);
285
      this.checkboxes.splice(iqc, 1);
286
      this.divHelpContents.splice(iqc, 1);
287
    }
288
    this.countDivHelpContents();
289
  }
290

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

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

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

    
336
  public divHelpContentSavedSuccessfully(divHelpContent: DivHelpContent) {
337
    this.cutContent(divHelpContent);
338
    this.checkboxes.push(<CheckDivHelpContent>{divHelpContent: divHelpContent, checked: false});
339
    this.divHelpContents.push(divHelpContent);
340
    this.applyCheck(false);
341
    this.countDivHelpContents();
342
  }
343

    
344
  public divHelpContentUpdatedSuccessfully(divHelpContent: DivHelpContent) {
345
    this.checkboxes.find(checkItem => checkItem.divHelpContent._id == divHelpContent._id).divHelpContent = divHelpContent;
346
    let index = this.divHelpContents.findIndex(checkItem => checkItem._id == divHelpContent._id);
347
    this.divHelpContents[index] = divHelpContent;
348
    this.applyCheck(false);
349
    this.countDivHelpContents();
350
  }
351

    
352

    
353
  public filterDivHelpContent(divHelpContent: DivHelpContent, filters: DivHelpContentFilterOptions): boolean {
354
    let divId: DivId = divHelpContent.divId as DivId;
355
    let pages: Page[] = <Page[]>divId.pages;
356
    let pageIds: string[] = pages.map(x => x._id);
357

    
358
    let idFlag = filters.id == '' || /*(<Page[]>divId.pages)._id == filters.id*/ pageIds.includes(filters.id);
359
    let activeFlag = filters.active == null || divHelpContent.isActive == filters.active;
360
    let textFlag = filters.text.toString() == '' || (divHelpContent.content).match(filters.text) != null
361
      || ((<DivId>divHelpContent.divId).name).match(filters.text) != null;
362
    return idFlag && activeFlag && textFlag;
363
  }
364

    
365
  public cutContent(divHelpContent: DivHelpContent) {
366
    divHelpContent.content = divHelpContent.content.replace(/<[^>]*>/g, '');
367
    divHelpContent.content = divHelpContent.content.replace(/(\r\n|\n|\r| +(?= ))|\s\s+/gm, " ");
368
    if (divHelpContent.content.length > 200) {
369
      divHelpContent.content = divHelpContent.content.substr(0, 200) + "...";
370
    }
371
  }
372

    
373
  public applyFilter() {
374
    this.checkboxes = [];
375
    this.divHelpContents.filter(item => this.filterDivHelpContent(item, this.filters)).forEach(
376
      _ => {
377
        this.cutContent(_);
378
        this.checkboxes.push(<CheckDivHelpContent>{divHelpContent: _, checked: false})
379
      }
380
    );
381
    this.countDivHelpContents();
382
  }
383

    
384
  public filterByPage(value: any) {
385
    this.filters.id = value;
386
    this.applyFilter();
387
  }
388

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

    
394
  public displayActiveDivHelpContents() {
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 displayInactiveDivHelpContents() {
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 newClassContent() {
424
    console.log("AAA")
425
    if (this.selectedPageId) {
426
      this.router.navigate(['../../classContents/new'], {
427
        queryParams: {
428
          communityId: this.selectedCommunityPid,
429
          pageId: this.selectedPageId
430
        }, relativeTo: this.route
431
      });
432
    } else {
433
      this.router.navigate(['../../classContents/new'], {queryParams: {communityId: this.selectedCommunityPid}, relativeTo: this.route});
434
    }
435
  }
436
}
(6-6/15)