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 {IAppConfigurtaion, IHostedApplication, IUserProperties} from "app/models";
23 import {IUserResourceClass} from "app/services";
24 import {MenuItemGroup, MenuItem} from "app/utils";
26 export interface ITopNavScope extends ng.IScope {
27 topLvlSelectedIndex:number;
30 menuModel:Array<MenuItemGroup>;
32 topLvlMenu:MenuItemGroup;
33 goToState(state:string, params:Array<any>):ng.IPromise<boolean>;
34 menuItemClick:Function;
40 export class TopNavDirective implements ng.IDirective {
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) {
49 public replace = true;
50 public restrict = 'E';
51 public transclude = false;
55 topLvlSelectedIndex: '@?',
59 notificationIconCallback: '=',
63 template = ():string => {
64 return require('./top-nav.html');
67 public link = (scope:ITopNavScope, $elem:ng.IAugmentedJQuery, $attrs:angular.IAttributes) => {
69 let getTopLvlSelectedIndexByState = ():number => {
70 if (!scope.topLvlMenu.menuItems) {
76 //set result to current state
77 scope.topLvlMenu.menuItems.forEach((item:MenuItem, index:number)=> {
78 if (item.state === this.$state.current.name) {
83 //if it's a different state , checking previous state param
85 scope.topLvlMenu.menuItems.forEach((item:MenuItem, index:number)=> {
86 if (item.state === this.$state.params['previousState']) {
99 scope.user = this.userResourceService.getLoggedinUser();
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)
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));
116 scope.topLvlMenu = new MenuItemGroup(0, tmpArray, true);
117 scope.topLvlMenu.selectedIndex = isNaN(scope.topLvlSelectedIndex) ? getTopLvlSelectedIndexByState() : scope.topLvlSelectedIndex;
119 let generateMenu = () => {
120 if (scope.menuModel && scope.menuModel[0] !== scope.topLvlMenu) {
121 scope.menuModel.unshift(scope.topLvlMenu);
124 scope.$watch('menuModel', generateMenu);
128 /////scope functions////
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;
137 scope.menuItemClick = (itemGroup:MenuItemGroup, item:MenuItem) => {
139 itemGroup.itemClick = false;
141 let onSuccess = ():void => {
142 itemGroup.selectedIndex = itemGroup.menuItems.indexOf(item);
144 let onFailed = ():void => {
148 (item.callback.apply(undefined, item.params)).then(onSuccess, onFailed);
150 scope[item.action](item.state, item.params).then(onSuccess, onFailed);
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);
161 TopNavDirective.factory.$inject = ['$filter', '$state', '$q', 'Sdc.Services.UserResourceService', 'sdcConfig'];