1
0

amber.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 nocache = '?' + (new Date()).getTime();
  14. var debug;
  15. var deploy;
  16. var localStorageSource = [];
  17. var localPackages;
  18. var spec;
  19. that.toggleIDE = function() {
  20. if ($('#jtalk').length == 0) {
  21. smalltalk.Browser._open();
  22. } else if ($('#jtalk').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. loadJS('compat.js');
  49. loadJS('boot.js');
  50. populateLocalPackages();
  51. if (deploy) {
  52. loadPackages([
  53. 'Kernel-Objects.deploy',
  54. 'Kernel-Classes.deploy',
  55. 'Kernel-Methods.deploy',
  56. 'Kernel-Collections.deploy',
  57. 'Kernel-Exceptions.deploy',
  58. 'Kernel-Transcript.deploy',
  59. 'Kernel-Announcements.deploy',
  60. 'Canvas.deploy'
  61. ]);
  62. } else {
  63. loadIDEDependencies();
  64. loadCSS('amber.css');
  65. loadPackages([
  66. 'Kernel-Objects',
  67. 'Kernel-Classes',
  68. 'Kernel-Methods',
  69. 'Kernel-Collections',
  70. 'Kernel-Exceptions',
  71. 'Kernel-Transcript',
  72. 'Kernel-Announcements',
  73. 'Canvas',
  74. 'Compiler',
  75. 'parser',
  76. 'IDE',
  77. 'SUnit',
  78. 'Examples',
  79. 'Benchfib',
  80. 'Kernel-Tests'
  81. ]);
  82. }
  83. var additionalFiles = spec.packages || spec.files;
  84. if (additionalFiles) {
  85. loadPackages(additionalFiles, spec.prefix);
  86. }
  87. // Always load all local packages
  88. for (name in localPackages) {
  89. log('Local package: ' + name);
  90. var sourceCode = unescape(localPackages[name]);
  91. sourceCode += "\nsmalltalk.Package._init_('"+name+"')";
  92. localStorageSource.push(sourceCode);
  93. }
  94. // Be sure to setup & initialize smalltalk classes
  95. loadJS('init.js');
  96. initializeSmalltalk();
  97. };
  98. function loadPackages(names, prefix){
  99. var name, url;
  100. var prefix = prefix || 'js';
  101. for (var i=0; i < names.length; i++) {
  102. name = names[i].split(/\.js$/)[0];
  103. // Only load package from the server if it isn't stored in
  104. // localStorage
  105. if (!(name in localPackages)) {
  106. log('Server package: ' + name);
  107. loadJS(name + '.js', prefix);
  108. }
  109. }
  110. };
  111. function loadJS(name, prefix) {
  112. var prefix = prefix || 'js';
  113. var name = name;
  114. // Specify a version string to avoid wrong browser caching
  115. if (spec.version) {
  116. nocache = '?' + spec.version;
  117. }
  118. loadDependencies();
  119. loadJS('compat.js');
  120. loadJS('boot.js');
  121. populateLocalPackages();
  122. if (deploy) {
  123. loadPackages([
  124. 'Kernel-Objects.deploy',
  125. 'Kernel-Classes.deploy',
  126. 'Kernel-Methods.deploy',
  127. 'Kernel-Collections.deploy',
  128. 'Kernel-Exceptions.deploy',
  129. 'Kernel-Transcript.deploy',
  130. 'Kernel-Announcements.deploy',
  131. 'Canvas.deploy'
  132. ]);
  133. } else {
  134. loadIDEDependencies();
  135. loadCSS('amber.css');
  136. loadPackages([
  137. 'Kernel-Objects',
  138. 'Kernel-Classes',
  139. 'Kernel-Methods',
  140. 'Kernel-Collections',
  141. 'Kernel-Exceptions',
  142. 'Kernel-Transcript',
  143. 'Kernel-Announcements',
  144. 'Canvas',
  145. 'Compiler',
  146. 'parser',
  147. 'IDE',
  148. 'SUnit',
  149. 'Examples',
  150. 'Benchfib',
  151. 'Kernel-Tests'
  152. ]);
  153. }
  154. var additionalFiles = spec.packages || spec.files;
  155. if (additionalFiles) {
  156. loadPackages(additionalFiles, spec.prefix);
  157. }
  158. // Always load all local packages
  159. for (name in localPackages) {
  160. log('Local package: ' + name);
  161. localStorageSource.push(localPackages[name]);
  162. }
  163. // Be sure to setup & initialize smalltalk classes
  164. loadJS('init.js');
  165. initializeSmalltalk();
  166. };
  167. function loadPackages(names, prefix){
  168. var name, url;
  169. var prefix = prefix || 'js';
  170. for (var i=0; i < names.length; i++) {
  171. name = names[i].split(/\.js$/)[0];
  172. // Only load package from the server if it isn't stored in
  173. // localStorage
  174. if (!(name in localPackages)) {
  175. log('Server package: ' + name);
  176. loadJS(name + '.js', prefix);
  177. }
  178. }
  179. };
  180. function loadJS(name, prefix) {
  181. var prefix = prefix || 'js';
  182. var name = name;
  183. if (!deploy) {
  184. name = name + nocache;
  185. }
  186. var url = home + prefix + '/' + name;
  187. var scriptString = '<script src="' + url + '" type="text/javascript"></script>';
  188. document.write(scriptString);
  189. };
  190. function loadCSS(name, prefix) {
  191. var prefix = prefix || 'css';
  192. var name = name;
  193. if (!deploy) {
  194. name = name + nocache;
  195. }
  196. var url = home + prefix + '/' + name;
  197. var link = document.createElement("link");
  198. link.setAttribute("rel", "stylesheet");
  199. link.setAttribute("type", "text/css");
  200. link.setAttribute("href", url);
  201. document.getElementsByTagName("head")[0].appendChild(link);
  202. };
  203. function loadDependencies() {
  204. if (typeof jQuery == 'undefined') {
  205. loadJS('lib/jQuery/jquery-1.6.4.min.js');
  206. }
  207. if ((typeof jQuery == 'undefined') || (typeof jQuery.ui == 'undefined')) {
  208. loadJS('lib/jQuery/jquery-ui-1.8.16.custom.min.js');
  209. }
  210. };
  211. function loadIDEDependencies() {
  212. loadJS('lib/jQuery/jquery.textarea.js');
  213. loadJS('lib/CodeMirror/codemirror.js');
  214. loadJS('lib/CodeMirror/smalltalk.js');
  215. loadCSS('lib/CodeMirror/codemirror.css', 'js');
  216. loadCSS('lib/CodeMirror/amber.css', 'js');
  217. };
  218. // This will be called after JS files have been loaded
  219. function initializeSmalltalk() {
  220. window.smalltalkReady = function() {
  221. for (var i=0; i < localStorageSource.length; i++) {
  222. eval(localStorageSource[i]);
  223. }
  224. if (deploy) {
  225. smalltalk.setDeploymentMode();
  226. }
  227. if (spec.ready) {
  228. spec.ready();
  229. }
  230. }
  231. };
  232. function populateLocalPackages(){
  233. var localStorageRE = /^smalltalk\.packages\.(.*)$/;
  234. localPackages = {};
  235. var match, key;
  236. for(var i=0; i < localStorage.length; i++) {
  237. key = localStorage.key(i);
  238. if (match = key.match(localStorageRE)) {
  239. localPackages[match[1]] = localStorage[key];
  240. }
  241. }
  242. return localPackages;
  243. };
  244. function clearLocalPackages() {
  245. for (var name in localPackages) {
  246. log('Removing ' + name + ' from local storage');
  247. localStorage.removeItem('smalltalk.packages.' + name);
  248. }
  249. };
  250. function log(string) {
  251. if (debug) {
  252. console.log(string);
  253. }
  254. }
  255. return that;
  256. })();
  257. window.loadAmber = amber.load;
  258. window.toggleAmberIDE = amber.toggleIDE;