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'; @Component({ selector: 'menuSelector', templateUrl: './menu.component.html', }) export class MenuComponent implements OnInit { @ViewChild('searchInputComponent') searchInputComponent: SearchInputComponent; @ViewChild('editModal') editModal: AlertModal; @ViewChild('deleteModal') deleteModal: AlertModal; private selectedMenuItems: string[] = []; public checkboxes: CheckMenuItem[] = []; // public menuItems: MenuItem[] = []; // public rootMenuForm: FormGroup; public activeRootMenu: string; public menuItemForm: FormGroup; public rootMenuItems = []; public menuItems = []; public allPages = []; public keyword = ''; public communities: Portal[] = []; public portal: string; public properties: EnvProperties = properties; 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 selectedKeyword: string; 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: [''], status: ['resources', Validators.required] }); 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 => { console.log(data); this.rootMenuItems = data; if(data && data.length > 0) { this.activeRootMenu = data[0]['_id']; console.log(this.activeRootMenu); } }, 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 newRootMenu() { 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(""), }); this.menuItemsModalOpen('Create Root Menu', 'Save Changes'); } public editRootMenu() { this.menuItemForm = this._fb.group({ id: this._fb.control("id"), title: this._fb.control("Resources",Validators.required), type: this._fb.control("",Validators.required), route: this._fb.control("noNeed"), url: this._fb.control("noNeed"), isEnabled: this._fb.control("enabled",Validators.required), }); this.menuItemsModalOpen('Edit Root Menu', 'Save Changes'); } public deleteRootMenu() { console.log('Delete root menu'); } public getSelectedMenuItems(): string[] { return this.checkboxes.filter(menuItem => menuItem.checked == true).map(checkedMenuItem => checkedMenuItem.menuItem).map(res => res.id); } public confirmDeleteSelectedMenuItems() { this.selectedMenuItems = this.getSelectedMenuItems(); 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 the selected menu item(s)?'; this.deleteModal.okButtonText = 'Yes'; this.deleteModal.open(); } public newMenuItem() { this.menuItemForm = this._fb.group({ id: this._fb.control(""), title: this._fb.control("",Validators.required), type: this._fb.control("",Validators.required), route: this._fb.control(""), url: this._fb.control(""), isEnabled: this._fb.control("",Validators.required), }); this.menuItemsModalOpen('Create Menu Item', 'Save Changes'); } public editMenuItem() { this.menuItemForm = this._fb.group({ id: this._fb.control("id"), title: this._fb.control("HardcodedName",Validators.required), type: this._fb.control("internal",Validators.required), route: this._fb.control("routeAlex"), url: this._fb.control("urlAlex"), isEnabled: this._fb.control("enabled",Validators.required), }); this.menuItemsModalOpen('Edit Menu Item', 'Save Changes'); } public deleteMenuItem() { console.log('Delete menu item'); } public newPageWindow() { this.newPageWindowOpen = !this.newPageWindowOpen; } 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 toggleMenuItems(status: boolean, ids: string[]) { } public onSearchClose() { this.selectedKeyword = this.filterForm.get('keyword').value; } public reset() { this.selectedKeyword = null; this.searchInputComponent.reset(); } selectAll() { let checked = this.getSelectedMenuItems().length != this.checkboxes.length; for (let check of this.checkboxes) { check.checked = checked; } } }