Catalog alignment
[sdc.git] / catalog-ui / src / app / utils / menu-handler.ts
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 'use strict';
22 import * as _ from "lodash";
23 import {WorkspaceMode, ComponentState} from "./constants";
24 import {IAppConfigurtaion, IAppMenu, Component} from "../models";
25 import {ComponentFactory} from "./component-factory";
26 import {ModalsHandler} from "./modals-handler";
27
28 export class MenuItem {
29     text:string;
30     callback:(...args:Array<any>) => ng.IPromise<boolean>;
31     state:string;
32     action:string;
33     params:any;
34     isDisabled:boolean;
35     disabledCategory:boolean;
36     disabledRoles:Array<string>;
37     blockedForTypes:Array<string>;  // This item will not be shown for specific components types.
38
39     //TODO check if needed
40     alertModal:string;
41     conformanceLevelModal: boolean; // Call validateConformanceLevel API and shows conformanceLevelModal if necessary, then continue with action or invokes another action
42     confirmationModal:string;       // Open confirmation modal (user should select "OK" or "Cancel"), and continue with the action.
43     url:string;                     // Data added to menu item, in case the function need to use it, example: for function "changeLifecycleState", I need to pass also the state "CHECKOUT" that I want the state to change to.
44
45
46     constructor(text:string, callback:(...args:Array<any>) => ng.IPromise<boolean>, state:string, action:string, params?:any, blockedForTypes?:Array<string>, disabledCategory?:boolean) {
47         this.text = text;
48         this.callback = callback;
49         this.state = state;
50         this.action = action;
51         this.params = params;
52         this.blockedForTypes = blockedForTypes;
53         this.disabledCategory = disabledCategory;
54     }
55 }
56
57 export class MenuItemGroup {
58     selectedIndex:number;
59     menuItems:Array<MenuItem>;
60     itemClick:boolean;
61
62     constructor(selectedIndex?:number, menuItems?:Array<MenuItem>, itemClick?:boolean) {
63         this.selectedIndex = selectedIndex;
64         this.menuItems = menuItems;
65         this.itemClick = itemClick;
66     }
67
68     public updateSelectedMenuItemText(newText:string) {
69         const selectedMenuItem = this.menuItems[this.selectedIndex];
70         if (selectedMenuItem) {
71             this.menuItems[this.selectedIndex].text = newText;
72         }
73     }
74 }
75
76
77 export class MenuHandler {
78
79     static '$inject' = [
80         'sdcConfig',
81         'sdcMenu',
82         'ComponentFactory',
83         '$filter',
84         'ModalsHandler',
85         '$state',
86         '$q'
87     ];
88
89     constructor(private sdcConfig:IAppConfigurtaion,
90                 private sdcMenu:IAppMenu,
91                 private ComponentFactory:ComponentFactory,
92                 private $filter:ng.IFilterService,
93                 private ModalsHandler:ModalsHandler,
94                 private $state:ng.ui.IStateService,
95                 private $q:ng.IQService) {
96
97     }
98
99
100     findBreadcrumbComponentIndex = (components:Array<Component>, selected:Component):number => {
101         let selectedItemIdx;
102
103         // Search the component in all components by uuid (and not uniqueid, gives access to an assets's minor versions).
104         selectedItemIdx = _.findIndex(components, (item:Component) => {
105             return item.uuid === selected.uuid;
106         });
107
108         // If not found search by invariantUUID
109         if (selectedItemIdx === -1) {
110             selectedItemIdx = _.findIndex(components, (item:Component) => {
111                 //invariantUUID && Certified State matches between major versions
112                 return item.invariantUUID === selected.invariantUUID && item.lifecycleState === ComponentState.CERTIFIED;
113             });
114         }
115
116         // If not found search by name (name is unique).
117         if (selectedItemIdx === -1) {
118             selectedItemIdx = _.findIndex(components, (item:Component) => {
119                 return item.name === selected.name && item.componentType === selected.componentType;
120             });
121         }
122
123         return selectedItemIdx;
124     };
125
126     generateBreadcrumbsModelFromComponents = (components:Array<Component>, selected:Component):MenuItemGroup => {
127         let result = new MenuItemGroup(0, [], false);
128         if (components) {
129             result.selectedIndex = this.findBreadcrumbComponentIndex(components, selected);
130             let clickItemCallback = (component:Component):ng.IPromise<boolean> => {
131                 this.$state.go('workspace.general', {
132                     id: component.uniqueId,
133                     type: component.componentType.toLowerCase(),
134                     mode: WorkspaceMode.VIEW
135                 });
136                 return this.$q.when(true);
137             };
138
139             components.forEach((component:Component) => {
140                 let menuItem = new MenuItem(
141                     //  component.name,
142                     component.getComponentSubType() + ': ' + this.$filter('resourceName')(component.name),
143                     clickItemCallback,
144                     null,
145                     null,
146                     [component]
147                 );
148                 //  menuItem.text = component.name;
149                 result.menuItems.push(menuItem);
150             });
151
152             result.selectedIndex = this.findBreadcrumbComponentIndex(components, selected);
153
154             // if component does not exist, then add a temporary menu item for the current component
155             if (result.selectedIndex === -1) {
156                 let menuItem = new MenuItem(
157                     //  component.name,
158                     selected.getComponentSubType() + ': ' + this.$filter('resourceName')(selected.name),
159                     clickItemCallback,
160                     null,
161                     null,
162                     [selected]
163                 );
164                 result.menuItems.unshift(menuItem);
165                 result.selectedIndex = 0;
166             }
167         }
168         return result;
169     };
170 }