Merge "JUNIT- DeleteDomainObjectFailedException.java"
[portal.git] / portal-FE-os / src / app / layout / components / header-menu / header-menu.component.ts
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * 
37  */
38 import * as _ from 'underscore';
39 import { Component, OnInit } from '@angular/core';
40 import { MenusService } from 'src/app/shared/services/menus/menus.service';
41 import { AddTabFunctionService } from 'src/app/shared/services/tab/add-tab-function.service';
42 import { Router } from '@angular/router';
43
44 @Component({
45   selector: 'app-header-menu',
46   templateUrl: './header-menu.component.html',
47   styleUrls: ['./header-menu.component.scss']
48 })
49 export class HeaderMenuComponent implements OnInit {
50   hideMenus: boolean[] = [];
51   hideSecondLevelMenus: boolean[][] = [];
52   hideThirdLevelMenus: boolean[] = [];
53   megaMenuDataObject: any[];
54   favoritesMenuItems: any[];
55   favoritesWindow: boolean;
56   showFavorites: boolean;
57   emptyFavorites: boolean;
58   favoriteItemsCount: number;
59
60   constructor(public router: Router, private menusService: MenusService, private addTabFuntionService: AddTabFunctionService) { }
61
62   ngOnInit() {
63     //this.hideMenus[0] = false;
64     this.getFunctionalMenuForUser();
65
66   }
67
68   unflatten(array: any, parent?: any, tree?: any) {
69     tree = typeof tree !== 'undefined' ? tree : [];
70     parent = typeof parent !== 'undefined' ? parent : { menuId: null };
71     var children = _.filter(array, function (child: any) {
72       return child.parentMenuId == parent.menuId;
73     });
74     if (!_.isEmpty(children)) {
75       if (parent.menuId === null) {
76         tree = children;
77       } else {
78         parent['children'] = children
79       }
80       _.each(children, function (child: any) {
81         this.unflatten(array, child)
82       }, this);
83     }
84
85     return tree;
86   }
87   getFunctionalMenuForUser() {
88     this.menusService.getFunctionalMenuForUser().subscribe((jsonHeaderMenu: any) => {
89       this.megaMenuDataObject = this.unflatten(jsonHeaderMenu);
90       // for (let entry of this.megaMenuDataObject) {
91       //   console.log('First level '+entry.text);
92       //   for (let secondLevel of entry.children)
93       //   {
94       //     if(secondLevel)
95       //   {
96       //     console.log('Second level '+secondLevel.text);
97       //     for (let thirdLevel of secondLevel.children)
98       //   {
99       //     console.log('Third level '+thirdLevel.text);
100       //   }
101       //   }
102
103       //   }
104
105       // }
106
107
108     }, (err) => {
109       console.log('HeaderCtrl::GetFunctionalMenuForUser: HeaderCtrl json returned: ' + err);
110     });
111
112   }
113   getFavoriteItems() {
114     this.menusService.getFavoriteItems().toPromise().then((jsonFavourites: any) => {
115       this.favoritesMenuItems = jsonFavourites;
116       if (this.favoritesMenuItems) {
117         this.favoriteItemsCount = this.favoritesMenuItems.length;
118       }
119     }, (err) => {
120       console.log('HeaderCtrl::getFavoriteItems: HeaderCtrl json returned: ' + err);
121     });
122   }
123   loadFirstLevel(index: any) {
124     this.hideMenus = [];
125     this.hideSecondLevelMenus = [];
126     for (let firstLevelIndex = 0; firstLevelIndex < this.megaMenuDataObject.length; firstLevelIndex++) {
127       this.hideMenus.push(false);
128       this.hideSecondLevelMenus.push(new Array(this.megaMenuDataObject[firstLevelIndex].length).fill(false));
129     }
130     this.hideMenus[index] = true;
131     if (!this.favoritesMenuItems) {
132       this.getFavoriteItems();
133     }
134
135   }
136   isUrlFavorite(menuId: any) {
137     if (this.favoritesMenuItems) {
138       var jsonMenu = JSON.stringify(this.favoritesMenuItems);
139       var isMenuFavorite = jsonMenu.indexOf('menuId\":' + menuId);
140       if (isMenuFavorite == -1) {
141         return false;
142       } else {
143         return true;
144       }
145     }
146     else {
147       return false;
148     }
149   }
150   submenuLevelAction(index: any, column: any) {
151     //console.log('index and column' + index + column);
152     if (index == 'Favorites' && this.favoriteItemsCount != 0) {
153       this.favoritesWindow = true;
154       this.showFavorites = true;
155       this.emptyFavorites = false;
156     }
157     if (index == 'Favorites' && this.favoriteItemsCount == 0) {
158       this.favoritesWindow = true;
159       this.showFavorites = false;
160       this.emptyFavorites = true;
161     }
162     if (index != 'Favorites') {
163       this.favoritesWindow = false;
164       this.showFavorites = false;
165       this.emptyFavorites = false;
166     }
167   }
168   hideFavoritesWindow() {
169     this.showFavorites = false;
170     this.emptyFavorites = false;
171   }
172   removeAsFavoriteItem(event: any, menuId: any) {
173     this.menusService.removeFavoriteItem(menuId).subscribe(() => {
174       //angular.element('#' + event.target.id).css('color', '#666666');
175       this.getFavoriteItems();
176     }, (err) => {
177       console.error('HeaderCtrl::removeAsFavoriteItem: API removeFavoriteItem error: ' + err);
178     });
179   }
180   hideThirdLevelMenu(firstLevelIndex: any, secondLevelIndex: any) {
181     this.hideSecondLevelMenus = [];
182     for (let firstLevelIndex = 0; firstLevelIndex < this.megaMenuDataObject.length; firstLevelIndex++) {
183       this.hideSecondLevelMenus.push(new Array(this.megaMenuDataObject[firstLevelIndex].length).fill(false));
184     }
185     this.hideSecondLevelMenus[firstLevelIndex][secondLevelIndex] = true;
186   }
187
188   clickOutSide(event: any) {
189     this.hideMenus = [];
190     this.hideSecondLevelMenus = [];
191     for (let firstLevelIndex = 0; firstLevelIndex < this.megaMenuDataObject.length; firstLevelIndex++) {
192       this.hideMenus.push(false);
193       this.hideSecondLevelMenus.push(new Array(this.megaMenuDataObject[firstLevelIndex].length).fill(false));
194     }
195
196   }
197   setAsFavoriteItem(event: any, menuId: any) {
198
199   }
200   goToUrl(item: any) {
201     //console.log('Get into URL function' + item.url);
202     let url = item.url;
203     let restrictedApp = item.restrictedApp;
204     if (!url) {
205       console.log('HeaderCtrl::goToUrl: No url found for this application, doing nothing..');
206       return;
207     }
208     if (restrictedApp) {
209       window.open(url, '_blank');
210     } else {
211       if (item.url == "getAccess" || item.url == "contactUs") {
212
213         this.router.navigate(['/' + item.url]);
214
215       } else {
216         var tabContent = {
217           id: new Date(),
218           title: item.text,
219           url: item.url,
220           appId: item.appid
221         };
222         this.addTabFuntionService.filter(tabContent);
223       }
224     }
225
226   }
227   auditLog(link: any, action: any) {
228
229   }
230
231 }