2021-12-09 14:52:39 +01:00
|
|
|
import {Component, ElementRef, OnInit, ViewChild} from '@angular/core';
|
|
|
|
import {ActivatedRoute, Router} from '@angular/router';
|
|
|
|
import {HelpContentService} from '../../services/help-content.service';
|
|
|
|
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
|
|
|
|
import {Portal} from '../../utils/entities/adminTool/portal';
|
|
|
|
import {EnvProperties} from '../../utils/properties/env-properties';
|
|
|
|
import {Session} from '../../login/utils/helper.class';
|
|
|
|
import {UserManagementService} from '../../services/user-management.service';
|
2022-01-11 11:05:40 +01:00
|
|
|
import {Subscriber, Subscription} from "rxjs";
|
2021-12-09 14:52:39 +01:00
|
|
|
import {properties} from "../../../../environments/environment";
|
|
|
|
import {StringUtils} from "../../utils/string-utils.class";
|
|
|
|
import {Title} from "@angular/platform-browser";
|
|
|
|
import {AlertModal} from '../../utils/modal/alert';
|
2022-01-11 11:05:40 +01:00
|
|
|
import {MenuItem} from '../../sharedComponents/menu';
|
2021-12-17 08:22:07 +01:00
|
|
|
import {SearchInputComponent} from '../../sharedComponents/search-input/search-input.component';
|
2022-01-11 11:05:40 +01:00
|
|
|
import {Page} from '../../utils/entities/adminTool/page';
|
|
|
|
import {Option} from '../../sharedComponents/input/input.component';
|
2021-12-09 14:52:39 +01:00
|
|
|
|
2022-01-05 15:41:49 +01:00
|
|
|
declare var UIkit;
|
|
|
|
|
2021-12-09 14:52:39 +01:00
|
|
|
@Component({
|
|
|
|
selector: 'menuSelector',
|
|
|
|
templateUrl: './menu.component.html',
|
2022-01-11 11:05:40 +01:00
|
|
|
styles: [`
|
|
|
|
.z-index-2 {
|
|
|
|
z-index: 2;
|
|
|
|
}
|
|
|
|
`]
|
2021-12-09 14:52:39 +01:00
|
|
|
})
|
|
|
|
export class MenuComponent implements OnInit {
|
|
|
|
|
2021-12-17 08:22:07 +01:00
|
|
|
@ViewChild('searchInputComponent') searchInputComponent: SearchInputComponent;
|
2021-12-09 14:52:39 +01:00
|
|
|
@ViewChild('editModal') editModal: AlertModal;
|
|
|
|
@ViewChild('deleteModal') deleteModal: AlertModal;
|
2021-12-23 15:12:51 +01:00
|
|
|
|
2022-01-11 11:05:40 +01:00
|
|
|
public activeRootMenuId: string;
|
|
|
|
public activeRootMenu: MenuItem;
|
|
|
|
public childrenMenuItems: MenuItem[] = [];
|
2022-01-05 15:41:49 +01:00
|
|
|
private index: number;
|
2021-12-23 15:12:51 +01:00
|
|
|
|
2021-12-17 08:22:07 +01:00
|
|
|
public menuItemForm: FormGroup;
|
2022-01-11 11:05:40 +01:00
|
|
|
public pageForm: FormGroup;
|
2021-12-23 15:12:51 +01:00
|
|
|
public rootMenuItems = [];
|
|
|
|
public menuItems = [];
|
2021-12-17 14:36:30 +01:00
|
|
|
public allPages = [];
|
2021-12-09 14:52:39 +01:00
|
|
|
|
2022-01-05 15:41:49 +01:00
|
|
|
public selectedMenuItem: string;
|
|
|
|
public isChild: boolean = false;
|
2021-12-09 14:52:39 +01:00
|
|
|
|
|
|
|
public communities: Portal[] = [];
|
|
|
|
public portal: string;
|
|
|
|
|
2021-12-23 08:22:07 +01:00
|
|
|
public newPageWindowOpen: boolean = false;
|
2021-12-09 14:52:39 +01:00
|
|
|
public showLoading = true;
|
|
|
|
public isPortalAdministrator = null;
|
|
|
|
public filterForm: FormGroup;
|
2022-01-11 11:05:40 +01:00
|
|
|
public typeOptions: Option[] = [
|
2021-12-17 08:22:07 +01:00
|
|
|
{label: 'Internal Link', value: 'internal'},
|
2022-01-11 11:05:40 +01:00
|
|
|
{label: 'External Link', value: 'external'},
|
|
|
|
]
|
|
|
|
public rootOnlyTypeOptions: Option[] = [
|
|
|
|
{label: 'No Action', value: 'noAction'}
|
2021-12-17 08:22:07 +01:00
|
|
|
]
|
2022-01-05 15:41:49 +01:00
|
|
|
|
|
|
|
public keyword: string = '';
|
|
|
|
public selectedKeyword: string = '';
|
|
|
|
private searchText: string = '';
|
|
|
|
|
|
|
|
public properties: EnvProperties = properties;
|
2021-12-09 14:52:39 +01:00
|
|
|
private subscriptions: any[] = [];
|
2022-01-11 11:05:40 +01:00
|
|
|
private typeSub: Subscription = null;
|
2021-12-09 14:52:39 +01:00
|
|
|
|
|
|
|
constructor(private element: ElementRef, private route: ActivatedRoute,
|
|
|
|
private _router: Router, private title: Title,
|
|
|
|
private _helpContentService: HelpContentService,
|
|
|
|
private userManagementService: UserManagementService, private _fb: FormBuilder) {
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
this.filterForm = this._fb.group({
|
|
|
|
keyword: [''],
|
|
|
|
});
|
2022-01-05 15:41:49 +01:00
|
|
|
this.subscriptions.push(this.filterForm.get('keyword').valueChanges.subscribe(value => {
|
|
|
|
this.searchText = value.toLowerCase();
|
|
|
|
this.applyFilters();
|
|
|
|
}));
|
2021-12-09 14:52:39 +01:00
|
|
|
this.userManagementService.getUserInfo().subscribe(user => {
|
|
|
|
this.portal = (this.route.snapshot.data.portal) ? this.route.snapshot.data.portal : this.route.snapshot.params[this.route.snapshot.data.param];
|
|
|
|
if (this.route.snapshot.data.portal) {
|
|
|
|
this.title.setTitle(StringUtils.capitalize(this.portal) + ' | Menu');
|
|
|
|
} else if (this.route.snapshot.params[this.route.snapshot.data.param]) {
|
|
|
|
this.title.setTitle(this.portal.toUpperCase() + ' | Menu');
|
|
|
|
} else {
|
|
|
|
this.title.setTitle('Administrator Dashboard | Menu');
|
|
|
|
}
|
|
|
|
this.isPortalAdministrator = Session.isPortalAdministrator(user) && !this.portal;
|
|
|
|
});
|
|
|
|
this.showLoading = false;
|
2021-12-23 08:22:07 +01:00
|
|
|
this.getMenuItems();
|
|
|
|
this.getPages();
|
2021-12-09 14:52:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ngOnDestroy(): void {
|
|
|
|
this.subscriptions.forEach(value => {
|
|
|
|
if (value instanceof Subscriber) {
|
|
|
|
value.unsubscribe();
|
|
|
|
} else if (value instanceof Function) {
|
|
|
|
value();
|
|
|
|
}
|
|
|
|
});
|
2022-01-11 11:05:40 +01:00
|
|
|
this.destroyTypeSubscription();
|
2021-12-09 14:52:39 +01:00
|
|
|
}
|
2021-12-17 08:22:07 +01:00
|
|
|
|
2022-01-11 11:05:40 +01:00
|
|
|
destroyTypeSubscription() {
|
|
|
|
if(this.typeSub instanceof Subscriber) {
|
|
|
|
this.typeSub.unsubscribe();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-23 08:22:07 +01:00
|
|
|
getMenuItems() {
|
|
|
|
this.subscriptions.push(
|
|
|
|
this._helpContentService.getMenuItems(this.portal).subscribe(
|
|
|
|
data => {
|
2021-12-23 15:12:51 +01:00
|
|
|
this.rootMenuItems = data;
|
|
|
|
if(data && data.length > 0) {
|
2022-01-11 11:05:40 +01:00
|
|
|
// this.activeRootMenuId = data[0]['_id'];
|
|
|
|
this.changeActiveRootMenuItem(data[0]);
|
2021-12-23 15:12:51 +01:00
|
|
|
}
|
2021-12-23 08:22:07 +01:00
|
|
|
},
|
|
|
|
err => console.error("Server error fetching menu items: ", err)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-01-11 11:05:40 +01:00
|
|
|
getTypeOptions() {
|
|
|
|
if(this.isChild) {
|
|
|
|
return this.typeOptions;
|
|
|
|
} else {
|
|
|
|
return this.typeOptions.concat(this.rootOnlyTypeOptions);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-23 15:12:51 +01:00
|
|
|
getActiveRootItem(id: string): MenuItem {
|
|
|
|
return this.rootMenuItems.find(element => element['_id'] == id);
|
|
|
|
}
|
|
|
|
|
2022-01-11 11:05:40 +01:00
|
|
|
changeActiveRootMenuItem(item: MenuItem) {
|
|
|
|
this.activeRootMenuId = item['_id']
|
|
|
|
this.activeRootMenu = item;
|
|
|
|
this.childrenMenuItems = item.items;
|
|
|
|
this.applyFilters();
|
|
|
|
}
|
|
|
|
|
2021-12-23 08:22:07 +01:00
|
|
|
getPages() {
|
|
|
|
this.subscriptions.push(
|
|
|
|
this._helpContentService.getAllPages(this.properties.adminToolsAPIURL,this.portal).subscribe(
|
|
|
|
data => {
|
|
|
|
let pages = data;
|
|
|
|
this.allPages = [];
|
|
|
|
for(let i = 0; i < pages.length; i++) {
|
|
|
|
if(pages[i] && pages[i].name && pages[i].route) {
|
|
|
|
this.allPages.push({value: pages[i].route, label: pages[i].name});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
err => console.error("Server error fetching pages: ", err)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-01-05 15:41:49 +01:00
|
|
|
public newPageWindow() {
|
|
|
|
this.newPageWindowOpen = !this.newPageWindowOpen;
|
2022-01-11 11:05:40 +01:00
|
|
|
this.pageForm = this._fb.group({
|
|
|
|
_id: this._fb.control(null),
|
|
|
|
route: this._fb.control('', Validators.required),
|
|
|
|
name: this._fb.control('', Validators.required),
|
|
|
|
isEnabled: this._fb.control(true),
|
|
|
|
portalType: this._fb.control(this.properties.adminToolsPortalType, Validators.required),
|
|
|
|
portalPid: this._fb.control(this.portal),
|
|
|
|
top: this._fb.control(true),
|
|
|
|
bottom: this._fb.control(false),
|
|
|
|
left: this._fb.control(false),
|
|
|
|
right: this._fb.control(false),
|
|
|
|
type: this._fb.control('html', Validators.required),
|
|
|
|
entities: this._fb.control([''])
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public createPage() {
|
|
|
|
if(!this.pageForm.value.route.startsWith('/')) {
|
|
|
|
this.pageForm.value.route = '/'.concat(this.pageForm.value.route);
|
|
|
|
}
|
|
|
|
this.showLoading = true;
|
|
|
|
this.subscriptions.push(
|
|
|
|
this._helpContentService.savePage(<Page>this.pageForm.value, this.properties.adminToolsAPIURL).subscribe(
|
|
|
|
page => {
|
|
|
|
this.allPages.push({value: page.route, label: page.name});
|
|
|
|
UIkit.notification('Page <b>' + page.name + '</b> has been <b>successfully created</b>', {
|
|
|
|
status: 'success',
|
|
|
|
timeout: 6000,
|
|
|
|
pos: 'bottom-right'
|
|
|
|
});
|
|
|
|
this.newPageWindowOpen = !this.newPageWindowOpen;
|
|
|
|
this.menuItemForm.get('route').setValue(page.route);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
addValidatorForUrlOrRoute() {
|
|
|
|
this.destroyTypeSubscription();
|
|
|
|
this.typeSub = this.menuItemForm.get('type').valueChanges.subscribe(value => {
|
|
|
|
setTimeout(() => {
|
|
|
|
// console.log(value);
|
|
|
|
if(value === "internal") {
|
|
|
|
this.menuItemForm.controls['route'].setValidators([Validators.required]);
|
|
|
|
this.menuItemForm.controls['route'].updateValueAndValidity();
|
|
|
|
} else if(value === "external") {
|
|
|
|
this.menuItemForm.controls['url'].setValidators([Validators.required]);
|
|
|
|
this.menuItemForm.controls['url'].updateValueAndValidity();
|
|
|
|
}
|
|
|
|
}, 0);
|
|
|
|
});
|
2022-01-05 15:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public newMenuItem(isChild: boolean = false) {
|
2021-12-17 14:36:30 +01:00
|
|
|
this.menuItemForm = this._fb.group({
|
2022-01-05 15:41:49 +01:00
|
|
|
_id: this._fb.control(""),
|
2021-12-17 14:36:30 +01:00
|
|
|
title: this._fb.control("",Validators.required),
|
2022-01-11 11:05:40 +01:00
|
|
|
type: this._fb.control("",Validators.required),
|
2021-12-17 14:36:30 +01:00
|
|
|
route: this._fb.control(""),
|
|
|
|
url: this._fb.control(""),
|
2021-12-23 15:12:51 +01:00
|
|
|
isEnabled: this._fb.control(""),
|
2022-01-11 11:05:40 +01:00
|
|
|
parentItemId: this._fb.control(isChild ? this.activeRootMenuId : null)
|
2021-12-17 14:36:30 +01:00
|
|
|
});
|
2022-01-05 15:41:49 +01:00
|
|
|
this.isChild = isChild;
|
2022-01-11 11:05:40 +01:00
|
|
|
this.addValidatorForUrlOrRoute();
|
2022-01-05 15:41:49 +01:00
|
|
|
this.menuItemsModalOpen('Create Menu Item', 'Create');
|
2021-12-17 14:36:30 +01:00
|
|
|
}
|
|
|
|
|
2022-01-05 15:41:49 +01:00
|
|
|
public editMenuItem(menuItem: MenuItem, isChild: boolean = false) {
|
2021-12-17 14:36:30 +01:00
|
|
|
this.menuItemForm = this._fb.group({
|
2022-01-05 15:41:49 +01:00
|
|
|
_id: this._fb.control(menuItem['_id']),
|
|
|
|
title: this._fb.control(menuItem.title,Validators.required),
|
2022-01-11 11:05:40 +01:00
|
|
|
type: this._fb.control(menuItem['type'],Validators.required),
|
2022-01-05 15:41:49 +01:00
|
|
|
route: this._fb.control(menuItem.route),
|
|
|
|
url: this._fb.control(menuItem.url),
|
|
|
|
parentItemId: this._fb.control(menuItem['parentItemId'])
|
2021-12-17 14:36:30 +01:00
|
|
|
});
|
2022-01-05 15:41:49 +01:00
|
|
|
this.isChild = isChild;
|
|
|
|
if(this.isChild) {
|
2022-01-11 11:05:40 +01:00
|
|
|
this.index = this.getActiveRootItem(this.activeRootMenuId).items.findIndex(value => value._id === menuItem['_id']);
|
2022-01-05 15:41:49 +01:00
|
|
|
} else {
|
|
|
|
this.index = this.rootMenuItems.findIndex(value => value._id === menuItem['_id']);
|
|
|
|
}
|
2022-01-11 11:05:40 +01:00
|
|
|
this.addValidatorForUrlOrRoute();
|
2022-01-05 15:41:49 +01:00
|
|
|
this.menuItemsModalOpen('Edit Menu Item', 'Save Changes');
|
2021-12-17 14:36:30 +01:00
|
|
|
}
|
|
|
|
|
2022-01-05 15:41:49 +01:00
|
|
|
public confirmDeleteMenuItem(id: string, isChild: boolean = false) {
|
|
|
|
this.selectedMenuItem = id;
|
|
|
|
this.isChild = isChild;
|
|
|
|
this.confirmModalOpen();
|
2021-12-17 08:22:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private confirmModalOpen() {
|
|
|
|
this.deleteModal.cancelButton = true;
|
|
|
|
this.deleteModal.okButton = true;
|
|
|
|
this.deleteModal.alertTitle = 'Delete Confirmation';
|
2022-01-05 15:41:49 +01:00
|
|
|
this.deleteModal.message = 'Are you sure you want to delete this menu item?';
|
2021-12-17 08:22:07 +01:00
|
|
|
this.deleteModal.okButtonText = 'Yes';
|
|
|
|
this.deleteModal.open();
|
|
|
|
}
|
|
|
|
|
2022-01-05 15:41:49 +01:00
|
|
|
public confirmedDeleteMenuItem(data: any, isChild: boolean = false) {
|
|
|
|
this.showLoading = true;
|
|
|
|
this.subscriptions.push(
|
|
|
|
this._helpContentService.deleteMenuItem(this.selectedMenuItem, this.portal).subscribe(
|
|
|
|
_ => {
|
|
|
|
this.deleteMenuItemFromArray(this.selectedMenuItem, this.isChild);
|
|
|
|
UIkit.notification('Menu item have been <b>successfully deleted</b>', {
|
|
|
|
status: 'success',
|
|
|
|
timeout: 6000,
|
|
|
|
pos: 'bottom-right'
|
|
|
|
});
|
|
|
|
this.showLoading = false;
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
2021-12-09 14:52:39 +01:00
|
|
|
}
|
|
|
|
|
2022-01-05 15:41:49 +01:00
|
|
|
private deleteMenuItemFromArray(id: string, isChild: boolean = false) {
|
|
|
|
if(isChild) {
|
2022-01-11 11:05:40 +01:00
|
|
|
let i = this.getActiveRootItem(this.activeRootMenuId).items.findIndex(_ => _._id == id);
|
|
|
|
this.getActiveRootItem(this.activeRootMenuId).items.splice(i, 1);
|
2022-01-05 15:41:49 +01:00
|
|
|
} else {
|
|
|
|
let i = this.rootMenuItems.findIndex(_ => _._id == id);
|
|
|
|
this.rootMenuItems.splice(i, 1);
|
2022-01-11 11:05:40 +01:00
|
|
|
this.changeActiveRootMenuItem(this.rootMenuItems[0]);
|
2022-01-05 15:41:49 +01:00
|
|
|
}
|
2022-01-11 11:05:40 +01:00
|
|
|
this.applyFilters();
|
2021-12-23 08:22:07 +01:00
|
|
|
}
|
|
|
|
|
2021-12-09 14:52:39 +01:00
|
|
|
private menuItemsModalOpen(title: string, yesBtn: string) {
|
|
|
|
this.editModal.cancelButton = true;
|
|
|
|
this.editModal.okButton = true;
|
|
|
|
this.editModal.okButtonLeft = false;
|
|
|
|
this.editModal.alertTitle = title;
|
|
|
|
this.editModal.okButtonText = yesBtn;
|
|
|
|
this.editModal.open();
|
|
|
|
}
|
2021-12-17 08:22:07 +01:00
|
|
|
|
2022-01-05 15:41:49 +01:00
|
|
|
public menuItemSaveConfirmed(data: any) {
|
2022-01-11 11:05:40 +01:00
|
|
|
this.destroyTypeSubscription();
|
2022-01-05 15:41:49 +01:00
|
|
|
this.showLoading = true;
|
|
|
|
if(!this.menuItemForm.value._id) {
|
|
|
|
this.subscriptions.push(
|
|
|
|
this._helpContentService.saveMenuItem(<MenuItem>this.menuItemForm.value, this.portal).subscribe(
|
|
|
|
menuItem => {
|
|
|
|
this.menuItemSavedSuccessfully(menuItem, true);
|
|
|
|
UIkit.notification('Menu item <b>' + menuItem.title + '</b> has been <b>successfully created</b>', {
|
|
|
|
status: 'success',
|
|
|
|
timeout: 6000,
|
|
|
|
pos: 'bottom-right'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
this.subscriptions.push(
|
|
|
|
this._helpContentService.updateMenuItem(<MenuItem>this.menuItemForm.value, this.portal).subscribe(
|
|
|
|
menuItem => {
|
|
|
|
this.menuItemSavedSuccessfully(menuItem, false);
|
|
|
|
UIkit.notification('Menu item <b>' + menuItem.title + '</b> has been <b>successfully updated</b>', {
|
|
|
|
status: 'success',
|
|
|
|
timeout: 6000,
|
|
|
|
pos: 'bottom-right'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public menuItemSavedSuccessfully(menuItem: MenuItem, isNew: boolean) {
|
|
|
|
if(isNew) {
|
|
|
|
if(this.isChild) {
|
2022-01-11 11:05:40 +01:00
|
|
|
this.getActiveRootItem(this.activeRootMenuId).items.push(menuItem);
|
2022-01-05 15:41:49 +01:00
|
|
|
} else {
|
|
|
|
this.rootMenuItems.push(menuItem);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(this.isChild) {
|
2022-01-11 11:05:40 +01:00
|
|
|
this.getActiveRootItem(this.activeRootMenuId).items[this.index] = menuItem;
|
2022-01-05 15:41:49 +01:00
|
|
|
} else {
|
|
|
|
this.rootMenuItems[this.index] = menuItem;
|
|
|
|
}
|
|
|
|
}
|
2022-01-11 11:05:40 +01:00
|
|
|
this.applyFilters();
|
2022-01-05 15:41:49 +01:00
|
|
|
this.showLoading = false;
|
|
|
|
}
|
2021-12-17 08:22:07 +01:00
|
|
|
|
2022-01-05 15:41:49 +01:00
|
|
|
public applyFilters() {
|
2022-01-11 11:05:40 +01:00
|
|
|
this.childrenMenuItems = this.activeRootMenu.items.filter(item => item.title.toLowerCase().includes(this.searchText));
|
2021-12-17 08:22:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public onSearchClose() {
|
2021-12-17 14:36:30 +01:00
|
|
|
this.selectedKeyword = this.filterForm.get('keyword').value;
|
2021-12-17 08:22:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public reset() {
|
|
|
|
this.selectedKeyword = null;
|
|
|
|
this.searchInputComponent.reset();
|
|
|
|
}
|
2021-12-09 14:52:39 +01:00
|
|
|
}
|