Project

General

Profile

1 57928 argiro.kok
import {Component, ElementRef, OnInit, ViewChild} from '@angular/core';
2
import {ActivatedRoute, Router} from '@angular/router';
3
import {HelpContentService} from '../../services/help-content.service';
4
import {FormArray, FormBuilder, FormControl, FormGroup, Validators} from '@angular/forms';
5
import {CheckPage, Page} from '../../utils/entities/adminTool/page';
6 59465 konstantin
import {Portal} from '../../utils/entities/adminTool/portal';
7 60202 argiro.kok
import {CheckEntity, Entity} from '../../utils/entities/adminTool/entity';
8 57928 argiro.kok
import {EnvProperties} from '../../utils/properties/env-properties';
9
import {Session} from '../../login/utils/helper.class';
10
import {LoginErrorCodes} from '../../login/utils/guardHelper.class';
11
import {HelperFunctions} from '../../utils/HelperFunctions.class';
12
import {UserManagementService} from '../../services/user-management.service';
13
import {Observable, Subscriber} from "rxjs";
14
import {map, startWith} from "rxjs/operators";
15
import {MatAutocompleteSelectedEvent} from "@angular/material";
16 59726 argiro.kok
import {PortalUtils} from "../portal/portalHelper";
17 60170 argiro.kok
import {properties} from "../../../../environments/environment";
18 60294 k.triantaf
import {ConnectHelper} from "../../connect/connectHelper";
19 60310 k.triantaf
import {Option} from "../../sharedComponents/input/input.component";
20
import {AlertModal} from "../../utils/modal/alert";
21 57928 argiro.kok
22
@Component({
23
  selector: 'pages',
24
  templateUrl: './pages.component.html',
25
})
26
27
export class PagesComponent implements OnInit {
28 60310 k.triantaf
29
  @ViewChild('editModal') editModal: AlertModal;
30
  @ViewChild('deleteModal') deleteModal: AlertModal;
31 57928 argiro.kok
  private selectedPages: string[] = [];
32 60310 k.triantaf
33 57928 argiro.kok
  public checkboxes: CheckPage[] = [];
34 60310 k.triantaf
35 57928 argiro.kok
  public pages: Page[] = [];
36
  public pageWithDivIds: string[] = [];
37 60310 k.triantaf
38 57928 argiro.kok
  //public errorMessage: string;
39 60310 k.triantaf
40
  public pageForm: FormGroup;
41
42 57928 argiro.kok
  private searchText: RegExp = new RegExp('');
43
  public keyword: string = '';
44 60310 k.triantaf
45 60297 k.triantaf
  public portal: string;
46 60310 k.triantaf
47 57928 argiro.kok
  public pagesType: string;
48 60310 k.triantaf
  public properties: EnvProperties = properties;
49
50 57928 argiro.kok
  public showLoading: boolean = true;
51
  public errorMessage: string = '';
52
  public updateErrorMessage: string = '';
53
  public modalErrorMessage: string = '';
54
  public isPortalAdministrator = null;
55 60202 argiro.kok
  public filterForm: FormGroup;
56 60310 k.triantaf
  public typeOptions = [{label: 'Search', value: 'search'}, {
57 57928 argiro.kok
    label: 'Share',
58
    value: 'share'
59
  }, {label: 'Landing', value: 'landing'}, {label: 'HTML', value: 'html'}, {
60
    label: 'Link',
61
    value: 'link'
62 60202 argiro.kok
  }, {label: 'Other', value: 'other'}];
63 57928 argiro.kok
  public entitiesCtrl: FormArray;
64
  @ViewChild('PageInput') pageInput: ElementRef<HTMLInputElement>;
65 60310 k.triantaf
  allEntities: Option[] = [];
66 57928 argiro.kok
  private subscriptions: any[] = [];
67 60310 k.triantaf
  public portalUtils: PortalUtils = new PortalUtils();
68
  private index: number;
69
70 57928 argiro.kok
  constructor(private element: ElementRef, private route: ActivatedRoute,
71
              private _router: Router, private _helpContentService: HelpContentService,
72
              private userManagementService: UserManagementService, private _fb: FormBuilder) {
73
  }
74 60310 k.triantaf
75 57928 argiro.kok
  ngOnInit() {
76 60310 k.triantaf
    this.filterForm = this._fb.group({
77 60202 argiro.kok
      keyword: [''],
78 60310 k.triantaf
      type: ['all', Validators.required]
79
    });
80 60202 argiro.kok
    this.subscriptions.push(this.filterForm.get('keyword').valueChanges.subscribe(value => {
81 57928 argiro.kok
      this.filterBySearch(value);
82
    }));
83 60202 argiro.kok
    this.subscriptions.push(this.filterForm.get('type').valueChanges.subscribe(value => {
84
      this.applyTypeFilter();
85
    }));
86 60170 argiro.kok
    this.subscriptions.push(this.route.queryParams.subscribe(params => {
87
      this.pagesType = '';
88
      if (params['type']) {
89 60202 argiro.kok
        // this.pagesType = params['type'];
90
        this.filterForm.get('type').setValue(params['type']);
91 60170 argiro.kok
      }
92 60310 k.triantaf
      this.portal = (this.route.snapshot.data.portal) ? this.route.snapshot.data.portal : this.route.snapshot.params[this.route.snapshot.data.param];
93
      if (this.portal === 'connect' || this.portal === 'explore') {
94 60297 k.triantaf
        ConnectHelper.setPortalTypeFromPid(this.portal);
95
      }
96 60170 argiro.kok
      this.keyword = '';
97
      this.subscriptions.push(this.userManagementService.getUserInfo().subscribe(user => {
98 60297 k.triantaf
        this.applyPortalFilter(this.portal);
99
        this.isPortalAdministrator = Session.isPortalAdministrator(user) && !this.portal;
100 60170 argiro.kok
      }));
101
    }));
102 60310 k.triantaf
103 60170 argiro.kok
    this.subscriptions.push(this._helpContentService.getEntities(this.properties.adminToolsAPIURL).subscribe(
104
      entities => {
105 60310 k.triantaf
        this.allEntities = [];
106
        entities.forEach(entity => {
107
         this.allEntities.push({
108
           label: entity.name,
109
           value: entity
110
         });
111
        });
112 60170 argiro.kok
        this.showLoading = false;
113
      },
114 60202 argiro.kok
      error => this.handleError('System error retrieving pages', error)));
115 60310 k.triantaf
116
117 57928 argiro.kok
  }
118 60310 k.triantaf
119 57928 argiro.kok
  ngOnDestroy(): void {
120
    this.subscriptions.forEach(value => {
121
      if (value instanceof Subscriber) {
122
        value.unsubscribe();
123
      } else if (value instanceof Function) {
124
        value();
125
      }
126
    });
127
  }
128 60310 k.triantaf
129 60297 k.triantaf
  getPages(portal: string) {
130 57928 argiro.kok
    if (!Session.isLoggedIn()) {
131
      this._router.navigate(['/user-info'], {
132
        queryParams: {
133
          'errorCode': LoginErrorCodes.NOT_VALID,
134
          'redirectUrl': this._router.url
135
        }
136
      });
137
    } else {
138
      this.showLoading = true;
139
      this.updateErrorMessage = '';
140
      this.errorMessage = '';
141 60310 k.triantaf
142 57928 argiro.kok
      this.pageWithDivIds = [];
143 60310 k.triantaf
144 57928 argiro.kok
      let parameters = '';
145
      if (this.pagesType) {
146
        parameters = '?page_type=' + this.pagesType;
147
      }
148 60297 k.triantaf
      if (portal) {
149
        this.subscriptions.push(this._helpContentService.getCommunityPagesByType(portal, parameters, this.properties.adminToolsAPIURL).subscribe(
150 57928 argiro.kok
          pages => {
151
            this.pagesReturned(pages);
152
            //if(!this.pagesType || this.pagesType == "link") {
153 60297 k.triantaf
            this.getPagesWithDivIds(portal);
154 57928 argiro.kok
            //} else {
155
            //this.showLoading = false;
156
            //}
157
          },
158
          error => this.handleError('System error retrieving pages', error)
159 60170 argiro.kok
        ));
160 57928 argiro.kok
      } else {
161 60170 argiro.kok
        this.subscriptions.push(this._helpContentService.getAllPagesFull(this.properties.adminToolsAPIURL).subscribe(
162 57928 argiro.kok
          pages => {
163
            this.pagesReturned(pages);
164
            this.showLoading = false;
165
          },
166
          error => this.handleError('System error retrieving pages', error)
167 60170 argiro.kok
        ));
168 57928 argiro.kok
      }
169
    }
170
  }
171 60310 k.triantaf
172 60297 k.triantaf
  getPagesWithDivIds(portal: string) {
173 57928 argiro.kok
    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 60297 k.triantaf
      this.subscriptions.push(this._helpContentService.getPageIdsFromDivIds(portal, this.properties.adminToolsAPIURL).subscribe(
182 57928 argiro.kok
        pages => {
183
          this.pageWithDivIds = pages;
184
          this.showLoading = false;
185
        },
186 60170 argiro.kok
        error => this.handleError('System error retrieving information about pages\' classes', error)));
187 57928 argiro.kok
    }
188
  }
