Catalog alignment
[sdc.git] / catalog-ui / src / app / view-models / onboard-vendor / onboard-vendor-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 "underscore";
23 import {IUserProperties} from "app/models";
24 import {MenuItemGroup, MenuItem} from "app/utils";
25 import {CacheService} from "app/services-ng2";
26 declare var PunchOutRegistry;
27
28 export class BreadcrumbsMenuItem {
29     key:string;
30     displayText:string;
31 }
32
33 export class BreadcrumbsMenu {
34     selectedKey:string;
35     menuItems:Array<BreadcrumbsMenuItem>;
36 }
37
38 export class BreadcrumbsPath {
39     selectedKeys:Array<string>;
40 }
41
42 export class VendorData {
43     breadcrumbs:BreadcrumbsPath;
44 }
45
46 export interface IOnboardVendorViewModelScope extends ng.IScope {
47     vendorData:VendorData;
48     onVendorEvent:Function;
49     topNavMenuModel:Array<MenuItemGroup>;
50     topNavRootMenu:MenuItemGroup;
51     user:IUserProperties;
52     version:string;
53     isLoading:boolean;
54 }
55
56 export class OnboardVendorViewModel {
57     static '$inject' = [
58         '$scope',
59         '$q',
60         'Sdc.Services.CacheService'
61     ];
62
63     private firstControlledTopNavMenu:MenuItemGroup;
64
65     constructor(private $scope:IOnboardVendorViewModelScope,
66                 private $q:ng.IQService,
67                 private cacheService:CacheService) {
68
69         this.$scope.isLoading = true;
70
71         PunchOutRegistry.loadOnBoarding(()=> {
72             this.$scope.isLoading = false;
73         });
74         this.initScope();
75
76     }
77     private initScope = ():void => {
78         this.$scope.vendorData = {
79             breadcrumbs: {
80                 selectedKeys: []
81             }
82         };
83
84         this.$scope.version = this.cacheService.get('version');
85
86         this.$scope.onVendorEvent = (eventName:string, data:any):void => {
87             switch (eventName) {
88                 case 'breadcrumbsupdated':
89                     this.handleBreadcrumbsUpdate(data);
90                     break;
91             }
92         };
93
94         this.$scope.topNavMenuModel = [];
95
96         this.$scope.user = this.cacheService.get('user');
97
98     }
99
100     updateBreadcrumbsPath = (selectedKeys:Array<string>):ng.IPromise<boolean> => {
101         let topNavMenuModel = this.$scope.topNavMenuModel;
102         let startIndex = topNavMenuModel.indexOf(this.firstControlledTopNavMenu);
103         if (startIndex === -1) {
104             startIndex = topNavMenuModel.length;
105         }
106         topNavMenuModel.splice(startIndex + selectedKeys.length);
107         this.$scope.vendorData = {
108             breadcrumbs: {selectedKeys: selectedKeys}
109         };
110
111         return this.$q.when(true);
112     };
113
114     handleBreadcrumbsUpdate(breadcrumbsMenus:Array<BreadcrumbsMenu>):void {
115         let selectedKeys = [];
116         let topNavMenus = breadcrumbsMenus.map((breadcrumbMenu, breadcrumbIndex) => {
117             let topNavMenu = new MenuItemGroup();
118             topNavMenu.menuItems = breadcrumbMenu.menuItems.map(menuItem =>
119                 new MenuItem(
120                     menuItem.displayText,
121                     this.updateBreadcrumbsPath,
122                     null,
123                     null,
124                     [selectedKeys.concat([menuItem.key])]
125                 )
126             );
127             topNavMenu.selectedIndex = _.findIndex(
128                 breadcrumbMenu.menuItems,
129                 menuItem => menuItem.key === breadcrumbMenu.selectedKey
130             );
131             selectedKeys.push(breadcrumbMenu.selectedKey);
132             return topNavMenu;
133         });
134
135         let topNavMenuModel = this.$scope.topNavMenuModel;
136         let len = topNavMenuModel.length;
137         let startIndex = topNavMenuModel.indexOf(this.firstControlledTopNavMenu);
138         if (startIndex === -1) {
139             startIndex = len;
140         }
141         topNavMenuModel.splice(startIndex, len - startIndex);
142         topNavMenuModel.push.apply(topNavMenuModel, topNavMenus);
143         this.firstControlledTopNavMenu = topNavMenus[0];
144
145         if (startIndex === 1 && this.$scope.topNavRootMenu == null) {
146             let topNavRootMenu = topNavMenuModel[0];
147             let onboardItem = topNavRootMenu.menuItems[topNavRootMenu.selectedIndex];
148             let originalCallback = onboardItem.callback;
149             //noinspection TypeScriptValidateTypes
150             onboardItem.callback = (...args) => {
151                 let ret = this.updateBreadcrumbsPath([]);
152                 return originalCallback && originalCallback.apply(undefined, args) || ret;
153             };
154             this.$scope.topNavRootMenu = topNavRootMenu;
155         }
156
157         this.updateBreadcrumbsPath(selectedKeys);
158     }
159 }