2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
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";
28 export class MenuItem {
30 callback:(...args:Array<any>) => ng.IPromise<boolean>;
35 disabledCategory:boolean;
36 disabledRoles:Array<string>;
37 blockedForTypes:Array<string>; // This item will not be shown for specific components types.
39 //TODO check if needed
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.
46 constructor(text:string, callback:(...args:Array<any>) => ng.IPromise<boolean>, state:string, action:string, params?:any, blockedForTypes?:Array<string>, disabledCategory?:boolean) {
48 this.callback = callback;
52 this.blockedForTypes = blockedForTypes;
53 this.disabledCategory = disabledCategory;
57 export class MenuItemGroup {
59 menuItems:Array<MenuItem>;
62 constructor(selectedIndex?:number, menuItems?:Array<MenuItem>, itemClick?:boolean) {
63 this.selectedIndex = selectedIndex;
64 this.menuItems = menuItems;
65 this.itemClick = itemClick;
68 public updateSelectedMenuItemText(newText:string) {
69 const selectedMenuItem = this.menuItems[this.selectedIndex];
70 if (selectedMenuItem) {
71 this.menuItems[this.selectedIndex].text = newText;
77 export class MenuHandler {
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) {
100 findBreadcrumbComponentIndex = (components:Array<Component>, selected:Component):number => {
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;
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;
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;
123 return selectedItemIdx;
126 generateBreadcrumbsModelFromComponents = (components:Array<Component>, selected:Component):MenuItemGroup => {
127 let result = new MenuItemGroup(0, [], false);
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
136 return this.$q.when(true);
139 components.forEach((component:Component) => {
140 let menuItem = new MenuItem(
142 component.getComponentSubType() + ': ' + this.$filter('resourceName')(component.name),
148 // menuItem.text = component.name;
149 result.menuItems.push(menuItem);
152 result.selectedIndex = this.findBreadcrumbComponentIndex(components, selected);
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(
158 selected.getComponentSubType() + ': ' + this.$filter('resourceName')(selected.name),
164 result.menuItems.unshift(menuItem);
165 result.selectedIndex = 0;