1
|
/**
|
2
|
* Created by stefania on 7/13/17.
|
3
|
*/
|
4
|
import {Component, ElementRef, OnInit, ViewChild} from '@angular/core';
|
5
|
import {ActivatedRoute, Router} from '@angular/router';
|
6
|
import {HelpContentService} from '../../services/help-content.service';
|
7
|
import {FormGroup} from '@angular/forms';
|
8
|
import {PageFormComponent} from './page-form.component';
|
9
|
import {CheckPage, Page} from '../../domain/page';
|
10
|
import {Portal} from '../../domain/portal';
|
11
|
import {Entity} from '../../domain/entity';
|
12
|
import {EnvProperties} from '../../openaireLibrary/utils/properties/env-properties';
|
13
|
import {Session} from '../../openaireLibrary/login/utils/helper.class';
|
14
|
import {LoginErrorCodes} from '../../openaireLibrary/login/utils/guardHelper.class';
|
15
|
import {HelperFunctions} from '../../openaireLibrary/utils/HelperFunctions.class';
|
16
|
import {UserManagementService} from '../../openaireLibrary/services/user-management.service';
|
17
|
import {Title} from '@angular/platform-browser';
|
18
|
import {StringUtils} from '../../openaireLibrary/utils/string-utils.class';
|
19
|
import {ClearCacheService} from "../../openaireLibrary/services/clear-cache.service";
|
20
|
|
21
|
@Component({
|
22
|
selector: 'pages',
|
23
|
templateUrl: './pages.component.html',
|
24
|
})
|
25
|
|
26
|
export class PagesComponent implements OnInit {
|
27
|
|
28
|
@ViewChild('AlertModalSavePage') alertModalSavePage;
|
29
|
@ViewChild('AlertModalUpdatePage') alertModalUpdatePage;
|
30
|
@ViewChild('AlertModalDeletePages') alertModalDeletePages;
|
31
|
private selectedPages: string[] = [];
|
32
|
|
33
|
@ViewChild(PageFormComponent)
|
34
|
public formComponent: PageFormComponent;
|
35
|
|
36
|
public checkboxes: CheckPage[] = [];
|
37
|
|
38
|
public pages: Page[] = [];
|
39
|
public pageWithDivIds: string[] = [];
|
40
|
|
41
|
//public errorMessage: string;
|
42
|
|
43
|
public formGroup: FormGroup;
|
44
|
|
45
|
private searchText: RegExp = new RegExp('');
|
46
|
public keyword: string = '';
|
47
|
|
48
|
public communities: Portal[] = [];
|
49
|
|
50
|
public selectedCommunityPid: string;
|
51
|
|
52
|
public pagesType: string;
|
53
|
public properties: EnvProperties = null;
|
54
|
|
55
|
public showLoading: boolean = true;
|
56
|
public errorMessage: string = '';
|
57
|
public updateErrorMessage: string = '';
|
58
|
public modalErrorMessage: string = '';
|
59
|
public isPortalAdministrator = null;
|
60
|
|
61
|
constructor(private element: ElementRef, private route: ActivatedRoute,
|
62
|
private title: Title,
|
63
|
private _router: Router, private _helpContentService: HelpContentService,
|
64
|
private userManagementService: UserManagementService,
|
65
|
private _clearCacheService: ClearCacheService) {
|
66
|
}
|
67
|
|
68
|
ngOnInit() {
|
69
|
this.formGroup = this.formComponent.form;
|
70
|
this.route.data
|
71
|
.subscribe((data: { envSpecific: EnvProperties }) => {
|
72
|
this.properties = data.envSpecific;
|
73
|
|
74
|
this.route.queryParams.subscribe(params => {
|
75
|
HelperFunctions.scroll();
|
76
|
this.title.setTitle('Administration Dashboard | Pages');
|
77
|
this.pagesType = '';
|
78
|
if (params['type']) {
|
79
|
this.pagesType = params['type'];
|
80
|
this.title.setTitle('Administration Dashboard | ' + StringUtils.capitalize(this.pagesType) + ' Pages');
|
81
|
}
|
82
|
|
83
|
this.keyword = '';
|
84
|
this.userManagementService.getUserInfo().subscribe( user => {
|
85
|
this.selectedCommunityPid = params['communityId'];
|
86
|
this.applyCommunityFilter(this.selectedCommunityPid);
|
87
|
this.isPortalAdministrator = Session.isPortalAdministrator(user) && !this.selectedCommunityPid;
|
88
|
});
|
89
|
//this.getCommunities();
|
90
|
});
|
91
|
});
|
92
|
}
|
93
|
|
94
|
getPages(community_pid: string) {
|
95
|
if (!Session.isLoggedIn()) {
|
96
|
this._router.navigate(['/user-info'], {queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}});
|
97
|
} else {
|
98
|
this.showLoading = true;
|
99
|
this.updateErrorMessage = '';
|
100
|
this.errorMessage = '';
|
101
|
|
102
|
this.pageWithDivIds = [];
|
103
|
|
104
|
let parameters = '';
|
105
|
if (this.pagesType) {
|
106
|
parameters = '?page_type=' + this.pagesType;
|
107
|
}
|
108
|
if (community_pid) {
|
109
|
this._helpContentService.getCommunityPagesByType(community_pid, this.pagesType, this.properties.adminToolsAPIURL).subscribe(
|
110
|
pages => {
|
111
|
this.pagesReturned(pages);
|
112
|
//if(!this.pagesType || this.pagesType == "link") {
|
113
|
this.getPagesWithDivIds(community_pid);
|
114
|
//} else {
|
115
|
//this.showLoading = false;
|
116
|
//}
|
117
|
},
|
118
|
error => this.handleError('System error retrieving pages', error)
|
119
|
);
|
120
|
} else {
|
121
|
this._helpContentService.getAllPagesFull(this.properties.adminToolsAPIURL).subscribe(
|
122
|
pages => {
|
123
|
this.pagesReturned(pages);
|
124
|
this.showLoading = false;
|
125
|
},
|
126
|
error => this.handleError('System error retrieving pages', error)
|
127
|
);
|
128
|
}
|
129
|
}
|
130
|
}
|
131
|
|
132
|
getPagesWithDivIds(community_pid: string) {
|
133
|
if (!Session.isLoggedIn()) {
|
134
|
this._router.navigate(['/user-info'], {queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}});
|
135
|
} else {
|
136
|
this._helpContentService.getPageIdsFromDivIds(community_pid, this.properties.adminToolsAPIURL).subscribe(
|
137
|
pages => {
|
138
|
this.pageWithDivIds = pages;
|
139
|
this.showLoading = false;
|
140
|
},
|
141
|
error => this.handleError('System error retrieving information about pages\' classes', error));
|
142
|
}
|
143
|
}
|
144
|
|
145
|
pagesReturned(pages: Page[]) {
|
146
|
this.pages = pages;
|
147
|
this.checkboxes = [];
|
148
|
|
149
|
if (pages) {
|
150
|
pages.forEach(_ => {
|
151
|
this.checkboxes.push(<CheckPage>{page: _, checked: false});
|
152
|
});
|
153
|
}
|
154
|
}
|
155
|
|
156
|
/*
|
157
|
getCommunities() {
|
158
|
this._helpContentService.getCommunities(this.properties.adminToolsAPIURL).subscribe(
|
159
|
communities => {
|
160
|
this.communities = communities;
|
161
|
this.selectedCommunityPid = this.communities[0].pid;
|
162
|
this.getPages(this.selectedCommunityPid);
|
163
|
this.getPagesWithDivIds(this.selectedCommunityPid);
|
164
|
},
|
165
|
error => this.handleError('System error retrieving communities', error));
|
166
|
}
|
167
|
*/
|
168
|
|
169
|
public toggleCheckBoxes(event) {
|
170
|
this.checkboxes.forEach(_ => _.checked = event.target.checked);
|
171
|
}
|
172
|
|
173
|
public applyCheck(flag: boolean) {
|
174
|
this.checkboxes.forEach(_ => _.checked = flag);
|
175
|
}
|
176
|
|
177
|
public getSelectedPages(): string[] {
|
178
|
return this.checkboxes.filter(page => page.checked == true).map(checkedPage => checkedPage.page).map(res => res._id);
|
179
|
}
|
180
|
|
181
|
private deletePagesFromArray(ids: string[]): void {
|
182
|
for (let id of ids) {
|
183
|
let i = this.checkboxes.findIndex(_ => _.page._id == id);
|
184
|
this.checkboxes.splice(i, 1);
|
185
|
}
|
186
|
}
|
187
|
|
188
|
public confirmDeletePage(id: string) {
|
189
|
//this.deleteConfirmationModal.ids = [id];
|
190
|
//this.deleteConfirmationModal.showModal();
|
191
|
this.selectedPages = [id];
|
192
|
this.confirmModalOpen();
|
193
|
}
|
194
|
|
195
|
public confirmDeleteSelectedPages() {
|
196
|
//this.deleteConfirmationModal.ids = this.getSelectedPages();
|
197
|
//this.deleteConfirmationModal.showModal();
|
198
|
this.selectedPages = this.getSelectedPages();
|
199
|
this.confirmModalOpen();
|
200
|
}
|
201
|
|
202
|
private confirmModalOpen() {
|
203
|
if (!Session.isLoggedIn()) {
|
204
|
this._router.navigate(['/user-info'], {queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}});
|
205
|
} else {
|
206
|
this.alertModalDeletePages.cancelButton = true;
|
207
|
this.alertModalDeletePages.okButton = true;
|
208
|
this.alertModalDeletePages.alertTitle = 'Delete Confirmation';
|
209
|
this.alertModalDeletePages.message = 'Are you sure you want to delete the selected page(s)?';
|
210
|
this.alertModalDeletePages.okButtonText = 'Yes';
|
211
|
this.alertModalDeletePages.open();
|
212
|
}
|
213
|
}
|
214
|
|
215
|
public confirmedDeletePages(data: any) {
|
216
|
if (!Session.isLoggedIn()) {
|
217
|
this._router.navigate(['/user-info'], {queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}});
|
218
|
} else {
|
219
|
this.showLoading = true;
|
220
|
this.updateErrorMessage = '';
|
221
|
|
222
|
this._helpContentService.deletePages(this.selectedPages, this.properties.adminToolsAPIURL).subscribe(
|
223
|
_ => {
|
224
|
this.deletePagesFromArray(this.selectedPages);
|
225
|
this.showLoading = false;
|
226
|
this._clearCacheService.clearCache("pages deleted");
|
227
|
},
|
228
|
error => this.handleUpdateError('System error deleting the selected pages', error)
|
229
|
);
|
230
|
}
|
231
|
}
|
232
|
|
233
|
public editPage(i: number) {
|
234
|
let page: Page = this.checkboxes[i].page;
|
235
|
this.formGroup.patchValue(page);
|
236
|
this.formComponent.setEntities(page.entities as Entity[]);
|
237
|
this.formGroup.controls['portalType'].disable();
|
238
|
|
239
|
//console.info(this.formGroup.value);
|
240
|
//this.updateModal.showModal();
|
241
|
this.modalErrorMessage = '';
|
242
|
this.pagesModalOpen(this.alertModalUpdatePage, 'Update', 'Update Page');
|
243
|
}
|
244
|
|
245
|
public newPage() {
|
246
|
this.formGroup.controls['portalType'].enable();
|
247
|
this.formComponent.reset();
|
248
|
this.modalErrorMessage = '';
|
249
|
this.pagesModalOpen(this.alertModalSavePage, 'Save', 'Add a new Page');
|
250
|
}
|
251
|
|
252
|
private pagesModalOpen(modal: any, title: string, yesBtn: string) {
|
253
|
if (!Session.isLoggedIn()) {
|
254
|
this._router.navigate(['/user-info'], {queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}});
|
255
|
} else {
|
256
|
modal.cancelButton = true;
|
257
|
modal.okButton = true;
|
258
|
modal.alertTitle = title;
|
259
|
modal.okButtonText = yesBtn;
|
260
|
modal.open();
|
261
|
}
|
262
|
}
|
263
|
|
264
|
public pageSaveConfirmed(data: any) {
|
265
|
if (!Session.isLoggedIn()) {
|
266
|
this._router.navigate(['/user-info'], {queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}});
|
267
|
} else {
|
268
|
if (!this.formGroup.valid) {
|
269
|
this.pagesModalOpen(this.alertModalSavePage, 'Save', 'Add a new Page');
|
270
|
this.modalErrorMessage = 'Please fill in all required fields marked with *';
|
271
|
} else {
|
272
|
this.modalErrorMessage = '';
|
273
|
this._helpContentService.savePage(<Page>this.formGroup.value, this.properties.adminToolsAPIURL).subscribe(
|
274
|
page => {
|
275
|
this.pageSavedSuccessfully(page);
|
276
|
this._clearCacheService.clearCache("page saved");
|
277
|
},
|
278
|
error => this.handleUpdateError('System error creating page', error)
|
279
|
);
|
280
|
}
|
281
|
}
|
282
|
}
|
283
|
|
284
|
public pageUpdateConfirmed(data: any) {
|
285
|
if (!Session.isLoggedIn()) {
|
286
|
this._router.navigate(['/user-info'], {queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}});
|
287
|
} else {
|
288
|
if (!this.formGroup.valid) {
|
289
|
this.pagesModalOpen(this.alertModalUpdatePage, 'Update', 'Update Page');
|
290
|
this.modalErrorMessage = 'Please fill in all required fields marked with *';
|
291
|
} else {
|
292
|
this.formGroup.controls['portalType'].enable();
|
293
|
this._helpContentService.updatePage(<Page>this.formGroup.value, this.properties.adminToolsAPIURL).subscribe(
|
294
|
page => {
|
295
|
this.pageUpdatedSuccessfully(page);
|
296
|
this._clearCacheService.clearCache("page updated");
|
297
|
},
|
298
|
error => this.handleUpdateError('System error updating page', error)
|
299
|
);
|
300
|
}
|
301
|
}
|
302
|
}
|
303
|
|
304
|
public pageSavedSuccessfully(page: Page) {
|
305
|
this.checkboxes.push(<CheckPage>{page: page, checked: false});
|
306
|
this.applyCheck(false);
|
307
|
}
|
308
|
|
309
|
public pageUpdatedSuccessfully(page: Page) {
|
310
|
this.checkboxes.find(checkItem => checkItem.page._id == page._id).page = page;
|
311
|
this.applyCheck(false);
|
312
|
}
|
313
|
|
314
|
public filterBySearch(text: string) {
|
315
|
this.searchText = new RegExp(text, 'i');
|
316
|
this.applyFilter();
|
317
|
}
|
318
|
|
319
|
public applyFilter() {
|
320
|
this.checkboxes = [];
|
321
|
this.pages.filter(item => this.filterPages(item)).forEach(
|
322
|
_ => this.checkboxes.push(<CheckPage>{page: _, checked: false})
|
323
|
);
|
324
|
}
|
325
|
|
326
|
public filterPages(page: Page): boolean {
|
327
|
let textFlag = this.searchText.toString() == '' || (page.route + ' ' + page.name + ' ' + page.type + ' ' + page.portalType).match(this.searchText) != null;
|
328
|
return textFlag;
|
329
|
}
|
330
|
|
331
|
handleError(message: string, error) {
|
332
|
// if(error == null) {
|
333
|
// this.formComponent.reset();
|
334
|
// } else {
|
335
|
this.errorMessage = message;// + ' (Server responded: ' + error + ')';
|
336
|
console.log('Server responded: ' + error);
|
337
|
//}
|
338
|
|
339
|
this.showLoading = false;
|
340
|
}
|
341
|
|
342
|
handleUpdateError(message: string, error) {
|
343
|
if (error == null) {
|
344
|
this.formComponent.reset();
|
345
|
} else {
|
346
|
this.updateErrorMessage = message;// + ' (Server responded: ' + error + ')';
|
347
|
console.log('Server responded: ' + error);
|
348
|
}
|
349
|
|
350
|
this.showLoading = false;
|
351
|
}
|
352
|
|
353
|
// public filterByCommunity(event: any) {
|
354
|
// this.selectedCommunityPid = event.target.value;
|
355
|
// this.applyCommunityFilter(this.selectedCommunityPid);
|
356
|
// }
|
357
|
|
358
|
public applyCommunityFilter(community_pid: string) {
|
359
|
this.getPages(community_pid);
|
360
|
}
|
361
|
|
362
|
public togglePages(status: boolean, ids: string[]) {
|
363
|
if (!Session.isLoggedIn()) {
|
364
|
this._router.navigate(['/user-info'], {queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}});
|
365
|
} else {
|
366
|
this.updateErrorMessage = '';
|
367
|
|
368
|
this._helpContentService.togglePages(this.selectedCommunityPid, ids, status, this.properties.adminToolsAPIURL).subscribe(
|
369
|
() => {
|
370
|
for (let id of ids) {
|
371
|
let i = this.checkboxes.findIndex(_ => _.page._id == id);
|
372
|
this.checkboxes[i].page.isEnabled = status;
|
373
|
}
|
374
|
this.applyCheck(false);
|
375
|
this._clearCacheService.clearCache("pages toggled (status: "+status+")");
|
376
|
},
|
377
|
error => this.handleUpdateError('System error changing the status of the selected page(s)', error)
|
378
|
);
|
379
|
}
|
380
|
}
|
381
|
|
382
|
public capitalizeFirstLetter(str: string) {
|
383
|
return str.charAt(0).toUpperCase() + str.slice(1);
|
384
|
}
|
385
|
}
|