blob: d48412a6eddaebe78e6a97ad67cb4332cedb89c3 [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';
22import {WorkspaceMode, ComponentState} from "./constants";
23import {IAppConfigurtaion, IAppMenu, Component} from "../models";
24import {ComponentFactory} from "./component-factory";
25import {ModalsHandler} from "./modals-handler";
26
27export class MenuItem {
28 text:string;
29 callback:(...args:Array<any>) => ng.IPromise<boolean>;
30 state:string;
31 action:string;
Idan Amit5197c8b2018-01-15 14:31:42 +020032 params:any;
Michael Landoed64b5e2017-06-09 03:19:04 +030033 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
Idan Amit5197c8b2018-01-15 14:31:42 +020045 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 +030046 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
55export 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
72export 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
Tal Gitelman51d50f02017-12-10 18:55:03 +0200119 if(!selectedItem){
120 result.selectedIndex = components.length;
121 }else{
122 result.selectedIndex = components.indexOf(selectedItem);
123 }
Michael Landoed64b5e2017-06-09 03:19:04 +0300124 components[result.selectedIndex] = selected;
125 let clickItemCallback = (component:Component):ng.IPromise<boolean> => {
126 this.$state.go('workspace.general', {
127 id: component.uniqueId,
128 type: component.componentType.toLowerCase(),
129 mode: WorkspaceMode.VIEW
130 });
131 return this.$q.when(true);
132 };
133
134 components.forEach((component:Component) => {
135 let menuItem = new MenuItem(
136 // component.name,
137 component.getComponentSubType() + ': ' + this.$filter('resourceName')(component.name),
138 clickItemCallback,
139 null,
140 null,
141 [component]
142 );
143 // menuItem.text = component.name;
144 result.menuItems.push(menuItem);
145 });
146 }
147 return result;
148 };
149}