amber.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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.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. // Specify a version string to avoid wrong browser caching
  26. if (spec.version) {
  27. nocache = '?' + spec.version;
  28. }
  29. loadDependencies();
  30. loadJS('boot.js');
  31. populateLocalPackages();
  32. if (deploy) {
  33. loadPackages([
  34. 'Kernel.deploy',
  35. 'Canvas.deploy'
  36. ]);
  37. } else {
  38. loadIDEDependencies();
  39. loadCSS('amber.css');
  40. loadPackages([
  41. 'Kernel',
  42. 'Canvas',
  43. 'Compiler',
  44. 'parser',
  45. 'IDE',
  46. 'SUnit',
  47. 'Examples',
  48. 'Benchfib',
  49. 'Kernel-Tests'
  50. ]);
  51. }
  52. var additionalFiles = spec.packages || spec.files;
  53. if (additionalFiles) {
  54. loadPackages(additionalFiles, spec.prefix);
  55. }
  56. // Always load all local packages
  57. for (name in localPackages) {
  58. log('Local package: ' + name);
  59. localStorageSource.push(localPackages[name]);
  60. }
  61. // Be sure to setup & initialize smalltalk classes
  62. loadJS('init.js');
  63. initializeSmalltalk();
  64. };
  65. function loadPackages(names, prefix){
  66. var name, url;
  67. var prefix = prefix || 'js';
  68. for (var i=0; i < names.length; i++) {
  69. name = names[i].split(/\.js$/)[0];
  70. // Only load package from the server if it isn't stored in
  71. // localStorage
  72. if (!(name in localPackages)) {
  73. log('Server package: ' + name);
  74. loadJS(name + '.js', prefix);
  75. }
  76. }
  77. };
  78. function loadJS(name, prefix) {
  79. var prefix = prefix || 'js';
  80. var name = name;
  81. if (!deploy) {
  82. name = name + nocache;
  83. }
  84. var url = home + prefix + '/' + name;
  85. var scriptString = '<script src="' + url + '" type="text/javascript"></script>';
  86. document.write(scriptString);
  87. };
  88. function loadCSS(name, prefix) {
  89. var prefix = prefix || 'css';
  90. var name = name;
  91. if (!deploy) {
  92. name = name + nocache;
  93. }
  94. var url = home + prefix + '/' + name;
  95. var link = document.createElement("link");
  96. link.setAttribute("rel", "stylesheet");
  97. link.setAttribute("type", "text/css");
  98. link.setAttribute("href", url);
  99. document.getElementsByTagName("head")[0].appendChild(link);
  100. };
  101. function loadDependencies() {
  102. if (typeof jQuery == 'undefined') {
  103. loadJS('lib/jQuery/jquery-1.6.4.min.js');
  104. }
  105. if ((typeof jQuery == 'undefined') || (typeof jQuery.ui == 'undefined')) {
  106. loadJS('lib/jQuery/jquery-ui-1.8.16.custom.min.js');
  107. }
  108. };
  109. function loadIDEDependencies() {
  110. loadJS('lib/jQuery/jquery.textarea.js');
  111. loadJS('lib/CodeMirror/lib/codemirror.js');
  112. loadCSS('lib/CodeMirror/lib/codemirror.css', 'js');
  113. loadJS('lib/CodeMirror/mode/smalltalk/smalltalk.js');
  114. loadCSS('lib/CodeMirror/theme/amber.css', 'js');
  115. };
  116. // This will be called after JS files have been loaded
  117. function initializeSmalltalk() {
  118. window.smalltalkReady = function() {
  119. for (var i=0; i < localStorageSource.length; i++) {
  120. eval(localStorageSource[i]);
  121. }
  122. if (deploy) {
  123. smalltalk.setDeploymentMode();
  124. }
  125. if (spec.ready) {
  126. spec.ready();
  127. }
  128. }
  129. };
  130. function populateLocalPackages(){
  131. var localStorageRE = /^smalltalk\.packages\.(.*)$/;
  132. localPackages = {};
  133. var match, key;
  134. for(var i=0; i < localStorage.length; i++) {
  135. key = localStorage.key(i);
  136. if (match = key.match(localStorageRE)) {
  137. localPackages[match[1]] = localStorage[key];
  138. }
  139. }
  140. return localPackages;
  141. };
  142. function clearLocalPackages() {
  143. for (var name in localPackages) {
  144. log('Removing ' + name + ' from local storage');
  145. localStorage.removeItem('smalltalk.packages.' + name);
  146. }
  147. };
  148. function log(string) {
  149. if (debug) {
  150. console.log(string);
  151. }
  152. }
  153. return that;
  154. })();
  155. window.loadAmber = function(spec) {
  156. amber.load(spec);
  157. }