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, FormGroup, Validators} from "@angular/forms";
|
5
|
import {CheckDivId, DivId} from "../../utils/entities/adminTool/divId";
|
6
|
import {Page} from "../../utils/entities/adminTool/page";
|
7
|
import {EnvProperties} from '../../utils/properties/env-properties';
|
8
|
|
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 {Subscriber} from "rxjs";
|
13
|
import {properties} from "../../../../environments/environment";
|
14
|
import {PortalUtils} from "../portal/portalHelper";
|
15
|
import {AlertModal} from "../../utils/modal/alert";
|
16
|
import {Option} from "../../sharedComponents/input/input.component";
|
17
|
|
18
|
declare var UIkit;
|
19
|
|
20
|
@Component({
|
21
|
selector: 'divIds',
|
22
|
templateUrl: './divIds.component.html',
|
23
|
})
|
24
|
|
25
|
export class DivIdsComponent implements OnInit {
|
26
|
@ViewChild('editModal') editModal: AlertModal;
|
27
|
@ViewChild('deleteModal') deleteModal: AlertModal;
|
28
|
private selectedDivIds: string[] = [];
|
29
|
public checkboxes: CheckDivId[] = [];
|
30
|
public divIds: DivId[] = [];
|
31
|
public classForm: FormGroup;
|
32
|
public pagesCtrl: FormArray;
|
33
|
|
34
|
private searchText: RegExp = new RegExp('');
|
35
|
public keyword: string = "";
|
36
|
public properties: EnvProperties = properties;
|
37
|
public formPages: Page[] = [];
|
38
|
public showLoading: boolean = true;
|
39
|
public errorMessage: string = '';
|
40
|
public updateErrorMessage: string = '';
|
41
|
public modalErrorMessage: string = '';
|
42
|
public filterForm: FormGroup;
|
43
|
private subscriptions: any[] = [];
|
44
|
public allPages: Option[] = [];
|
45
|
@ViewChild('PageInput') pageInput: ElementRef<HTMLInputElement>;
|
46
|
selectedCommunityPid = null;
|
47
|
public portalUtils:PortalUtils = new PortalUtils();
|
48
|
private index: number;
|
49
|
|
50
|
|
51
|
ngOnInit() {
|
52
|
this.filterForm = this._fb.group({
|
53
|
keyword: [''],
|
54
|
type: ['all', Validators.required]});
|
55
|
this.subscriptions.push(this.filterForm.get('keyword').valueChanges.subscribe(value => {
|
56
|
this.searchText = new RegExp(value, 'i');
|
57
|
this.applyFilters();
|
58
|
}));
|
59
|
this.subscriptions.push(this.filterForm.get('type').valueChanges.subscribe(value => {
|
60
|
this.applyFilters();
|
61
|
}));
|
62
|
this.getDivIds();
|
63
|
this.subscriptions.push(this.route.queryParams.subscribe(params => {
|
64
|
HelperFunctions.scroll();
|
65
|
this.selectedCommunityPid = params['communityId'];
|
66
|
this.getPages();
|
67
|
}));
|
68
|
|
69
|
}
|
70
|
|
71
|
constructor(private element: ElementRef, private route: ActivatedRoute, private _router: Router, private _helpContentService: HelpContentService, private _fb: FormBuilder) {
|
72
|
}
|
73
|
|
74
|
ngOnDestroy(): void {
|
75
|
this.subscriptions.forEach(value => {
|
76
|
if (value instanceof Subscriber) {
|
77
|
value.unsubscribe();
|
78
|
} else if (value instanceof Function) {
|
79
|
value();
|
80
|
}
|
81
|
});
|
82
|
}
|
83
|
|
84
|
getDivIds() {
|
85
|
if (!Session.isLoggedIn()) {
|
86
|
this._router.navigate(['/user-info'], {
|
87
|
queryParams: {
|
88
|
"errorCode": LoginErrorCodes.NOT_VALID,
|
89
|
"redirectUrl": this._router.url
|
90
|
}
|
91
|
});
|
92
|
} else {
|
93
|
this.showLoading = true;
|
94
|
this.updateErrorMessage = "";
|
95
|
this.errorMessage = "";
|
96
|
|
97
|
this.subscriptions.push(this._helpContentService.getAllDivIdsFull( this.properties.adminToolsAPIURL).subscribe(
|
98
|
divIds => {
|
99
|
this.divIds = divIds;
|
100
|
this.checkboxes = [];
|
101
|
|
102
|
let self = this;
|
103
|
divIds.forEach(_ => {
|
104
|
self.checkboxes.push(<CheckDivId>{divId: _, checked: false});
|
105
|
});
|
106
|
|
107
|
this.showLoading = false;
|
108
|
},
|
109
|
error => this.handleError('System error retrieving classes', error)));
|
110
|
}
|
111
|
}
|
112
|
|
113
|
// public showModal():void {
|
114
|
// this.modal.showModal();
|
115
|
// }
|
116
|
|
117
|
public toggleCheckBoxes(event) {
|
118
|
this.checkboxes.forEach(_ => _.checked = event.target.checked);
|
119
|
}
|
120
|
|
121
|
public applyCheck(flag: boolean) {
|
122
|
this.checkboxes.forEach(_ => _.checked = flag);
|
123
|
}
|
124
|
|
125
|
public getSelectedDivIds(): string[] {
|
126
|
return this.checkboxes.filter(divId => divId.checked == true).map(checkedDivId => checkedDivId.divId).map(res => res._id);
|
127
|
}
|
128
|
|
129
|
private deleteDivIdsFromArray(ids: string[]): void {
|
130
|
for (let id of ids) {
|
131
|
let i = this.divIds.findIndex(_ => _._id == id);
|
132
|
this.divIds.splice(i, 1);
|
133
|
}
|
134
|
this.applyFilters();
|
135
|
}
|
136
|
|
137
|
public confirmDeleteDivId(id: string) {
|
138
|
this.selectedDivIds = [id];
|
139
|
this.confirmModalOpen();
|
140
|
}
|
141
|
|
142
|
public confirmDeleteSelectedDivIds() {
|
143
|
this.selectedDivIds = this.getSelectedDivIds();
|
144
|
this.confirmModalOpen();
|
145
|
}
|
146
|
|
147
|
private confirmModalOpen() {
|
148
|
if (!Session.isLoggedIn()) {
|
149
|
this._router.navigate(['/user-info'], {
|
150
|
queryParams: {
|
151
|
"errorCode": LoginErrorCodes.NOT_VALID,
|
152
|
"redirectUrl": this._router.url
|
153
|
}
|
154
|
});
|
155
|
} else {
|
156
|
this.deleteModal.cancelButton = true;
|
157
|
this.deleteModal.okButton = true;
|
158
|
this.deleteModal.alertTitle = "Delete Confirmation";
|
159
|
this.deleteModal.message = "Are you sure you want to delete the selected class(es)?";
|
160
|
this.deleteModal.okButtonText = "Yes";
|
161
|
this.deleteModal.open();
|
162
|
}
|
163
|
}
|
164
|
|
165
|
public confirmedDeleteDivIds(data: any) {
|
166
|
if (!Session.isLoggedIn()) {
|
167
|
this._router.navigate(['/user-info'], {
|
168
|
queryParams: {
|
169
|
"errorCode": LoginErrorCodes.NOT_VALID,
|
170
|
"redirectUrl": this._router.url
|
171
|
}
|
172
|
});
|
173
|
} else {
|
174
|
this.showLoading = true;
|
175
|
this.updateErrorMessage = "";
|
176
|
this.subscriptions.push(this._helpContentService.deleteDivIds(this.selectedDivIds, this.properties.adminToolsAPIURL).subscribe(
|
177
|
_ => {
|
178
|
this.deleteDivIdsFromArray(this.selectedDivIds);
|
179
|
UIkit.notification('Classes have been <b>successfully deleted</b>', {
|
180
|
status: 'success',
|
181
|
timeout: 6000,
|
182
|
pos: 'bottom-right'
|
183
|
});
|
184
|
this.showLoading = false;
|
185
|
},
|
186
|
error => this.handleUpdateError('System error deleting the selected classes', error)
|
187
|
));
|
188
|
}
|
189
|
}
|
190
|
|
191
|
public editDivId(i: number) {
|
192
|
let divId: DivId = this.checkboxes[i].divId;
|
193
|
this.index = this.divIds.findIndex(value => value._id === divId._id);
|
194
|
this.formPages = <Page[]>divId.pages;
|
195
|
this.pagesCtrl = this._fb.array([], Validators.required);
|
196
|
this.classForm = this._fb.group({
|
197
|
_id: this._fb.control(divId._id),
|
198
|
name: this._fb.control(divId.name, Validators.required),
|
199
|
pages: this.pagesCtrl,
|
200
|
portalType: this._fb.control(divId.portalType, Validators.required)
|
201
|
});
|
202
|
this.classForm.get('portalType').disable();
|
203
|
for(let i = 0; i < divId.pages.length; i++) {
|
204
|
this.pagesCtrl.push(this._fb.control(divId.pages[i]));
|
205
|
}
|
206
|
this.divIdsModalOpen("Edit class", "Save Changes");
|
207
|
}
|
208
|
|
209
|
public newDivId() {
|
210
|
this.pagesCtrl = this._fb.array([], Validators.required);
|
211
|
if(this.classForm) {
|
212
|
this.classForm.get('portalType').enable();
|
213
|
}
|
214
|
this.classForm = this._fb.group({
|
215
|
_id: this._fb.control(null),
|
216
|
name: this._fb.control('', Validators.required),
|
217
|
pages: this.pagesCtrl,
|
218
|
portalType: this._fb.control('', Validators.required)
|
219
|
});
|
220
|
this.modalErrorMessage = "";
|
221
|
this.divIdsModalOpen("Create class", "Create");
|
222
|
}
|
223
|
|
224
|
private divIdsModalOpen(title: string, yesBtn: string) {
|
225
|
if (!Session.isLoggedIn()) {
|
226
|
this._router.navigate(['/user-info'], {
|
227
|
queryParams: {
|
228
|
"errorCode": LoginErrorCodes.NOT_VALID,
|
229
|
"redirectUrl": this._router.url
|
230
|
}
|
231
|
});
|
232
|
} else {
|
233
|
this.editModal.cancelButton = true;
|
234
|
this.editModal.okButton = true;
|
235
|
this.editModal.okButtonLeft = false;
|
236
|
this.editModal.alertTitle = title;
|
237
|
this.editModal.okButtonText = yesBtn;
|
238
|
this.editModal.open();
|
239
|
}
|
240
|
}
|
241
|
|
242
|
public divIdSaveConfirmed(data: any) {
|
243
|
this.showLoading = true;
|
244
|
if (!Session.isLoggedIn()) {
|
245
|
this._router.navigate(['/user-info'], {
|
246
|
queryParams: {
|
247
|
"errorCode": LoginErrorCodes.NOT_VALID,
|
248
|
"redirectUrl": this._router.url
|
249
|
}
|
250
|
});
|
251
|
} else {
|
252
|
if (!this.classForm.value._id) {
|
253
|
this.modalErrorMessage = "";
|
254
|
this.subscriptions.push(this._helpContentService.saveDivId(<DivId>this.classForm.value, this.properties.adminToolsAPIURL).subscribe(
|
255
|
divId => {
|
256
|
this.divIdSavedSuccessfully(divId);
|
257
|
UIkit.notification('Class <b>' + divId.name + '</b> has been <b>successfully created</b>', {
|
258
|
status: 'success',
|
259
|
timeout: 6000,
|
260
|
pos: 'bottom-right'
|
261
|
});
|
262
|
},
|
263
|
error => this.handleUpdateError("System error creating class", error)
|
264
|
));
|
265
|
} else {
|
266
|
this.classForm.get('portalType').enable();
|
267
|
this.subscriptions.push(this._helpContentService.updateDivId(<DivId>this.classForm.value, this.properties.adminToolsAPIURL).subscribe(
|
268
|
divId => {
|
269
|
this.divIdUpdatedSuccessfully(divId);
|
270
|
UIkit.notification('Class <b>' + divId.name + '</b> has been <b>successfully updated</b>', {
|
271
|
status: 'success',
|
272
|
timeout: 6000,
|
273
|
pos: 'bottom-right'
|
274
|
});
|
275
|
},
|
276
|
error => this.handleUpdateError("System error updating class", error)
|
277
|
));
|
278
|
|
279
|
}
|
280
|
}
|
281
|
}
|
282
|
|
283
|
public divIdSavedSuccessfully(divId: DivId) {
|
284
|
this.divIds.push(divId);
|
285
|
this.applyFilters();
|
286
|
this.applyCheck(false);
|
287
|
this.showLoading = false;
|
288
|
}
|
289
|
|
290
|
public divIdUpdatedSuccessfully(divId: DivId) {
|
291
|
this.divIds[this.index] = divId;
|
292
|
this.applyFilters();
|
293
|
this.applyCheck(false);
|
294
|
this.showLoading = false;
|
295
|
}
|
296
|
|
297
|
public applyFilters() {
|
298
|
this.checkboxes = [];
|
299
|
this.divIds.filter(item => this.filterByType(item)).forEach(
|
300
|
item => this.checkboxes.push(<CheckDivId>{divId: item, checked: false})
|
301
|
);
|
302
|
this.checkboxes = this.checkboxes.filter(item => this.filterDivIds(item.divId));
|
303
|
}
|
304
|
|
305
|
public filterByType(divId: DivId): boolean {
|
306
|
let type = this.filterForm.get("type").value;
|
307
|
return type == "all" || (type == divId.portalType);
|
308
|
}
|
309
|
public filterDivIds(divId: DivId): boolean {
|
310
|
let textFlag = this.searchText.toString() == '' || (divId.name + ' ' + divId.portalType).match(this.searchText) != null;
|
311
|
return textFlag;
|
312
|
}
|
313
|
|
314
|
handleUpdateError(message: string, error) {
|
315
|
if (error == null) {
|
316
|
// this.formComponent.reset();
|
317
|
} else {
|
318
|
this.updateErrorMessage = message;
|
319
|
console.log('Server responded: ' + error);
|
320
|
}
|
321
|
UIkit.notification('An error has occurred. Please try again later', {
|
322
|
status: 'danger',
|
323
|
timeout: 6000,
|
324
|
pos: 'bottom-right'
|
325
|
});
|
326
|
this.showLoading = false;
|
327
|
}
|
328
|
|
329
|
handleError(message: string, error) {
|
330
|
this.errorMessage = message;
|
331
|
console.log('Server responded: ' + error);
|
332
|
|
333
|
this.showLoading = false;
|
334
|
}
|
335
|
|
336
|
getPages() {
|
337
|
this.showLoading = true;
|
338
|
this.errorMessage = "";
|
339
|
this.subscriptions.push(this._helpContentService.getAllPages(this.properties.adminToolsAPIURL).subscribe(
|
340
|
pages => {
|
341
|
this.allPages = [];
|
342
|
pages.forEach(page => {
|
343
|
this.allPages.push({
|
344
|
label: page.name,
|
345
|
value: page
|
346
|
});
|
347
|
});
|
348
|
this.showLoading = false;
|
349
|
},
|
350
|
error => this.handleError('System error retrieving pages', error)
|
351
|
));
|
352
|
}
|
353
|
}
|