189 60310 k.triantaf
190 57928 argiro.kok
  pagesReturned(pages: Page[]) {
191
    this.pages = pages;
192
    this.checkboxes = [];
193 60310 k.triantaf
194 57928 argiro.kok
    if (pages) {
195
      pages.forEach(_ => {
196
        this.checkboxes.push(<CheckPage>{page: _, checked: false});
197
      });
198
    }
199
  }
200 60310 k.triantaf
201 57928 argiro.kok
  /*
202 60297 k.triantaf
      getPortals() {
203 57928 argiro.kok
          this._helpContentService.getCommunities(this.properties.adminToolsAPIURL).subscribe(
204
              communities => {
205
                  this.communities = communities;
206 60297 k.triantaf
                  this.portal = this.communities[0].pid;
207
                  this.getPages(this.portal);
208
                  this.getPagesWithDivIds(this.portal);
209 57928 argiro.kok
          },
210
          error => this.handleError('System error retrieving communities', error));
211
      }
212
  */
213 60310 k.triantaf
214 57928 argiro.kok
  public toggleCheckBoxes(event) {
215
    this.checkboxes.forEach(_ => _.checked = event.target.checked);
216
  }
217 60310 k.triantaf
218 57928 argiro.kok
  public applyCheck(flag: boolean) {
219
    this.checkboxes.forEach(_ => _.checked = flag);
220
  }
