Added projects to user management
[osm/UI.git] / skyquake / framework / utils / utils.js
index d08fe5f..93f8d7d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * 
+ *
  *   Copyright 2016 RIFT.IO Inc
  *
  *   Licensed under the Apache License, Version 2.0 (the "License");
 //Login needs to be refactored. Too many cross dependencies
 var AuthActions = require('../widgets/login/loginAuthActions.js');
 var $ = require('jquery');
-var rw = require('utils/rw.js');
+import rw from './rw.js';
 var API_SERVER = rw.getSearchParams(window.location).api_server;
-var NODE_PORT = 3000;
+let NODE_PORT = rw.getSearchParams(window.location).api_port || ((window.location.protocol == 'https:') ? 8443 : 8000);
 var SockJS = require('sockjs-client');
 
 var Utils = {};
 
 Utils.DescriptorModelMeta = null;
+// Utils.DescriptorModelMeta = require('./../../plugins/composer/src/src/libraries/model/DescriptorModelMeta.json');
 
 var INACTIVITY_TIMEOUT = 600000;
 
@@ -129,8 +130,9 @@ Utils.getDescriptorModelMeta = function() {
 }
 
 Utils.addAuthorizationStub = function(xhr) {
-    var Auth = window.sessionStorage.getItem("auth");
-    xhr.setRequestHeader('Authorization', 'Basic ' + Auth);
+    // NO-OP now that we are dealing with it on the server
+    // var Auth = window.sessionStorage.getItem("auth");
+    // xhr.setRequestHeader('Authorization', 'Basic ' + Auth);
 };
 
 Utils.getByteDataWithUnitPrefix = function(number, precision) {
@@ -183,24 +185,28 @@ Utils.getPacketDataWithUnitPrefix = function(number, precision) {
     }
 }
 Utils.loginHash = "#/login";
-Utils.setAuthentication = function(username, password, cb) {
-    var self = this;
-    var AuthBase64 = btoa(username + ":" + password);
-    window.sessionStorage.setItem("auth", AuthBase64);
-    self.detectInactivity();
-    if (cb) {
-        cb();
-    }
-}
+
 Utils.clearAuthentication = function(callback) {
     var self = this;
     window.sessionStorage.removeItem("auth");
     AuthActions.notAuthenticated();
     window.sessionStorage.setItem("locationRefHash", window.location.hash);
+    $.ajax({
+        url: '//' + window.location.hostname + ':' + window.location.port + '/session?api_server=' + API_SERVER,
+        type: 'DELETE',
+        success: function(data) {
+            console.log('User logged out');
+        },
+        error: function(data) {
+            console.log('Problem logging user out');
+        }
+    });
+
+
     if (callback) {
         callback();
     } else {
-        window.location.hash = Utils.loginHash;
+        window.location.replace(window.location.protocol + '//' + window.location.hostname + ':' + window.location.port + '/?api_server=' + API_SERVER);
     }
 }
 Utils.isNotAuthenticated = function(windowLocation, callback) {
@@ -284,4 +290,20 @@ Utils.arrayIntersperse = (arr, sep) => {
     }, [arr[0]]);
 }
 
+Utils.cleanImageDataURI = (imageString, type, id) => {
+    if (/\bbase64\b/g.test(imageString)) {
+        return imageString;
+    } else if (/<\?xml\b/g.test(imageString)) {
+        const imgStr = imageString.substring(imageString.indexOf('<?xml'));
+        return 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(imgStr);
+    } else if (/\.(svg|png|gif|jpeg|jpg)$/.test(imageString)) {
+        return '/composer/assets/logos/' + type + '/' + id + '/' + imageString;
+        // return require('../images/logos/' + imageString);
+    }
+    if(type == 'nsd' || type == 'vnfd') {
+        return require('style/img/catalog-'+type+'-default.svg');
+    }
+    return require('style/img/catalog-default.svg');
+}
+
 module.exports = Utils;