/builder/boilerplate/pygwt.js

http://pyjamas.googlecode.com/ · JavaScript · 156 lines · 105 code · 26 blank · 25 comment · 21 complexity · ba8f20cd3dca3b84f0afcb40c3e40553 MD5 · raw file

  1. // this is almost directly taken from Google's GWT which is now open source
  2. var __PYGWT_JS_INCLUDED;
  3. if (!__PYGWT_JS_INCLUDED) {
  4. __PYGWT_JS_INCLUDED = true;
  5. var __pygwt_retryWaitMs = 50;
  6. var __pygwt_moduleNames = [];
  7. var __pygwt_isHostPageLoaded = false;
  8. var __pygwt_onLoadError = null;
  9. function __pygwt_processMetas() {
  10. var metas = document.getElementsByTagName("meta");
  11. for (var i = 0, n = metas.length; i < n; ++i) {
  12. var meta = metas[i];
  13. var name = meta.getAttribute("name");
  14. if (name) {
  15. if (name == "pygwt:module") {
  16. var content = meta.getAttribute("content");
  17. if (content) {
  18. __pygwt_moduleNames = __pygwt_moduleNames.concat(content);
  19. }
  20. }
  21. }
  22. }
  23. }
  24. function __pygwt_forEachModule(lambda) {
  25. for (var i = 0; i < __pygwt_moduleNames.length; ++i) {
  26. lambda(__pygwt_moduleNames[i]);
  27. }
  28. }
  29. // When nested IFRAMEs load, they reach up into the parent page to announce that
  30. // they are ready to run. Because IFRAMEs load asynchronously relative to the
  31. // host page, one of two things can happen when they reach up:
  32. // (1) The host page's onload handler has not yet been called, in which case we
  33. // retry until it has been called.
  34. // (2) The host page's onload handler has already been called, in which case the
  35. // nested IFRAME should be initialized immediately.
  36. //
  37. function __pygwt_webModeFrameOnLoad(iframeWindow, name) {
  38. var moduleInitFn = iframeWindow.pygwtOnLoad;
  39. if (__pygwt_isHostPageLoaded && moduleInitFn) {
  40. var old = window.status;
  41. window.status = "Initializing module '" + name + "'";
  42. try {
  43. moduleInitFn(__pygwt_onLoadError, name);
  44. } finally {
  45. window.status = old;
  46. }
  47. } else {
  48. setTimeout(function() { __pygwt_webModeFrameOnLoad(iframeWindow, name); }, __pygwt_retryWaitMs);
  49. }
  50. }
  51. function __pygwt_hookOnLoad() {
  52. var oldHandler = window.onload;
  53. window.onload = function() {
  54. __pygwt_isHostPageLoaded = true;
  55. if (oldHandler) {
  56. oldHandler();
  57. }
  58. };
  59. }
  60. // Returns an array that splits the module name from the meta content into
  61. // [0] the prefix url, if any, guaranteed to end with a slash
  62. // [1] the dotted module name
  63. //
  64. function __pygwt_splitModuleNameRef(moduleName) {
  65. var parts = ['', moduleName];
  66. var i = moduleName.lastIndexOf("=");
  67. if (i != -1) {
  68. parts[0] = moduleName.substring(0, i) + '/';
  69. parts[1] = moduleName.substring(i+1);
  70. }
  71. return parts;
  72. }
  73. //////////////////////////////////////////////////////////////////
  74. // Called directly from compiled code
  75. //
  76. function __pygwt_initHandlers(resize, beforeunload, unload) {
  77. var oldOnResize = window.onresize;
  78. window.onresize = function() {
  79. resize();
  80. if (oldOnResize)
  81. oldOnResize();
  82. };
  83. var oldOnBeforeUnload = window.onbeforeunload;
  84. window.onbeforeunload = function() {
  85. var ret = beforeunload();
  86. var oldRet;
  87. if (oldOnBeforeUnload)
  88. oldRet = oldOnBeforeUnload();
  89. if (ret !== null)
  90. return ret;
  91. return oldRet;
  92. };
  93. var oldOnUnload = window.onunload;
  94. window.onunload = function() {
  95. unload();
  96. if (oldOnUnload)
  97. oldOnUnload();
  98. };
  99. }
  100. //////////////////////////////////////////////////////////////////
  101. // Web Mode
  102. //
  103. function __pygwt_injectWebModeFrame(name) {
  104. if (document.body) {
  105. var parts = __pygwt_splitModuleNameRef(name);
  106. // Insert an IFRAME
  107. var iframe = document.createElement("iframe");
  108. var selectorURL = parts[0] + parts[1] + ".nocache.html";
  109. iframe.src = selectorURL;
  110. iframe.style.border = '0px';
  111. iframe.style.width = '0px';
  112. iframe.style.height = '0px';
  113. if (document.body.firstChild) {
  114. document.body.insertBefore(iframe, document.body.firstChild);
  115. } else {
  116. document.body.appendChild(iframe);
  117. }
  118. } else {
  119. // Try again in a moment.
  120. //
  121. window.setTimeout(function() { __pygwt_injectWebModeFrame(name); }, __pygwt_retryWaitMs);
  122. }
  123. }
  124. //////////////////////////////////////////////////////////////////
  125. // Set it up
  126. //
  127. __pygwt_processMetas();
  128. __pygwt_hookOnLoad();
  129. __pygwt_forEachModule(__pygwt_injectWebModeFrame);
  130. } // __PYGWT_JS_INCLUDED