amber.js 5.9 KB

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