blob: 4c25a025da22549f24b12332a5deb4c1841cec39 [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;
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 Amit5197c8b2018-01-15 14:31:42 +020046 constructor(text:string, callback:(...args:Array<any>) => ng.IPromise<boolean>, state:string, action:string, params?:any, blockedForTypes?:Array<string>) {
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;
53 }
54}
55
56export 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) {
Michael Lando5b593492018-07-29 16:13:45 +030068 const selectedMenuItem = this.menuItems[this.selectedIndex];
69 if (selectedMenuItem) {
70 this.menuItems[this.selectedIndex].text = newText;
71 }
Michael Landoed64b5e2017-06-09 03:19:04 +030072 }
73}
74
75
76export class MenuHandler {
77
78 static '$inject' = [
79 'sdcConfig',
80 'sdcMenu',
81 'ComponentFactory',
82 '$filter',
83 'ModalsHandler',
84 '$state',
85 '$q'
86 ];
87
88 constructor(private sdcConfig:IAppConfigurtaion,
89 private sdcMenu:IAppMenu,
90 private ComponentFactory:ComponentFactory,
91 private $filter:ng.IFilterService,
92 private ModalsHandler:ModalsHandler,
93 private $state:ng.ui.IStateService,
94 private $q:ng.IQService) {
95
96 }
97
98
Michael Landoa5445102018-03-04 14:53:33 +020099 findBreadcrumbComponentIndex = (components:Array<Component>, selected:Component):number => {
100 let selectedItemIdx;
101
102 // Search the component in all components by uuid (and not uniqueid, gives access to an assets's minor versions).
103 selectedItemIdx = _.findIndex(components, (item:Component) => {
104 return item.uuid === selected.uuid;
105 });
106
107 // If not found search by invariantUUID
108 if (selectedItemIdx === -1) {
109 selectedItemIdx = _.findIndex(components, (item:Component) => {
110 //invariantUUID && Certified State matches between major versions
111 return item.invariantUUID === selected.invariantUUID && item.lifecycleState === ComponentState.CERTIFIED;
112 });
113 }
114
115 // If not found search by name (name is unique).
116 if (selectedItemIdx === -1) {
117 selectedItemIdx = _.findIndex(components, (item:Component) => {
Michael Lando5b593492018-07-29 16:13:45 +0300118 return item.name === selected.name && item.componentType === selected.componentType;
Michael Landoa5445102018-03-04 14:53:33 +0200119 });
120 }
121
122 return selectedItemIdx;
123 };
124
Michael Landoed64b5e2017-06-09 03:19:04 +0300125 generateBreadcrumbsModelFromComponents = (components:Array<Component>, selected:Component):MenuItemGroup => {
126 let result = new MenuItemGroup(0, [], false);
127 if (components) {
Michael Landoa5445102018-03-04 14:53:33 +0200128 result.selectedIndex = this.findBreadcrumbComponentIndex(components, selected);
Michael Landoed64b5e2017-06-09 03:19:04 +0300129 let clickItemCallback = (component:Component):ng.IPromise<boolean> => {
130 this.$state.go('workspace.general', {
131 id: component.uniqueId,
132 type: component.componentType.toLowerCase(),
133 mode: WorkspaceMode.VIEW
134 });
135 return this.$q.when(true);
136 };
137
138 components.forEach((component:Component) => {
139 let menuItem = new MenuItem(
140 // component.name,
141 component.getComponentSubType() + ': ' + this.$filter('resourceName')(component.name),
142 clickItemCallback,
143 null,
144 null,
145 [component]
146 );
147 // menuItem.text = component.name;
148 result.menuItems.push(menuItem);
149 });
Michael Lando5b593492018-07-29 16:13:45 +0300150
151 result.selectedIndex = this.findBreadcrumbComponentIndex(components, selected);
152
153 // if component does not exist, then add a temporary menu item for the current component
154 if (result.selectedIndex === -1) {
155 let menuItem = new MenuItem(
156 // component.name,
157 selected.getComponentSubType() + ': ' + this.$filter('resourceName')(selected.name),
158 clickItemCallback,
159 null,
160 null,
161 [selected]
162 );
163 result.menuItems.unshift(menuItem);
164 result.selectedIndex = 0;
165 }
Michael Landoed64b5e2017-06-09 03:19:04 +0300166 }
167 return result;
168 };
169}