amber.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 = resolveViaDOM(src).replace(/[^\/]+\/[^\/]+$/, "");
  13. var debug;
  14. var deploy;
  15. var spec;
  16. var jsToLoad = [];
  17. var loadJS;
  18. var nocache = '';
  19. function resolveViaDOM(url) {
  20. var a = document.createElement("a");
  21. a.href = url;
  22. return a.href;
  23. }
  24. that.load = function(obj) {
  25. spec = obj || {};
  26. // In deployment mode, only the compressed version of Kernel
  27. // and Canvas are loaded
  28. deploy = spec.deploy || false;
  29. debug = spec.debug || false;
  30. // When debug is turned on, logs are written to the console,
  31. // and the user will be prompted before they leave the page.
  32. if (debug) {
  33. window.onbeforeunload = function(){ return 'You will loose all code that you have not committed'; };
  34. }
  35. // Allow loading default Amber files from a different location
  36. // e.g. http://amber-lang.net/amber/
  37. if (spec.home) home = spec.home;
  38. // Specify a version string to avoid wrong browser caching
  39. if (spec.version) {
  40. nocache = '?' + spec.version;
  41. }
  42. loadDependencies();
  43. addJSToLoad('support/es5-shim-2.0.2/es5-shim.min.js');
  44. addJSToLoad('support/es5-shim-2.0.2/es5-sham.min.js');
  45. addJSToLoad('support/boot.js');
  46. if (deploy) {
  47. loadPackages([
  48. 'Kernel-Objects.deploy',
  49. 'Kernel-Classes.deploy',
  50. 'Kernel-Methods.deploy',
  51. 'Kernel-Collections.deploy',
  52. 'Kernel-Infrastructure.deploy',
  53. 'Kernel-Exceptions.deploy',
  54. 'Kernel-Transcript.deploy',
  55. 'Kernel-Announcements.deploy',
  56. 'Canvas.deploy'
  57. ]);
  58. } else {
  59. loadIDEDependencies();
  60. loadCSS('amber.css');
  61. addJSToLoad('support/parser.js');
  62. loadPackages([
  63. 'Kernel-Objects',
  64. 'Kernel-Classes',
  65. 'Kernel-Methods',
  66. 'Kernel-Collections',
  67. 'Kernel-Infrastructure',
  68. 'Kernel-Exceptions',
  69. 'Kernel-Transcript',
  70. 'Kernel-Announcements',
  71. 'Canvas',
  72. 'SUnit',
  73. 'Importer-Exporter',
  74. 'Compiler-Exceptions',
  75. 'Compiler-Core',
  76. 'Compiler-AST',
  77. 'Compiler-Semantic',
  78. 'Compiler-IR',
  79. 'Compiler-Inlining',
  80. 'Compiler-Interpreter',
  81. 'Compiler-Tests',
  82. 'IDE',
  83. 'Examples',
  84. 'Benchfib',
  85. 'Kernel-Tests',
  86. 'SUnit-Tests'
  87. ]);
  88. }
  89. var additionalFiles = spec.packages || spec.files;
  90. if (additionalFiles) {
  91. that.commitPath = loadPackages(additionalFiles, spec.prefix, spec.packageHome);
  92. }
  93. // Be sure to setup & initialize smalltalk classes
  94. addJSToLoad('support/init.js');
  95. initializeSmalltalk();
  96. };
  97. function loadPackages(names, prefix, urlHome){
  98. var name;
  99. prefix = prefix || 'js';
  100. urlHome = urlHome || home;
  101. for (var i=0; i < names.length; i++) {
  102. name = names[i].split(/\.js$/)[0];
  103. addJSToLoad(name + '.js', prefix, urlHome);
  104. }
  105. return {
  106. js: urlHome + prefix,
  107. st: urlHome + (prefix.match(/\/js$/) ? prefix.replace(/\/js$/, "/st") : "st")
  108. };
  109. }
  110. function addJSToLoad(name, prefix, urlHome) {
  111. urlHome = urlHome || home;
  112. jsToLoad.push(buildJSURL(name, prefix, urlHome));
  113. }
  114. function resolve(base, path) {
  115. if (/(^|:)\/\//.test(path)) {
  116. // path: [http:]//foo.com/bar/; base: whatever/
  117. // -> http://foo.com/bar/
  118. return path;
  119. }
  120. if (!/^\//.test(path)) {
  121. // path: relative/; base: whatever/
  122. // -> whatever/relative/
  123. return base + path;
  124. }
  125. var match = base.match(/^(([^:/]*:|^)\/\/[^/]*)/);
  126. if (match) {
  127. // path: /absolute/; base: [http:]//foo.com/whatever/
  128. // -> [http:]//foo.com/absolute/
  129. return match[1] + path;
  130. }
  131. // path: /absolute/; base: whatever/path/
  132. // -> /absolute/
  133. return path;
  134. }
  135. function buildJSURL(name, prefix, urlHome) {
  136. prefix = prefix ? prefix + '/' : '';
  137. urlHome = urlHome || home;
  138. var parts = name.match(/^(.*\/)([^/]*)$/);
  139. if (parts) {
  140. name = parts[2];
  141. urlHome = resolve(urlHome, parts[1]);
  142. }
  143. if (!deploy) {
  144. name = name + nocache;
  145. }
  146. return urlHome + prefix + name;
  147. }
  148. function loadCSS(name, prefix) {
  149. prefix = prefix || 'css';
  150. if (!deploy) {
  151. name = name + nocache;
  152. }
  153. var url = home + prefix + '/' + name;
  154. var link = document.createElement("link");
  155. link.setAttribute("rel", "stylesheet");
  156. link.setAttribute("type", "text/css");
  157. link.setAttribute("href", url);
  158. document.getElementsByTagName("head")[0].appendChild(link);
  159. }
  160. function loadDependencies() {
  161. if (typeof jQuery == 'undefined') {
  162. addJSToLoad('support/jQuery/jquery-1.8.2.min.js');
  163. }
  164. if ((typeof jQuery == 'undefined') || (typeof jQuery.ui == 'undefined')) {
  165. addJSToLoad('support/jQuery/jquery-ui-1.8.16.custom.min.js');
  166. }
  167. }
  168. function loadIDEDependencies() {
  169. addJSToLoad('support/jQuery/jquery.textarea.js');
  170. addJSToLoad('support/CodeMirror/codemirror.js');
  171. addJSToLoad('support/CodeMirror/smalltalk.js');
  172. addJSToLoad('support/CodeMirror/addon/hint/show-hint.js');
  173. loadCSS('support/CodeMirror/codemirror.css', '.');
  174. loadCSS('support/CodeMirror/theme/ambiance.css', '.');
  175. loadCSS('support/CodeMirror/addon/hint/show-hint.css', '.');
  176. loadCSS('support/CodeMirror/amber.css', '.');
  177. }
  178. // This will be called after JS files have been loaded
  179. function initializeSmalltalk() {
  180. that.smalltalkReady = function(smalltalk,nil,_st) {
  181. if (spec.ready) {
  182. spec.ready(smalltalk,nil,_st);
  183. }
  184. evaluateSmalltalkScripts();
  185. };
  186. loadAllJS();
  187. }
  188. /*
  189. * When loaded using AJAX, scripts order not guaranteed.
  190. * Load JS in the order they have been added using addJSToLoad().
  191. * If loaded, will use jQuery's getScript instead of adding a script element
  192. */
  193. function loadAllJS() {
  194. loadJS = loadJSViaScriptTag;
  195. if (typeof jQuery != 'undefined') {
  196. loadJS = loadJSViaJQuery;
  197. }
  198. loadNextJS();
  199. }
  200. function loadNextJS() {
  201. loadJS(jsToLoad[0], function(){
  202. jsToLoad.shift();
  203. if (jsToLoad.length > 0) {
  204. loadNextJS();
  205. }
  206. });
  207. }
  208. function loadJSViaScriptTag(url, callback) {
  209. writeScriptTag(url);
  210. callback();
  211. }
  212. function loadJSViaJQuery(url, callback) {
  213. // The order of loading is as specified, but order of execution may get wrong
  214. // (observed when loading amber in testing environment using zombiejs).
  215. // jQuery's getScript/get/ajax does not give any guarantee as to the order of execution.
  216. // The callback is called after the file is fully load, but it may not have been executed.
  217. // When given a small timeout between ending previous loading and start of next loading,
  218. // it is very probable that the time window is used to actually start executing the script.
  219. $.ajax({
  220. dataType: "script",
  221. url: url,
  222. cache: deploy,
  223. success: function () { setTimeout(callback, 5); }
  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.Compiler._new()._evaluateExpression_(jQuery(elt).html());
  234. });
  235. });
  236. }
  237. var localPackages;
  238. function populateLocalPackages(){
  239. var localStorageRE = /^smalltalk\.packages\.(.*)$/;
  240. localPackages = {};
  241. var match, key;
  242. for(var i=0; i < localStorage.length; i++) {
  243. key = localStorage.key(i);
  244. if (match = key.match(localStorageRE)) {
  245. localPackages[match[1]] = localStorage[key];
  246. }
  247. }
  248. return localPackages;
  249. }
  250. function clearLocalPackages() {
  251. for (var name in localPackages) {
  252. log('Removing ' + name + ' from local storage');
  253. localStorage.removeItem('smalltalk.packages.' + name);
  254. }
  255. }
  256. function log(string) {
  257. if (debug) {
  258. console.log(string);
  259. }
  260. }
  261. that.loadHelios = function() {
  262. loadCSS('helios_frame.css');
  263. var frame = jQuery('<div id="helios"><iframe frameborder=0 src="' + home + 'helios.html"></iframe></div>');
  264. jQuery('body').append(frame);
  265. jQuery(frame).resizable({
  266. handles: 'n',
  267. start: onResizeStart,
  268. stop: onResizeStop,
  269. resize: onResize
  270. });
  271. function onResize() {
  272. jQuery('#helios')
  273. .css('top', '')
  274. .css('width', '100%')
  275. .css('bottom', '0px');
  276. }
  277. function onResizeStart() {
  278. jQuery('#helios').append('<div class="overlay"></div>');
  279. }
  280. function onResizeStop() {
  281. jQuery('#helios').find('.overlay').remove();
  282. }
  283. };
  284. that.popupHelios = function() {
  285. window.open(home + 'helios.html', "Helios", "menubar=no, status=no, scrollbars=no, menubar=no, width=1000, height=600");
  286. };
  287. return that;
  288. })();
  289. window.loadAmber = amber.load;
  290. window.loadHelios = amber.loadHelios;
  291. window.popupHelios = amber.popupHelios;
  292. // Backward compatibility
  293. function toggleAmberIDE () {
  294. return smalltalk.TabManager._toggleAmberIDE();
  295. }