221 60310 k.triantaf
222 57928 argiro.kok
  public getSelectedPages(): string[] {
223
    return this.checkboxes.filter(page => page.checked == true).map(checkedPage => checkedPage.page).map(res => res._id);
224
  }
225 60310 k.triantaf
226 57928 argiro.kok
  private deletePagesFromArray(ids: string[]): void {
227
    for (let id of ids) {
228 60310 k.triantaf
      let i = this.pages.findIndex(_ => _._id == id);
229
      this.pages.splice(i, 1);
230 57928 argiro.kok
    }
231 60310 k.triantaf
    this.applyTypeFilter();
232
    this.applyFilter();
233 57928 argiro.kok
  }
234 60310 k.triantaf
235 57928 argiro.kok
  public confirmDeletePage(id: string) {
236
    //this.deleteConfirmationModal.ids = [id];
237
    //this.deleteConfirmationModal.showModal();
238
    this.selectedPages = [id];
239
    this.confirmModalOpen();
240
  }
241 60310 k.triantaf
242 57928 argiro.kok
  public confirmDeleteSelectedPages() {
243
    //this.deleteConfirmationModal.ids = this.getSelectedPages();
244
    //this.deleteConfirmationModal.showModal();
245
    this.selectedPages = this.getSelectedPages();
246
    this.confirmModalOpen();
247
  }
