Revision 60310
Added by Konstantinos Triantafyllou about 3 years ago
divIds.component.ts | ||
---|---|---|
1 | 1 |
import {Component, ElementRef, OnInit, ViewChild} from '@angular/core'; |
2 | 2 |
import {ActivatedRoute, Router} from "@angular/router"; |
3 | 3 |
import {HelpContentService} from "../../services/help-content.service"; |
4 |
import {FormArray, FormBuilder, FormControl, FormGroup, Validators} from "@angular/forms";
|
|
4 |
import {FormArray, FormBuilder, FormGroup, Validators} from "@angular/forms"; |
|
5 | 5 |
import {CheckDivId, DivId} from "../../utils/entities/adminTool/divId"; |
6 | 6 |
import {Page} from "../../utils/entities/adminTool/page"; |
7 | 7 |
import {EnvProperties} from '../../utils/properties/env-properties'; |
... | ... | |
9 | 9 |
import {Session} from '../../login/utils/helper.class'; |
10 | 10 |
import {LoginErrorCodes} from '../../login/utils/guardHelper.class'; |
11 | 11 |
import {HelperFunctions} from "../../utils/HelperFunctions.class"; |
12 |
import {Observable, Subscriber} from "rxjs"; |
|
13 |
import {map, startWith} from "rxjs/operators"; |
|
14 |
import {MatAutocompleteSelectedEvent, MatChipInputEvent} from "@angular/material"; |
|
12 |
import {Subscriber} from "rxjs"; |
|
15 | 13 |
import {properties} from "../../../../environments/environment"; |
16 | 14 |
import {PortalUtils} from "../portal/portalHelper"; |
17 |
import {CheckPortal, Portal} from "../../utils/entities/adminTool/portal"; |
|
15 |
import {AlertModal} from "../../utils/modal/alert"; |
|
16 |
import {Option} from "../../sharedComponents/input/input.component"; |
|
18 | 17 |
|
19 | 18 |
@Component({ |
20 | 19 |
selector: 'divIds', |
... | ... | |
22 | 21 |
}) |
23 | 22 |
|
24 | 23 |
export class DivIdsComponent implements OnInit { |
25 |
@ViewChild('AlertModalSaveDivId') alertModalSaveDivId;
|
|
26 |
@ViewChild('AlertModalDeleteDivIds') alertModalDeleteDivIds;
|
|
24 |
@ViewChild('editModal') editModal: AlertModal;
|
|
25 |
@ViewChild('deleteModal') deleteModal: AlertModal;
|
|
27 | 26 |
private selectedDivIds: string[] = []; |
28 | 27 |
public checkboxes: CheckDivId[] = []; |
29 | 28 |
public divIds: DivId[] = []; |
30 |
|
|
31 |
public myForm: FormGroup; |
|
32 |
public pageSearchCtrl: FormControl; |
|
29 |
public classForm: FormGroup; |
|
33 | 30 |
public pagesCtrl: FormArray; |
34 | 31 |
|
35 | 32 |
private searchText: RegExp = new RegExp(''); |
36 | 33 |
public keyword: string = ""; |
37 |
|
|
38 |
public properties: EnvProperties = null; |
|
34 |
public properties: EnvProperties = properties; |
|
39 | 35 |
public formPages: Page[] = []; |
40 |
|
|
41 | 36 |
public showLoading: boolean = true; |
42 | 37 |
public errorMessage: string = ''; |
43 | 38 |
public updateErrorMessage: string = ''; |
44 | 39 |
public modalErrorMessage: string = ''; |
45 | 40 |
public filterForm: FormGroup; |
46 | 41 |
private subscriptions: any[] = []; |
47 |
public allPages: Page[] = []; |
|
48 |
filteredPages: Observable<Page[]>; |
|
42 |
public allPages: Option[] = []; |
|
49 | 43 |
@ViewChild('PageInput') pageInput: ElementRef<HTMLInputElement>; |
50 |
selectedPages: Page[] = []; |
|
51 | 44 |
selectedCommunityPid = null; |
52 | 45 |
public portalUtils:PortalUtils = new PortalUtils(); |
46 |
private index: number; |
|
47 |
|
|
48 |
|
|
53 | 49 |
ngOnInit() { |
54 | 50 |
this.filterForm = this._fb.group({ |
55 | 51 |
keyword: [''], |
... | ... | |
60 | 56 |
this.subscriptions.push(this.filterForm.get('type').valueChanges.subscribe(value => { |
61 | 57 |
this.applyTypeFilter(); |
62 | 58 |
})); |
63 |
this.pageSearchCtrl = this._fb.control(''); |
|
64 |
this.pagesCtrl = this._fb.array([]); |
|
65 |
this.myForm = this._fb.group({ |
|
66 |
_id: '', |
|
67 |
name: ['', Validators.required], |
|
68 |
pages: this.pagesCtrl, |
|
69 |
portalType: ['', Validators.required] |
|
70 |
}); |
|
71 |
this.properties = properties; |
|
72 | 59 |
this.getDivIds(); |
73 | 60 |
this.subscriptions.push(this.route.queryParams.subscribe(params => { |
74 | 61 |
HelperFunctions.scroll(); |
... | ... | |
138 | 125 |
|
139 | 126 |
private deleteDivIdsFromArray(ids: string[]): void { |
140 | 127 |
for (let id of ids) { |
141 |
let i = this.checkboxes.findIndex(_ => _.divId._id == id);
|
|
142 |
this.checkboxes.splice(i, 1);
|
|
128 |
let i = this.divIds.findIndex(_ => _._id == id);
|
|
129 |
this.divIds.splice(i, 1);
|
|
143 | 130 |
} |
131 |
this.applyTypeFilter(); |
|
132 |
this.applyFilter(); |
|
144 | 133 |
} |
145 | 134 |
|
146 | 135 |
public confirmDeleteDivId(id: string) { |
... | ... | |
162 | 151 |
} |
163 | 152 |
}); |
164 | 153 |
} else { |
165 |
this.alertModalDeleteDivIds.cancelButton = true;
|
|
166 |
this.alertModalDeleteDivIds.okButton = true;
|
|
167 |
this.alertModalDeleteDivIds.alertTitle = "Delete Confirmation";
|
|
168 |
this.alertModalDeleteDivIds.message = "Are you sure you want to delete the selected class(es)?";
|
|
169 |
this.alertModalDeleteDivIds.okButtonText = "Yes";
|
|
170 |
this.alertModalDeleteDivIds.open();
|
|
154 |
this.deleteModal.cancelButton = true;
|
|
155 |
this.deleteModal.okButton = true;
|
|
156 |
this.deleteModal.alertTitle = "Delete Confirmation";
|
|
157 |
this.deleteModal.message = "Are you sure you want to delete the selected class(es)?";
|
|
158 |
this.deleteModal.okButtonText = "Yes";
|
|
159 |
this.deleteModal.open();
|
|
171 | 160 |
} |
172 | 161 |
} |
173 | 162 |
|
... | ... | |
194 | 183 |
|
195 | 184 |
public editDivId(i: number) { |
196 | 185 |
let divId: DivId = this.checkboxes[i].divId; |
186 |
this.index = this.divIds.findIndex(value => value._id === divId._id); |
|
197 | 187 |
this.formPages = <Page[]>divId.pages; |
198 |
this.pagesCtrl = this._fb.array([]); |
|
199 |
this.myForm = this._fb.group({
|
|
200 |
_id: divId._id,
|
|
201 |
name: [divId.name,Validators.required],
|
|
188 |
this.pagesCtrl = this._fb.array([], Validators.required);
|
|
189 |
this.classForm = this._fb.group({
|
|
190 |
_id: this._fb.control(divId._id),
|
|
191 |
name: this._fb.control(divId.name, Validators.required),
|
|
202 | 192 |
pages: this.pagesCtrl, |
203 |
portalType: [divId.portalType, Validators.required]
|
|
193 |
portalType: this._fb.control(divId.portalType, Validators.required)
|
|
204 | 194 |
}); |
205 |
this.myForm.controls['portalType'].disable(); |
|
206 |
|
|
195 |
this.classForm.get('portalType').disable(); |
|
207 | 196 |
for(let i = 0; i < divId.pages.length; i++) { |
208 | 197 |
this.pagesCtrl.push(this._fb.control(divId.pages[i])); |
209 | 198 |
} |
210 |
this.filteredPages = this.pageSearchCtrl.valueChanges.pipe(startWith(''), |
|
211 |
map(page => this._filter(page))); |
|
212 |
this.selectedPages = JSON.parse(JSON.stringify(divId.pages)); |
|
213 |
this.divIdsModalOpen(this.alertModalSaveDivId, "", "Save Changes"); |
|
199 |
this.divIdsModalOpen("Edit class", "Save Changes"); |
|
214 | 200 |
} |
215 | 201 |
|
216 | 202 |
public newDivId() { |
217 |
this.myForm.controls['portalType'].enable(); |
|
218 |
|
|
219 |
this.pagesCtrl = this._fb.array([]); |
|
220 |
this.myForm = this._fb.group({ |
|
221 |
_id: '', |
|
222 |
name: ['', Validators.required], |
|
203 |
this.pagesCtrl = this._fb.array([], Validators.required); |
|
204 |
if(this.classForm) { |
|
205 |
this.classForm.get('portalType').enable(); |
|
206 |
} |
|
207 |
this.classForm = this._fb.group({ |
|
208 |
_id: this._fb.control(null), |
|
209 |
name: this._fb.control('', Validators.required), |
|
223 | 210 |
pages: this.pagesCtrl, |
224 |
//openaire: this._fb.control(true), |
|
225 |
portalType: ['', Validators.required] |
|
211 |
portalType: this._fb.control('', Validators.required) |
|
226 | 212 |
}); |
227 |
this.filteredPages = this.pageSearchCtrl.valueChanges.pipe(startWith(''), |
|
228 |
map(page => this._filter(page))); |
|
229 | 213 |
this.modalErrorMessage = ""; |
230 |
this.selectedPages = []; |
|
231 |
this.divIdsModalOpen(this.alertModalSaveDivId, "", "Save"); |
|
214 |
this.divIdsModalOpen("Create class", "Create"); |
|
232 | 215 |
} |
233 | 216 |
|
234 |
private divIdsModalOpen(modal: any, title: string, yesBtn: string) {
|
|
217 |
private divIdsModalOpen(title: string, yesBtn: string) { |
|
235 | 218 |
if (!Session.isLoggedIn()) { |
236 | 219 |
this._router.navigate(['/user-info'], { |
237 | 220 |
queryParams: { |
... | ... | |
240 | 223 |
} |
241 | 224 |
}); |
242 | 225 |
} else { |
243 |
modal.cancelButton = true; |
|
244 |
modal.okButton = true; |
|
245 |
modal.alertTitle = title; |
|
246 |
modal.okButtonText = yesBtn; |
|
247 |
modal.open(); |
|
226 |
this.editModal.cancelButton = true; |
|
227 |
this.editModal.okButton = true; |
|
228 |
this.editModal.okButtonLeft = false; |
|
229 |
this.editModal.alertTitle = title; |
|
230 |
this.editModal.okButtonText = yesBtn; |
|
231 |
this.editModal.open(); |
|
248 | 232 |
} |
249 | 233 |
} |
250 | 234 |
|
... | ... | |
257 | 241 |
} |
258 | 242 |
}); |
259 | 243 |
} else { |
260 |
console.log(this.myForm.value) |
|
261 |
if (this.myForm.value['_id'].length == 0) { |
|
262 |
this.myForm.controls['portalType'].enable(); |
|
263 |
|
|
244 |
if (!this.classForm.value._id) { |
|
264 | 245 |
this.modalErrorMessage = ""; |
265 |
|
|
266 |
this.subscriptions.push(this._helpContentService.saveDivId(<DivId>this.myForm.value, this.properties.adminToolsAPIURL).subscribe( |
|
246 |
this.subscriptions.push(this._helpContentService.saveDivId(<DivId>this.classForm.value, this.properties.adminToolsAPIURL).subscribe( |
|
267 | 247 |
divId => { |
268 | 248 |
this.divIdSavedSuccessfully(divId); |
269 | 249 |
}, |
270 | 250 |
error => this.handleUpdateError("System error creating class", error) |
271 | 251 |
)); |
272 | 252 |
} else { |
273 |
this.subscriptions.push(this._helpContentService.updateDivId(<DivId>this.myForm.value, this.properties.adminToolsAPIURL).subscribe( |
|
253 |
this.classForm.get('portalType').enable(); |
|
254 |
this.subscriptions.push(this._helpContentService.updateDivId(<DivId>this.classForm.value, this.properties.adminToolsAPIURL).subscribe( |
|
274 | 255 |
divId => { |
275 | 256 |
this.divIdUpdatedSuccessfully(divId); |
276 | 257 |
}, |
... | ... | |
282 | 263 |
} |
283 | 264 |
|
284 | 265 |
public divIdSavedSuccessfully(divId: DivId) { |
285 |
this.checkboxes.push(<CheckDivId>{divId: divId, checked: false}); |
|
266 |
this.divIds.push(divId); |
|
267 |
this.applyTypeFilter(); |
|
268 |
this.applyFilter(); |
|
286 | 269 |
this.applyCheck(false); |
287 | 270 |
} |
288 | 271 |
|
289 | 272 |
public divIdUpdatedSuccessfully(divId: DivId) { |
290 |
this.checkboxes.find(checkItem => checkItem.divId._id == divId._id).divId = divId; |
|
273 |
this.divIds[this.index] = divId; |
|
274 |
this.applyTypeFilter(); |
|
275 |
this.applyFilter(); |
|
291 | 276 |
this.applyCheck(false); |
292 | 277 |
} |
293 | 278 |
|
... | ... | |
341 | 326 |
this.errorMessage = ""; |
342 | 327 |
this.subscriptions.push(this._helpContentService.getAllPages(this.properties.adminToolsAPIURL).subscribe( |
343 | 328 |
pages => { |
344 |
this.allPages = pages; |
|
329 |
this.allPages = []; |
|
330 |
pages.forEach(page => { |
|
331 |
this.allPages.push({ |
|
332 |
label: page.name, |
|
333 |
value: page |
|
334 |
}); |
|
335 |
}); |
|
345 | 336 |
this.showLoading = false; |
346 | 337 |
}, |
347 | 338 |
error => this.handleError('System error retrieving pages', error) |
348 | 339 |
)); |
349 |
|
|
350 | 340 |
} |
351 |
|
|
352 |
remove(page): void { |
|
353 |
let index = this.selectedPages.indexOf(page); |
|
354 |
if (index >= 0) { |
|
355 |
this.selectedPages.splice(index, 1); |
|
356 |
this.pagesCtrl.value.splice(index, 1); |
|
357 |
this.pagesCtrl.markAsDirty(); |
|
358 |
} |
|
359 |
} |
|
360 |
|
|
361 |
selected(event: MatAutocompleteSelectedEvent): void { |
|
362 |
let newPage = event.option.value; |
|
363 |
if (this.selectedPages.indexOf(newPage) == -1) { |
|
364 |
this.selectedPages.push(event.option.value); |
|
365 |
this.pagesCtrl.push(this._fb.control(newPage)); |
|
366 |
this.pagesCtrl.markAsDirty(); |
|
367 |
} |
|
368 |
this.pageInput.nativeElement.value = ''; |
|
369 |
this.pageSearchCtrl.setValue(''); |
|
370 |
} |
|
371 |
|
|
372 |
private _filter(value: string): Page[] { |
|
373 |
if (!value || value.length == 0) { |
|
374 |
return this.allPages.slice(); |
|
375 |
} |
|
376 |
const filterValue = value.toString().toLowerCase(); |
|
377 |
return this.allPages.filter(page => page.name.toLowerCase().indexOf(filterValue) != -1); |
|
378 |
} |
|
379 | 341 |
} |
Also available in: Unified diff
[Library | Trunk]: Admin tools pages, fix modals and update behaviour