amber.js 7.2 KB

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