2019-12-18 16:14:21 +01:00
|
|
|
import {Component, ElementRef, OnInit, ViewChild} from '@angular/core';
|
|
|
|
import {ActivatedRoute, Router} from "@angular/router";
|
|
|
|
import {HelpContentService} from "../../services/help-content.service";
|
2021-01-28 17:33:16 +01:00
|
|
|
import {FormArray, FormBuilder, FormGroup, Validators} from "@angular/forms";
|
2019-12-18 16:14:21 +01:00
|
|
|
import {CheckDivId, DivId} from "../../utils/entities/adminTool/divId";
|
|
|
|
import {Page} from "../../utils/entities/adminTool/page";
|
|
|
|
import {EnvProperties} from '../../utils/properties/env-properties';
|
|
|
|
import {HelperFunctions} from "../../utils/HelperFunctions.class";
|
2021-01-28 17:33:16 +01:00
|
|
|
import {Subscriber} from "rxjs";
|
2021-01-11 10:21:42 +01:00
|
|
|
import {properties} from "../../../../environments/environment";
|
|
|
|
import {PortalUtils} from "../portal/portalHelper";
|
2021-01-28 17:33:16 +01:00
|
|
|
import {AlertModal} from "../../utils/modal/alert";
|
|
|
|
import {Option} from "../../sharedComponents/input/input.component";
|
2021-02-02 13:29:18 +01:00
|
|
|
import {SearchInputComponent} from "../../sharedComponents/search-input/search-input.component";
|
2021-03-19 17:51:02 +01:00
|
|
|
import {Title} from "@angular/platform-browser";
|
2019-12-18 16:14:21 +01:00
|
|
|
|
2021-02-01 20:17:55 +01:00
|
|
|
declare var UIkit;
|
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
@Component({
|
|
|
|
selector: 'divIds',
|
|
|
|
templateUrl: './divIds.component.html',
|
|
|
|
})
|
|
|
|
|
|
|
|
export class DivIdsComponent implements OnInit {
|
2021-01-28 17:33:16 +01:00
|
|
|
@ViewChild('editModal') editModal: AlertModal;
|
|
|
|
@ViewChild('deleteModal') deleteModal: AlertModal;
|
2019-12-18 16:14:21 +01:00
|
|
|
private selectedDivIds: string[] = [];
|
|
|
|
public checkboxes: CheckDivId[] = [];
|
|
|
|
public divIds: DivId[] = [];
|
2021-01-28 17:33:16 +01:00
|
|
|
public classForm: FormGroup;
|
2019-12-18 16:14:21 +01:00
|
|
|
public pagesCtrl: FormArray;
|
2021-03-19 17:51:02 +01:00
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
private searchText: RegExp = new RegExp('');
|
|
|
|
public keyword: string = "";
|
2021-01-28 17:33:16 +01:00
|
|
|
public properties: EnvProperties = properties;
|
2019-12-18 16:14:21 +01:00
|
|
|
public formPages: Page[] = [];
|
|
|
|
public showLoading: boolean = true;
|
2021-01-11 10:21:42 +01:00
|
|
|
public filterForm: FormGroup;
|
2019-12-18 16:14:21 +01:00
|
|
|
private subscriptions: any[] = [];
|
2021-01-28 17:33:16 +01:00
|
|
|
public allPages: Option[] = [];
|
2019-12-18 16:14:21 +01:00
|
|
|
selectedCommunityPid = null;
|
2021-03-19 17:51:02 +01:00
|
|
|
public portalUtils: PortalUtils = new PortalUtils();
|
2021-01-28 17:33:16 +01:00
|
|
|
private index: number;
|
2021-02-02 13:29:18 +01:00
|
|
|
public selectedKeyword: string;
|
|
|
|
@ViewChild('searchInputComponent') searchInputComponent: SearchInputComponent;
|
2021-01-28 17:33:16 +01:00
|
|
|
|
2021-03-19 17:51:02 +01:00
|
|
|
constructor(private element: ElementRef, private route: ActivatedRoute, private _router: Router,
|
|
|
|
private title: Title,
|
|
|
|
private _helpContentService: HelpContentService, private _fb: FormBuilder) {
|
|
|
|
}
|
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
ngOnInit() {
|
2021-03-19 17:51:02 +01:00
|
|
|
this.title.setTitle('Administrator Dashboard | Classes');
|
|
|
|
this.filterForm = this._fb.group({
|
2021-01-11 10:21:42 +01:00
|
|
|
keyword: [''],
|
2021-03-19 17:51:02 +01:00
|
|
|
type: ['all', Validators.required]
|
|
|
|
});
|
2021-01-11 10:21:42 +01:00
|
|
|
this.subscriptions.push(this.filterForm.get('keyword').valueChanges.subscribe(value => {
|
2021-01-29 11:47:37 +01:00
|
|
|
this.searchText = new RegExp(value, 'i');
|
|
|
|
this.applyFilters();
|
2019-12-18 16:14:21 +01:00
|
|
|
}));
|
2021-01-11 10:21:42 +01:00
|
|
|
this.subscriptions.push(this.filterForm.get('type').valueChanges.subscribe(value => {
|
2021-01-29 11:47:37 +01:00
|
|
|
this.applyFilters();
|
2021-01-11 10:21:42 +01:00
|
|
|
}));
|
|
|
|
this.getDivIds();
|
|
|
|
this.subscriptions.push(this.route.queryParams.subscribe(params => {
|
|
|
|
HelperFunctions.scroll();
|
|
|
|
this.selectedCommunityPid = params['communityId'];
|
|
|
|
this.getPages();
|
|
|
|
}));
|
2021-03-19 17:51:02 +01:00
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
}
|
2021-03-19 17:51:02 +01:00
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
ngOnDestroy(): void {
|
|
|
|
this.subscriptions.forEach(value => {
|
|
|
|
if (value instanceof Subscriber) {
|
|
|
|
value.unsubscribe();
|
|
|
|
} else if (value instanceof Function) {
|
|
|
|
value();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2021-03-19 17:51:02 +01:00
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
getDivIds() {
|
2021-03-19 17:51:02 +01:00
|
|
|
this.showLoading = true;
|
|
|
|
this.subscriptions.push(this._helpContentService.getAllDivIdsFull(this.properties.adminToolsAPIURL).subscribe(
|
|
|
|
divIds => {
|
|
|
|
this.divIds = divIds;
|
|
|
|
this.checkboxes = [];
|
|
|
|
|
|
|
|
let self = this;
|
|
|
|
divIds.forEach(_ => {
|
|
|
|
self.checkboxes.push(<CheckDivId>{divId: _, checked: false});
|
|
|
|
});
|
|
|
|
|
|
|
|
this.showLoading = false;
|
|
|
|
},
|
|
|
|
error => this.handleError('System error retrieving classes', error)));
|
2019-12-18 16:14:21 +01:00
|
|
|
}
|
2021-03-19 17:51:02 +01:00
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
// public showModal():void {
|
|
|
|
// this.modal.showModal();
|
|
|
|
// }
|
2021-03-19 17:51:02 +01:00
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
public toggleCheckBoxes(event) {
|
|
|
|
this.checkboxes.forEach(_ => _.checked = event.target.checked);
|
|
|
|
}
|
2021-03-19 17:51:02 +01:00
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
public applyCheck(flag: boolean) {
|
|
|
|
this.checkboxes.forEach(_ => _.checked = flag);
|
|
|
|
}
|
2021-03-19 17:51:02 +01:00
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
public getSelectedDivIds(): string[] {
|
|
|
|
return this.checkboxes.filter(divId => divId.checked == true).map(checkedDivId => checkedDivId.divId).map(res => res._id);
|
|
|
|
}
|
2021-03-19 17:51:02 +01:00
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
private deleteDivIdsFromArray(ids: string[]): void {
|
|
|
|
for (let id of ids) {
|
2021-01-28 17:33:16 +01:00
|
|
|
let i = this.divIds.findIndex(_ => _._id == id);
|
|
|
|
this.divIds.splice(i, 1);
|
2019-12-18 16:14:21 +01:00
|
|
|
}
|
2021-01-29 11:47:37 +01:00
|
|
|
this.applyFilters();
|
2019-12-18 16:14:21 +01:00
|
|
|
}
|
2021-03-19 17:51:02 +01:00
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
public confirmDeleteDivId(id: string) {
|
|
|
|
this.selectedDivIds = [id];
|
|
|
|
this.confirmModalOpen();
|
|
|
|
}
|
2021-03-19 17:51:02 +01:00
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
public confirmDeleteSelectedDivIds() {
|
|
|
|
this.selectedDivIds = this.getSelectedDivIds();
|
|
|
|
this.confirmModalOpen();
|
|
|
|
}
|
2021-03-19 17:51:02 +01:00
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
private confirmModalOpen() {
|
2021-03-19 17:51:02 +01:00
|
|
|
this.deleteModal.cancelButton = true;
|
|
|
|
this.deleteModal.okButton = true;
|
|
|
|
this.deleteModal.alertTitle = "Delete Confirmation";
|
|
|
|
this.deleteModal.message = "Are you sure you want to delete the selected class(es)?";
|
|
|
|
this.deleteModal.okButtonText = "Yes";
|
|
|
|
this.deleteModal.open();
|
2019-12-18 16:14:21 +01:00
|
|
|
}
|
2021-03-19 17:51:02 +01:00
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
public confirmedDeleteDivIds(data: any) {
|
2021-03-19 17:51:02 +01:00
|
|
|
this.showLoading = true;
|
|
|
|
this.subscriptions.push(this._helpContentService.deleteDivIds(this.selectedDivIds, this.properties.adminToolsAPIURL).subscribe(
|
|
|
|
_ => {
|
|
|
|
this.deleteDivIdsFromArray(this.selectedDivIds);
|
|
|
|
UIkit.notification('Classes have been <b>successfully deleted</b>', {
|
|
|
|
status: 'success',
|
|
|
|
timeout: 6000,
|
|
|
|
pos: 'bottom-right'
|
|
|
|
});
|
|
|
|
this.showLoading = false;
|
|
|
|
},
|
|
|
|
error => this.handleUpdateError('System error deleting the selected classes', error)
|
|
|
|
));
|
2019-12-18 16:14:21 +01:00
|
|
|
}
|
2021-03-19 17:51:02 +01:00
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
public editDivId(i: number) {
|
|
|
|
let divId: DivId = this.checkboxes[i].divId;
|
2021-01-28 17:33:16 +01:00
|
|
|
this.index = this.divIds.findIndex(value => value._id === divId._id);
|
2019-12-18 16:14:21 +01:00
|
|
|
this.formPages = <Page[]>divId.pages;
|
2021-01-28 17:33:16 +01:00
|
|
|
this.pagesCtrl = this._fb.array([], Validators.required);
|
|
|
|
this.classForm = this._fb.group({
|
|
|
|
_id: this._fb.control(divId._id),
|
|
|
|
name: this._fb.control(divId.name, Validators.required),
|
2019-12-18 16:14:21 +01:00
|
|
|
pages: this.pagesCtrl,
|
2021-01-28 17:33:16 +01:00
|
|
|
portalType: this._fb.control(divId.portalType, Validators.required)
|
2019-12-18 16:14:21 +01:00
|
|
|
});
|
2021-01-28 17:33:16 +01:00
|
|
|
this.classForm.get('portalType').disable();
|
2021-03-19 17:51:02 +01:00
|
|
|
for (let i = 0; i < divId.pages.length; i++) {
|
2019-12-18 16:14:21 +01:00
|
|
|
this.pagesCtrl.push(this._fb.control(divId.pages[i]));
|
|
|
|
}
|
2021-01-28 17:33:16 +01:00
|
|
|
this.divIdsModalOpen("Edit class", "Save Changes");
|
2019-12-18 16:14:21 +01:00
|
|
|
}
|
2021-03-19 17:51:02 +01:00
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
public newDivId() {
|
2021-01-28 17:33:16 +01:00
|
|
|
this.pagesCtrl = this._fb.array([], Validators.required);
|
2021-03-19 17:51:02 +01:00
|
|
|
if (this.classForm) {
|
2021-01-28 17:33:16 +01:00
|
|
|
this.classForm.get('portalType').enable();
|
|
|
|
}
|
|
|
|
this.classForm = this._fb.group({
|
|
|
|
_id: this._fb.control(null),
|
|
|
|
name: this._fb.control('', Validators.required),
|
2019-12-18 16:14:21 +01:00
|
|
|
pages: this.pagesCtrl,
|
2021-01-28 17:33:16 +01:00
|
|
|
portalType: this._fb.control('', Validators.required)
|
2019-12-18 16:14:21 +01:00
|
|
|
});
|
2021-01-28 17:33:16 +01:00
|
|
|
this.divIdsModalOpen("Create class", "Create");
|
2019-12-18 16:14:21 +01:00
|
|
|
}
|
2021-03-19 17:51:02 +01:00
|
|
|
|
2021-01-28 17:33:16 +01:00
|
|
|
private divIdsModalOpen(title: string, yesBtn: string) {
|
2021-03-19 17:51:02 +01:00
|
|
|
this.editModal.cancelButton = true;
|
|
|
|
this.editModal.okButton = true;
|
|
|
|
this.editModal.okButtonLeft = false;
|
|
|
|
this.editModal.alertTitle = title;
|
|
|
|
this.editModal.okButtonText = yesBtn;
|
|
|
|
this.editModal.open();
|
2019-12-18 16:14:21 +01:00
|
|
|
}
|
2021-03-19 17:51:02 +01:00
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
public divIdSaveConfirmed(data: any) {
|
2021-02-01 20:17:55 +01:00
|
|
|
this.showLoading = true;
|
2021-03-19 17:51:02 +01:00
|
|
|
if (!this.classForm.value._id) {
|
|
|
|
this.subscriptions.push(this._helpContentService.saveDivId(<DivId>this.classForm.value, this.properties.adminToolsAPIURL).subscribe(
|
|
|
|
divId => {
|
|
|
|
this.divIdSavedSuccessfully(divId);
|
|
|
|
UIkit.notification('Class <b>' + divId.name + '</b> has been <b>successfully created</b>', {
|
|
|
|
status: 'success',
|
|
|
|
timeout: 6000,
|
|
|
|
pos: 'bottom-right'
|
|
|
|
});
|
|
|
|
},
|
|
|
|
error => this.handleUpdateError("System error creating class", error)
|
|
|
|
));
|
2019-12-18 16:14:21 +01:00
|
|
|
} else {
|
2021-03-19 17:51:02 +01:00
|
|
|
this.classForm.get('portalType').enable();
|
|
|
|
this.subscriptions.push(this._helpContentService.updateDivId(<DivId>this.classForm.value, this.properties.adminToolsAPIURL).subscribe(
|
|
|
|
divId => {
|
|
|
|
this.divIdUpdatedSuccessfully(divId);
|
|
|
|
UIkit.notification('Class <b>' + divId.name + '</b> has been <b>successfully updated</b>', {
|
|
|
|
status: 'success',
|
|
|
|
timeout: 6000,
|
|
|
|
pos: 'bottom-right'
|
|
|
|
});
|
|
|
|
},
|
|
|
|
error => this.handleUpdateError("System error updating class", error)
|
|
|
|
));
|
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
}
|
|
|
|
}
|
2021-03-19 17:51:02 +01:00
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
public divIdSavedSuccessfully(divId: DivId) {
|
2021-01-28 17:33:16 +01:00
|
|
|
this.divIds.push(divId);
|
2021-01-29 11:47:37 +01:00
|
|
|
this.applyFilters();
|
2019-12-18 16:14:21 +01:00
|
|
|
this.applyCheck(false);
|
2021-02-01 20:17:55 +01:00
|
|
|
this.showLoading = false;
|
2019-12-18 16:14:21 +01:00
|
|
|
}
|
2021-03-19 17:51:02 +01:00
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
public divIdUpdatedSuccessfully(divId: DivId) {
|
2021-01-28 17:33:16 +01:00
|
|
|
this.divIds[this.index] = divId;
|
2021-01-29 11:47:37 +01:00
|
|
|
this.applyFilters();
|
2019-12-18 16:14:21 +01:00
|
|
|
this.applyCheck(false);
|
2021-02-01 20:17:55 +01:00
|
|
|
this.showLoading = false;
|
2019-12-18 16:14:21 +01:00
|
|
|
}
|
2021-03-19 17:51:02 +01:00
|
|
|
|
2021-01-29 11:47:37 +01:00
|
|
|
public applyFilters() {
|
2021-01-11 10:21:42 +01:00
|
|
|
this.checkboxes = [];
|
|
|
|
this.divIds.filter(item => this.filterByType(item)).forEach(
|
2021-01-29 11:47:37 +01:00
|
|
|
item => this.checkboxes.push(<CheckDivId>{divId: item, checked: false})
|
2021-01-11 10:21:42 +01:00
|
|
|
);
|
2021-01-29 11:47:37 +01:00
|
|
|
this.checkboxes = this.checkboxes.filter(item => this.filterDivIds(item.divId));
|
2021-01-11 10:21:42 +01:00
|
|
|
}
|
2021-03-19 17:51:02 +01:00
|
|
|
|
2021-01-11 10:21:42 +01:00
|
|
|
public filterByType(divId: DivId): boolean {
|
|
|
|
let type = this.filterForm.get("type").value;
|
2021-03-19 17:51:02 +01:00
|
|
|
return type == "all" || (type == divId.portalType);
|
2021-01-11 10:21:42 +01:00
|
|
|
}
|
2021-03-19 17:51:02 +01:00
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
public filterDivIds(divId: DivId): boolean {
|
2020-09-24 13:18:24 +02:00
|
|
|
let textFlag = this.searchText.toString() == '' || (divId.name + ' ' + divId.portalType).match(this.searchText) != null;
|
2019-12-18 16:14:21 +01:00
|
|
|
return textFlag;
|
|
|
|
}
|
2021-03-19 17:51:02 +01:00
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
handleUpdateError(message: string, error) {
|
|
|
|
if (error == null) {
|
|
|
|
// this.formComponent.reset();
|
|
|
|
} else {
|
2021-03-18 15:11:41 +01:00
|
|
|
UIkit.notification(message, {
|
|
|
|
status: 'danger',
|
|
|
|
timeout: 6000,
|
|
|
|
pos: 'bottom-right'
|
|
|
|
});
|
2019-12-18 16:14:21 +01:00
|
|
|
console.log('Server responded: ' + error);
|
|
|
|
}
|
|
|
|
this.showLoading = false;
|
|
|
|
}
|
2021-03-19 17:51:02 +01:00
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
handleError(message: string, error) {
|
2021-03-18 15:11:41 +01:00
|
|
|
UIkit.notification(message, {
|
|
|
|
status: 'danger',
|
|
|
|
timeout: 6000,
|
|
|
|
pos: 'bottom-right'
|
|
|
|
});
|
2019-12-18 16:14:21 +01:00
|
|
|
console.log('Server responded: ' + error);
|
|
|
|
this.showLoading = false;
|
|
|
|
}
|
2021-03-19 17:51:02 +01:00
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
getPages() {
|
|
|
|
this.showLoading = true;
|
2021-01-11 10:21:42 +01:00
|
|
|
this.subscriptions.push(this._helpContentService.getAllPages(this.properties.adminToolsAPIURL).subscribe(
|
2019-12-18 16:14:21 +01:00
|
|
|
pages => {
|
2021-01-28 17:33:16 +01:00
|
|
|
this.allPages = [];
|
|
|
|
pages.forEach(page => {
|
|
|
|
this.allPages.push({
|
2021-05-25 14:29:07 +02:00
|
|
|
label: page.name + " [" + page.portalType + "]",
|
2021-01-28 17:33:16 +01:00
|
|
|
value: page
|
|
|
|
});
|
|
|
|
});
|
2019-12-18 16:14:21 +01:00
|
|
|
this.showLoading = false;
|
|
|
|
},
|
|
|
|
error => this.handleError('System error retrieving pages', error)
|
2021-01-11 10:21:42 +01:00
|
|
|
));
|
2019-12-18 16:14:21 +01:00
|
|
|
}
|
2021-02-02 13:29:18 +01:00
|
|
|
|
|
|
|
public onSearchClose() {
|
|
|
|
this.selectedKeyword = this.filterForm.get('keyword').value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public reset() {
|
|
|
|
this.selectedKeyword = null;
|
|
|
|
this.searchInputComponent.reset()
|
|
|
|
}
|
2019-12-18 16:14:21 +01:00
|
|
|
}
|