Browse Source

Merge pull request #318 from herby/loader-external-packages

Loader loads external / other-path packages
Nicolas Petton 11 years ago
parent
commit
c739b517ca
1 changed files with 62 additions and 34 deletions
  1. 62 34
      js/amber.js

+ 62 - 34
js/amber.js

@@ -19,7 +19,7 @@ amber = (function() {
 	var spec;
 	var jsToLoad = [];
 	var loadJS;
-    var nocache = '';
+	var nocache = '';
 
 	that.toggleIDE = function() {
 		if ($('#amber').length == 0) {
@@ -56,9 +56,9 @@ amber = (function() {
 		}
 
 		loadDependencies();
-		addJSToLoad('lib/es5-shim-2.0.2/es5-shim.min.js');
-		addJSToLoad('lib/es5-shim-2.0.2/es5-sham.min.js');
-		addJSToLoad('boot.js');
+		addJSToLoad('js/lib/es5-shim-2.0.2/es5-shim.min.js');
+		addJSToLoad('js/lib/es5-shim-2.0.2/es5-sham.min.js');
+		addJSToLoad('js/boot.js');
 
 		if (deploy) {
 			loadPackages([
@@ -102,41 +102,69 @@ amber = (function() {
 		}
 
 		var additionalFiles = spec.packages || spec.files;
-        var commitPathForInit = null;
+		var commitPathForInit = null;
 		if (additionalFiles) {
 			commitPathForInit = loadPackages(additionalFiles, spec.prefix, spec.packageHome);
 		}
 
 		// Be sure to setup & initialize smalltalk classes
-		addJSToLoad('init.js');
+		addJSToLoad('js/init.js');
 		initializeSmalltalk(commitPathForInit);
 	};
 
 	function loadPackages(names, prefix, urlHome){
 		var name, url;
 		var prefix = prefix || 'js';
-    var urlHome = urlHome || home;
+		var urlHome = urlHome || home;
 
 		for (var i=0; i < names.length; i++) {
 			name = names[i].split(/\.js$/)[0];
 			addJSToLoad(name + '.js', prefix, urlHome);
 		}
 
-        return  {
-            js: urlHome+prefix,
-            st: urlHome+'st'
-        };
+		return  {
+			js: urlHome+prefix,
+			st: urlHome+'st'
+		};
 	};
 
 	function addJSToLoad(name, prefix, urlHome) {
-    var urlHome = urlHome || home;
+		var urlHome = urlHome || home;
 		jsToLoad.push(buildJSURL(name, prefix, urlHome));
 	};
 
+	function resolve(base, path) {
+		if (/(^|:)\/\//.test(path)) {
+			// path: [http:]//foo.com/bar/; base: whatever/
+			// -> http://foo.com/bar/
+			return path;
+		}
+		if (!/^\//.test(path)) {
+			// path: relative/; base: whatever/
+			// -> whatever/relative/
+			return base + path;
+		}
+		var match = base.match(/^(([^:/]*:|^)\/\/[^/]*)/);
+		if (match) {
+			// path: /absolute/; base: [http:]//foo.com/whatever/
+			// -> [http:]//foo.com/absolute/
+			return match[1] + path;
+		}
+		// path: /absolute/; base: whatever/path/
+		// -> /absolute/
+		return path;
+	}
+
 	function buildJSURL(name, prefix, urlHome) {
-		var prefix = prefix || 'js';
+		var prefix = prefix || '';
 		var name = name;
-    var urlHome = urlHome || home;
+		var urlHome = urlHome || home;
+
+		var parts = name.match(/^(.*\/)([^/]*)$/);
+		if (parts) {
+			name = parts[2];
+			urlHome = resolve(urlHome, parts[1]);
+		}
 
 		if (!deploy) {
 			name = name + nocache;
@@ -163,18 +191,18 @@ amber = (function() {
 
 	function loadDependencies() {
 		if (typeof jQuery == 'undefined') {
-			writeScriptTag(buildJSURL('lib/jQuery/jquery-1.8.2.min.js'));
+			writeScriptTag(buildJSURL('js/lib/jQuery/jquery-1.8.2.min.js'));
 		}
 
 		if ((typeof jQuery == 'undefined') || (typeof jQuery.ui == 'undefined')) {      
-			writeScriptTag(buildJSURL('lib/jQuery/jquery-ui-1.8.16.custom.min.js'));
+			writeScriptTag(buildJSURL('js/lib/jQuery/jquery-ui-1.8.16.custom.min.js'));
 		}
 	};
 
 	function loadIDEDependencies() {
-		addJSToLoad('lib/jQuery/jquery.textarea.js');
-		addJSToLoad('lib/CodeMirror/codemirror.js');
-		addJSToLoad('lib/CodeMirror/smalltalk.js');
+		addJSToLoad('js/lib/jQuery/jquery.textarea.js');
+		addJSToLoad('js/lib/CodeMirror/codemirror.js');
+		addJSToLoad('js/lib/CodeMirror/smalltalk.js');
 		loadCSS('lib/CodeMirror/codemirror.css', 'js');
 		loadCSS('lib/CodeMirror/amber.css', 'js');
 	};
@@ -182,14 +210,14 @@ amber = (function() {
 	// This will be called after JS files have been loaded
 	function initializeSmalltalk(commitPath) {
 		window.smalltalkReady = function() {
-            if (commitPath) {
-                smalltalk['@@commitPath'] = commitPath;
-                smalltalk.Package._commitPathsFromLoader();
-            }
+			if (commitPath) {
+				smalltalk['@@commitPath'] = commitPath;
+				smalltalk.Package._commitPathsFromLoader();
+			}
 			if (spec.ready) {
 				spec.ready();
 			};
-            evaluateSmalltalkScripts();
+			evaluateSmalltalkScripts();
 		};
 
 		loadAllJS(); 
@@ -236,16 +264,16 @@ amber = (function() {
 		document.write(scriptString);
 	};
 
-    function evaluateSmalltalkScripts() {
-        jQuery(document).ready(function() {
-            jQuery('script[type="text/smalltalk"]').each(function(i, elt) {
-                smalltalk.send(
-                    smalltalk.send(smalltalk.Compiler, '_new'),
-                    '_evaluateExpression_',
-                    [jQuery(elt).html()])
-            });
-        })
-    };
+	function evaluateSmalltalkScripts() {
+		jQuery(document).ready(function() {
+			jQuery('script[type="text/smalltalk"]').each(function(i, elt) {
+				smalltalk.send(
+					smalltalk.send(smalltalk.Compiler, '_new'),
+					'_evaluateExpression_',
+					[jQuery(elt).html()])
+			});
+		})
+	};
 
 	function populateLocalPackages(){
 		var localStorageRE = /^smalltalk\.packages\.(.*)$/;