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; |
| 35 | disabledRoles:Array<string>; |
| 36 | blockedForTypes:Array<string>; // This item will not be shown for specific components types. |
| 37 | |
| 38 | //TODO check if needed |
| 39 | alertModal:string; |
| 40 | conformanceLevelModal: boolean; // Call validateConformanceLevel API and shows conformanceLevelModal if necessary, then continue with action or invokes another action |
| 41 | confirmationModal:string; // Open confirmation modal (user should select "OK" or "Cancel"), and continue with the action. |
| 42 | emailModal:string; // Open email modal (user should fill email details), 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 | |
Idan Amit | 5197c8b | 2018-01-15 14:31:42 +0200 | [diff] [blame] | 46 | constructor(text:string, callback:(...args:Array<any>) => ng.IPromise<boolean>, state:string, action:string, params?:any, blockedForTypes?:Array<string>) { |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 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 | } |
| 54 | } |
| 55 | |
| 56 | export class MenuItemGroup { |
| 57 | selectedIndex:number; |
| 58 | menuItems:Array<MenuItem>; |
| 59 | itemClick:boolean; |
| 60 | |
| 61 | constructor(selectedIndex?:number, menuItems?:Array<MenuItem>, itemClick?:boolean) { |
| 62 | this.selectedIndex = selectedIndex; |
| 63 | this.menuItems = menuItems; |
| 64 | this.itemClick = itemClick; |
| 65 | } |
| 66 | |
| 67 | public updateSelectedMenuItemText(newText:string) { |
| 68 | this.menuItems[this.selectedIndex].text = newText; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | |
| 73 | export class MenuHandler { |
| 74 | |
| 75 | static '$inject' = [ |
| 76 | 'sdcConfig', |
| 77 | 'sdcMenu', |
| 78 | 'ComponentFactory', |
| 79 | '$filter', |
| 80 | 'ModalsHandler', |
| 81 | '$state', |
| 82 | '$q' |
| 83 | ]; |
| 84 | |
| 85 | constructor(private sdcConfig:IAppConfigurtaion, |
| 86 | private sdcMenu:IAppMenu, |
| 87 | private ComponentFactory:ComponentFactory, |
| 88 | private $filter:ng.IFilterService, |
| 89 | private ModalsHandler:ModalsHandler, |
| 90 | private $state:ng.ui.IStateService, |
| 91 | private $q:ng.IQService) { |
| 92 | |
| 93 | } |
| 94 | |
| 95 | |
Michael Lando | a544510 | 2018-03-04 14:53:33 +0200 | [diff] [blame] | 96 | findBreadcrumbComponentIndex = (components:Array<Component>, selected:Component):number => { |
| 97 | let selectedItemIdx; |
| 98 | |
| 99 | // Search the component in all components by uuid (and not uniqueid, gives access to an assets's minor versions). |
| 100 | selectedItemIdx = _.findIndex(components, (item:Component) => { |
| 101 | return item.uuid === selected.uuid; |
| 102 | }); |
| 103 | |
| 104 | // If not found search by invariantUUID |
| 105 | if (selectedItemIdx === -1) { |
| 106 | selectedItemIdx = _.findIndex(components, (item:Component) => { |
| 107 | //invariantUUID && Certified State matches between major versions |
| 108 | return item.invariantUUID === selected.invariantUUID && item.lifecycleState === ComponentState.CERTIFIED; |
| 109 | }); |
| 110 | } |
| 111 | |
| 112 | // If not found search by name (name is unique). |
| 113 | if (selectedItemIdx === -1) { |
| 114 | selectedItemIdx = _.findIndex(components, (item:Component) => { |
| 115 | return item.name === selected.name; |
| 116 | }); |
| 117 | } |
| 118 | |
| 119 | return selectedItemIdx; |
| 120 | }; |
| 121 | |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 122 | generateBreadcrumbsModelFromComponents = (components:Array<Component>, selected:Component):MenuItemGroup => { |
| 123 | let result = new MenuItemGroup(0, [], false); |
| 124 | if (components) { |
Michael Lando | a544510 | 2018-03-04 14:53:33 +0200 | [diff] [blame] | 125 | result.selectedIndex = this.findBreadcrumbComponentIndex(components, selected); |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 126 | let clickItemCallback = (component:Component):ng.IPromise<boolean> => { |
| 127 | this.$state.go('workspace.general', { |
| 128 | id: component.uniqueId, |
| 129 | type: component.componentType.toLowerCase(), |
| 130 | mode: WorkspaceMode.VIEW |
| 131 | }); |
| 132 | return this.$q.when(true); |
| 133 | }; |
| 134 | |
| 135 | components.forEach((component:Component) => { |
| 136 | let menuItem = new MenuItem( |
| 137 | // component.name, |
| 138 | component.getComponentSubType() + ': ' + this.$filter('resourceName')(component.name), |
| 139 | clickItemCallback, |
| 140 | null, |
| 141 | null, |
| 142 | [component] |
| 143 | ); |
| 144 | // menuItem.text = component.name; |
| 145 | result.menuItems.push(menuItem); |
| 146 | }); |
| 147 | } |
| 148 | return result; |
| 149 | }; |
| 150 | } |