blob: e17012a45e641c0ca3d43c2c79945540c211baa6 [file] [log] [blame]
Michael Landodd603392017-07-12 00:54:52 +03001/*-
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 Amit5197c8b2018-01-15 14:31:42 +020010 *
Michael Landodd603392017-07-12 00:54:52 +030011 * http://www.apache.org/licenses/LICENSE-2.0
Idan Amit5197c8b2018-01-15 14:31:42 +020012 *
Michael Landodd603392017-07-12 00:54:52 +030013 * 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 Landoed64b5e2017-06-09 03:19:04 +030021'use strict';
Michael Landoa5445102018-03-04 14:53:33 +020022import * as _ from "lodash";
Michael Landoed64b5e2017-06-09 03:19:04 +030023import {WorkspaceMode, ComponentState} from "./constants";
24import {IAppConfigurtaion, IAppMenu, Component} from "../models";
25import {ComponentFactory} from "./component-factory";
26import {ModalsHandler} from "./modals-handler";
27
28export class MenuItem {
29 text:string;
30 callback:(...args:Array<any>) => ng.IPromise<boolean>;
31 state:string;
32 action:string;
Idan Amit5197c8b2018-01-15 14:31:42 +020033 params:any;
Michael Landoed64b5e2017-06-09 03:19:04 +030034 isDisabled:boolean;
atif husaind2891082019-10-02 18:03:57 +100035 disabledCategory:boolean;
Michael Landoed64b5e2017-06-09 03:19:04 +030036 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.
Michael Landoed64b5e2017-06-09 03:19:04 +030043 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
atif husaind2891082019-10-02 18:03:57 +100046 constructor(text:string, callback:(...args:Array<any>) => ng.IPromise<boolean>, state:string, action:string, params?:any, blockedForTypes?:Array<string>, disabledCategory?:boolean) {
Michael Landoed64b5e2017-06-09 03:19:04 +030047 this.text = text;
48 this.callback = callback;
49 this.state = state;
50 this.action = action;
51 this.params = params;
52 this.blockedForTypes = blockedForTypes;
atif husaind2891082019-10-02 18:03:57 +100053 this.disabledCategory = disabledCategory;
Michael Landoed64b5e2017-06-09 03:19:04 +030054 }
55}
56
57export class MenuItemGroup {
58 selectedIndex:number;
59 menuItems:Array<MenuItem>;
60 itemClick:boolean;
61
62 constructor(selectedIndex?:number, menuItems?:Array<MenuItem>, itemClick?:boolean) {
63 this.selectedIndex = selectedIndex;
64 this.menuItems = menuItems;
65 this.itemClick = itemClick;
66 }
67
68 public updateSelectedMenuItemText(newText:string) {
Michael Lando5b593492018-07-29 16:13:45 +030069 const selectedMenuItem = this.menuItems[this.selectedIndex];
70 if (selectedMenuItem) {
71 this.menuItems[this.selectedIndex].text = newText;
72 }
Michael Landoed64b5e2017-06-09 03:19:04 +030073 }
74}
75
76
77export class MenuHandler {
78
79 static '$inject' = [
80 'sdcConfig',
81 'sdcMenu',
82 'ComponentFactory',
83 '$filter',
84 'ModalsHandler',
85 '$state',
86 '$q'
87 ];
88
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) {
96
97 }
98
99
Michael Landoa5445102018-03-04 14:53:33 +0200100 findBreadcrumbComponentIndex = (components:Array<Component>, selected:Component):number => {
101 let selectedItemIdx;
102
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;
106 });
107
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;
113 });
114 }
115
116 // If not found search by name (name is unique).
117 if (selectedItemIdx === -1) {
118 selectedItemIdx = _.findIndex(components, (item:Component) => {
Michael Lando5b593492018-07-29 16:13:45 +0300119 return item.name === selected.name && item.componentType === selected.componentType;
Michael Landoa5445102018-03-04 14:53:33 +0200120 });
121 }
122
123 return selectedItemIdx;
124 };
125
Michael Landoed64b5e2017-06-09 03:19:04 +0300126 generateBreadcrumbsModelFromComponents = (components:Array<Component>, selected:Component):MenuItemGroup => {
127 let result = new MenuItemGroup(0, [], false);
128 if (components) {
Michael Landoa5445102018-03-04 14:53:33 +0200129 result.selectedIndex = this.findBreadcrumbComponentIndex(components, selected);
Michael Landoed64b5e2017-06-09 03:19:04 +0300130 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
135 });
136 return this.$q.when(true);
137 };
138
139 components.forEach((component:Component) => {
140 let menuItem = new MenuItem(
141 // component.name,
142 component.getComponentSubType() + ': ' + this.$filter('resourceName')(component.name),
143 clickItemCallback,
144 null,
145 null,
146 [component]
147 );
148 // menuItem.text = component.name;
149 result.menuItems.push(menuItem);
150 });
Michael Lando5b593492018-07-29 16:13:45 +0300151
152 result.selectedIndex = this.findBreadcrumbComponentIndex(components, selected);
153
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(
157 // component.name,
158 selected.getComponentSubType() + ': ' + this.$filter('resourceName')(selected.name),
159 clickItemCallback,
160 null,
161 null,
162 [selected]
163 );
164 result.menuItems.unshift(menuItem);
165 result.selectedIndex = 0;
166 }
Michael Landoed64b5e2017-06-09 03:19:04 +0300167 }
168 return result;
169 };
170}