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 |
| 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 | |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 21 | 'use strict'; |
| 22 | import {WorkspaceMode, ComponentState} from "./constants"; |
| 23 | import {IAppConfigurtaion, IAppMenu, Component} from "../models"; |
| 24 | import {ComponentFactory} from "./component-factory"; |
| 25 | import {ModalsHandler} from "./modals-handler"; |
| 26 | |
| 27 | export class MenuItem { |
| 28 | text:string; |
| 29 | callback:(...args:Array<any>) => ng.IPromise<boolean>; |
| 30 | state:string; |
| 31 | action:string; |
| 32 | params:Array<any>; |
| 33 | isDisabled:boolean; |
| 34 | disabledRoles:Array<string>; |
| 35 | blockedForTypes:Array<string>; // This item will not be shown for specific components types. |
| 36 | |
| 37 | //TODO check if needed |
| 38 | alertModal:string; |
| 39 | conformanceLevelModal: boolean; // Call validateConformanceLevel API and shows conformanceLevelModal if necessary, then continue with action or invokes another action |
| 40 | confirmationModal:string; // Open confirmation modal (user should select "OK" or "Cancel"), and continue with the action. |
| 41 | emailModal:string; // Open email modal (user should fill email details), and continue with the action. |
| 42 | 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. |
| 43 | |
| 44 | |
| 45 | constructor(text:string, callback:(...args:Array<any>) => ng.IPromise<boolean>, state:string, action:string, params?:Array<any>, blockedForTypes?:Array<string>) { |
| 46 | this.text = text; |
| 47 | this.callback = callback; |
| 48 | this.state = state; |
| 49 | this.action = action; |
| 50 | this.params = params; |
| 51 | this.blockedForTypes = blockedForTypes; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | export class MenuItemGroup { |
| 56 | selectedIndex:number; |
| 57 | menuItems:Array<MenuItem>; |
| 58 | itemClick:boolean; |
| 59 | |
| 60 | constructor(selectedIndex?:number, menuItems?:Array<MenuItem>, itemClick?:boolean) { |
| 61 | this.selectedIndex = selectedIndex; |
| 62 | this.menuItems = menuItems; |
| 63 | this.itemClick = itemClick; |
| 64 | } |
| 65 | |
| 66 | public updateSelectedMenuItemText(newText:string) { |
| 67 | this.menuItems[this.selectedIndex].text = newText; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | |
| 72 | export class MenuHandler { |
| 73 | |
| 74 | static '$inject' = [ |
| 75 | 'sdcConfig', |
| 76 | 'sdcMenu', |
| 77 | 'ComponentFactory', |
| 78 | '$filter', |
| 79 | 'ModalsHandler', |
| 80 | '$state', |
| 81 | '$q' |
| 82 | ]; |
| 83 | |
| 84 | constructor(private sdcConfig:IAppConfigurtaion, |
| 85 | private sdcMenu:IAppMenu, |
| 86 | private ComponentFactory:ComponentFactory, |
| 87 | private $filter:ng.IFilterService, |
| 88 | private ModalsHandler:ModalsHandler, |
| 89 | private $state:ng.ui.IStateService, |
| 90 | private $q:ng.IQService) { |
| 91 | |
| 92 | } |
| 93 | |
| 94 | |
| 95 | generateBreadcrumbsModelFromComponents = (components:Array<Component>, selected:Component):MenuItemGroup => { |
| 96 | let result = new MenuItemGroup(0, [], false); |
| 97 | if (components) { |
| 98 | |
| 99 | // Search the component in all components by uuid (and not uniqueid, gives access to an assets's minor versions). |
| 100 | let selectedItem = _.find(components, (item:Component) => { |
| 101 | return item.uuid === selected.uuid; |
| 102 | }); |
| 103 | |
| 104 | // If not found search by invariantUUID |
| 105 | if (undefined == selectedItem) { |
| 106 | selectedItem = _.find(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 (undefined == selectedItem) { |
| 114 | selectedItem = _.find(components, (item:Component) => { |
| 115 | return item.name === selected.name; |
| 116 | }); |
| 117 | } |
| 118 | |
| 119 | result.selectedIndex = components.indexOf(selectedItem); |
| 120 | components[result.selectedIndex] = selected; |
| 121 | let clickItemCallback = (component:Component):ng.IPromise<boolean> => { |
| 122 | this.$state.go('workspace.general', { |
| 123 | id: component.uniqueId, |
| 124 | type: component.componentType.toLowerCase(), |
| 125 | mode: WorkspaceMode.VIEW |
| 126 | }); |
| 127 | return this.$q.when(true); |
| 128 | }; |
| 129 | |
| 130 | components.forEach((component:Component) => { |
| 131 | let menuItem = new MenuItem( |
| 132 | // component.name, |
| 133 | component.getComponentSubType() + ': ' + this.$filter('resourceName')(component.name), |
| 134 | clickItemCallback, |
| 135 | null, |
| 136 | null, |
| 137 | [component] |
| 138 | ); |
| 139 | // menuItem.text = component.name; |
| 140 | result.menuItems.push(menuItem); |
| 141 | }); |
| 142 | } |
| 143 | return result; |
| 144 | }; |
| 145 | } |