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