amber.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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',
  75. 'amber_css': amber_home + '/css',
  76. 'amber_lib': library_home,
  77. 'amber_core': amber_home + '/src',
  78. 'helios': amber_home + '/src',
  79. 'helios/all': amber_home + '/support/helios/all',
  80. 'helios/resources': amber_home + '/support/helios',
  81. 'helios/index': amber_home + '/support/helios/index',
  82. 'jquery': library_home + '/jquery/jquery.min'
  83. },
  84. map: {
  85. '*': {
  86. 'css': 'amber_lib/require-css/css'
  87. }
  88. },
  89. shim: {
  90. 'ensure-console': {
  91. exports: 'console'
  92. }
  93. }
  94. };
  95. // This is to allow both alternatives of loading:
  96. // before require.js as well as after require.js
  97. // See http://requirejs.org/docs/api.html#config for details
  98. // of usage of 'require' global to allow to pre-define configuration
  99. // before require.js is loaded.
  100. if (require) {
  101. require.config(config);
  102. return require;
  103. } else {
  104. return config;
  105. }
  106. }(require);