global-search and search user components
[portal.git] / portal-FE-common / src / app / layout / components / global-search / global-search.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 { Component, OnInit, Output, EventEmitter } from '@angular/core';
39 import { GlobalSearchService } from 'src/app/shared/services/global-search/global-search.service';
40 import { GlobalSearchItem } from 'src/app/shared/model/global-search-item.model';
41 import * as $ from 'jquery';
42 import { AddTabFunctionService } from 'src/app/shared/services/tab/add-tab-function.service';
43 import { Router } from '@angular/router';
44 import { environment } from 'src/environments/environment';
45
46 @Component({
47   selector: 'app-global-search',
48   templateUrl: './global-search.component.html',
49   styleUrls: ['./global-search.component.scss']
50 })
51 export class GlobalSearchComponent implements OnInit {
52   searchResDialog: boolean = false;
53   items: any;
54   searchString: string;
55   api = environment.api;
56   constructor(private globalSearchService: GlobalSearchService, private addTabFuntionService: AddTabFunctionService, private router: Router) { }
57
58   ngOnInit() {
59   }
60
61   showHideSearchSnippet() {
62     setTimeout(() => {
63       $('#mainSearchSnippet').click();
64     }, 1000);
65     setTimeout(() => {
66       $('mainSearchText').focus();
67     }, 1000);
68   }
69
70   mainSearchEvent = $('#mainSearchDiv').keyup((event) => {
71     if (event.keyCode == 13) {
72
73       this.getSearchResult(<string><any>$('#mainSearchText').val());
74
75       // opens the popup
76       var popupDomObj = $("[content='searchSnippet.html']");
77       if (popupDomObj.length == 0) {
78         this.showHideSearchSnippet();
79       } else {
80         $('#mainSearchSnippet').click();
81         this.showHideSearchSnippet();
82       }
83
84
85
86     }
87   });
88
89   clickOutSide(event: any) {
90
91     this.searchResDialog = false;
92
93   }
94   searchDialogToggle(event: any) {
95     if (event.keyCode == 13) {
96       this.searchResDialog = true;
97       this.searchString = <string><any>$('#mainSearchText').val();
98       this.getSearchResult(<string><any>$('#mainSearchText').val());
99     }
100   }
101
102   getSearchResult(searchString: string) {
103     //console.log("getSearch Result");
104     this.globalSearchService.getSearchResults(searchString).subscribe(data => {
105       //console.log("Response data" + data);
106       this.items = data.response;
107       //console.log("search result data" + JSON.stringify(data));
108
109     }, error => {
110       console.log('getSearchResult Error Object' + error);
111     });
112   };
113   goToUrl(item: any, type?: any) {
114     //console.log('check goto');
115     //var a = { 'test1': 'value1', 'test2': 'value3', 'test3': 'value2' };
116     if (type == 'intra') {
117
118       var intraSearcLink = this.api.intraSearcLink;
119       var intraSpecSearcLink = intraSearcLink + encodeURIComponent(this.searchString);
120       window.open(intraSpecSearcLink, '_blank');
121
122     } else if (type == 'extra') {
123       var extraSearcLink = this.api.extraSearcLink;
124       var extraSpecSearcLink = extraSearcLink + encodeURIComponent(this.searchString);
125       window.open(extraSpecSearcLink, '_blank');
126     }
127     let url = item.target;
128     let restrictedApp = item.uuid;
129     let getAccessState = "getAccess"
130     console.log("item.target " + item.target + "item.uuid " + item.uuid);
131     if (!url) {
132       this.router.navigate(['/' + getAccessState]);
133       //$log.info('No url found for this application, doing nothing..');
134       return;
135     }
136     if (!restrictedApp) {
137       window.open(url, '_blank');
138     } else {
139       if (item.url == "root.access") {
140         this.router.navigate(['/' + item.url]);
141         var tabContent = { id: new Date(), title: 'Home', url: url };
142         // $cookies.putObject('addTab', tabContent );
143         this.addTabFuntionService.filter(tabContent);
144       } else {
145         var tabContentCtrl = { id: new Date(), title: item.name, url: url };
146         this.addTabFuntionService.filter(tabContentCtrl);
147       }
148     }
149
150   }
151
152 }