248 60310 k.triantaf
249 57928 argiro.kok
  private confirmModalOpen() {
250
    if (!Session.isLoggedIn()) {
251
      this._router.navigate(['/user-info'], {
252
        queryParams: {
253
          'errorCode': LoginErrorCodes.NOT_VALID,
254
          'redirectUrl': this._router.url
255
        }
256
      });
257
    } else {
258 60310 k.triantaf
      this.deleteModal.cancelButton = true;
259
      this.deleteModal.okButton = true;
260
      this.deleteModal.alertTitle = 'Delete Confirmation';
261
      this.deleteModal.message = 'Are you sure you want to delete the selected page(s)?';
262
      this.deleteModal.okButtonText = 'Yes';
263
      this.deleteModal.open();
264 57928 argiro.kok
    }
265
  }
266 60310 k.triantaf
267 57928 argiro.kok
  public confirmedDeletePages(data: any) {
268
    if (!Session.isLoggedIn()) {
269
      this._router.navigate(['/user-info'], {
270
        queryParams: {
271
          'errorCode': LoginErrorCodes.NOT_VALID,
272
          'redirectUrl': this._router.url
273
        }
274
      });
275
    } else {
276
      this.showLoading = true;
277
      this.updateErrorMessage = '';
278 60310 k.triantaf
279 60170 argiro.kok
      this.subscriptions.push(this._helpContentService.deletePages(this.selectedPages, this.properties.adminToolsAPIURL).subscribe(
280 57928 argiro.kok
        _ => {
281
          this.deletePagesFromArray(this.selectedPages);
282
          this.showLoading = false;
283
        },
284
        error => this.handleUpdateError('System error deleting the selected pages', error)
285 60170 argiro.kok
      ));
286 57928 argiro.kok
    }
287
  }
288 60310 k.triantaf
289 57928 argiro.kok
  public editPage(i: number) {
290
    this.entitiesCtrl = this._fb.array([]);
291
    let page: Page = this.checkboxes[i].page;
292 60310 k.triantaf
    this.index = this.pages.findIndex(value => value._id === page._id);
293
    this.pageForm = this._fb.group({
294
      _id: this._fb.control(page._id),
295
      route: this._fb.control(page.route, Validators.required),
296
      name: this._fb.control(page.name, Validators.required),
297
      isEnabled: this._fb.control(page.isEnabled),
298
      portalType: this._fb.control(page.portalType, Validators.required),
299
      top: this._fb.control(page.top),
300
      bottom: this._fb.control(page.bottom),
301
      left: this._fb.control(page.left),
302
      right: this._fb.control(page.right),
303
      type: this._fb.control(page.type, Validators.required),
304
      entities: this.entitiesCtrl
305 57928 argiro.kok
    });
306 60310 k.triantaf
    this.pageForm.get('portalType').disable();
307 57928 argiro.kok
    for (let i = 0; i < page.entities.length; i++) {
308
      this.entitiesCtrl.push(this._fb.control(page.entities[i]));
309
    }
310
    this.modalErrorMessage = '';
311 60310 k.triantaf
    this.pagesModalOpen('Edit Page', 'Save changes');
312 57928 argiro.kok
  }
313 60310 k.triantaf
314
  public newPage() {
315
    if(this.pageForm) {
316
      this.pageForm.get('portalType').enable();
317 57928 argiro.kok
    }
318
    this.entitiesCtrl = this._fb.array([]);
319 60310 k.triantaf
    this.pageForm = this._fb.group({
320
      _id: this._fb.control(null),
321
      route: this._fb.control('', Validators.required),
322
      name: this._fb.control('', Validators.required),
323
      isEnabled: this._fb.control(true),
324
      portalType: this._fb.control('', Validators.required),
325
      top: this._fb.control(true),
326
      bottom: this._fb.control(true),
327
      left: this._fb.control(true),
328
      right: this._fb.control(true),
329
      type: this._fb.control(this.typeOptions[0].value, Validators.required),
330
      entities: this.entitiesCtrl,
331 57928 argiro.kok
    });
332
    this.modalErrorMessage = '';
333 60310 k.triantaf
    this.pagesModalOpen('Create Page', 'Create');
334 57928 argiro.kok
  }
