1
0

amber.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* Amber package loading.
  2. Load this script as well as require.js (works in any order;
  3. either defines 'require', thus passing config, if loaded prior require.js;
  4. or calls require.config, if loaded post require.js).
  5. Usage example:
  6. require(['amber/devel'], function(smalltalk) {
  7. smallralk.initialize();
  8. smalltalk.Browser._open();
  9. });
  10. For detailed explanation of amber loading, see:
  11. https://github.com/amber-smalltalk/amber/wiki/How-to-load-amber
  12. */
  13. var require;
  14. require = function (require) {
  15. // To be able to use its path and attrubutes
  16. // to map other parts of Amber, this code must find its <script> tag.
  17. // It first looks for id 'amber-path-mapper'.
  18. // When loading amber.js asynchronously, you must include this id,
  19. // or the code can not reliably find its <script>.
  20. var me = document.getElementById("amber-path-mapper");
  21. if (!me || me.tagName.toLowerCase() !== "script") {
  22. // If <script> with 'amber-path-mapper' id is not present,
  23. // (this id is not necessary for inline <script> tag in HTML),
  24. // it uses the "find the last <script> tag present in the moment" method.
  25. var scripts = document.getElementsByTagName("script");
  26. me = scripts[scripts.length - 1];
  27. }
  28. var src = me.src;
  29. // strip the last two elements from the URL
  30. // e.g. http://app.com/amber/support/amber.js -> http://app.com/amber
  31. var amber_home = resolveViaDOM(src).replace(/\/[^\/]+\/[^\/]+$/, "");
  32. // In case of nonstandard deployment, you can specify libraries placement directly ...
  33. var library_home = me.hasAttribute('data-libs') && me.getAttribute('data-libs');
  34. // ... otherwise, this heuristics is used:
  35. if (!library_home) {
  36. // At the present moment, bower tries to have flat hierarchy,
  37. // which leads to two possible scenarios:
  38. // 1. amber itself was deployed via bower,
  39. // its libraries are at the same bower dir
  40. // where amber itself is placed
  41. // 2. amber was deployed in different fashion,
  42. // its libraries are included by bower locally, inside amber
  43. // The detection is done by looking for '/bower_components/' in amber path.
  44. var match = amber_home.match(/^(.*\/bower_components)\//);
  45. library_home = match ? match[1] : amber_home + '/bower_components';
  46. }
  47. function resolveViaDOM(url) {
  48. var a = document.createElement("a");
  49. a.href = url;
  50. return a.href;
  51. }
  52. var config = {
  53. paths: {
  54. 'amber': amber_home + '/support',
  55. 'amber_vm': amber_home + '/support',
  56. 'amber_css': amber_home + '/css',
  57. 'amber_lib': library_home,
  58. 'amber_inc': amber_home + '/support',
  59. 'amber_core': amber_home + '/js',
  60. 'amber_core/_source': amber_home + '/st',
  61. 'amber_helios/html': amber_home,
  62. 'jquery': library_home + '/jquery/jquery.min',
  63. 'jquery-ui': library_home + '/jquery-ui/ui/minified/jquery-ui.min'
  64. },
  65. map: {
  66. '*': {
  67. 'css': 'amber_lib/require-css/css'
  68. }
  69. },
  70. shim: {
  71. 'jquery-ui': {
  72. deps: [ 'jquery' ]
  73. },
  74. 'amber_lib/bootstrap/js/bootstrap': {
  75. deps: [ 'jquery', 'css!amber_lib/bootstrap/css/bootstrap' ]
  76. },
  77. 'amber_lib/codemirror/lib/codemirror': {
  78. deps: [ 'css!amber_lib/codemirror/lib/codemirror' ]
  79. },
  80. 'amber_lib/jquery-tabby/jquery.textarea': {
  81. deps: [ 'jquery' ]
  82. },
  83. 'amber_inc/CodeMirror/smalltalk': {
  84. deps: [ 'amber_lib/codemirror/lib/codemirror' ]
  85. },
  86. 'amber_lib/codemirror/addon/hint/show-hint': {
  87. deps: [ '../../lib/codemirror' ]
  88. },
  89. 'ensure-console': {
  90. exports: 'console'
  91. }
  92. }
  93. };
  94. // This is to allow both alternatives of loading:
  95. // before require.js as well as after require.js
  96. // See http://requirejs.org/docs/api.html#config for details
  97. // of usage of 'require' global to allow to pre-define configuration
  98. // before require.js is loaded.
  99. if (require) {
  100. require.config(config);
  101. return require;
  102. } else {
  103. return config;
  104. }
  105. }(require);