2
0

amber.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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(amber) {
  7. amber.initialize({"transport.defaultAmdNamespace": "com_example_myproject"});
  8. amber.globals.Browser._open(); // for legacy IDE
  9. amber.popupHelios(); // for Helios IDE
  10. });
  11. For detailed explanation of amber loading, see:
  12. https://github.com/amber-smalltalk/amber/wiki/How-to-load-amber
  13. */
  14. var require;
  15. require = function (require) {
  16. function uniquelyMapped(symbolicPath) {
  17. if (require && typeof define !== "undefined" && define.amd) {
  18. var mappedPath = require.toUrl(symbolicPath),
  19. basePath = require.toUrl('') + symbolicPath;
  20. if (resolveViaDOM(mappedPath) !== resolveViaDOM(basePath)) {
  21. return mappedPath;
  22. }
  23. }
  24. }
  25. function myTag() {
  26. // To be able to use its path and attributes
  27. // to map other parts of Amber, this code must find its path.
  28. // It first looks if require is already present && 'amber' path mapped.
  29. // It not, it looks for id 'amber-path-mapper'.
  30. // When loading amber.js asynchronously, you must include this id,
  31. // or the code can not reliably find its <script>.
  32. // If neither 'amber' mapping, nor script#amber-path-mapper is present,
  33. // (the id is not necessary for inline <script> tag in HTML),
  34. // it uses the "find the last <script> tag present in the moment" method.
  35. var result = uniquelyMapped('amber/amber');
  36. if (result) {
  37. return {src: result, hasAttribute: function () { return false; }};
  38. }
  39. var me = document.getElementById("amber-path-mapper");
  40. if (me && me.tagName.toLowerCase() === "script") {
  41. return me;
  42. }
  43. var scripts = document.getElementsByTagName("script");
  44. return scripts[scripts.length - 1];
  45. }
  46. var me = myTag();
  47. var src = me.src;
  48. // strip the last two elements from the URL
  49. // e.g. http://app.com/amber/support/amber.js -> http://app.com/amber
  50. var amber_home = resolveViaDOM(src).replace(/\/[^\/]+\/[^\/]+$/, "");
  51. // In case of nonstandard deployment, you can specify libraries placement directly ...
  52. var library_home = uniquelyMapped('amber_lib') || me.hasAttribute('data-libs') && me.getAttribute('data-libs');
  53. // ... otherwise, this heuristics is used:
  54. if (!library_home) {
  55. // At the present moment, bower tries to have flat hierarchy,
  56. // which leads to two possible scenarios:
  57. // 1. amber itself was deployed via bower,
  58. // its libraries are at the same bower dir
  59. // where amber itself is placed
  60. // 2. amber was deployed in different fashion,
  61. // its libraries are included by bower locally, inside amber
  62. // The detection is done by looking for '/bower_components/' in amber path.
  63. var match = amber_home.match(/^(.*\/bower_components)\//);
  64. library_home = match ? match[1] : amber_home + '/bower_components';
  65. }
  66. function resolveViaDOM(url) {
  67. var a = document.createElement("a");
  68. a.href = url;
  69. return a.href;
  70. }
  71. var config = {
  72. paths: {
  73. 'amber': amber_home + '/support',
  74. 'amber_vm': amber_home + '/support/deprecated-vm-files',
  75. 'amber_vm/_st': amber_home + '/support/deprecated-vm-files/as-receiver',
  76. 'amber_css': amber_home + '/support/resources', // deprecated
  77. 'amber_lib': library_home,
  78. 'amber_core': amber_home + '/src',
  79. 'helios': amber_home + '/support/helios/src',
  80. 'helios/all': amber_home + '/support/helios/all',
  81. 'helios/resources': amber_home + '/support/helios/resources',
  82. 'helios/index': amber_home + '/support/helios/index',
  83. 'jquery': library_home + '/jquery/jquery.min'
  84. },
  85. map: {
  86. '*': {
  87. 'css': 'amber_lib/require-css/css'
  88. }
  89. },
  90. shim: {
  91. 'ensure-console': {
  92. exports: 'console'
  93. }
  94. }
  95. };
  96. // This is to allow both alternatives of loading:
  97. // before require.js as well as after require.js
  98. // See http://requirejs.org/docs/api.html#config for details
  99. // of usage of 'require' global to allow to pre-define configuration
  100. // before require.js is loaded.
  101. if (require) {
  102. require.config(config);
  103. return require;
  104. } else {
  105. return config;
  106. }
  107. }(require);