2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
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;
28 export class BreadcrumbsMenuItem {
33 export class BreadcrumbsMenu {
35 menuItems:Array<BreadcrumbsMenuItem>;
38 export class BreadcrumbsPath {
39 selectedKeys:Array<string>;
42 export class VendorData {
43 breadcrumbs:BreadcrumbsPath;
46 export interface IOnboardVendorViewModelScope extends ng.IScope {
47 vendorData:VendorData;
48 onVendorEvent:Function;
49 topNavMenuModel:Array<MenuItemGroup>;
50 topNavRootMenu:MenuItemGroup;
56 export class OnboardVendorViewModel {
60 'Sdc.Services.CacheService'
63 private firstControlledTopNavMenu:MenuItemGroup;
65 constructor(private $scope:IOnboardVendorViewModelScope,
66 private $q:ng.IQService,
67 private cacheService:CacheService) {
69 this.$scope.isLoading = true;
71 PunchOutRegistry.loadOnBoarding(()=> {
72 this.$scope.isLoading = false;
77 private initScope = ():void => {
78 this.$scope.vendorData = {
84 this.$scope.version = this.cacheService.get('version');
86 this.$scope.onVendorEvent = (eventName:string, data:any):void => {
88 case 'breadcrumbsupdated':
89 this.handleBreadcrumbsUpdate(data);
94 this.$scope.topNavMenuModel = [];
96 this.$scope.user = this.cacheService.get('user');
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;
106 topNavMenuModel.splice(startIndex + selectedKeys.length);
107 this.$scope.vendorData = {
108 breadcrumbs: {selectedKeys: selectedKeys}
111 return this.$q.when(true);
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 =>
120 menuItem.displayText,
121 this.updateBreadcrumbsPath,
124 [selectedKeys.concat([menuItem.key])]
127 topNavMenu.selectedIndex = _.findIndex(
128 breadcrumbMenu.menuItems,
129 menuItem => menuItem.key === breadcrumbMenu.selectedKey
131 selectedKeys.push(breadcrumbMenu.selectedKey);
135 let topNavMenuModel = this.$scope.topNavMenuModel;
136 let len = topNavMenuModel.length;
137 let startIndex = topNavMenuModel.indexOf(this.firstControlledTopNavMenu);
138 if (startIndex === -1) {
141 topNavMenuModel.splice(startIndex, len - startIndex);
142 topNavMenuModel.push.apply(topNavMenuModel, topNavMenus);
143 this.firstControlledTopNavMenu = topNavMenus[0];
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;
154 this.$scope.topNavRootMenu = topNavRootMenu;
157 this.updateBreadcrumbsPath(selectedKeys);