Michael Lando | dd60339 | 2017-07-12 00:54:52 +0300 | [diff] [blame] | 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 |
Idan Amit | 5197c8b | 2018-01-15 14:31:42 +0200 | [diff] [blame] | 10 | * |
Michael Lando | dd60339 | 2017-07-12 00:54:52 +0300 | [diff] [blame] | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
Idan Amit | 5197c8b | 2018-01-15 14:31:42 +0200 | [diff] [blame] | 12 | * |
Michael Lando | dd60339 | 2017-07-12 00:54:52 +0300 | [diff] [blame] | 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 | |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 21 | 'use strict'; |
Michael Lando | a544510 | 2018-03-04 14:53:33 +0200 | [diff] [blame] | 22 | import * as _ from "lodash"; |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 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; |
Idan Amit | 5197c8b | 2018-01-15 14:31:42 +0200 | [diff] [blame] | 33 | params:any; |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 34 | isDisabled:boolean; |
atif husain | d289108 | 2019-10-02 18:03:57 +1000 | [diff] [blame] | 35 | disabledCategory:boolean; |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 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 | emailModal:string; // Open email modal (user should fill email details), and continue with the action. |
| 44 | 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. |
| 45 | |
| 46 | |
atif husain | d289108 | 2019-10-02 18:03:57 +1000 | [diff] [blame] | 47 | constructor(text:string, callback:(...args:Array<any>) => ng.IPromise<boolean>, state:string, action:string, params?:any, blockedForTypes?:Array<string>, disabledCategory?:boolean) { |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 48 | this.text = text; |
| 49 | this.callback = callback; |
| 50 | this.state = state; |
| 51 | this.action = action; |
| 52 | this.params = params; |
| 53 | this.blockedForTypes = blockedForTypes; |
atif husain | d289108 | 2019-10-02 18:03:57 +1000 | [diff] [blame] | 54 | this.disabledCategory = disabledCategory; |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | |
| 58 | export class MenuItemGroup { |
| 59 | selectedIndex:number; |
| 60 | menuItems:Array<MenuItem>; |
| 61 | itemClick:boolean; |
| 62 | |
| 63 | constructor(selectedIndex?:number, menuItems?:Array<MenuItem>, itemClick?:boolean) { |
| 64 | this.selectedIndex = selectedIndex; |
| 65 | this.menuItems = menuItems; |
| 66 | this.itemClick = itemClick; |
| 67 | } |
| 68 | |
| 69 | public updateSelectedMenuItemText(newText:string) { |
Michael Lando | 5b59349 | 2018-07-29 16:13:45 +0300 | [diff] [blame] | 70 | const selectedMenuItem = this.menuItems[this.selectedIndex]; |
| 71 | if (selectedMenuItem) { |
| 72 | this.menuItems[this.selectedIndex].text = newText; |
| 73 | } |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
| 77 | |
| 78 | export class MenuHandler { |
| 79 | |
| 80 | static '$inject' = [ |
| 81 | 'sdcConfig', |
| 82 | 'sdcMenu', |
| 83 | 'ComponentFactory', |
| 84 | '$filter', |
| 85 | 'ModalsHandler', |
| 86 | '$state', |
| 87 | '$q' |
| 88 | ]; |
| 89 | |
| 90 | constructor(private sdcConfig:IAppConfigurtaion, |
| 91 | private sdcMenu:IAppMenu, |
| 92 | private ComponentFactory:ComponentFactory, |
| 93 | private $filter:ng.IFilterService, |
| 94 | private ModalsHandler:ModalsHandler, |
| 95 | private $state:ng.ui.IStateService, |
| 96 | private $q:ng.IQService) { |
| 97 | |
| 98 | } |
| 99 | |
| 100 | |
Michael Lando | a544510 | 2018-03-04 14:53:33 +0200 | [diff] [blame] | 101 | findBreadcrumbComponentIndex = (components:Array<Component>, selected:Component):number => { |
| 102 | let selectedItemIdx; |
| 103 | |
| 104 | // Search the component in all components by uuid (and not uniqueid, gives access to an assets's minor versions). |
| 105 | selectedItemIdx = _.findIndex(components, (item:Component) => { |
| 106 | return item.uuid === selected.uuid; |
| 107 | }); |
| 108 | |
| 109 | // If not found search by invariantUUID |
| 110 | if (selectedItemIdx === -1) { |
| 111 | selectedItemIdx = _.findIndex(components, (item:Component) => { |
| 112 | //invariantUUID && Certified State matches between major versions |
| 113 | return item.invariantUUID === selected.invariantUUID && item.lifecycleState === ComponentState.CERTIFIED; |
| 114 | }); |
| 115 | } |
| 116 | |
| 117 | // If not found search by name (name is unique). |
| 118 | if (selectedItemIdx === -1) { |
| 119 | selectedItemIdx = _.findIndex(components, (item:Component) => { |
Michael Lando | 5b59349 | 2018-07-29 16:13:45 +0300 | [diff] [blame] | 120 | return item.name === selected.name && item.componentType === selected.componentType; |
Michael Lando | a544510 | 2018-03-04 14:53:33 +0200 | [diff] [blame] | 121 | }); |
| 122 | } |
| 123 | |
| 124 | return selectedItemIdx; |
| 125 | }; |
| 126 | |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 127 | generateBreadcrumbsModelFromComponents = (components:Array<Component>, selected:Component):MenuItemGroup => { |
| 128 | let result = new MenuItemGroup(0, [], false); |
| 129 | if (components) { |
Michael Lando | a544510 | 2018-03-04 14:53:33 +0200 | [diff] [blame] | 130 | result.selectedIndex = this.findBreadcrumbComponentIndex(components, selected); |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 131 | let clickItemCallback = (component:Component):ng.IPromise<boolean> => { |
| 132 | this.$state.go('workspace.general', { |
| 133 | id: component.uniqueId, |
| 134 | type: component.componentType.toLowerCase(), |
| 135 | mode: WorkspaceMode.VIEW |
| 136 | }); |
| 137 | return this.$q.when(true); |
| 138 | }; |
| 139 | |
| 140 | components.forEach((component:Component) => { |
| 141 | let menuItem = new MenuItem( |
| 142 | // component.name, |
| 143 | component.getComponentSubType() + ': ' + this.$filter('resourceName')(component.name), |
| 144 | clickItemCallback, |
| 145 | null, |
| 146 | null, |
| 147 | [component] |
| 148 | ); |
| 149 | // menuItem.text = component.name; |
| 150 | result.menuItems.push(menuItem); |
| 151 | }); |
Michael Lando | 5b59349 | 2018-07-29 16:13:45 +0300 | [diff] [blame] | 152 | |
| 153 | result.selectedIndex = this.findBreadcrumbComponentIndex(components, selected); |
| 154 | |
| 155 | // if component does not exist, then add a temporary menu item for the current component |
| 156 | if (result.selectedIndex === -1) { |
| 157 | let menuItem = new MenuItem( |
| 158 | // component.name, |
| 159 | selected.getComponentSubType() + ': ' + this.$filter('resourceName')(selected.name), |
| 160 | clickItemCallback, |
| 161 | null, |
| 162 | null, |
| 163 | [selected] |
| 164 | ); |
| 165 | result.menuItems.unshift(menuItem); |
| 166 | result.selectedIndex = 0; |
| 167 | } |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 168 | } |
| 169 | return result; |
| 170 | }; |
| 171 | } |