2
0

amber.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /* Adapted from Clamato by Avi Bryant. http://www.clamato.net */
  2. (function() {
  3. var
  4. scripts = document.getElementsByTagName("script"),
  5. src = scripts[ scripts.length - 1 ].src,
  6. home = src.split("/").slice(0, -2).join("/") + "/";
  7. window.Amber = {
  8. debug: false,
  9. deploy: false,
  10. nocache: '?' + (new Date()).getTime(),
  11. home: home,
  12. /*
  13. example:
  14. Amber.load({
  15. files: ['MyCategory1.js', 'MyCategory2.js'],
  16. ready: function() {smalltalk.Browser._open()}
  17. })
  18. */
  19. load: function(spec) {
  20. var self = this;
  21. self.localStorageSource = [];
  22. self.spec = ((typeof spec == 'undefined') ? {} : spec);
  23. // In deployment mode, only the compressed version of Kernel
  24. // and Canvas are loaded
  25. self.deploy = self.spec.deploy || self.deploy;
  26. self.debug = self.spec.debug || self.debug;
  27. // Specify a version string to avoid wrong browser caching
  28. if (self.spec.version)
  29. self.nocache = '?' + self.spec.version;
  30. self.loadDependencies();
  31. self.loadJS('boot.js');
  32. self.populateLocalPackages();
  33. if (self.deploy) {
  34. self.loadPackages([
  35. 'Kernel.deploy',
  36. 'Canvas.deploy'
  37. ]);
  38. } else {
  39. self.loadIDEDependencies();
  40. self.loadCSS('amber.css');
  41. self.loadPackages([
  42. 'Kernel',
  43. 'Canvas',
  44. 'Compiler',
  45. 'parser',
  46. 'IDE',
  47. 'SUnit',
  48. 'Examples',
  49. 'Benchfib',
  50. 'Kernel-Tests',
  51. ]);
  52. }
  53. var additionalFiles = self.spec.packages || self.spec.files;
  54. if (additionalFiles)
  55. self.loadPackages(additionalFiles, self.spec.prefix);
  56. // Always load all local packages
  57. for (name in self.localPackages) {
  58. self.log('Local package: ' + name);
  59. self.localStorageSource.push(self.localPackages[name]);
  60. }
  61. // Be sure to setup & initialize smalltalk classes
  62. self.loadJS('init.js');
  63. },
  64. loadPackages: function(names, prefix){
  65. var self = this;
  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 self.localPackages)) {
  73. self.log('Server package: ' + name);
  74. self.loadJS(name + '.js', prefix);
  75. }
  76. }
  77. },
  78. loadJS: function(name, prefix) {
  79. var self = this;
  80. var prefix = prefix || 'js';
  81. var name = name;
  82. if (!self.deploy)
  83. name = name + self.nocache;
  84. var url = self.home + prefix + '/' + name;
  85. var scriptString = '<script src="' + url + '" type="text/javascript"></script>';
  86. document.write(scriptString);
  87. },
  88. loadCSS: function (name, prefix) {
  89. var self = this;
  90. var prefix = prefix || 'css';
  91. var name = name;
  92. if (!self.deploy)
  93. name = name + self.nocache;
  94. var url = self.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. loadDependencies: function() {
  102. var self = this;
  103. self.loadJS('lib/jQuery/jquery-1.6.4.min.js');
  104. self.loadJS('lib/jQuery/jquery-ui-1.8.9.custom.min.js');
  105. },
  106. loadIDEDependencies: function() {
  107. var self = this;
  108. self.loadJS('lib/jQuery/jquery.textarea.js');
  109. self.loadJS('lib/CodeMirror/lib/codemirror.js');
  110. self.loadCSS('lib/CodeMirror/lib/codemirror.css', 'js');
  111. self.loadJS('lib/CodeMirror/mode/smalltalk/smalltalk.js');
  112. self.loadCSS('lib/CodeMirror/theme/amber.css', 'js');
  113. },
  114. // This will be called after JS files have been loaded
  115. initializeSmalltalk: function(){
  116. var self = this;
  117. for (var i=0; i < self.localStorageSource.length; i++)
  118. eval(self.localStorageSource[i]);
  119. smalltalk.init(smalltalk.Object);
  120. smalltalk.classes()._do_(function(each) {each._initialize()});
  121. if (self.spec.ready)
  122. self.spec.ready();
  123. if (self.deploy)
  124. smalltalk.setDeploymentMode();
  125. },
  126. populateLocalPackages: function(){
  127. var self = this;
  128. var localStorageRE = /^smalltalk\.packages\.(.*)$/;
  129. self.localPackages = {};
  130. var match, key;
  131. for(var i=0; i < localStorage.length; i++) {
  132. key = localStorage.key(i);
  133. if (match = key.match(localStorageRE)) {
  134. self.localPackages[match[1]] = localStorage[key];
  135. }
  136. }
  137. return self.localPackages;
  138. },
  139. clearLocalPackages: function(){
  140. var self = this;
  141. for (var name in self.localPackages) {
  142. self.log('Removing ' + name + ' from local storage');
  143. localStorage.removeItem('smalltalk.packages.' + name);
  144. }
  145. },
  146. log: function(string) {
  147. var self = this;
  148. if (self.debug)
  149. console.log(string);
  150. }
  151. };
  152. window.loadAmber = function(spec) {
  153. Amber.load(spec);
  154. }
  155. })();