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
|
import {Option} from "../../sharedComponents/input/input.component";
|
20
|
import {AlertModal} from "../../utils/modal/alert";
|
21
|
|
22
|
@Component({
|
23
|
selector: 'pages',
|
24
|
templateUrl: './pages.component.html',
|
25
|
})
|
26
|
|
27
|
export class PagesComponent implements OnInit {
|
28
|
|
29
|
@ViewChild('editModal') editModal: AlertModal;
|
30
|
@ViewChild('deleteModal') deleteModal: AlertModal;
|
31
|
private selectedPages: string[] = [];
|
32
|
|
33
|
public checkboxes: CheckPage[] = [];
|
34
|
|
35
|
public pages: Page[] = [];
|
36
|
public pageWithDivIds: string[] = [];
|
37
|
|
38
|
//public errorMessage: string;
|
39
|
|
40
|
public pageForm: FormGroup;
|
41
|
|
42
|
private searchText: RegExp = new RegExp('');
|
43
|
public keyword: string = '';
|
44
|
|
45
|
public portal: string;
|
46
|
|
47
|
public pagesType: string;
|
48
|
public properties: EnvProperties = properties;
|
49
|
|
50
|
public showLoading: boolean = true;
|
51
|
public errorMessage: string = '';
|
52
|
public updateErrorMessage: string = '';
|
53
|
public modalErrorMessage: string = '';
|
54
|
public isPortalAdministrator = null;
|
55
|
public filterForm: FormGroup;
|
56
|
public typeOptions = [{label: 'Search', value: 'search'}, {
|
57
|
label: 'Share',
|
58
|
value: 'share'
|
59
|
}, {label: 'Landing', value: 'landing'}, {label: 'HTML', value: 'html'}, {
|
60
|
label: 'Link',
|
61
|
value: 'link'
|
62
|
}, {label: 'Other', value: 'other'}];
|
63
|
public entitiesCtrl: FormArray;
|
64
|
@ViewChild('PageInput') pageInput: ElementRef<HTMLInputElement>;
|
65
|
allEntities: Option[] = [];
|
66
|
private subscriptions: any[] = [];
|
67
|
public portalUtils: PortalUtils = new PortalUtils();
|
68
|
private index: number;
|
69
|
|
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
|
});
|
80
|
this.subscriptions.push(this.filterForm.get('keyword').valueChanges.subscribe(value => {
|
81
|
this.filterBySearch(value);
|
82
|
}));
|
83
|
this.subscriptions.push(this.filterForm.get('type').valueChanges.subscribe(value => {
|
84
|
this.applyTypeFilter();
|
85
|
}));
|
86
|
this.subscriptions.push(this.route.queryParams.subscribe(params => {
|
87
|
this.pagesType = '';
|
88
|
if (params['type']) {
|
89
|
// this.pagesType = params['type'];
|
90
|
this.filterForm.get('type').setValue(params['type']);
|
91
|
}
|
92
|
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
|
ConnectHelper.setPortalTypeFromPid(this.portal);
|
95
|
}
|
96
|
this.keyword = '';
|
97
|
this.subscriptions.push(this.userManagementService.getUserInfo().subscribe(user => {
|
98
|
this.applyPortalFilter(this.portal);
|
99
|
this.isPortalAdministrator = Session.isPortalAdministrator(user) && !this.portal;
|
100
|
}));
|
101
|
}));
|
102
|
|
103
|
this.subscriptions.push(this._helpContentService.getEntities(this.properties.adminToolsAPIURL).subscribe(
|
104
|
entities => {
|
105
|
this.allEntities = [];
|
106
|
entities.forEach(entity => {
|
107
|
this.allEntities.push({
|
108
|
label: entity.name,
|
109
|
value: entity
|
110
|
});
|
111
|
});
|
112
|
this.showLoading = false;
|
113
|
},
|
114
|
error => this.handleError('System error retrieving pages', error)));
|
115
|
|
116
|
|
117
|
}
|
118
|
|
119
|
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
|
|
129
|
getPages(portal: string) {
|
130
|
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
|
|
142
|
this.pageWithDivIds = [];
|
143
|
|
144
|
let parameters = '';
|
145
|
if (this.pagesType) {
|
146
|
parameters = '?page_type=' + this.pagesType;
|
147
|
}
|
148
|
if (portal) {
|
149
|
this.subscriptions.push(this._helpContentService.getCommunityPagesByType(portal, parameters, this.properties.adminToolsAPIURL).subscribe(
|
150
|
pages => {
|
151
|
this.pagesReturned(pages);
|
152
|
//if(!this.pagesType || this.pagesType == "link") {
|
153
|
this.getPagesWithDivIds(portal);
|
154
|
//} else {
|
155
|
//this.showLoading = false;
|
156
|
//}
|
157
|
},
|
158
|
error => this.handleError('System error retrieving pages', error)
|
159
|
));
|
160
|
} else {
|
161
|
this.subscriptions.push(this._helpContentService.getAllPagesFull(this.properties.adminToolsAPIURL).subscribe(
|
162
|
pages => {
|
163
|
this.pagesReturned(pages);
|
164
|
this.showLoading = false;
|
165
|
},
|
166
|
error => this.handleError('System error retrieving pages', error)
|
167
|
));
|
168
|
}
|
169
|
}
|
170
|
}
|
171
|
|
172
|
getPagesWithDivIds(portal: 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.subscriptions.push(this._helpContentService.getPageIdsFromDivIds(portal, this.properties.adminToolsAPIURL).subscribe(
|
182
|
pages => {
|
183
|
this.pageWithDivIds = pages;
|
184
|
this.showLoading = false;
|
185
|
},
|
186
|
error => this.handleError('System error retrieving information about pages\' classes', error)));
|
187
|
}
|
188
|
}
|
189
|
|
190
|
pagesReturned(pages: Page[]) {
|
191
|
this.pages = pages;
|
192
|
this.checkboxes = [];
|
193
|
|
194
|
if (pages) {
|
195
|
pages.forEach(_ => {
|
196
|
this.checkboxes.push(<CheckPage>{page: _, checked: false});
|
197
|
});
|
198
|
}
|
199
|
}
|
200
|
|
201
|
/*
|
202
|
getPortals() {
|
203
|
this._helpContentService.getCommunities(this.properties.adminToolsAPIURL).subscribe(
|
204
|
communities => {
|
205
|
this.communities = communities;
|
206
|
this.portal = this.communities[0].pid;
|
207
|
this.getPages(this.portal);
|
208
|
this.getPagesWithDivIds(this.portal);
|
209
|
},
|
210
|
error => this.handleError('System error retrieving communities', error));
|
211
|
}
|
212
|
*/
|
213
|
|
214
|
public toggleCheckBoxes(event) {
|
215
|
this.checkboxes.forEach(_ => _.checked = event.target.checked);
|
216
|
}
|
217
|
|
218
|
public applyCheck(flag: boolean) {
|
219
|
this.checkboxes.forEach(_ => _.checked = flag);
|
220
|
}
|
221
|
|
222
|
public getSelectedPages(): string[] {
|
223
|
return this.checkboxes.filter(page => page.checked == true).map(checkedPage => checkedPage.page).map(res => res._id);
|
224
|
}
|
225
|
|
226
|
private deletePagesFromArray(ids: string[]): void {
|
227
|
for (let id of ids) {
|
228
|
let i = this.pages.findIndex(_ => _._id == id);
|
229
|
this.pages.splice(i, 1);
|
230
|
}
|
231
|
this.applyTypeFilter();
|
232
|
this.applyFilter();
|
233
|
}
|
234
|
|
235
|
public confirmDeletePage(id: string) {
|
236
|
//this.deleteConfirmationModal.ids = [id];
|
237
|
//this.deleteConfirmationModal.showModal();
|
238
|
this.selectedPages = [id];
|
239
|
this.confirmModalOpen();
|
240
|
}
|
241
|
|
242
|
public confirmDeleteSelectedPages() {
|
243
|
//this.deleteConfirmationModal.ids = this.getSelectedPages();
|
244
|
//this.deleteConfirmationModal.showModal();
|
245
|
this.selectedPages = this.getSelectedPages();
|
246
|
this.confirmModalOpen();
|
247
|
}
|
248
|
|
249
|
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
|
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
|
}
|
265
|
}
|
266
|
|
267
|
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
|
|
279
|
this.subscriptions.push(this._helpContentService.deletePages(this.selectedPages, this.properties.adminToolsAPIURL).subscribe(
|
280
|
_ => {
|
281
|
this.deletePagesFromArray(this.selectedPages);
|
282
|
this.showLoading = false;
|
283
|
},
|
284
|
error => this.handleUpdateError('System error deleting the selected pages', error)
|
285
|
));
|
286
|
}
|
287
|
}
|
288
|
|
289
|
public editPage(i: number) {
|
290
|
this.entitiesCtrl = this._fb.array([]);
|
291
|
let page: Page = this.checkboxes[i].page;
|
292
|
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
|
});
|
306
|
this.pageForm.get('portalType').disable();
|
307
|
for (let i = 0; i < page.entities.length; i++) {
|
308
|
this.entitiesCtrl.push(this._fb.control(page.entities[i]));
|
309
|
}
|
310
|
this.modalErrorMessage = '';
|
311
|
this.pagesModalOpen('Edit Page', 'Save changes');
|
312
|
}
|
313
|
|
314
|
public newPage() {
|
315
|
if(this.pageForm) {
|
316
|
this.pageForm.get('portalType').enable();
|
317
|
}
|
318
|
this.entitiesCtrl = this._fb.array([]);
|
319
|
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
|
});
|
332
|
this.modalErrorMessage = '';
|
333
|
this.pagesModalOpen('Create Page', 'Create');
|
334
|
}
|
335
|
|
336
|
private pagesModalOpen(title: string, yesBtn: string) {
|
337
|
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
|
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
|
}
|
352
|
}
|
353
|
|
354
|
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
|
if (!this.pageForm.value._id) {
|
364
|
this.modalErrorMessage = '';
|
365
|
this.subscriptions.push(this._helpContentService.savePage(<Page>this.pageForm.value, this.properties.adminToolsAPIURL).subscribe(
|
366
|
page => {
|
367
|
this.pageSavedSuccessfully(page, true);
|
368
|
},
|
369
|
error => this.handleUpdateError('System error creating page', error)
|
370
|
));
|
371
|
} else {
|
372
|
this.pageForm.get('portalType').enable();
|
373
|
this.subscriptions.push(this._helpContentService.updatePage(<Page>this.pageForm.value, this.properties.adminToolsAPIURL).subscribe(
|
374
|
page => {
|
375
|
this.pageSavedSuccessfully(page, false);
|
376
|
},
|
377
|
error => this.handleUpdateError('System error updating page', error)
|
378
|
));
|
379
|
}
|
380
|
|
381
|
}
|
382
|
}
|
383
|
|
384
|
public pageSavedSuccessfully(page: Page, isNew: boolean) {
|
385
|
if (isNew) {
|
386
|
this.pages.push(page);
|
387
|
} else {
|
388
|
this.pages[this.index] = page;
|
389
|
}
|
390
|
this.applyTypeFilter();
|
391
|
this.applyFilter();
|
392
|
this.applyCheck(false);
|
393
|
}
|
394
|
|
395
|
|
396
|
public filterBySearch(text: string) {
|
397
|
this.searchText = new RegExp(text, 'i');
|
398
|
this.applyFilter();
|
399
|
}
|
400
|
|
401
|
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
|
|
408
|
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
|
|
415
|
public filterByType(page: Page): boolean {
|
416
|
let type = this.filterForm.get("type").value;
|
417
|
return type == "all" || (type == page.type);
|
418
|
}
|
419
|
|
420
|
public filterPages(page: Page): boolean {
|
421
|
let textFlag = this.searchText.toString() == '' || (page.route + ' ' + page.name + ' ' + page.portalType).match(this.searchText) != null;
|
422
|
return textFlag;
|
423
|
}
|
424
|
|
425
|
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
|
|
433
|
this.showLoading = false;
|
434
|
}
|
435
|
|
436
|
handleUpdateError(message: string, error) {
|
437
|
if (error == null) {
|
438
|
// this.formComponent.reset();
|
439
|
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
|
});
|
452
|
} else {
|
453
|
this.updateErrorMessage = message;// + ' (Server responded: ' + error + ')';
|
454
|
console.log('Server responded: ' + error);
|
455
|
}
|
456
|
|
457
|
this.showLoading = false;
|
458
|
}
|
459
|
|
460
|
// public filterByPortal(event: any) {
|
461
|
// this.portal = event.target.value;
|
462
|
// this.applyPortalFilter(this.portal);
|
463
|
// }
|
464
|
|
465
|
public applyPortalFilter(portal: string) {
|
466
|
this.getPages(portal);
|
467
|
}
|
468
|
|
469
|
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
|
|
480
|
this.subscriptions.push(this._helpContentService.togglePages(this.portal, ids, status, this.properties.adminToolsAPIURL).subscribe(
|
481
|
() => {
|
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
|
));
|
490
|
}
|
491
|
}
|
492
|
|
493
|
public capitalizeFirstLetter(str: string) {
|
494
|
return str.charAt(0).toUpperCase() + str.slice(1);
|
495
|
}
|
496
|
}
|