[SDC-29] catalog 1707 rebase commit.
[sdc.git] / catalog-ui / src / app / directives / ecomp-header / ecomp-header.ts
1 'use strict';
2 import {IAppConfigurtaion, User, IUser} from "app/models";
3 import {IUserResourceClass, IUserResource} from "app/services";
4
5 export class MenuItem {
6     menuId:number;
7     column:number;
8     text:string;
9     parentMenuId:number;
10     url:string;
11     children:Array<MenuItem>
12 }
13
14 export interface IEcompHeaderDirectiveScope extends ng.IScope {
15     menuData:Array<MenuItem>;
16     version:string;
17     clickableLogo:string;
18     contactUsUrl:string;
19     getAccessUrl:string;
20     megaMenuDataObjectTemp:Array<any>;
21     megaMenuDataObject:Array<any>;
22
23     selectedTopMenu:MenuItem;
24     selectedSubMenu:MenuItem;
25
26     firstMenuLevelClick:Function;
27     subMenuEnterAction:Function;
28     subMenuLeaveAction:Function;
29
30     memuItemClick:Function;
31     user:IUser;
32 }
33
34 export class EcompHeaderDirective implements ng.IDirective {
35
36     constructor(private $http:ng.IHttpService,
37                 private sdcConfig:IAppConfigurtaion,
38                 private UserResourceClass:IUserResourceClass) {
39
40     }
41
42     scope = {
43         menuData: '=',
44         version: '@',
45         clickableLogo: '@?'
46     };
47
48     public replace = true;
49     public restrict = 'E';
50     public controller = EcompHeaderController;
51
52     template = ():string => {
53         return 'src/app/directives/ecomp-header/ecomp-header.html';
54     };
55
56     link = ($scope:IEcompHeaderDirectiveScope, $elem:JQuery, attr:any) => {
57
58         if (!$scope.clickableLogo) {
59             $scope.clickableLogo = "true";
60         }
61
62         let findMenuItemById = (menuId):MenuItem => {
63             let selectedMenuItem:MenuItem = _.find($scope.menuData, (item:MenuItem)=> {
64                 if (item.menuId === menuId) {
65                     return item;
66                 }
67             });
68             return selectedMenuItem;
69         };
70
71         let initUser = ():void => {
72             let defaultUserId:string;
73             let user:IUserResource = this.UserResourceClass.getLoggedinUser();
74             if (!user) {
75                 defaultUserId = this.$http.defaults.headers.common[this.sdcConfig.cookie.userIdSuffix];
76                 user = this.UserResourceClass.get({id: defaultUserId}, ():void => {
77                     $scope.user = new User(user);
78                 });
79             } else {
80                 $scope.user = new User(user);
81             }
82         };
83
84         $scope.firstMenuLevelClick = (menuId:number):void => {
85             let selectedMenuItem:MenuItem = _.find($scope.megaMenuDataObjectTemp, (item:MenuItem)=> {
86                 if (item.menuId === menuId) {
87                     return item;
88                 }
89             });
90             if (selectedMenuItem) {
91                 $scope.selectedTopMenu = selectedMenuItem;
92                 //console.log("Selected menu item: " + selectedMenuItem.text);
93             }
94         };
95
96         $scope.subMenuEnterAction = (menuId:number):void => {
97             $scope.selectedSubMenu = findMenuItemById(menuId);
98         };
99
100         $scope.subMenuLeaveAction = (menuId:number):void => {
101             $scope.selectedTopMenu = undefined;
102         };
103
104         $scope.memuItemClick = (menuItem:MenuItem):void => {
105             if (menuItem.url) {
106                 window.location.href = menuItem.url;
107             } else {
108                 console.log("Menu item: " + menuItem.text + " does not have defined URL!");
109             }
110         };
111
112         initUser();
113
114     };
115
116     public static factory = ($http:ng.IHttpService,
117                              sdcConfig:IAppConfigurtaion,
118                              UserResourceClass:IUserResourceClass)=> {
119         return new EcompHeaderDirective($http, sdcConfig, UserResourceClass);
120     };
121
122 }
123
124 export class EcompHeaderController {
125
126     messages:any;
127     getAttachId:Function;
128     render:any;
129     reRender:Function;
130     register:Function;
131     deregister:Function;
132     head:any;
133
134     static '$inject' = [
135         '$element',
136         '$scope',
137         '$attrs',
138         '$animate'
139     ];
140
141     constructor(private $element:JQuery,
142                 private $scope:IEcompHeaderDirectiveScope,
143                 private $attrs:ng.IAttributes,
144                 private $animate:any) {
145
146         this.$scope = $scope;
147
148         this.$scope.$watch('menuData', (newVal, oldVal) => {
149             if (newVal) {
150                 this.init();
151             }
152         });
153
154     }
155
156     init = ():void => {
157
158         this.$scope.contactUsUrl = "https://wiki.web.att.com/display/EcompPortal/ECOMP+Portal+Home";
159         this.$scope.getAccessUrl = "http://ecomp-tlv-dev2.uccentral.att.com:8080/ecompportal/get_access";
160
161         let unflatten = (array, parent?, tree?) => {
162             tree = typeof tree !== 'undefined' ? tree : [];
163             parent = typeof parent !== 'undefined' ? parent : {menuId: null};
164             let children = _.filter(array, function (child) {
165                 return child["parentMenuId"] == parent.menuId;
166             });
167             if (!_.isEmpty(children)) {
168                 if (parent.menuId === null) {
169                     tree = children;
170                 } else {
171                     parent['children'] = children
172                 }
173                 _.each(children, function (child) {
174                     unflatten(array, child)
175                 });
176             }
177             return tree;
178         };
179
180         let menuStructureConvert = (menuItems) => {
181             console.log(menuItems);
182             this.$scope.megaMenuDataObjectTemp = [
183                 {
184                     menuId: 1001,
185                     text: "ECOMP",
186                     children: menuItems
187                 },
188                 {
189                     menuId: 1002,
190                     text: "Help",
191                     children: [
192                         {
193                             text: "Contact Us",
194                             url: this.$scope.contactUsUrl
195                         }]
196                 }
197             ];
198
199             /*{
200              text:"Get Access",
201              url: this.$scope.getAccessUrl
202              }*/
203             return this.$scope.megaMenuDataObjectTemp;
204         };
205
206         let a = unflatten(this.$scope.menuData);
207         this.$scope.megaMenuDataObject = menuStructureConvert(a);
208         //console.log(this.$scope.megaMenuDataObject);
209     };
210 }
211
212 EcompHeaderDirective.factory.$inject = ['$http', 'sdcConfig', 'Sdc.Services.UserResourceService'];
213
214
215
216
217