Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Bind objects to newly added iframes in the Application

Conflicts:

	lib/ripple/bootstrap.js
  • Loading branch information...
commit 86276ae6738fb89e5016f1c818d9f00d83041cd4 1 parent eed2c71
@zhizhangchen authored
Showing with 71 additions and 14 deletions.
  1. +71 −14 lib/ripple/bootstrap.js
View
85 lib/ripple/bootstrap.js
@@ -13,12 +13,22 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-var _bound,
- _console = require('ripple/console'),
+var _console = require('ripple/console'),
+ utils = require('ripple/utils'),
ui = require('ripple/ui'),
db = require('ripple/db'),
_CURRENT_URL = "current-url";
+var _srcChangedObserver = new WebKitMutationObserver(function (mutations) {
+ utils.forEach(mutations, function (mutation) {
+ _bindObjectsToFrame(mutation.target);
+ });
+ });
+
+function _observeIframeAdded(doc) {
+ doc._iframeAddedObserver.observe(doc, {childList: true, subtree: true});
+}
+
function _bindObjects(win, doc) {
if (!win.tinyHippos) {
require('ripple/emulatorBridge').link(win, doc);
@@ -27,7 +37,64 @@ function _bindObjects(win, doc) {
require('ripple/documentEventListener').mask(win, doc);
require('ripple/deviceMotionEmulator').init(win, doc);
require('ripple/resizer').init(win, doc);
- _bound = true;
+ win.addEventListener("DOMContentLoaded", function () {
+ var iframes = $(this.document).find("iframe");
+ // Observe iframe added event so that we can bind objects to newly added iframes
+ if (!this.document._iframeAddedObserver) {
+ this.document._iframeAddedObserver = new WebKitMutationObserver(function (mutations) {
+ utils.forEach(mutations, function (mutation) {
+ for ( var i = 0; i < mutation.addedNodes.length; i ++ ) {
+ var node = mutation.addedNodes[i];
+ if (node.tagName.toUpperCase() === "IFRAME") {
+ _bindObjectsToFrame(node);
+ }
+ }
+ });
+ })
+ _observeIframeAdded(this.document);
+ }
+ iframes.each(function (){
+ _bindObjectsToFrame(this);
+ })
+
+ });
+ win.frameElement._bound = true;
+ }
+}
+
+function _beforeLoad () {
+ this._bound = false;
+ _bindObjects(this.contentWindow, this.contentDocument);
+ this._intervalId = window.setInterval(function () {
+ if (this._bound) {
+ window.clearInterval(this._intervalId);
+ } else {
+ _bindObjects(this.contentWindow, this.contentDocument);
+ }
+ }.bind(this), 1);
+}
+
+function _bindObjectsToFrame (frame) {
+ _srcChangedObserver.observe(frame, {attributes: true, attributeFilter: ["src"]});
+ frame.addEventListener("beforeload", _beforeLoad);
+ // beforeload event of an iframe will not be triggered unless we detach and
+ // then attach the iframe to the dom tree
+ var parentNode = frame.parentNode;
+ var nextNode = frame.nextNode;
+ if (parentNode) {
+ // Disable iframe added observer to avoid infinite loop of binding objects
+ if (frame.ownerDocument && frame.ownerDocument._iframeAddedObserver) {
+ frame.ownerDocument._iframeAddedObserver.disconnect();
+ }
+ parentNode.removeChild(frame);
+ if (nextNode)
+ nextNode.insertBefore(frame);
+ else
+ parentNode.appendChild(frame);
+
+ if (frame.ownerDocument && frame.ownerDocument._iframeAddedObserver) {
+ _observeIframeAdded(frame.ownerDocument);
+ }
}
}
@@ -37,17 +104,7 @@ function _createFrame(src) {
frame.src = src;
if (ui.registered("omnibar")) {
- frame.addEventListener("beforeload", function () {
- _bound = false;
- _bindObjects(frame.contentWindow, frame.contentDocument);
- var id = window.setInterval(function () {
- if (_bound) {
- window.clearInterval(id);
- } else {
- _bindObjects(frame.contentWindow, frame.contentDocument);
- }
- }, 1);
- });
+ _bindObjectsToFrame(frame);
}
return frame;
Please sign in to comment.
Something went wrong with that request. Please try again.