blob: eaef63fcc74dcfaabc4a9e1e0148b2b876918a39 [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.
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 husaind2891082019-10-02 18:03:57 +100047 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 +030048 this.text = text;
49 this.callback = callback;
50 this.state = state;
51 this.action = action;
52 this.params = params;
53 this.blockedForTypes = blockedForTypes;
atif husaind2891082019-10-02 18:03:57 +100054 this.disabledCategory = disabledCategory;
Michael Landoed64b5e2017-06-09 03:19:04 +030055 }
56}
57
58export 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 Lando5b593492018-07-29 16:13:45 +030070 const selectedMenuItem = this.menuItems[this.selectedIndex];
71 if (selectedMenuItem) {
72 this.menuItems[this.selectedIndex].text = newText;
73 }
Michael Landoed64b5e2017-06-09 03:19:04 +030074 }
75}
76
77
78export 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 Landoa5445102018-03-04 14:53:33 +0200101 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 Lando5b593492018-07-29 16:13:45 +0300120 return item.name === selected.name && item.componentType === selected.componentType;
Michael Landoa5445102018-03-04 14:53:33 +0200121 });
122 }
123
124 return selectedItemIdx;
125 };
126
Michael Landoed64b5e2017-06-09 03:19:04 +0300127 generateBreadcrumbsModelFromComponents = (components:Array<Component>, selected:Component):MenuItemGroup => {
128 let result = new MenuItemGroup(0, [], false);
129 if (components) {
Michael Landoa5445102018-03-04 14:53:33 +0200130 result.selectedIndex = this.findBreadcrumbComponentIndex(components, selected);
Michael Landoed64b5e2017-06-09 03:19:04 +0300131 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 Lando5b593492018-07-29 16:13:45 +0300152
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 Landoed64b5e2017-06-09 03:19:04 +0300168 }
169 return result;
170 };
171}