From a9ba38237096ed73a0c3da59a7461bfaa1b2ba5d Mon Sep 17 00:00:00 2001
From: juanf <juanf>
Date: Wed, 8 Mar 2017 09:59:20 +0000
Subject: [PATCH] SSDM-4841 : V3 Javascript API requireJS don't loads API
 classes from a different domain

SVN: 37865
---
 .../cisd/openbis/public/resources/api/v3/config.js | 14 +++++++++++---
 .../cisd/openbis/public/resources/config.js        |  2 +-
 2 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/public/resources/api/v3/config.js b/openbis/source/java/ch/systemsx/cisd/openbis/public/resources/api/v3/config.js
index 2ec2bcdee97..6401bdbfc03 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/public/resources/api/v3/config.js
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/public/resources/api/v3/config.js
@@ -1,9 +1,17 @@
 var require = (function() {
 
 	var getBaseUrl = function() {
-		var path = window.location.pathname;
-		var index = path.substr(1).indexOf("/") + 1;
-		var baseUrl = path.substring(0, index) + "/resources/api/v3";
+		// To find where the V3 API has been loaded from we scan the script tags until finding this file
+		// This way is possible to load the V3 API from a separate domain
+		var scripts = document.getElementsByTagName("script");
+		var baseUrl = null;
+		for(var sIdx = 0; sIdx < scripts.length; sIdx++) {
+			var src = scripts[sIdx].src;
+			var substringEnd = -1;
+			if((substringEnd = src.indexOf("/api/v3/config.js")) > -1) {
+				baseUrl = src.substring(0, substringEnd+"/api/v3".length);
+			}
+		}
 		return baseUrl;
 	}
 
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/public/resources/config.js b/openbis/source/java/ch/systemsx/cisd/openbis/public/resources/config.js
index d07541fdce8..fbeb2e1c5c0 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/public/resources/config.js
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/public/resources/config.js
@@ -9,7 +9,7 @@ var require = (function() {
 			var src = scripts[sIdx].src;
 			var substringEnd = -1;
 			if((substringEnd = src.indexOf("/api/v3/config.js")) > -1) {
-				baseUrl = src.substring(0, substringEnd+"/api/v3".length);
+				baseUrl = src.substring(0, substringEnd);
 			}
 		}
 		return baseUrl;
-- 
GitLab