3 * Copyright 2016 RIFT.IO Inc
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 //Login needs to be refactored. Too many cross dependencies
19 var AuthActions
= require('../widgets/login/loginAuthActions.js');
20 var $ = require('jquery');
21 import rw
from './rw.js';
22 var API_SERVER
= rw
.getSearchParams(window
.location
).api_server
;
23 let NODE_PORT
= rw
.getSearchParams(window
.location
).api_port
|| ((window
.location
.protocol
== 'https:') ? 8443 : 8000);
24 var SockJS
= require('sockjs-client');
28 Utils
.DescriptorModelMeta
= null;
30 var INACTIVITY_TIMEOUT
= 600000;
32 Utils
.getInactivityTimeout = function() {
33 return new Promise(function(resolve
, reject
) {
35 url
: '/inactivity-timeout',
37 success: function(data
) {
40 error: function(error
) {
41 console
.log("There was an error getting the inactivity-timeout: ", error
);
44 }).fail(function(xhr
) {
45 console
.log('There was an xhr error getting the inactivity-timeout', xhr
);
51 Utils
.isMultiplexerLoaded = function() {
52 if (window
.multiplexer
) {
58 Utils
.setupMultiplexClient = function() {
59 var sockjs_url
= '/multiplex';
61 var sockjs
= new SockJS(sockjs_url
);
63 var loadChecker = function() {
65 window
.multiplexer
= new WebSocketMultiplex(sockjs
);
66 console
.log('WebSocketMultiplex loaded');
68 // caught an error, retry in someTime
69 console
.log('WebSocketMultiplex not loaded yet. will try again in 1 second:', e
);
70 setTimeout(function() {
78 Utils
.checkAndResolveSocketRequest = function(data
, resolve
, reject
) {
79 const checker
= () => {
80 if (!Utils
.isMultiplexerLoaded()) {
92 Utils
.bootstrapApplication = function() {
94 return new Promise(function(resolve
, reject
) {
95 Promise
.all([self
.getInactivityTimeout()]).then(function(results
) {
96 INACTIVITY_TIMEOUT
= results
[0]['inactivity-timeout'];
99 console
.log("Error bootstrapping application ", error
);
105 Utils
.getDescriptorModelMeta = function() {
106 return new Promise(function(resolve
, reject
) {
107 if (!Utils
.DescriptorModelMeta
) {
109 url
: '/descriptor-model-meta?api_server=' + API_SERVER
,
111 beforeSend
: Utils
.addAuthorizationStub
,
112 success: function(data
) {
113 Utils
.DescriptorModelMeta
= data
;
114 Utils
.DescriptorModelMetaLoaded
= true;
117 error: function(error
) {
118 console
.log("There was an error getting the schema: ", error
);
121 }).fail(function(xhr
) {
122 console
.log("There was an error getting the schema: ", xhr
);
123 Utils
.checkAuthentication(xhr
.status
);
126 resolve(Utils
.DescriptorModelMeta
);
131 Utils
.addAuthorizationStub = function(xhr
) {
132 var Auth
= window
.sessionStorage
.getItem("auth");
133 xhr
.setRequestHeader('Authorization', 'Basic ' + Auth
);
136 Utils
.getByteDataWithUnitPrefix = function(number
, precision
) {
137 var toPrecision
= precision
|| 3;
138 if (number
< Math
.pow(10, 3)) {
139 return [number
, 'B'];
140 } else if (number
< Math
.pow(10, 6)) {
141 return [(number
/ Math
.pow(10, 3)).toPrecision(toPrecision
), 'KB'];
142 } else if (number
< Math
.pow(10, 9)) {
143 return [(number
/ Math
.pow(10, 6)).toPrecision(toPrecision
), 'MB'];
144 } else if (number
< Math
.pow(10, 12)) {
145 return [(number
/ Math
.pow(10, 9)).toPrecision(toPrecision
), 'GB'];
146 } else if (number
< Math
.pow(10, 15)) {
147 return [(number
/ Math
.pow(10, 12)).toPrecision(toPrecision
), 'TB'];
148 } else if (number
< Math
.pow(10, 18)) {
149 return [(number
/ Math
.pow(10, 15)).toPrecision(toPrecision
), 'PB'];
150 } else if (number
< Math
.pow(10, 21)) {
151 return [(number
/ Math
.pow(10, 18)).toPrecision(toPrecision
), 'EB'];
152 } else if (number
< Math
.pow(10, 24)) {
153 return [(number
/ Math
.pow(10, 21)).toPrecision(toPrecision
), 'ZB'];
154 } else if (number
< Math
.pow(10, 27)) {
155 return [(number
/ Math
.pow(10, 24)).toPrecision(toPrecision
), 'ZB'];
157 return [(number
/ Math
.pow(10, 27)).toPrecision(toPrecision
), 'YB'];
161 Utils
.getPacketDataWithUnitPrefix = function(number
, precision
) {
162 var toPrecision
= precision
|| 3;
163 if (number
< Math
.pow(10, 3)) {
164 return [number
, 'P'];
165 } else if (number
< Math
.pow(10, 6)) {
166 return [(number
/ Math
.pow(10, 3)).toPrecision(toPrecision
), 'KP'];
167 } else if (number
< Math
.pow(10, 9)) {
168 return [(number
/ Math
.pow(10, 6)).toPrecision(toPrecision
), 'MP'];
169 } else if (number
< Math
.pow(10, 12)) {
170 return [(number
/ Math
.pow(10, 9)).toPrecision(toPrecision
), 'GP'];
171 } else if (number
< Math
.pow(10, 15)) {
172 return [(number
/ Math
.pow(10, 12)).toPrecision(toPrecision
), 'TP'];
173 } else if (number
< Math
.pow(10, 18)) {
174 return [(number
/ Math
.pow(10, 15)).toPrecision(toPrecision
), 'PP'];
175 } else if (number
< Math
.pow(10, 21)) {
176 return [(number
/ Math
.pow(10, 18)).toPrecision(toPrecision
), 'EP'];
177 } else if (number
< Math
.pow(10, 24)) {
178 return [(number
/ Math
.pow(10, 21)).toPrecision(toPrecision
), 'ZP'];
179 } else if (number
< Math
.pow(10, 27)) {
180 return [(number
/ Math
.pow(10, 24)).toPrecision(toPrecision
), 'ZP'];
182 return [(number
/ Math
.pow(10, 27)).toPrecision(toPrecision
), 'YP'];
185 Utils
.loginHash
= "#/login";
186 Utils
.setAuthentication = function(username
, password
, cb
) {
188 var AuthBase64
= btoa(username
+ ":" + password
);
189 window
.sessionStorage
.setItem("auth", AuthBase64
);
190 self
.detectInactivity();
192 url
: '//' + window
.location
.hostname
+ ':' + window
.location
.port
+ '/check-auth?api_server=' + API_SERVER
,
194 beforeSend
: Utils
.addAuthorizationStub
,
195 success: function(data
) {
196 //console.log("LoggingSource.getLoggingConfig success call. data=", data);
201 error: function(data
) {
202 Utils
.clearAuthentication();
206 Utils
.clearAuthentication = function(callback
) {
208 window
.sessionStorage
.removeItem("auth");
209 AuthActions
.notAuthenticated();
210 window
.sessionStorage
.setItem("locationRefHash", window
.location
.hash
);
214 window
.location
.hash
= Utils
.loginHash
;
217 Utils
.isNotAuthenticated = function(windowLocation
, callback
) {
219 self
.detectInactivity();
220 if (!window
.sessionStorage
.getItem("auth")) {
221 Utils
.clearAuthentication();
224 Utils
.isDetecting
= false;
225 Utils
.detectInactivity = function(callback
, duration
) {
227 if (!self
.isDetecting
) {
228 var cb = function() {
229 self
.clearAuthentication();
235 var timeout
= duration
|| INACTIVITY_TIMEOUT
;
236 var setInactive = function() {
237 isInactive
= setTimeout(cb
, timeout
);
239 var reset = function() {
240 clearTimeout(isInactive
);
244 window
.addEventListener('mousemove', reset
);
245 window
.addEventListener("keypress", reset
);
246 self
.isDetecting
= true;
249 Utils
.checkAuthentication = function(statusCode
, cb
) {
251 if (statusCode
== 401) {
255 window
.sessionStorage
.removeItem("auth")
256 self
.isNotAuthenticated(window
.location
)
262 Utils
.isAuthenticationCached = function() {
264 if (window
.sessionStorage
.getItem("auth")) {
270 Utils
.getHostNameFromURL = function(url
) {
271 var match
= url
.match(/^(https?\:)\/\/(([^:\/?#]*)(?:\:([0-9]+))?)([^?#]*)(\?[^#]*|)(#.*|)$/);
272 return match
&& match
[3];
275 Utils
.webSocketProtocol = function() {
279 if (window
.location
.protocol
== 'http:') {
282 this.wsProto
= 'wss:'
288 Utils
.arrayIntersperse
= (arr
, sep
) => {
289 if (arr
.length
=== 0) {
293 return arr
.slice(1).reduce((xs
, x
, i
) => {
294 return xs
.concat([sep
, x
]);
298 Utils
.cleanImageDataURI
= (imageString
, type
, id
) => {
299 if (/\bbase64\b/g.test(imageString
)) {
301 } else if (/<\?xml\b/g.test(imageString
)) {
302 const imgStr
= imageString
.substring(imageString
.indexOf('<?xml'));
303 return 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(imgStr
);
304 } else if (/\.(svg|png|gif|jpeg|jpg)$/.test(imageString
)) {
305 return '/composer/assets/logos/' + type
+ '/' + id
+ '/' + imageString
;
306 // return require('../images/logos/' + imageString);
308 if(type
== 'nsd' || type
== 'vnfd') {
309 return require('style/img/catalog-'+type
+'-default.svg');
311 return require('style/img/catalog-default.svg');
314 module
.exports
= Utils
;