6487d745faa8d683d11b9fc521750bca5548940d
[portal/sdk.git] /
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal SDK
4  * ===================================================================
5  * Copyright © 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 { Component, Output, EventEmitter, OnInit } from '@angular/core';
39 import { Router, NavigationEnd } from '@angular/router';
40 import { SidebarService} from '../../../shared/services/index'
41 import { CookieService } from 'ngx-cookie-service';
42
43 @Component({
44     selector: 'app-sidebar',
45     templateUrl: './sidebar.component.html',
46     styleUrls: ['./sidebar.component.scss']
47 })
48 export class SidebarComponent implements OnInit {
49     isActive: boolean;
50     collapsed: boolean;
51     showMenu: string;
52     pushRightClass: string;
53     result:any;
54     leftParentData;
55     leftChildData;
56     hash:string="#";  
57     menuData:Array<object> = [];
58     page;
59     showHeader:boolean = true;
60
61     @Output() collapsedEvent = new EventEmitter<boolean>();
62
63     constructor(public router: Router, public sidebarService: SidebarService,public cookieService: CookieService) {
64         this.router.events.subscribe(val => {
65             if (
66                 val instanceof NavigationEnd &&
67                 window.innerWidth <= 992 &&
68                 this.isToggled()
69             ) {
70                 this.toggleSidebar();
71             }
72         });
73     }
74
75     ngOnInit() {
76         if(this.cookieService.get('show_app_header') == 'false'){
77             this.showHeader = false;
78         } 
79         this.isActive = false;
80         this.collapsed = false;
81         this.showMenu = '';
82         this.pushRightClass = 'push-right';
83         this.sidebarService.getLeftMenu()
84         .subscribe(data => {
85
86         this.result = data;
87         this.leftParentData = JSON.parse(this.result.data);
88         this.leftChildData = JSON.parse(this.result.data2);
89         
90         for (let  i = 0; i < this.leftParentData.length; i++) {    
91         let parentItem={
92             name:null,
93             imageSrc:null,
94             href:null,
95             target : null,
96             menuItems:[]
97           };
98     
99          
100           parentItem.name = this.leftParentData[i].label;
101           parentItem.imageSrc = this.leftParentData[i].imageSrc;
102           // Add link to items with no subitems
103           if (this.leftChildData[i].length === 0){
104             parentItem.href = this.leftParentData[i].action;
105             parentItem.target = this.leftParentData[i].target;
106           }else{
107              
108           
109           for (let j = 0; j < this.leftChildData[i].length; j++) {
110     
111             let childItem={
112               name:null,
113               href:null,
114               target : null,
115               router:null
116             };
117             if (this.leftChildData[i][j].label != null && this.leftChildData[i][j].label.length > 0) {
118              
119               childItem.name = this.leftChildData[i][j].label;
120               if(null!==this.leftChildData[i][j].action){
121               if(this.leftChildData[i][j].action.match('v2/*'))
122                   childItem.router = '/'+ this.leftChildData[i][j].action;
123               else childItem.href = this.leftChildData[i][j].action;
124               childItem.target = this.leftChildData[i][j].target;
125               parentItem.menuItems.push(childItem)
126               }
127             }
128           }
129         }
130          
131          this.menuData.push(parentItem);
132          
133         }
134     });
135
136     }
137     eventCalled() {
138         this.isActive = !this.isActive;
139     }
140
141     addExpandClass(element: any) {
142         if (element === this.showMenu) {
143             this.showMenu = '0';
144         } else {
145             this.showMenu = element;
146         }
147     }
148
149     toggleCollapsed() {
150         this.collapsed = !this.collapsed;
151         this.collapsedEvent.emit(this.collapsed);
152     }
153
154     isToggled(): boolean {
155         const dom: Element = document.querySelector('body');
156         return dom.classList.contains(this.pushRightClass);
157     }
158
159     toggleSidebar() {
160         const dom: any = document.querySelector('body');
161         dom.classList.toggle(this.pushRightClass);
162     }
163
164     onLoggedout() {
165         localStorage.removeItem('isLoggedin');
166     }
167 }