Import data type in UI
[sdc.git] / catalog-ui / src / app / ng2 / pages / type-workspace / workspace-menu / workspace-menu.component.ts
1 /*
2  * -
3  *  ============LICENSE_START=======================================================
4  *  Copyright (C) 2022 Nordix Foundation.
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *       http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 import {Component, EventEmitter, Inject, Input, OnInit, Output} from '@angular/core';
23 import {MenuItem, MenuItemGroup} from "../../../../utils/menu-handler";
24 import {CacheService} from "../../../services/cache.service";
25 import {IAppMenu} from "../../../../models/app-config";
26 import {IUserProperties} from "../../../../models/user";
27 import {SdcMenuToken} from "../../../config/sdc-menu.config";
28 import {IWorkspaceViewModelScope} from "../../../../view-models/workspace/workspace-view-model";
29
30 @Component({
31     selector: 'app-workspace-menu',
32     templateUrl: './workspace-menu.component.html',
33     styleUrls: ['./workspace-menu.component.less']
34 })
35 export class WorkspaceMenuComponent implements OnInit {
36
37     @Input() menuHeader: string = '';
38     @Output() onMenuUpdate: EventEmitter<MenuItemGroup> = new EventEmitter<MenuItemGroup>();
39     @Output() onClick: EventEmitter<MenuItem> = new EventEmitter<MenuItem>();
40
41     private role: string;
42     private user: IUserProperties;
43     private $state: ng.ui.IStateService;
44     private $q: ng.IQService;
45
46     leftBarTabs: MenuItemGroup = new MenuItemGroup();
47
48     constructor(private cacheService: CacheService,
49                 @Inject('$scope') private $scope: IWorkspaceViewModelScope,
50                 @Inject(SdcMenuToken) private sdcMenu: IAppMenu,
51                 @Inject('$injector') $injector) {
52         this.$state = $injector.get('$state');
53         this.$q = $injector.get('$q');
54     }
55
56     ngOnInit(): void {
57         this.user = this.cacheService.get('user');
58         this.role = this.user.role;
59         this.initMenuItems();
60     }
61
62     private initMenuItems(): void {
63         this.leftBarTabs = new MenuItemGroup();
64         const menuItemsObjects: MenuItem[] = this.updateMenuItemByRole(this.sdcMenu.component_workspace_menu_option["DataType"], this.role);
65
66         this.leftBarTabs.menuItems = menuItemsObjects.map((item: MenuItem) => {
67             const menuCallback = () => this[menuItem.action](menuItem);
68             const menuItem = new MenuItem(item.text, menuCallback, item.state, item.action, item.params, item.blockedForTypes, item.disabledCategory);
69             if (menuItem.params) {
70                 menuItem.params.state = menuItem.state;
71             } else {
72                 menuItem.params = {state: menuItem.state};
73             }
74             return menuItem;
75         });
76         this.updateSelectedMenuItem();
77         this.$scope.leftBarTabs = this.leftBarTabs;
78         this.onMenuUpdate.emit(this.leftBarTabs);
79     }
80
81     isSelected(menuItem: MenuItem): boolean {
82         return this.leftBarTabs.selectedIndex === this.leftBarTabs.menuItems.indexOf(menuItem);
83     }
84
85     private onMenuItemPressed(menuItem: MenuItem): angular.IPromise<boolean> {
86         this.leftBarTabs.selectedIndex = this.leftBarTabs.menuItems.indexOf(menuItem);
87         this.onClick.emit(this.leftBarTabs.menuItems[this.leftBarTabs.selectedIndex]);
88         const deferred: ng.IDeferred<boolean> = this.$q.defer();
89         deferred.resolve(true);
90         return deferred.promise;
91     }
92
93     private updateMenuItemByRole(menuItems: MenuItem[], role: string): MenuItem[] {
94         const filteredMenuItems: MenuItem[] = [];
95         menuItems.forEach((item: any) => {
96             if (!(item.disabledRoles && item.disabledRoles.indexOf(role) > -1)) {
97                 filteredMenuItems.push(item);
98             }
99         });
100         return filteredMenuItems;
101     }
102
103     private updateSelectedMenuItem(): void {
104         const stateArray: Array<string> = this.$state.current.name.split('.', 2);
105         const stateWithoutInternalNavigate: string = stateArray[0] + '.' + stateArray[1];
106         const selectedItem: MenuItem = this.leftBarTabs.menuItems.find((item: MenuItem) => {
107             let itemStateArray: Array<string> = item.state.split('.', 2);
108             let itemStateWithoutNavigation: string = itemStateArray[0] + '.' + itemStateArray[1];
109             return (itemStateWithoutNavigation === stateWithoutInternalNavigate);
110         });
111         this.leftBarTabs.selectedIndex = selectedItem ? this.leftBarTabs.menuItems.indexOf(selectedItem) : 0;
112         this.onClick.emit(this.leftBarTabs.menuItems[this.leftBarTabs.selectedIndex]);
113     }
114
115 }