[PORTAL-7] Rebase
[portal.git] / ecomp-portal-FE-common / client / app / views / tabs / tabs.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     const HTTP_PROTOCOL_RGX = /https?:\/\//;
23     class  TabsCtrl {
24         constructor(applicationsService, $log, $window, conf, userProfileService, userbarUpdateService, $scope,$cookies,$rootScope,confirmBoxService,auditLogService) {         
25                 // Tab counter
26             var counter = 1;
27             var tabLimit = 6;
28             this.conf = conf;
29             var cookieDomain = this.conf.cookieDomain;
30             // Array to store the tabs
31             $scope.tabs = [];
32             $scope.notificationShow=true;
33             $rootScope.showFooter = "";
34             $cookies.putObject('show_app_header', false,{domain: cookieDomain, path : '/'});
35
36
37             let noRefresh = function () {
38                     window.onbeforeunload = function(e) {
39
40                         var isQtoHref = false;
41                         try{
42                                 isQtoHref = e.srcElement.activeElement.href.includes("to");
43                         } catch(err) {
44
45                         }
46
47                         if ($scope.tabs.length > 1 && isQtoHref == false) {
48                             return "Changes you made may not be saved. Are you sure you want to refresh?";
49                         } else {
50                             return null;
51                         }
52                     }
53             }
54             // Add tab to the end of the array
55             var addTab = function (title, content) {
56                 if($scope.tabs.length===tabLimit){
57                         //alert
58                         confirmBoxService.showInformation('You have reached your maximum limit of tabs allowed.').then(isConfirmed => {});
59                 } else {
60                         // console.log(window.performance.memory);
61                         // var usedperc = (window.performance.memory.usedJSHeapSize/window.performance.memory.jsHeapSizeLimit)*100;
62                         // console.log('Current memory usage: '+usedperc+'%');
63                         if(title!=='Home' && content.indexOf('https') == -1){
64                                 console.log('App URL: '+content+'. The application URL you are trying to open is not HTTPS. We recommend to use secured HTTPS URL while on-boarding the application.');
65                                 //confirmBoxService.showInformation('The application URL you are trying to open is not HTTPS. We recommend to use secured HTTPS URL while on-boarding the application.').then(isConfirmed => {});
66                         }
67                         
68                     $scope.tabs.push({ title: title, content: content });
69                     counter++;
70                     //$scope.tabs[$scope.tabs.length - 1].disabled = false;
71                     $scope.selectedIndex = $scope.tabs.length - 1;
72                     if ($scope.tabs.length > 1) {
73                         noRefresh();
74                     }
75                     $cookies.putObject('cookieTabs', $scope.tabs,{domain: cookieDomain, path : '/'});
76                     $cookies.putObject('visInVisCookieTabs', $scope.tabs,{domain: cookieDomain, path : '/'});
77                 }
78             };
79             
80             //with APP ID
81             var addTab = function (title, content, appId) {
82                 if($scope.tabs.length===tabLimit){
83                         //alert
84                         confirmBoxService.showInformation('You have reached your maximum limit of tabs allowed.').then(isConfirmed => {});
85                 } else {                
86                         if(title!=='Home' && content.indexOf('https') === -1){
87                                 console.log('App URL: '+content+'. The application URL you are trying to open is not HTTPS. We recommend to use secured HTTPS URL while on-boarding the application.');
88                         }
89                     $scope.tabs.push({ title: title, content: content, appId:appId });
90                     counter++;
91                     $scope.selectedIndex = $scope.tabs.length - 1;
92                     if ($scope.tabs.length > 1) {
93                         noRefresh();
94                     }
95                     $cookies.putObject('cookieTabs', $scope.tabs,{domain: cookieDomain, path : '/'});
96                     $cookies.putObject('visInVisCookieTabs', $scope.tabs,{domain: cookieDomain, path : '/'});
97                 }
98             };
99             
100             // adjust title - trim the title and append ...
101             var adjustTitle = function (title) {
102                 var index = 15;
103                 var nonEmptyCharPattern = /(\s|\w)/;
104                 var adjustedTitle = title.substring(0,index);
105                 var ext = title.charAt(index).replace(nonEmptyCharPattern,'...');
106                 return adjustedTitle.concat(ext);
107                 
108                 
109             };
110             
111             //store audit log
112             $scope.auditLog = function(app) {
113                 $log.debug('auditLog::auditLog: auditLog.ping() = ' + app);
114                 var comment = '';
115                 if(app.content==null || app.content=='')
116                         comment= app.title;
117                 else
118                         comment = app.content;
119                 auditLogService.storeAudit(app.appId, 'tab', comment);
120                 };
121             
122             // Remove tab by index
123             var removeTab = function (event, index) {
124               event.preventDefault();
125               event.stopPropagation();
126               $scope.tabs.splice(index, 1);
127               $cookies.putObject('cookieTabs', $scope.tabs,{domain: cookieDomain, path : '/'});
128             };
129             
130           //adjust height of the tab due to the search popup being hidden 
131                 $scope.adjustTabStyle = function(title){
132                         if(title=='Home'){
133                                 $(".w-ecomp-tabs").css('height',"50px");
134                         }else{
135                                 $(".w-ecomp-tabs").css('height',"100%");
136                         }
137                 }
138                 
139            // select tab 
140             var selectTab = function (title) {
141                 $scope.adjustTabStyle(title);
142                 if(title==='Home') {
143                     $rootScope.ContentModel.IsVisible=true;
144                     $rootScope.showFooter = true;
145                     $rootScope.tabBottom = 75;
146                     userbarUpdateService.setRefreshCount(userbarUpdateService.maxCount);
147                 }
148                 else {
149                     $rootScope.ContentModel.IsVisible=false;
150                     $rootScope.showFooter = false;
151                     $rootScope.tabBottom = 0;
152                 }
153             };
154
155             // Initialize the scope functions
156             $scope.addTab    = addTab;
157             $scope.removeTab = removeTab;
158             $scope.selectTab = selectTab;
159             $scope.adjustTitle = adjustTitle;
160             
161
162             $rootScope.ContentModel = {
163                     IsVisible : false,
164                     ViewUrl : null,
165                 };
166             
167             
168                 var sessionActive = applicationsService.ping()
169                 .then(sessionActive => {
170             // $log.debug('TabsCtrl::addTab: applicationsService.ping() = ' + JSON.stringify(sessionActive));
171                 // For demonstration add 5 tabs
172                
173             var cookieTabs = $cookies.getObject('cookieTabs');
174                 if(cookieTabs!=null){
175                         for(var t in cookieTabs){
176                                 // console.log('TabsCtrl::addTab: cookieTabs title: '+cookieTabs[t].title);
177                                 if(cookieTabs[t].title!=null && cookieTabs[t].title==='Home'){
178                                         cookieTabs[t].content = "";
179                                         $rootScope.ContentModel.IsVisible=true;
180                                 addTab( cookieTabs[t].title, cookieTabs[t].content,1) ;
181                                 }else{
182                                 addTab( cookieTabs[t].title, cookieTabs[t].content,cookieTabs[t].appId) ;
183                                 }
184                                         
185                         }
186                 } else {
187             for (var i = 0; i < 1; i++) {
188                 var content="";
189                 var title="";
190                 var appId=""
191                 if(i===0){
192                         title="Home";
193                         $rootScope.ContentModel.IsVisible=true;
194                     addTab(title, content,1);
195                 }else{
196                     addTab(title, content,appId);
197                 } 
198             }
199                 }
200             
201
202             
203             //$scope.tabs[$scope.tabs.length - 1].active = false;
204             //$scope.tabs[0].disabled = false;
205             $scope.selectedIndex = 0;
206                 });
207                 
208             $scope.$watchCollection(function() { return $cookies.getObject('addTab'); }, function(newValue) {
209                 // $log.log('Cookie string: ' + $cookies.get('test'))
210                 var tabContent = $cookies.getObject('addTab');
211                 if(tabContent!=null && tabContent.url!=null){
212                         var tabExists = false;
213                         for(var x in $scope.tabs){
214                                 // console.log($scope.tabs[x].content);
215                                 if($scope.tabs[x].title==tabContent.title){
216                                         tabExists = true;
217                                         //$scope.tabs[x].disabled = false;
218                                         $scope.selectedIndex = x;
219                             // added dummy variable to get iframe reloded if tab is already opened.
220                             if(tabContent.url.indexOf('?')===-1){
221                                 $scope.tabs[x].content=tabContent.url+'?dummyVar='+(new Date()).getTime();
222                             }
223                             else{
224                                 $scope.tabs[x].content=tabContent.url+'&dummyVar='+(new Date()).getTime();
225                             }
226                                 }
227                         }
228                         if(!tabExists){
229                          addTab( tabContent.title, tabContent.url,tabContent.appId) ;
230                         }
231                         $cookies.remove('addTab');
232                 }
233             });
234         }
235     }
236     TabsCtrl.$inject = ['applicationsService', '$log', '$window', 'conf', 'userProfileService', 'userbarUpdateService', '$scope','$cookies','$rootScope','confirmBoxService','auditLogService'];
237     angular.module('ecompApp').controller('TabsCtrl', TabsCtrl);
238     
239     angular.module('ecompApp').directive('mainArea', function() {
240         return {
241             restrict: "E",
242             templateUrl: 'app/views/tabs/tabframe.html',
243             link: function(scope, element) {
244                                 
245                 //var iframeId = "#tabframe-" + scope.$parent.tab.title.split(' ').join('-');
246                 // jQuery(iframeId).load(function() {
247                 //        alert("hello");
248                 //    }); //.attr("src",'{{tab.content | trusted}}' ); //src='{{tab.content | trusted}}'
249                 // jQuery(iframeId).attr('src', '{{tab.content | trusted}}');
250                  
251                 //element.childNodes[0].on('load', function() {
252                 //      alert('hello');
253                 //});
254             }
255         }
256     });
257     
258    
259     
260     angular.module('ecompApp').directive('tabHighlight', [function () {
261         return {
262           restrict: 'A',
263           link: function (scope, element) {
264             // Here is the major jQuery usage where we add the event
265             // listeners mousemove and mouseout on the tabs to initalize
266             // the moving highlight for the inactive tabs
267             var x, y, initial_background = '#c3d5e6';
268
269             element
270               .removeAttr('style')
271               .mousemove(function (e) {
272                 // Add highlight effect on inactive tabs
273                 if(!element.hasClass('md-active'))
274                 {
275                   x = e.pageX - this.offsetLeft;
276                   y = e.pageY - this.offsetTop;
277
278                   // Set the background when mouse moves over inactive tabs
279                   element
280                     .css({ background: '-moz-radial-gradient(circle at ' + x + 'px ' + y + 'px, rgba(255,255,255,0.4) 0px, rgba(255,255,255,0.0) 45px), ' + initial_background })
281                     .css({ background: '-webkit-radial-gradient(circle at ' + x + 'px ' + y + 'px, rgba(255,255,255,0.4) 0px, rgba(255,255,255,0.0) 45px), ' + initial_background })
282                     .css({ background: 'radial-gradient(circle at ' + x + 'px ' + y + 'px, rgba(255,255,255,0.4) 0px, rgba(255,255,255,0.0) 45px), ' + initial_background });
283                 }
284               })
285               .mouseout(function () {
286                 // Return the inital background color of the tab
287                 element.removeAttr('style');
288               });
289           }
290         };
291       }]);
292
293
294     
295 })();
296
297 function getParameterByName(name, url) {
298     if (!url) url = window.location.href;
299     name = name.replace(/[\[\]]/g, "\\$&");
300     var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
301         results = regex.exec(url);
302     if (!results) return '';
303     if (!results[2]) return '';
304     return (results[2].replace(/\+/g, " "));
305 }
306
307 function isCascadeFrame(ref) {
308           // alert(ref.id);
309            if (self != top) {
310                    var e = document.body;
311                    e.parentNode.removeChild(e);
312                    window.location = "unKnownError";
313                    }
314 }