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'; import {Subscriber} from "rxjs"; import {properties} from "../../../../environments/environment"; import {StringUtils} from "../../utils/string-utils.class"; import {Title} from "@angular/platform-browser"; import {AlertModal} from '../../utils/modal/alert'; import {CheckMenuItem, MenuItem} from '../../sharedComponents/menu'; import {SearchInputComponent} from '../../sharedComponents/search-input/search-input.component'; declare var UIkit; @Component({ selector: 'menuSelector', templateUrl: './menu.component.html', }) export class MenuComponent implements OnInit { @ViewChild('searchInputComponent') searchInputComponent: SearchInputComponent; @ViewChild('editModal') editModal: AlertModal; @ViewChild('deleteModal') deleteModal: AlertModal; public activeRootMenu: string; private index: number; public menuItemForm: FormGroup; public rootMenuItems = []; public menuItems = []; public allPages = []; public selectedMenuItem: string; public isChild: boolean = false; public communities: Portal[] = []; public portal: string; public newPageWindowOpen: boolean = false; public showLoading = true; public isPortalAdministrator = null; public filterForm: FormGroup; public typeOptions = [ {label: 'Internal Link', value: 'internal'}, {label: 'External Link', value: 'external'} ] public keyword: string = ''; public selectedKeyword: string = ''; private searchText: string = ''; public properties: EnvProperties = properties; private subscriptions: any[] = []; 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: [''], }); this.subscriptions.push(this.filterForm.get('keyword').valueChanges.subscribe(value => { this.searchText = value.toLowerCase(); this.applyFilters(); })); 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; this.getMenuItems(); this.getPages(); } ngOnDestroy(): void { this.subscriptions.forEach(value => { if (value instanceof Subscriber) { value.unsubscribe(); } else if (value instanceof Function) { value(); } }); } getMenuItems() { this.subscriptions.push( this._helpContentService.getMenuItems(this.portal).subscribe( data => { this.rootMenuItems = data; if(data && data.length > 0) { this.activeRootMenu = data[0]['_id']; } }, err => console.error("Server error fetching menu items: ", err) ) ); } getActiveRootItem(id: string): MenuItem { return this.rootMenuItems.find(element => element['_id'] == id); } 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) ) ); } public newPageWindow() { this.newPageWindowOpen = !this.newPageWindowOpen; } public newMenuItem(isChild: boolean = false) { this.menuItemForm = this._fb.group({ _id: this._fb.control(""), title: this._fb.control("",Validators.required), type: this._fb.control(""), route: this._fb.control(""), url: this._fb.control(""), isEnabled: this._fb.control(""), parentItemId: this._fb.control(isChild ? this.activeRootMenu : null) }); this.isChild = isChild; this.menuItemsModalOpen('Create Menu Item', 'Create'); } public editMenuItem(menuItem: MenuItem, isChild: boolean = false) { this.menuItemForm = this._fb.group({ _id: this._fb.control(menuItem['_id']), title: this._fb.control(menuItem.title,Validators.required), type: this._fb.control(menuItem['type']), route: this._fb.control(menuItem.route), url: this._fb.control(menuItem.url), parentItemId: this._fb.control(menuItem['parentItemId']) }); this.isChild = isChild; if(this.isChild) { this.index = this.getActiveRootItem(this.activeRootMenu).items.findIndex(value => value._id === menuItem['_id']); } else { this.index = this.rootMenuItems.findIndex(value => value._id === menuItem['_id']); } this.menuItemsModalOpen('Edit Menu Item', 'Save Changes'); } public confirmDeleteMenuItem(id: string, isChild: boolean = false) { this.selectedMenuItem = id; this.isChild = isChild; this.confirmModalOpen(); } private confirmModalOpen() { this.deleteModal.cancelButton = true; this.deleteModal.okButton = true; this.deleteModal.alertTitle = 'Delete Confirmation'; this.deleteModal.message = 'Are you sure you want to delete this menu item?'; this.deleteModal.okButtonText = 'Yes'; this.deleteModal.open(); } 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 successfully deleted', { status: 'success', timeout: 6000, pos: 'bottom-right' }); this.showLoading = false; } ) ) } private deleteMenuItemFromArray(id: string, isChild: boolean = false) { if(isChild) { let i = this.getActiveRootItem(this.activeRootMenu).items.findIndex(_ => _._id == id); this.getActiveRootItem(this.activeRootMenu).items.splice(i, 1); } else { let i = this.rootMenuItems.findIndex(_ => _._id == id); this.rootMenuItems.splice(i, 1); } } 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(); } public menuItemSaveConfirmed(data: any) { this.showLoading = true; if(!this.menuItemForm.value._id) { this.subscriptions.push( this._helpContentService.saveMenuItem(this.menuItemForm.value, this.portal).subscribe( menuItem => { this.menuItemSavedSuccessfully(menuItem, true); UIkit.notification('Menu item ' + menuItem.title + ' has been successfully created', { status: 'success', timeout: 6000, pos: 'bottom-right' }); } ) ) } else { this.subscriptions.push( this._helpContentService.updateMenuItem(this.menuItemForm.value, this.portal).subscribe( menuItem => { this.menuItemSavedSuccessfully(menuItem, false); UIkit.notification('Menu item ' + menuItem.title + ' has been successfully updated', { status: 'success', timeout: 6000, pos: 'bottom-right' }); } ) ) } } public menuItemSavedSuccessfully(menuItem: MenuItem, isNew: boolean) { if(isNew) { if(this.isChild) { this.getActiveRootItem(this.activeRootMenu).items.push(menuItem); } else { this.rootMenuItems.push(menuItem); } } else { if(this.isChild) { this.getActiveRootItem(this.activeRootMenu).items[this.index] = menuItem; } else { this.rootMenuItems[this.index] = menuItem; } } this.showLoading = false; } public applyFilters() { console.log(this.searchText); this.getActiveRootItem(this.activeRootMenu).items = this.getActiveRootItem(this.activeRootMenu).items.filter(item => item.title.toLowerCase().includes(this.searchText)); } public onSearchClose() { this.selectedKeyword = this.filterForm.get('keyword').value; } public reset() { this.selectedKeyword = null; this.searchInputComponent.reset(); } }