nexus site path corrected
[portal.git] / ecomp-portal-FE / 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, $scope,$cookies,$rootScope,confirmBoxService) {
25
26             var counter = 1;
27             var tabLimit = 6;
28             this.conf = conf;
29             var cookieDomain = this.conf.cookieDomain;
30             $scope.tabs = [];
31             $scope.notificationShow=true;
32             $rootScope.showFooter = "";
33             console.log("****************** get cookie domain from config json file " + this.conf.cookieDomain);
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("mailto");
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             var addTab = function (title, content) {
55                 if($scope.tabs.length==tabLimit){
56                         confirmBoxService.showInformation('You have reached your maximum limit of tabs allowed.').then(isConfirmed => {});
57                 } else {
58                         if(title!=='Home' && content.indexOf('https') == -1){
59                                 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.');
60                         }
61                         
62                     $scope.tabs.push({ title: title, content: content });
63                     counter++;
64                     $scope.selectedIndex = $scope.tabs.length - 1;
65                     if ($scope.tabs.length > 1) {
66                         noRefresh();
67                     }
68                     $cookies.putObject('cookieTabs', $scope.tabs,{domain: cookieDomain, path : '/'});
69                     $cookies.putObject('visInVisCookieTabs', $scope.tabs,{domain: cookieDomain, path : '/'});
70                 }
71             };
72             
73             var adjustTitle = function (title) {
74                 var index = 15;
75                 var nonEmptyCharPattern = /(\s|\w)/;
76                 var adjustedTitle = title.substring(0,index);
77                 var ext = title.charAt(index).replace(nonEmptyCharPattern,'...');
78                 return adjustedTitle.concat(ext);
79                 
80                 
81             };
82             
83             var removeTab = function (event, index) {
84               event.preventDefault();
85               event.stopPropagation();
86               $scope.tabs.splice(index, 1);
87               $cookies.putObject('cookieTabs', $scope.tabs,{domain: cookieDomain, path : '/'});
88             };
89             
90             var selectTab = function (title) {
91               if(title=='Home') {
92                   $rootScope.ContentModel.IsVisible=true;
93                   $rootScope.showFooter = true;
94                     $rootScope.tabBottom = 75;
95               }
96               else {
97                   $rootScope.ContentModel.IsVisible=false;
98                   $rootScope.showFooter = false;
99                     $rootScope.tabBottom = 0;
100               }
101             };
102
103             $scope.addTab    = addTab;
104             $scope.removeTab = removeTab;
105             $scope.selectTab = selectTab;
106             $scope.adjustTitle = adjustTitle;
107             
108
109             $rootScope.ContentModel = {
110                     IsVisible : false,
111                     ViewUrl : null,
112                 };
113             
114             
115                 var sessionActive = applicationsService.ping()
116                 .then(sessionActive => {
117             $log.debug('TabsCtrl::addTab: applicationsService.ping() = ' + JSON.stringify(sessionActive));
118
119             var cookieTabs = $cookies.getObject('cookieTabs');
120                 if(cookieTabs!=null){
121                         for(var t in cookieTabs){
122                                 console.log('TabsCtrl::addTab: cookieTabs title: '+cookieTabs[t].title);
123                                 if(cookieTabs[t].title!=null && cookieTabs[t].title==='Home'){
124                                         cookieTabs[t].content = "";
125                                         $rootScope.ContentModel.IsVisible=true;
126                                 }
127                                         
128                                 addTab( cookieTabs[t].title, cookieTabs[t].content) ;
129                         }
130                 } else {
131                         for (var i = 0; i < 1; i++) {
132                         var content="";
133                         var title="";
134                         if(i==0){
135                                 title="Home";
136                                 $rootScope.ContentModel.IsVisible=true;
137                         }
138                   addTab(title, content);
139                 }
140                 }
141
142             $scope.selectedIndex = 0;
143                 });
144                 
145             $scope.$watchCollection(function() { return $cookies.getObject('addTab'); }, function(newValue) {
146                 var tabContent = $cookies.getObject('addTab');
147                 if(tabContent!=null && tabContent.url!=null){
148                         var tabExists = false;
149                         for(var x in $scope.tabs){
150                                 console.log($scope.tabs[x].content);
151                                 if($scope.tabs[x].title==tabContent.title){
152                                         tabExists = true;
153                                         $scope.selectedIndex = x;
154                                 }
155                         }
156                         if(!tabExists){
157                          addTab( tabContent.title, tabContent.url) ;
158                         }
159                         $cookies.remove('addTab');
160                 }
161             });
162             
163             
164         }
165         
166        
167     }
168     
169     TabsCtrl.$inject = ['applicationsService', '$log', '$window', 'conf','userProfileService', '$scope','$cookies','$rootScope','confirmBoxService'];
170     angular.module('ecompApp').controller('TabsCtrl', TabsCtrl);
171
172     angular.module('ecompApp').directive('mainArea', function() {
173         return {
174             restrict: "E",
175             templateUrl: 'app/views/tabs/tabframe.html',
176             link: function(scope, element) {
177                                 
178             }
179         }
180     });
181     
182    
183     
184     angular.module('ecompApp').directive('tabHighlight', [function () {
185         return {
186           restrict: 'A',
187           link: function (scope, element) {
188             var x, y, initial_background = '#c3d5e6';
189
190             element
191               .removeAttr('style')
192               .mousemove(function (e) {
193                 if(!element.hasClass('md-active'))
194                 {
195                   x = e.pageX - this.offsetLeft;
196                   y = e.pageY - this.offsetTop;
197
198                   element
199                     .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 })
200                     .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 })
201                     .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 });
202                 }
203               })
204               .mouseout(function () {
205                 element.removeAttr('style');
206               });
207           }
208         };
209       }]);
210
211
212     
213 })();
214
215 function getParameterByName(name, url) {
216     if (!url) url = window.location.href;
217     name = name.replace(/[\[\]]/g, "\\$&");
218     var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
219         results = regex.exec(url);
220     if (!results) return '';
221     if (!results[2]) return '';
222     return (results[2].replace(/\+/g, " "));
223 }
224
225 function isCascadeFrame(ref) {
226    if (self != top) {
227        var e = document.body;
228        e.parentNode.removeChild(e);
229        window.location = "unKnownError";
230    }
231 }