335 60310 k.triantaf
336
  private pagesModalOpen(title: string, yesBtn: string) {
337 57928 argiro.kok
    if (!Session.isLoggedIn()) {
338
      this._router.navigate(['/user-info'], {
339
        queryParams: {
340
          'errorCode': LoginErrorCodes.NOT_VALID,
341
          'redirectUrl': this._router.url
342
        }
343
      });
344
    } else {
345 60310 k.triantaf
      this.editModal.cancelButton = true;
346
      this.editModal.okButton = true;
347
      this.editModal.okButtonLeft = false;
348
      this.editModal.alertTitle = title;
349
      this.editModal.okButtonText = yesBtn;
350
      this.editModal.open();
351 57928 argiro.kok
    }
352
  }
353 60310 k.triantaf
354 57928 argiro.kok
  public pageSaveConfirmed(data: any) {
355
    if (!Session.isLoggedIn()) {
356
      this._router.navigate(['/user-info'], {
357
        queryParams: {
358
          'errorCode': LoginErrorCodes.NOT_VALID,
359
          'redirectUrl': this._router.url
360
        }
361
      });
362
    } else {
363 60310 k.triantaf
      if (!this.pageForm.value._id) {
364 57928 argiro.kok
        this.modalErrorMessage = '';
365 60310 k.triantaf
        this.subscriptions.push(this._helpContentService.savePage(<Page>this.pageForm.value, this.properties.adminToolsAPIURL).subscribe(
366 57928 argiro.kok
          page => {
367
            this.pageSavedSuccessfully(page, true);
368
          },
369
          error => this.handleUpdateError('System error creating page', error)
370 60170 argiro.kok
        ));
371 57928 argiro.kok
      } else {
372 60310 k.triantaf
        this.pageForm.get('portalType').enable();
373
        this.subscriptions.push(this._helpContentService.updatePage(<Page>this.pageForm.value, this.properties.adminToolsAPIURL).subscribe(
374 57928 argiro.kok
          page => {
375
            this.pageSavedSuccessfully(page, false);
376
          },
377
          error => this.handleUpdateError('System error updating page', error)
378 60170 argiro.kok
        ));
379 57928 argiro.kok
      }
380 60310 k.triantaf
381 57928 argiro.kok
    }
382
  }
383 60310 k.triantaf
384 57928 argiro.kok
  public pageSavedSuccessfully(page: Page, isNew: boolean) {
385
    if (isNew) {
386 60310 k.triantaf
      this.pages.push(page);
387 57928 argiro.kok
    } else {
388 60310 k.triantaf
      this.pages[this.index] = page;
389 57928 argiro.kok
    }
390 60310 k.triantaf
    this.applyTypeFilter();
391
    this.applyFilter();
392 57928 argiro.kok
    this.applyCheck(false);
393
  }
394 60310 k.triantaf
395
396 57928 argiro.kok
  public filterBySearch(text: string) {
397
    this.searchText = new RegExp(text, 'i');
398
    this.applyFilter();
399
  }
400 60310 k.triantaf
401 57928 argiro.kok
  public applyFilter() {
402
    this.checkboxes = [];
403
    this.pages.filter(item => this.filterPages(item)).forEach(
404
      _ => this.checkboxes.push(<CheckPage>{page: _, checked: false})
405
    );
406
  }
