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