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