fix bug - Service dependency - Can't select parent inputs that came from other instances
[sdc.git] / catalog-ui / src / app / utils / menu-handler.ts
index 1dc5a20..4c25a02 100644 (file)
@@ -1,4 +1,25 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * SDC
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
 'use strict';
+import * as _ from "lodash";
 import {WorkspaceMode, ComponentState} from "./constants";
 import {IAppConfigurtaion, IAppMenu, Component} from "../models";
 import {ComponentFactory} from "./component-factory";
@@ -9,7 +30,7 @@ export class MenuItem {
     callback:(...args:Array<any>) => ng.IPromise<boolean>;
     state:string;
     action:string;
-    params:Array<any>;
+    params:any;
     isDisabled:boolean;
     disabledRoles:Array<string>;
     blockedForTypes:Array<string>;  // This item will not be shown for specific components types.
@@ -22,7 +43,7 @@ export class MenuItem {
     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.
 
 
-    constructor(text:string, callback:(...args:Array<any>) => ng.IPromise<boolean>, state:string, action:string, params?:Array<any>, blockedForTypes?:Array<string>) {
+    constructor(text:string, callback:(...args:Array<any>) => ng.IPromise<boolean>, state:string, action:string, params?:any, blockedForTypes?:Array<string>) {
         this.text = text;
         this.callback = callback;
         this.state = state;
@@ -44,7 +65,10 @@ export class MenuItemGroup {
     }
 
     public updateSelectedMenuItemText(newText:string) {
-        this.menuItems[this.selectedIndex].text = newText;
+        const selectedMenuItem = this.menuItems[this.selectedIndex];
+        if (selectedMenuItem) {
+            this.menuItems[this.selectedIndex].text = newText;
+        }
     }
 }
 
@@ -72,32 +96,36 @@ export class MenuHandler {
     }
 
 
-    generateBreadcrumbsModelFromComponents = (components:Array<Component>, selected:Component):MenuItemGroup => {
-        let result = new MenuItemGroup(0, [], false);
-        if (components) {
+    findBreadcrumbComponentIndex = (components:Array<Component>, selected:Component):number => {
+        let selectedItemIdx;
+
+        // Search the component in all components by uuid (and not uniqueid, gives access to an assets's minor versions).
+        selectedItemIdx = _.findIndex(components, (item:Component) => {
+            return item.uuid === selected.uuid;
+        });
 
-            // Search the component in all components by uuid (and not uniqueid, gives access to an assets's minor versions).
-            let selectedItem = _.find(components, (item:Component) => {
-                return item.uuid === selected.uuid;
+        // If not found search by invariantUUID
+        if (selectedItemIdx === -1) {
+            selectedItemIdx = _.findIndex(components, (item:Component) => {
+                //invariantUUID && Certified State matches between major versions
+                return item.invariantUUID === selected.invariantUUID && item.lifecycleState === ComponentState.CERTIFIED;
             });
+        }
 
-            // If not found search by invariantUUID
-            if (undefined == selectedItem) {
-                selectedItem = _.find(components, (item:Component) => {
-                    //invariantUUID && Certified State matches between major versions
-                    return item.invariantUUID === selected.invariantUUID && item.lifecycleState === ComponentState.CERTIFIED;
-                });
-            }
+        // If not found search by name (name is unique).
+        if (selectedItemIdx === -1) {
+            selectedItemIdx = _.findIndex(components, (item:Component) => {
+                return item.name === selected.name && item.componentType === selected.componentType;
+            });
+        }
 
-            // If not found search by name (name is unique).
-            if (undefined == selectedItem) {
-                selectedItem = _.find(components, (item:Component) => {
-                    return item.name === selected.name;
-                });
-            }
+        return selectedItemIdx;
+    };
 
-            result.selectedIndex = components.indexOf(selectedItem);
-            components[result.selectedIndex] = selected;
+    generateBreadcrumbsModelFromComponents = (components:Array<Component>, selected:Component):MenuItemGroup => {
+        let result = new MenuItemGroup(0, [], false);
+        if (components) {
+            result.selectedIndex = this.findBreadcrumbComponentIndex(components, selected);
             let clickItemCallback = (component:Component):ng.IPromise<boolean> => {
                 this.$state.go('workspace.general', {
                     id: component.uniqueId,
@@ -119,6 +147,22 @@ export class MenuHandler {
                 //  menuItem.text = component.name;
                 result.menuItems.push(menuItem);
             });
+
+            result.selectedIndex = this.findBreadcrumbComponentIndex(components, selected);
+
+            // if component does not exist, then add a temporary menu item for the current component
+            if (result.selectedIndex === -1) {
+                let menuItem = new MenuItem(
+                    //  component.name,
+                    selected.getComponentSubType() + ': ' + this.$filter('resourceName')(selected.name),
+                    clickItemCallback,
+                    null,
+                    null,
+                    [selected]
+                );
+                result.menuItems.unshift(menuItem);
+                result.selectedIndex = 0;
+            }
         }
         return result;
     };