amber.js 7.0 KB

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