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