Catalog alignment
[sdc.git] / catalog-ui / src / app / view-models / dcae-app / dcae-app-view-model.ts
1 /*-
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * 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
21 'use strict';
22 import * as _ from "lodash";
23 import {MenuItemGroup, MenuItem} from "app/utils";
24 import {BreadcrumbsPath, BreadcrumbsMenu} from "../onboard-vendor/onboard-vendor-view-model";
25 import {CacheService} from "app/services-ng2";
26 import {IUserProperties} from "app/models";
27
28 export class TestData {
29     breadcrumbs:BreadcrumbsPath;
30 }
31
32 export interface IDcaeAppViewModelScope extends ng.IScope {
33     testData:TestData;
34     onTestEvent:Function;
35     topNavMenuModel:Array<MenuItemGroup>;
36     topNavRootMenu:MenuItemGroup;
37     user:IUserProperties;
38     version:string;
39 }
40
41 export class DcaeAppViewModel {
42     static '$inject' = [
43         '$scope',
44         '$q',
45         'Sdc.Services.CacheService'
46     ];
47
48     private firstControlledTopNavMenu:MenuItemGroup;
49
50     constructor(private $scope:IDcaeAppViewModelScope,
51                 private $q:ng.IQService,
52                 private cacheService:CacheService) {
53
54         this.$scope.testData = {
55             breadcrumbs: {
56                 selectedKeys: []
57             }
58         };
59
60         this.$scope.version = this.cacheService.get('version');
61
62         this.$scope.onTestEvent = (eventName:string, data:any):void => {
63             switch (eventName) {
64                 case 'breadcrumbsupdated':
65                     this.handleBreadcrumbsUpdate(data);
66                     break;
67             }
68         };
69
70         this.$scope.topNavMenuModel = [];
71
72         this.$scope.user = this.cacheService.get('user');
73     }
74
75     updateBreadcrumbsPath = (selectedKeys:Array<string>):ng.IPromise<boolean> => {
76         let topNavMenuModel = this.$scope.topNavMenuModel;
77         let startIndex = topNavMenuModel.indexOf(this.firstControlledTopNavMenu);
78         if (startIndex === -1) {
79             startIndex = topNavMenuModel.length;
80         }
81         topNavMenuModel.splice(startIndex + selectedKeys.length);
82         this.$scope.testData = {
83             breadcrumbs: {selectedKeys: selectedKeys}
84         };
85
86         return this.$q.when(true);
87     };
88
89     handleBreadcrumbsUpdate(breadcrumbsMenus:Array<BreadcrumbsMenu>):void {
90         let selectedKeys = [];
91         let topNavMenus = breadcrumbsMenus.map((breadcrumbMenu, breadcrumbIndex) => {
92             let topNavMenu = new MenuItemGroup();
93             topNavMenu.menuItems = breadcrumbMenu.menuItems.map(menuItem =>
94                 new MenuItem(
95                     menuItem.displayText,
96                     this.updateBreadcrumbsPath,
97                     null,
98                     null,
99                     [selectedKeys.concat([menuItem.key])]
100                 )
101             );
102             topNavMenu.selectedIndex = _.findIndex(
103                 breadcrumbMenu.menuItems,
104                 menuItem => menuItem.key === breadcrumbMenu.selectedKey
105             );
106             selectedKeys.push(breadcrumbMenu.selectedKey);
107             return topNavMenu;
108         });
109
110         let topNavMenuModel = this.$scope.topNavMenuModel;
111         let len = topNavMenuModel.length;
112         let startIndex = topNavMenuModel.indexOf(this.firstControlledTopNavMenu);
113         if (startIndex === -1) {
114             startIndex = len;
115         }
116         topNavMenuModel.splice(startIndex, len - startIndex);
117         topNavMenuModel.push.apply(topNavMenuModel, topNavMenus);
118         this.firstControlledTopNavMenu = topNavMenus[0];
119
120         if (startIndex === 1 && this.$scope.topNavRootMenu == null) {
121             let topNavRootMenu = topNavMenuModel[0];
122             let onboardItem = topNavRootMenu.menuItems[topNavRootMenu.selectedIndex];
123             let originalCallback = onboardItem.callback;
124             onboardItem.callback = (...args) => {
125                 let ret = this.updateBreadcrumbsPath([]);
126                 return originalCallback && originalCallback.apply(undefined, args) || ret;
127             };
128             this.$scope.topNavRootMenu = topNavRootMenu;
129         }
130
131         this.updateBreadcrumbsPath(selectedKeys);
132     }
133 }