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