amber.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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('compat.js');
  49. addJSToLoad('boot.js');
  50. if (deploy) {
  51. loadPackages([
  52. 'Kernel-Objects.deploy',
  53. 'Kernel-Classes.deploy',
  54. 'Kernel-Methods.deploy',
  55. 'Kernel-Collections.deploy',
  56. 'Kernel-Exceptions.deploy',
  57. 'Kernel-Transcript.deploy',
  58. 'Kernel-Announcements.deploy',
  59. 'Canvas.deploy'
  60. ]);
  61. } else {
  62. loadIDEDependencies();
  63. loadCSS('amber.css');
  64. loadPackages([
  65. 'Kernel-Objects',
  66. 'Kernel-Classes',
  67. 'Kernel-Methods',
  68. 'Kernel-Collections',
  69. 'Kernel-Exceptions',
  70. 'Kernel-Transcript',
  71. 'Kernel-Announcements',
  72. 'Canvas',
  73. 'SUnit',
  74. 'Importer-Exporter',
  75. 'Compiler-Exceptions',
  76. 'Compiler-Core',
  77. 'Compiler-AST',
  78. 'Compiler-Semantic',
  79. 'Compiler-IR',
  80. 'Compiler-Inlining',
  81. 'Compiler-Tests',
  82. 'parser',
  83. 'IDE',
  84. 'Examples',
  85. 'Benchfib',
  86. 'Kernel-Tests'
  87. ]);
  88. }
  89. var additionalFiles = spec.packages || spec.files;
  90. if (additionalFiles) {
  91. loadPackages(additionalFiles, spec.prefix, spec.packageHome);
  92. }
  93. // Be sure to setup & initialize smalltalk classes
  94. addJSToLoad('init.js');
  95. initializeSmalltalk();
  96. };
  97. function loadPackages(names, prefix, urlHome){
  98. var name, url;
  99. var prefix = prefix || 'js';
  100. var 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. };
  106. function addJSToLoad(name, prefix, urlHome) {
  107. var urlHome = urlHome || home;
  108. jsToLoad.push(buildJSURL(name, prefix, urlHome));
  109. };
  110. function buildJSURL(name, prefix, urlHome) {
  111. var prefix = prefix || 'js';
  112. var name = name;
  113. var urlHome = urlHome || home;
  114. if (!deploy) {
  115. name = name + nocache;
  116. }
  117. return urlHome + prefix + '/' + name;
  118. };
  119. function loadCSS(name, prefix) {
  120. var prefix = prefix || 'css';
  121. var name = name;
  122. if (!deploy) {
  123. name = name + nocache;
  124. }
  125. var url = home + prefix + '/' + name;
  126. var link = document.createElement("link");
  127. link.setAttribute("rel", "stylesheet");
  128. link.setAttribute("type", "text/css");
  129. link.setAttribute("href", url);
  130. document.getElementsByTagName("head")[0].appendChild(link);
  131. };
  132. function loadDependencies() {
  133. if (typeof jQuery == 'undefined') {
  134. writeScriptTag(buildJSURL('lib/jQuery/jquery-1.6.4.min.js'));
  135. }
  136. if ((typeof jQuery == 'undefined') || (typeof jQuery.ui == 'undefined')) {
  137. writeScriptTag(buildJSURL('lib/jQuery/jquery-ui-1.8.16.custom.min.js'));
  138. }
  139. };
  140. function loadIDEDependencies() {
  141. addJSToLoad('lib/jQuery/jquery.textarea.js');
  142. addJSToLoad('lib/CodeMirror/codemirror.js');
  143. addJSToLoad('lib/CodeMirror/smalltalk.js');
  144. loadCSS('lib/CodeMirror/codemirror.css', 'js');
  145. loadCSS('lib/CodeMirror/amber.css', 'js');
  146. };
  147. // This will be called after JS files have been loaded
  148. function initializeSmalltalk() {
  149. window.smalltalkReady = function() {
  150. if (spec.ready) {
  151. spec.ready();
  152. }
  153. }
  154. loadAllJS();
  155. };
  156. /*
  157. * When loaded using AJAX, scripts order not guaranteed.
  158. * Load JS in the order they have been added using addJSToLoad().
  159. * If loaded, will use jQuery's getScript instead of adding a script element
  160. */
  161. function loadAllJS() {
  162. loadJS = loadJSViaScriptTag;
  163. if (typeof jQuery != 'undefined') {
  164. loadJS = loadJSViaJQuery;
  165. }
  166. loadNextJS();
  167. };
  168. function loadNextJS() {
  169. loadJS(jsToLoad[0], function(){
  170. jsToLoad.shift();
  171. if (jsToLoad.length > 0) {
  172. loadNextJS();
  173. }
  174. });
  175. };
  176. function loadJSViaScriptTag(url, callback) {
  177. writeScriptTag(url);
  178. callback();
  179. };
  180. function loadJSViaJQuery(url, callback) {
  181. $.ajax({
  182. dataType: "script",
  183. url: jsToLoad[0],
  184. cache: deploy,
  185. success: callback
  186. });
  187. };
  188. function writeScriptTag(src) {
  189. var scriptString = '<script src="' + src + '" type="text/javascript"></script>';
  190. document.write(scriptString);
  191. };
  192. function populateLocalPackages(){
  193. var localStorageRE = /^smalltalk\.packages\.(.*)$/;
  194. localPackages = {};
  195. var match, key;
  196. for(var i=0; i < localStorage.length; i++) {
  197. key = localStorage.key(i);
  198. if (match = key.match(localStorageRE)) {
  199. localPackages[match[1]] = localStorage[key];
  200. }
  201. }
  202. return localPackages;
  203. };
  204. function clearLocalPackages() {
  205. for (var name in localPackages) {
  206. log('Removing ' + name + ' from local storage');
  207. localStorage.removeItem('smalltalk.packages.' + name);
  208. }
  209. };
  210. function log(string) {
  211. if (debug) {
  212. console.log(string);
  213. }
  214. }
  215. return that;
  216. })();
  217. window.loadAmber = amber.load;
  218. window.toggleAmberIDE = amber.toggleIDE;