import { Component, Input, OnInit, ViewChild, AfterViewInit } from '@angular/core'; import { ActivatedRoute, Router } from "@angular/router"; import { FormBuilder, FormGroup, Validators, FormControl, RequiredValidator } from '@angular/forms'; import { CommonService } from '../../../../services/common.service'; import { TreeNode, TreeModel, TreeComponent } from '@circlon/angular-tree-component'; import { IMenuItemAdmin, SaveData } from '../../../../models/admin/company/menu-management/menu-management'; import { MenuItemAdmin, MenuItemType } from '../../../../models/shared/helper/menu'; import { MenuManagementService } from '../../../../services/admin/company/menu-management/menu-management.service'; export enum AddNodeMenuLocation { CHILD = 0, ABOVE = 1, BELOW = 2 } interface AddEditNodeInfo { location: number; IsGroup: boolean; isAdd: boolean; } export enum AdminContextMenuAction { ADD_CHILD_NODE = 0, ADD_NODE_ABOVE = 1, ADD_NODE_BELOW = 2, TOGGLE_ACTIVE = 3, EDIT_NODE = 4 } @Component({ selector: 'app-menu-management', templateUrl: './menu-management.component.html', styleUrls: ['./menu-management.component.css'] }) export class MenuManagementComponent implements OnInit, AfterViewInit { private addEditNodeInfo: AddEditNodeInfo; private insertNodeFunction = this.insertNode.bind(this); private allowNodeCreationFunction = this.allowNodeCreation.bind(this); private parentIsGroupFunction = this.parentIsGroup.bind(this); private isEditableFunction = this.IsEditable.bind(this); formData: FormData = new FormData(); @Input() x = 0; @Input() y = 0; @Input() show = false; _paramId: number = 0; @ViewChild('tree') private tree: TreeComponent; @ViewChild('contexttree') private contexttree: TreeComponent; MenuManagementAPIResponse: any; actionInvoked = false; isBusy = false; isUniqueName: any; showContextMenu = false; contextmenuX = 0; contextmenuY = 0; selectedNode: TreeNode; menuItem: IMenuItemAdmin; saveMenuItem = new SaveData(); items: IMenuItemAdmin[]; newID: number = 0; nodes: IMenuItemAdmin[]; LstMenuAdmin: any; arrItems: any[] = []; contextMenuItems: any[] = [ { name: 'Add Child Node', renderCondition: this.AllowChildren.bind(this), children: [ { name: 'Group', action: this.insertNodeFunction, IsGroup: true, location: AddNodeMenuLocation.CHILD, renderCondition: this.parentIsGroupFunction }, { name: 'Static Content', action: this.insertNodeFunction, location: AddNodeMenuLocation.CHILD, renderCondition: this.parentIsGroupFunction } ] }, { name: 'Add Group Node', renderCondition: this.allowNodeCreationFunction, children: [ { name: 'Above', action: this.insertNodeFunction, IsGroup: true, location: AddNodeMenuLocation.ABOVE, renderCondition: this.allowNodeCreationFunction }, { name: 'Below', action: this.insertNodeFunction, IsGroup: true, location: AddNodeMenuLocation.BELOW, renderCondition: this.allowNodeCreationFunction } ] }, { name: 'Add Static Content Node', renderCondition: this.allowNodeCreationFunction, children: [ { name: 'Above', IsGroup: false, action: this.insertNodeFunction, location: AddNodeMenuLocation.ABOVE, renderCondition: this.allowNodeCreationFunction }, { name: 'Below', action: this.insertNodeFunction, IsGroup: false, location: AddNodeMenuLocation.BELOW, renderCondition: this.allowNodeCreationFunction } ] }, { name: 'Enable Node', action: this.toggleActive.bind(this), renderCondition: this.isEditableFunction, isCustomTemplate: true }, { name: 'Edit Node', action: this.editNode.bind(this), renderCondition: this.isEditableFunction }, { name: 'Delete Node', action: this.deleteNode.bind(this), renderCondition: this.isEditableFunction } ]; contextMenuOptions = { allowDrag: false, allowDrop: false, allowDragoverStyling: false, actionMapping: { mouse: { click: (tree, node, $event) => { if (!node.data.action) { return; } const hasParameters = node.data.location !== undefined && node.data.location !== null; if (hasParameters) { node.data.action(node.data.location, node.data.IsGroup); } else { node.data.action(); } } } } }; options = { actionMapping: { mouse: { contextMenu: this.contextMenu.bind(this) } }, allowDrag: (node) => { return node.data && node.data.IsMovable; }, allowDrop: (element, { parent }) => { // virtual represents the "in-between" state between nodes return parent.data && (parent.data.virtual || (parent.data.AllowChildren && parent.data.IsGroup)); }, displayField: 'Name', childrenField: 'ChildNode', animateExpand: true, animateAcceleration: 1.4 }; constructor( private menuManagementService: MenuManagementService, private router: Router, private common: CommonService, ) { } private updateTreeModel() { this.tree.treeModel.update(); } private beginFormActionInvoked() { // disable main menu this.actionInvoked = true; this.showContextMenu = false; } private endFormActionInvoked() { // enable main menu this.actionInvoked = false; this.contexttree.treeModel.collapseAll(); } private parentIsGroup() { return this.selectedNode.data.IsGroup; } private AllowChildren() { // if (this.selectedNode.data.ChildNode.length > 0 && this.selectedNode.data.ChildNode.data.ChildNode.length > 0) // return false // else return this.selectedNode.data.AllowChildren; } private allowNodeCreation() { return ( (this.selectedNode.parent && this.selectedNode.isRoot) || (this.selectedNode.parent && this.selectedNode.parent.data.AllowChildren && this.selectedNode.parent.data.IsGroup) ); } private IsEditable() { return this.selectedNode.data.IsEditable; } private insertNode(location: number, IsGroup: boolean = false) { this.beginFormActionInvoked(); if (this.newID == 0) { for (var i = 0; i < this.nodes.length; i++) { this.arrItems.push({ "IDs": this.nodes[i].MenuAdminId }); for (var j = 0; j < this.nodes[i].ChildNode.length; j++) { this.arrItems.push({ "IDs": this.nodes[i].ChildNode[j].MenuAdminId }); } } this.newID = (Math.max.apply(Math, this.arrItems.map(function (o) { return o.IDs; }))); } else this.newID = (this.newID + 1); if (this.selectedNode.parent) { // // store node location info locally so we can use it later if changes are actually applie this.addEditNodeInfo = { location, IsGroup, isAdd: true }; // // hit createMenuItem endpoint to get a valid GUID this.showContextMenu = false; this.menuItem = new MenuItemAdmin; var res: IMenuItemAdmin = { IsActive: true, IsEditable: true, IsInherited: false, IsMovable: true, IsPartiallyLoaded: false, AllowChildren: IsGroup, Content: null, Name: null, ImageVideo: null, ImageVideoFileName: null, isAdd: true, ChildNode: [], id: (this.newID + 1).toString(), IsMobile: true, isSettings: true, Type: 1, URL: null, IsHttp: false, IsRouterPath: false, IsGroup: IsGroup, MenuAdminId: (this.newID + 1), LstMenuAdmin: [], ParentId: 0, CompanyId: 1, ADMINID: 0, ImageVideoData: "" }; this.menuItem = res; } } private editNode() { this.beginFormActionInvoked(); this.menuItem = this.selectedNode.data; this.menuItem.isAdd = false; } private deleteNode() { this.nodes = this.nodes.filter(x => x.Name != this.selectedNode.data.Name); for (var i = 0; i < this.nodes.length; i++) { this.nodes[i].ChildNode = this.nodes[i].ChildNode.filter(x => x.Name != this.selectedNode.data.Name); for (var j = 0; j < this.nodes[i].ChildNode.length; j++) { this.nodes[i].ChildNode[j].ChildNode = this.nodes[i].ChildNode[j].ChildNode.filter(x => x.Name != this.selectedNode.data.Name); } } this.showContextMenu = false; } private insertAtLocation(menuItem: IMenuItemAdmin) { switch (this.addEditNodeInfo.location) { case AddNodeMenuLocation.ABOVE: this.selectedNode.parent.data.ChildNode.splice(this.selectedNode.index, 0, menuItem); break; case AddNodeMenuLocation.BELOW: this.selectedNode.parent.data.ChildNode.splice(this.selectedNode.index + 1, 0, menuItem); break; default: this.selectedNode.data.ChildNode.push(menuItem); break; } this.updateTreeModel(); if (this.addEditNodeInfo.location === AddNodeMenuLocation.CHILD) { this.tree.treeModel.getActiveNode().expandAll(); } // set selectedNode and set to active in the tree this.selectedNode = this.tree.treeModel.getNodeById(menuItem.id) as TreeNode; this.selectedNode.setIsActive(true); } private toggleActive() { this.selectedNode.data.IsActive = !this.selectedNode.data.IsActive; this.showContextMenu = false; } ngOnInit() { this.isBusy = true; this.menuManagementService.deleteNode.subscribe(isDeleteNode => { this.nodes = this.nodes.filter(x => x.Name != this.menuManagementService.deleteNodeName); for (var i = 0; i < this.nodes.length; i++) { this.nodes[i].ChildNode = this.nodes[i].ChildNode.filter(x => x.Name != this.menuManagementService.deleteNodeName); for (var j = 0; j < this.nodes[i].ChildNode.length; j++) { this.nodes[i].ChildNode[j].ChildNode = this.nodes[i].ChildNode[j].ChildNode.filter(x => x.Name != this.menuManagementService.deleteNodeName); } } this.endFormActionInvoked(); this.menuItem = null; }); // this._paramId = Number(this.route.snapshot.params.id); this._paramId = 1; this.menuManagementService.getById(this._paramId, data => { this.MenuManagementAPIResponse = data; this.nodes = this.MenuManagementAPIResponse.objResponse.responseContent; }, (error) => { console.error('Error: ' + error); return error(error); }); } onMoveNode($event) { const parentNode = this.tree.treeModel.getNodeById($event.to.parent.id) as TreeNode; if (parentNode) { parentNode.expandAll(); } } handleOutsideClick(event) { this.showContextMenu = false; } contextMenu(tree: TreeModel, node: TreeNode, $event: MouseEvent) { $event.preventDefault(); node.setIsActive(true); this.selectedNode = node; this.contextmenuX = $event.x; this.contextmenuY = $event.y; this.showContextMenu = true; } menuItemApplied(menuItem: IMenuItemAdmin) { this.isUniqueName = this.nodes.filter((x) => x.Name.toLowerCase().includes(menuItem.Name.toLowerCase()) && x.MenuAdminId != menuItem.MenuAdminId); if (this.isUniqueName.length == 0) for (var i = 0; i < this.nodes.length; i++) { this.isUniqueName = this.nodes[i].ChildNode.filter((x) => x.Name.toLowerCase().includes(menuItem.Name.toLowerCase()) && x.MenuAdminId != menuItem.MenuAdminId); if (this.isUniqueName.length > 0) break; for (var j = 0; j < this.nodes[i].ChildNode.length; j++) { this.isUniqueName = this.nodes[i].ChildNode[j].ChildNode.filter((x) => x.Name.toLowerCase().includes(menuItem.Name.toLowerCase()) && x.MenuAdminId != menuItem.MenuAdminId); if (this.isUniqueName.length > 0) break; } } if (menuItem.isAdd) { menuItem.AllowChildren = true; if (this.selectedNode.parent != null) { if (this.selectedNode.parent.parent != null) if (this.selectedNode.parent.parent.parent == null && this.addEditNodeInfo.location == AddNodeMenuLocation.CHILD) menuItem.AllowChildren = false; } } if (this.isUniqueName.length == 0) { if (menuItem.isAdd) { this.insertAtLocation(menuItem); } else { // don't use spread this.selectedNode.data.Name = menuItem.Name; this.selectedNode.data.URL = menuItem.URL; this.selectedNode.data.IsActive = menuItem.IsActive; this.selectedNode.data.Content = menuItem.Content; this.selectedNode.data.ImageVideo = menuItem.ImageVideo; this.selectedNode.data.ImageVideoData = menuItem.ImageVideoData; this.selectedNode.data.ImageVideoFileName = menuItem.ImageVideoFileName; this.selectedNode.data.Type = menuItem.Type; this.selectedNode.data.IsPartiallyLoaded = menuItem.IsPartiallyLoaded; this.selectedNode.setIsActive(true); } this.endFormActionInvoked(); this.menuItem = null; } else { this.common.showError("Menu name already exist.", "Validation Alert!") this.isUniqueName = ""; } } menuItemCancelDelete(isDelete: boolean) { if (isDelete) { this.deleteNode(); } this.newID = this.newID - 1; this.menuItem = null; this.endFormActionInvoked(); this.addEditNodeInfo = null; } saveMenu() { this.isBusy = true; this.saveMenuItem.LstMenuAdmin = this.nodes; this.SetParentChildTypeRelations(this.saveMenuItem.LstMenuAdmin); this.setModelBeforeSubmit(this.saveMenuItem.LstMenuAdmin); // for (let val of this.formData.entries()) { // console.log(val[0] + ', ' + val[1]); // } this.menuManagementService.SaveMenuManagement(this.formData, data => { this.MenuManagementAPIResponse = data; this.nodes = this.MenuManagementAPIResponse.objResponse.responseContent; if (this.MenuManagementAPIResponse.objResponse.statusCode == "200") { this.common.showSuccess("Menu Management changes applied successfully.", "Success") this.router.navigate(["/admin/admin-company"]); } else { this.common.showError("Transaction not completed.", "Transaction Fail") } }, (error) => { console.error('Error: ' + error); return error(error); }); } SetParentChildTypeRelations(data) { for (var i = 0; i < data.length; i++) { if (data[i].isAdd) { data[i].ParentId = 0; data[i].Type = data[i].IsHttp ? 1 : data[i].IsGroup ? 3 : (data[i].IsRouterPath && data[i].IsGroup) ? 0 : 2; } if (data[i].ChildNode.length > 0) { for (var j = 0; j < data[i].ChildNode.length; j++) { if (data[i].ChildNode[j].isAdd) { data[i].ChildNode[j].ParentId = data[i].MenuAdminId; data[i].ChildNode[j].Type = data[i].ChildNode[j].IsHttp ? 1 : data[i].ChildNode[j].IsGroup ? 3 : (data[i].ChildNode[j].IsRouterPath && data[i].ChildNode[j].IsGroup) ? 0 : 2; } if (data[i].ChildNode[j].ChildNode.length > 0) { for (var k = 0; k < data[i].ChildNode[j].ChildNode.length; k++) { if (data[i].ChildNode[j].ChildNode[k].isAdd) { data[i].ChildNode[j].ChildNode[k].ParentId = data[i].ChildNode[j].MenuAdminId; data[i].ChildNode[j].ChildNode[k].Type = data[i].ChildNode[j].ChildNode[k].IsHttp ? 1 : data[i].ChildNode[j].ChildNode[k].IsGroup ? 3 : (data[i].ChildNode[j].ChildNode[k].IsRouterPath && data[i].ChildNode[j].ChildNode[k].IsGroup) ? 0 : 2; } } } } } } } setModelBeforeSubmit(Data: any) { this.formData = new FormData(); this.formData = this.convertModelToFormData(Data, null, 'LstMenuAdmin'); } public convertModelToFormData(model: any, form: FormData = null, namespace: string = ''): FormData { let formData = form || new FormData(); let formKey; for (let propertyName in model) { if (!model.hasOwnProperty(propertyName) || !model[propertyName]) continue; let formKey = ""; if (namespace != "") { if (namespace.indexOf("[") > 0) formKey = `${namespace}.${propertyName}`; else formKey = `${namespace}[${propertyName}]`; } else { formKey = propertyName; } let formKey1 = namespace ? `${namespace}[${propertyName}]` : propertyName; if (model[propertyName] instanceof Date) formData.append(formKey, model[propertyName].toISOString()); else if (model[propertyName] instanceof Array) { model[propertyName].forEach((element, index) => { const tempFormKey = `${formKey}[${index}]`; this.convertModelToFormData(element, formData, tempFormKey); }); } else if (typeof model[propertyName] === 'object' && !(model[propertyName] instanceof File) && !(propertyName == 'ImageVideo')) this.convertModelToFormData(model[propertyName], formData, formKey); else if ((model[propertyName] instanceof File)) { formData.append(formKey, model[propertyName]) } else { propertyName != ('ImageVideo') ? formData.append(formKey, model[propertyName].toString()) : 1 == 1; } } return formData; } ngAfterViewInit(): void { } }