[sdc] rebase update
[sdc.git] / catalog-ui / src / app / directives / layout / top-nav / top-nav.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 {IAppConfigurtaion, IHostedApplication, IUserProperties} from "app/models";
23 import {IUserResourceClass} from "app/services";
24 import {MenuItemGroup, MenuItem} from "app/utils";
25
26 export interface ITopNavScope extends ng.IScope {
27     topLvlSelectedIndex:number;
28     hideSearch:boolean;
29     searchBind:any;
30     menuModel:Array<MenuItemGroup>;
31
32     topLvlMenu:MenuItemGroup;
33     goToState(state:string, params:Array<any>):ng.IPromise<boolean>;
34     menuItemClick:Function;
35     user:IUserProperties;
36     version:string;
37 }
38
39
40 export class TopNavDirective implements ng.IDirective {
41
42     constructor(private $filter:ng.IFilterService,
43                 private $state:ng.ui.IStateService,
44                 private $q:ng.IQService,
45                 private userResourceService:IUserResourceClass,
46                 private sdcConfig:IAppConfigurtaion) {
47     }
48
49     public replace = true;
50     public restrict = 'E';
51     public transclude = false;
52
53
54     scope = {
55         topLvlSelectedIndex: '@?',
56         hideSearch: '=',
57         searchBind: '=',
58         version: '@',
59         notificationIconCallback: '=',
60         menuModel: '=?',
61     };
62
63     template = ():string => {
64         return require('./top-nav.html');
65     };
66
67     public link = (scope:ITopNavScope, $elem:ng.IAugmentedJQuery, $attrs:angular.IAttributes) => {
68
69         let getTopLvlSelectedIndexByState = ():number => {
70             if (!scope.topLvlMenu.menuItems) {
71                 return 0;
72             }
73
74             let result = -1;
75
76             //set result to current state
77             scope.topLvlMenu.menuItems.forEach((item:MenuItem, index:number)=> {
78                 if (item.state === this.$state.current.name) {
79                     result = index;
80                 }
81             });
82
83             //if it's a different state , checking previous state param
84             if (result === -1) {
85                 scope.topLvlMenu.menuItems.forEach((item:MenuItem, index:number)=> {
86                     if (item.state === this.$state.params['previousState']) {
87                         result = index;
88                     }
89                 });
90             }
91
92             if (result === -1) {
93                 result = 0;
94             }
95
96             return result;
97         };
98
99         scope.user = this.userResourceService.getLoggedinUser();
100
101         let tmpArray:Array<MenuItem> = [
102             new MenuItem(this.$filter('translate')("TOP_MENU_HOME_BUTTON"), null, "dashboard", "goToState", null, null),
103             new MenuItem(this.$filter('translate')("TOP_MENU_CATALOG_BUTTON"), null, "catalog", "goToState", null, null)
104         ];
105
106         // Only designer can perform onboarding
107         if (scope.user && scope.user.role === 'DESIGNER') {
108             tmpArray.push(new MenuItem(this.$filter('translate')("TOP_MENU_ON_BOARD_BUTTON"), null, "onboardVendor", "goToState", null, null));
109             _.each(this.sdcConfig.hostedApplications, (hostedApp:IHostedApplication)=> {
110                 if (hostedApp.exists) {
111                     tmpArray.push(new MenuItem(hostedApp.navTitle, null, hostedApp.defaultState, "goToState", null, null));
112                 }
113             });
114         }
115
116         scope.topLvlMenu = new MenuItemGroup(0, tmpArray, true);
117         scope.topLvlMenu.selectedIndex = isNaN(scope.topLvlSelectedIndex) ? getTopLvlSelectedIndexByState() : scope.topLvlSelectedIndex;
118
119         let generateMenu = () => {
120             if (scope.menuModel && scope.menuModel[0] !== scope.topLvlMenu) {
121                 scope.menuModel.unshift(scope.topLvlMenu);
122             }
123         };
124         scope.$watch('menuModel', generateMenu);
125
126         generateMenu();
127
128         /////scope functions////
129
130         scope.goToState = (state:string, params:Array<any>):ng.IPromise<boolean> => {
131             let deferred = this.$q.defer();
132             this.$state.go(state, params && params.length > 0 ? [0] : undefined);
133             deferred.resolve(true);
134             return deferred.promise;
135         };
136
137         scope.menuItemClick = (itemGroup:MenuItemGroup, item:MenuItem) => {
138
139             itemGroup.itemClick = false;
140
141             let onSuccess = ():void => {
142                 itemGroup.selectedIndex = itemGroup.menuItems.indexOf(item);
143             };
144             let onFailed = ():void => {
145             };
146
147             if (item.callback) {
148                 (item.callback.apply(undefined, item.params)).then(onSuccess, onFailed);
149             } else {
150                 scope[item.action](item.state, item.params).then(onSuccess, onFailed);
151             }
152         };
153     };
154
155     public static factory = ($filter:ng.IFilterService, $state:ng.ui.IStateService, $q:ng.IQService, userResourceService:IUserResourceClass, sdcConfig:IAppConfigurtaion)=> {
156         return new TopNavDirective($filter, $state, $q, userResourceService, sdcConfig);
157     };
158
159 }
160
161 TopNavDirective.factory.$inject = ['$filter', '$state', '$q', 'Sdc.Services.UserResourceService', 'sdcConfig'];