2
0

amber.js 8.7 KB

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