407 60310 k.triantaf
408 60202 argiro.kok
  public applyTypeFilter() {
409
    this.checkboxes = [];
410
    this.pages.filter(item => this.filterByType(item)).forEach(
411
      _ => this.checkboxes.push(<CheckPage>{page: _, checked: false})
412
    );
413
  }
414 60310 k.triantaf
415 60202 argiro.kok
  public filterByType(page: Page): boolean {
416
    let type = this.filterForm.get("type").value;
417 60310 k.triantaf
    return type == "all" || (type == page.type);
418 60202 argiro.kok
  }
419 60310 k.triantaf
420 57928 argiro.kok
  public filterPages(page: Page): boolean {
421 59465 konstantin
    let textFlag = this.searchText.toString() == '' || (page.route + ' ' + page.name + ' ' + page.portalType).match(this.searchText) != null;
422 57928 argiro.kok
    return textFlag;
423
  }
424 60310 k.triantaf
425 57928 argiro.kok
  handleError(message: string, error) {
426
    // if(error == null) {
427
    //     this.formComponent.reset();
428
    // } else {
429
    this.errorMessage = message;// + ' (Server responded: ' + error + ')';
430
    console.log('Server responded: ' + error);
431
    //}
432 60310 k.triantaf
433 57928 argiro.kok
    this.showLoading = false;
434
  }
435 60310 k.triantaf
436 57928 argiro.kok
  handleUpdateError(message: string, error) {
437
    if (error == null) {
438
      // this.formComponent.reset();
439 60310 k.triantaf
      this.pageForm = this._fb.group({
440
        route: this._fb.control('', Validators.required),
441
        name: this._fb.control('', Validators.required),
442
        isEnabled: this._fb.control(true),
443
        portalType: this._fb.control('', Validators.required),
444
        top: this._fb.control(true),
445
        bottom: this._fb.control(true),
446
        left: this._fb.control(true),
447
        right: this._fb.control(true),
448
        type: this._fb.control(this.typeOptions[0].value, Validators.required),
449
        entities: this.entitiesCtrl,
450
        _id: this._fb.control(''),
451 57928 argiro.kok
      });
452
    } else {
453
      this.updateErrorMessage = message;// + ' (Server responded: ' + error + ')';
454
      console.log('Server responded: ' + error);
455
    }
456 60310 k.triantaf
457 57928 argiro.kok
    this.showLoading = false;
458
  }
459 60310 k.triantaf
460 60297 k.triantaf
  // public filterByPortal(event: any) {
461
  //     this.portal = event.target.value;
462
  //     this.applyPortalFilter(this.portal);
463 57928 argiro.kok
  // }
464 60310 k.triantaf
465 60297 k.triantaf
  public applyPortalFilter(portal: string) {
466
    this.getPages(portal);
467 57928 argiro.kok
  }
468 60310 k.triantaf
469 57928 argiro.kok
  public togglePages(status: boolean, ids: string[]) {
470
    if (!Session.isLoggedIn()) {
471
      this._router.navigate(['/user-info'], {
472
        queryParams: {
473
          'errorCode': LoginErrorCodes.NOT_VALID,
474
          'redirectUrl': this._router.url
475
        }
476
      });
477
    } else {
478
      this.updateErrorMessage = '';
479 60310 k.triantaf
480 60297 k.triantaf
      this.subscriptions.push(this._helpContentService.togglePages(this.portal, ids, status, this.properties.adminToolsAPIURL).subscribe(
481 57928 argiro.kok
        () => {
482
          for (let id of ids) {
483
            let i = this.checkboxes.findIndex(_ => _.page._id == id);
484
            this.checkboxes[i].page.isEnabled = status;
485
          }
486
          this.applyCheck(false);
487
        },
488
        error => this.handleUpdateError('System error changing the status of the selected page(s)', error)
489 60170 argiro.kok
      ));
490 57928 argiro.kok
    }
491
  }
492 60310 k.triantaf
493 57928 argiro.kok
  public capitalizeFirstLetter(str: string) {
494
    return str.charAt(0).toUpperCase() + str.slice(1);
495
  }
496
}