PageRenderTime 42ms CodeModel.GetById 20ms app.highlight 8ms RepoModel.GetById 12ms app.codeStats 0ms

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