amber.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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/addon/hint/show-hint.css', 'js');
  173. loadCSS('lib/CodeMirror/amber.css', 'js');
  174. }
  175. // This will be called after JS files have been loaded
  176. function initializeSmalltalk() {
  177. that.smalltalkReady = function() {
  178. if (spec.ready) {
  179. spec.ready();
  180. }
  181. evaluateSmalltalkScripts();
  182. };
  183. loadAllJS();
  184. }
  185. /*
  186. * When loaded using AJAX, scripts order not guaranteed.
  187. * Load JS in the order they have been added using addJSToLoad().
  188. * If loaded, will use jQuery's getScript instead of adding a script element
  189. */
  190. function loadAllJS() {
  191. loadJS = loadJSViaScriptTag;
  192. if (typeof jQuery != 'undefined') {
  193. loadJS = loadJSViaJQuery;
  194. }
  195. loadNextJS();
  196. }
  197. function loadNextJS() {
  198. loadJS(jsToLoad[0], function(){
  199. jsToLoad.shift();
  200. if (jsToLoad.length > 0) {
  201. loadNextJS();
  202. }
  203. });
  204. }
  205. function loadJSViaScriptTag(url, callback) {
  206. writeScriptTag(url);
  207. callback();
  208. }
  209. function loadJSViaJQuery(url, callback) {
  210. $.ajax({
  211. dataType: "script",
  212. url: url,
  213. cache: deploy,
  214. success: callback
  215. });
  216. }
  217. function writeScriptTag(src) {
  218. var scriptString = '<script src="' + src + '" type="text/javascript"></script>';
  219. document.write(scriptString);
  220. }
  221. function evaluateSmalltalkScripts() {
  222. jQuery(document).ready(function() {
  223. jQuery('script[type="text/smalltalk"]').each(function(i, elt) {
  224. smalltalk.Compiler._new()._evaluateExpression_(jQuery(elt).html());
  225. });
  226. })
  227. }
  228. var localPackages;
  229. function populateLocalPackages(){
  230. var localStorageRE = /^smalltalk\.packages\.(.*)$/;
  231. localPackages = {};
  232. var match, key;
  233. for(var i=0; i < localStorage.length; i++) {
  234. key = localStorage.key(i);
  235. if (match = key.match(localStorageRE)) {
  236. localPackages[match[1]] = localStorage[key];
  237. }
  238. }
  239. return localPackages;
  240. }
  241. function clearLocalPackages() {
  242. for (var name in localPackages) {
  243. log('Removing ' + name + ' from local storage');
  244. localStorage.removeItem('smalltalk.packages.' + name);
  245. }
  246. }
  247. function log(string) {
  248. if (debug) {
  249. console.log(string);
  250. }
  251. }
  252. that.loadHelios = function() {
  253. loadCSS('helios_frame.css');
  254. var frame = jQuery('<div id="helios"><iframe frameborder=0 src="../helios.html"></iframe></div>');
  255. jQuery('body').append(frame);
  256. jQuery(frame).resizable({
  257. handles: 'n',
  258. start: onResizeStart,
  259. stop: onResizeStop,
  260. resize: onResize,
  261. });
  262. function onResize() {
  263. jQuery('#helios')
  264. .css('top', '')
  265. .css('width', '100%')
  266. .css('bottom', '0px');
  267. }
  268. function onResizeStart() {
  269. jQuery('#helios').append('<div class="overlay"></div>')
  270. }
  271. function onResizeStop() {
  272. jQuery('#helios').find('.overlay').remove();
  273. }
  274. };
  275. return that;
  276. })();
  277. window.loadAmber = amber.load;
  278. window.loadHelios = amber.loadHelios;
  279. // Backward compatibility
  280. function toggleAmberIDE () {
  281. return smalltalk.TabManager._toggleAmberIDE();
  282. }