/Objective-J/Bootstrap.js

http://github.com/cacaodev/cappuccino · JavaScript · 157 lines · 93 code · 34 blank · 30 comment · 21 complexity · 6e261dc92a56b3b0c8aad9fd8a6475ee MD5 · raw file

  1. /*
  2. * Bootstrap.js
  3. * Objective-J
  4. *
  5. * Created by Francisco Tolmasky.
  6. * Copyright 2010, 280 North, Inc.
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #ifdef COMMONJS
  23. var mainBundleURL = new CFURL("file:" + require("file").cwd()).asDirectoryPathURL();
  24. #elif defined(BROWSER)
  25. // This is automatic when importing, but we'd like these important URLs to
  26. // be taken into consideration in the cache as well.
  27. enableCFURLCaching();
  28. // To determine where our application lives, start with the current URL of the page.
  29. var pageURL = new CFURL(window.location.href),
  30. // Look for any <base> tags and choose the last one (which is the one that will take effect).
  31. DOMBaseElements = document.getElementsByTagName("base"),
  32. DOMBaseElementsCount = DOMBaseElements.length;
  33. if (DOMBaseElementsCount > 0)
  34. {
  35. var DOMBaseElement = DOMBaseElements[DOMBaseElementsCount - 1],
  36. DOMBaseElementHref = DOMBaseElement && DOMBaseElement.getAttribute("href");
  37. // If we have one, use it instead.
  38. if (DOMBaseElementHref)
  39. pageURL = new CFURL(DOMBaseElementHref, pageURL);
  40. }
  41. // Turn the main file into a URL.
  42. var mainFileURL = new CFURL(window.OBJJ_MAIN_FILE || "main.j"),
  43. // The main bundle is the containing folder of the main file.
  44. mainBundleURL = new CFURL(".", new CFURL(mainFileURL, pageURL)).absoluteURL(),
  45. // We assume the "first part" of the path is completely resolved.
  46. assumedResolvedURL = new CFURL("..", mainBundleURL).absoluteURL();
  47. // .. doesn't work if we're already at root, so "go back" one more level to the scheme and authority.
  48. if (mainBundleURL === assumedResolvedURL)
  49. assumedResolvedURL = new CFURL(assumedResolvedURL.schemeAndAuthority());
  50. StaticResource.resourceAtURL(assumedResolvedURL, YES);
  51. exports.pageURL = pageURL;
  52. exports.bootstrap = function()
  53. {
  54. resolveMainBundleURL();
  55. }
  56. function resolveMainBundleURL()
  57. {
  58. StaticResource.resolveResourceAtURL(mainBundleURL, YES, function(/*StaticResource*/ aResource)
  59. {
  60. var includeURLs = StaticResource.includeURLs(),
  61. index = 0,
  62. count = includeURLs.length;
  63. for (; index < count; ++index)
  64. aResource.resourceAtURL(includeURLs[index], YES);
  65. Executable.fileImporterForURL(mainBundleURL)(mainFileURL.lastPathComponent(), YES, function()
  66. {
  67. disableCFURLCaching();
  68. afterDocumentLoad(function()
  69. {
  70. var hashString = window.location.hash.substring(1),
  71. args = [];
  72. if (hashString.length)
  73. {
  74. args = hashString.split("/");
  75. for (var i = 0, count = args.length; i < count; i++)
  76. args[i] = decodeURIComponent(args[i]);
  77. }
  78. var namedArgsArray = window.location.search.substring(1).split("&"),
  79. namedArgs = new CFMutableDictionary();
  80. for (var i = 0, count = namedArgsArray.length; i < count; i++)
  81. {
  82. var thisArg = namedArgsArray[i].split("=");
  83. if (!thisArg[0])
  84. continue;
  85. if (thisArg[1] == null)
  86. thisArg[1] = true;
  87. namedArgs.setValueForKey(decodeURIComponent(thisArg[0]), decodeURIComponent(thisArg[1]));
  88. }
  89. main(args, namedArgs);
  90. });
  91. });
  92. });
  93. }
  94. var documentLoaded = NO;
  95. function afterDocumentLoad(/*Function*/ aFunction)
  96. {
  97. if (documentLoaded || document.readyState === "complete")
  98. return aFunction();
  99. if (window.addEventListener)
  100. window.addEventListener("load", aFunction, NO);
  101. else if (window.attachEvent)
  102. window.attachEvent("onload", aFunction);
  103. }
  104. afterDocumentLoad(function()
  105. {
  106. documentLoaded = YES;
  107. });
  108. if (typeof OBJJ_AUTO_BOOTSTRAP === "undefined" || OBJJ_AUTO_BOOTSTRAP)
  109. exports.bootstrap();
  110. #endif
  111. function makeAbsoluteURL(/*CFURL|String*/ aURL)
  112. {
  113. if (aURL instanceof CFURL && aURL.scheme())
  114. return aURL;
  115. return new CFURL(aURL, mainBundleURL);
  116. }
  117. GLOBAL(objj_importFile) = Executable.fileImporterForURL(mainBundleURL);
  118. GLOBAL(objj_executeFile) = Executable.fileExecuterForURL(mainBundleURL);
  119. GLOBAL(objj_import) = function()
  120. {
  121. CPLog.warn("objj_import is deprecated, use objj_importFile instead");
  122. objj_importFile.apply(this, arguments);
  123. }