amber.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /* Amber package loading
  2. usage example:
  3. amber.load({
  4. files: ['MyCategory1.js', 'MyCategory2.js'],
  5. ready: function() {smalltalk.Browser._open()}
  6. })
  7. */
  8. amber = (function() {
  9. var that = {};
  10. var scripts = document.getElementsByTagName("script");
  11. var src = scripts[ scripts.length - 1 ].src;
  12. var home = src.split("/").slice(0, -2).join("/") + "/";
  13. var debug;
  14. var deploy;
  15. var spec;
  16. var jsToLoad = [];
  17. var loadJS;
  18. var nocache = '';
  19. that.load = function(obj) {
  20. spec = obj || {};
  21. // In deployment mode, only the compressed version of Kernel
  22. // and Canvas are loaded
  23. deploy = spec.deploy || false;
  24. debug = spec.debug || false;
  25. // When debug is turned on, logs are written to the console,
  26. // and the user will be prompted before they leave the page.
  27. if (debug) {
  28. window.onbeforeunload = function(){ return 'You will loose all code that you have not committed'; }
  29. }
  30. // Allow loading default Amber files from a different location
  31. // e.g. http://amber-lang.net/amber/
  32. if (spec.home) home = spec.home;
  33. // Specify a version string to avoid wrong browser caching
  34. if (spec.version) {
  35. nocache = '?' + spec.version;
  36. }
  37. loadDependencies();
  38. addJSToLoad('js/lib/es5-shim-2.0.2/es5-shim.min.js');
  39. addJSToLoad('js/lib/es5-shim-2.0.2/es5-sham.min.js');
  40. addJSToLoad('js/boot.js');
  41. if (deploy) {
  42. loadPackages([
  43. 'Kernel-Objects.deploy',
  44. 'Kernel-Classes.deploy',
  45. 'Kernel-Methods.deploy',
  46. 'Kernel-Collections.deploy',
  47. 'Kernel-Exceptions.deploy',
  48. 'Kernel-Transcript.deploy',
  49. 'Kernel-Announcements.deploy',
  50. 'Canvas.deploy'
  51. ]);
  52. } else {
  53. loadIDEDependencies();
  54. loadCSS('amber.css');
  55. loadPackages([
  56. 'Kernel-Objects',
  57. 'Kernel-Classes',
  58. 'Kernel-Methods',
  59. 'Kernel-Collections',
  60. 'Kernel-Exceptions',
  61. 'Kernel-Transcript',
  62. 'Kernel-Announcements',
  63. 'Canvas',
  64. 'SUnit',
  65. 'Importer-Exporter',
  66. 'Compiler-Exceptions',
  67. 'Compiler-Core',
  68. 'Compiler-AST',
  69. 'Compiler-Semantic',
  70. 'Compiler-IR',
  71. 'Compiler-Inlining',
  72. 'Compiler-Interpreter',
  73. 'Compiler-Tests',
  74. 'parser',
  75. 'IDE',
  76. 'Examples',
  77. 'Benchfib',
  78. 'Kernel-Tests',
  79. 'SUnit-Tests'
  80. ]);
  81. }
  82. var additionalFiles = spec.packages || spec.files;
  83. if (additionalFiles) {
  84. that.commitPath = loadPackages(additionalFiles, spec.prefix, spec.packageHome);
  85. }
  86. // Be sure to setup & initialize smalltalk classes
  87. addJSToLoad('js/init.js');
  88. initializeSmalltalk();
  89. };
  90. function loadPackages(names, prefix, urlHome){
  91. var name;
  92. prefix = prefix || 'js';
  93. urlHome = urlHome || home;
  94. for (var i=0; i < names.length; i++) {
  95. name = names[i].split(/\.js$/)[0];
  96. addJSToLoad(name + '.js', prefix, urlHome);
  97. }
  98. return {
  99. js: urlHome + prefix,
  100. st: urlHome + (prefix.match(/\/js$/) ? prefix.replace(/\/js$/, "/st") : "st")
  101. };
  102. }
  103. function addJSToLoad(name, prefix, urlHome) {
  104. urlHome = urlHome || home;
  105. jsToLoad.push(buildJSURL(name, prefix, urlHome));
  106. }
  107. function resolve(base, path) {
  108. if (/(^|:)\/\//.test(path)) {
  109. // path: [http:]//foo.com/bar/; base: whatever/
  110. // -> http://foo.com/bar/
  111. return path;
  112. }
  113. if (!/^\//.test(path)) {
  114. // path: relative/; base: whatever/
  115. // -> whatever/relative/
  116. return base + path;
  117. }
  118. var match = base.match(/^(([^:/]*:|^)\/\/[^/]*)/);
  119. if (match) {
  120. // path: /absolute/; base: [http:]//foo.com/whatever/
  121. // -> [http:]//foo.com/absolute/
  122. return match[1] + path;
  123. }
  124. // path: /absolute/; base: whatever/path/
  125. // -> /absolute/
  126. return path;
  127. }
  128. function buildJSURL(name, prefix, urlHome) {
  129. prefix = prefix || '';
  130. urlHome = urlHome || home;
  131. var parts = name.match(/^(.*\/)([^/]*)$/);
  132. if (parts) {
  133. name = parts[2];
  134. urlHome = resolve(urlHome, parts[1]);
  135. }
  136. if (!deploy) {
  137. name = name + nocache;
  138. }
  139. return urlHome + prefix + '/' + name;
  140. }
  141. function loadCSS(name, prefix) {
  142. prefix = prefix || 'css';
  143. if (!deploy) {
  144. name = name + nocache;
  145. }
  146. var url = home + prefix + '/' + name;
  147. var link = document.createElement("link");
  148. link.setAttribute("rel", "stylesheet");
  149. link.setAttribute("type", "text/css");
  150. link.setAttribute("href", url);
  151. document.getElementsByTagName("head")[0].appendChild(link);
  152. }
  153. function loadDependencies() {
  154. if (typeof jQuery == 'undefined') {
  155. writeScriptTag(buildJSURL('js/lib/jQuery/jquery-1.8.2.min.js'));
  156. }
  157. if ((typeof jQuery == 'undefined') || (typeof jQuery.ui == 'undefined')) {
  158. writeScriptTag(buildJSURL('js/lib/jQuery/jquery-ui-1.8.16.custom.min.js'));
  159. }
  160. }
  161. function loadIDEDependencies() {
  162. addJSToLoad('js/lib/jQuery/jquery.textarea.js');
  163. addJSToLoad('js/lib/CodeMirror/codemirror.js');
  164. addJSToLoad('js/lib/CodeMirror/smalltalk.js');
  165. loadCSS('lib/CodeMirror/codemirror.css', 'js');
  166. loadCSS('lib/CodeMirror/amber.css', 'js');
  167. }
  168. // This will be called after JS files have been loaded
  169. function initializeSmalltalk() {
  170. that.smalltalkReady = function() {
  171. if (spec.ready) {
  172. spec.ready();
  173. }
  174. evaluateSmalltalkScripts();
  175. };
  176. loadAllJS();
  177. }
  178. /*
  179. * When loaded using AJAX, scripts order not guaranteed.
  180. * Load JS in the order they have been added using addJSToLoad().
  181. * If loaded, will use jQuery's getScript instead of adding a script element
  182. */
  183. function loadAllJS() {
  184. loadJS = loadJSViaScriptTag;
  185. if (typeof jQuery != 'undefined') {
  186. loadJS = loadJSViaJQuery;
  187. }
  188. loadNextJS();
  189. }
  190. function loadNextJS() {
  191. loadJS(jsToLoad[0], function(){
  192. jsToLoad.shift();
  193. if (jsToLoad.length > 0) {
  194. loadNextJS();
  195. }
  196. });
  197. }
  198. function loadJSViaScriptTag(url, callback) {
  199. writeScriptTag(url);
  200. callback();
  201. }
  202. function loadJSViaJQuery(url, callback) {
  203. $.ajax({
  204. dataType: "script",
  205. url: url,
  206. cache: deploy,
  207. success: callback
  208. });
  209. }
  210. function writeScriptTag(src) {
  211. var scriptString = '<script src="' + src + '" type="text/javascript"></script>';
  212. document.write(scriptString);
  213. }
  214. function evaluateSmalltalkScripts() {
  215. jQuery(document).ready(function() {
  216. jQuery('script[type="text/smalltalk"]').each(function(i, elt) {
  217. smalltalk.Compiler._new()._evaluateExpression_(jQuery(elt).html());
  218. });
  219. })
  220. }
  221. var localPackages;
  222. function populateLocalPackages(){
  223. var localStorageRE = /^smalltalk\.packages\.(.*)$/;
  224. localPackages = {};
  225. var match, key;
  226. for(var i=0; i < localStorage.length; i++) {
  227. key = localStorage.key(i);
  228. if (match = key.match(localStorageRE)) {
  229. localPackages[match[1]] = localStorage[key];
  230. }
  231. }
  232. return localPackages;
  233. }
  234. function clearLocalPackages() {
  235. for (var name in localPackages) {
  236. log('Removing ' + name + ' from local storage');
  237. localStorage.removeItem('smalltalk.packages.' + name);
  238. }
  239. }
  240. function log(string) {
  241. if (debug) {
  242. console.log(string);
  243. }
  244. }
  245. return that;
  246. })();
  247. window.loadAmber = amber.load;
  248. // Backward compatibility
  249. function toggleAmberIDE () {
  250. return smalltalk.TabManager._toggleAmberIDE();
  251. }