3cb22bfd9243a038954de5450f744418981a082f
[portal.git] / ecomp-portal-FE-os / client / src / views / search / search.controller.js
1 /*-
2  * ================================================================================
3  * ECOMP Portal
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
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  * ================================================================================
19  */
20 'use strict';
21 (function () {
22
23     class SearchCtrl {
24         constructor($log, $scope, $cookies, $timeout, userProfileService, sessionService, dashboardService) {
25             $scope.firstName="";
26             $scope.lastName="";
27             
28             function  showHideSearchSnippet() {
29                 
30                 setTimeout(function() {
31                                 jQuery("#mainSearchSnippet").click();
32                                 },500);
33                         
34                         setTimeout(function() {
35                                 jQuery("#mainSearchText").focus();
36                                 },1000);
37             }
38             
39             jQuery("#mainSearchDiv").keyup(function(event){
40                 if(event.keyCode == 13){
41                     // there is a watch on this variable which will trigger the database pull
42                         dashboardService.searchString = jQuery("#mainSearchText").val();
43                         
44                         // opens the popup
45                     var popupDomObj = jQuery("[content='searchSnippet.html']");
46                         if(popupDomObj.length == 0) {
47                                 showHideSearchSnippet();
48                         } else {
49                                 jQuery("#mainSearchSnippet").click();
50                                 showHideSearchSnippet();
51                         }
52                                                 
53                         
54                          
55                 }
56             });
57             
58             
59             
60         }
61         
62         
63     }
64     
65     
66     class SearchSnippetCtrl {
67         constructor($log, $scope, $cookies, $timeout, userProfileService, sessionService, dashboardService,applicationsService, $window, $state) {
68             $scope.firstName="";
69             $scope.lastName="";
70             $scope.goToUrl = goToUrl;
71             $scope.dService = dashboardService;
72             
73             $scope.searchResDialog = false;
74             $scope.searchDialogToggle = function($event){
75                 var keyCode = $event.which || $event.keyCode;
76                 if (keyCode === 13) {
77                         $scope.searchResDialog = true;               
78                         dashboardService.searchString = jQuery("#mainSearchText").val();
79                 }
80                 
81             }
82             
83             window.onclick = function() {
84                         if ($scope.searchResDialog) {
85                                 $scope.searchResDialog = false;
86                                 $scope.$apply();
87                         }
88                 };      
89             function goToUrl (item, type) {
90                 $log.info("goToUrl called")
91                 $log.info(item + "/" + type);
92                 
93                 
94                 if(type == 'intra') {
95                         
96                         var intraSearcLink = "";
97                         var intraSpecSearcLink = intraSearcLink + encodeURIComponent(dashboardService.searchString);
98                         $window.open(intraSpecSearcLink, '_blank');
99                         
100                 } else if (type == 'extra') {
101                         var extraSearcLink = "";
102                         var extraSpecSearcLink = extraSearcLink + encodeURIComponent(dashboardService.searchString);
103                         $window.open(extraSpecSearcLink, '_blank');
104                 }
105
106                 let url = item.target;
107                 let restrictedApp = item.uuid;
108                 let getAccessState =  "root.getAccess"
109                 if (!url) {
110                         
111                         applicationsService.goGetAccessAppName = item.name;
112                         if($state.current.name == getAccessState)
113                                 $state.reload();
114                         else
115                                 $state.go(getAccessState);
116                     //$log.info('No url found for this application, doing nothing..');
117                     return;
118                 }
119                 
120                 if (restrictedApp != "true") {
121                     $window.open(url, '_blank');
122                 } else {
123                         if(item.url=="root.access"){
124                                 $state.go(url);
125                                 var tabContent = { id: new Date(), title: 'Home', url: url };
126                         $cookies.putObject('addTab', tabContent );
127                         } else {
128                         var tabContent = { id: new Date(), title: item.name, url: url };
129                         $cookies.putObject('addTab', tabContent );
130                     }
131                 }
132
133             }
134             
135             function getItems(searchString) {
136                 
137                 var items;
138                 var itemMap = dashboardService.getSearchAllByStringResults(searchString)                
139                  .then(res => {
140                          $scope.items = res;
141                          
142                          
143                  }).catch(err => {
144                          $scope.items = [];
145                      $log.error('Couldnt get search results...', err)
146                  });
147
148             }
149             
150             $scope.$watch('dService.searchString', function(searchString) {
151                 if(searchString != undefined )
152                         getItems(searchString);
153
154             });
155             
156              
157             
158         }
159         
160         
161         
162     }
163     
164     
165
166     SearchCtrl.$inject = ['$log', '$scope', '$cookies', '$timeout','userProfileService', 'sessionService', 'dashboardService'];
167     SearchSnippetCtrl.$inject = ['$log', '$scope', '$cookies', '$timeout','userProfileService', 'sessionService', 'dashboardService','applicationsService', '$window','$state'];
168     angular.module('ecompApp').controller('searchCtrl', SearchCtrl); 
169     angular.module('ecompApp').controller('searchSnippetCtrl', SearchSnippetCtrl);
170     angular.module('ecompApp').directive('searchBox', function() {
171         return {
172             restrict: "E",
173             templateUrl: 'app/views/search/search.tpl.html',
174             link: function(scope, element) {
175                                 
176                 //var iframeId = "#tabframe-" + scope.$parent.tab.title.split(' ').join('-');
177                 // jQuery(iframeId).load(function() {
178                 //        alert("hello");
179                 //    }); //.attr("src",'{{tab.content | trusted}}' ); //src='{{tab.content | trusted}}'
180                 // jQuery(iframeId).attr('src', '{{tab.content | trusted}}');
181                  
182                 //element.childNodes[0].on('load', function() {
183                 //      alert('hello');
184                 //});
185             }
186         }
187     });
188     angular.module( 'ecompApp' ).config( [
189         '$compileProvider',
190         function( $compileProvider )
191         {   
192             $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto|chrome-extension|qto):/);
193         }
194     ]);
195
196 })();
197
198