From 57c5f11aaeabd86f7bf9a94f33a06d01b82e3a60 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Wed, 16 Oct 2013 14:37:08 +0200 Subject: [PATCH] [FIX] don't use mutation events if unnecessary Mutation events are synchronous and one is triggered per observed alteration (they're not batched). When doing lots of modifications to the DOM tree, they will add a lot of overhead as (in this case) the event handler will run for *each* added node and it will run during (and slow down) node insertion rather than afterwards. bzr revid: xmo@openerp.com-20131016123708-lx389x0g6xs6zu10 --- addons/website/static/src/js/website.editor.js | 4 ++-- addons/website/static/src/js/website.snippets.js | 15 +++++++++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/addons/website/static/src/js/website.editor.js b/addons/website/static/src/js/website.editor.js index aef390027c5..a1b8ffdb9ee 100644 --- a/addons/website/static/src/js/website.editor.js +++ b/addons/website/static/src/js/website.editor.js @@ -927,7 +927,7 @@ } - var Observer = window.MutationObserver || window.WebkitMutationObserver || window.JsMutationObserver; + website.Observer = window.MutationObserver || window.WebkitMutationObserver || window.JsMutationObserver; var OBSERVER_CONFIG = { childList: true, attributes: true, @@ -935,7 +935,7 @@ subtree: true, attributeOldValue: true, }; - var observer = new Observer(function (mutations) { + var observer = new website.Observer(function (mutations) { // NOTE: Webkit does not fire DOMAttrModified => webkit browsers // relying on JsMutationObserver shim (Chrome < 18, Safari < 6) // will not mark dirty on attribute changes (@class, img/@src, diff --git a/addons/website/static/src/js/website.snippets.js b/addons/website/static/src/js/website.snippets.js index 5f6d8b5073c..17dd829708f 100644 --- a/addons/website/static/src/js/website.snippets.js +++ b/addons/website/static/src/js/website.snippets.js @@ -45,7 +45,14 @@ /* ----- SNIPPET SELECTOR ---- */ website.snippet = {}; - + var observer = new website.Observer(function (mutations) { + if (!_(mutations).find(function (m) { + return m.type === 'childList' && m.addedNodes.length > 0; + })) { + return; + } + hack_to_add_snippet_id() + }); // puts $el at the same absolute position as $target function hack_to_add_snippet_id () { @@ -80,7 +87,11 @@ this.$active_snipped_id = false; hack_to_add_snippet_id(); this.snippets = []; - $("body").on('DOMNodeInserted', hack_to_add_snippet_id); + + observer.observe(document.body, { + childList: true, + subtree: true, + }); }, dom_filter: function (dom, sibling) { if (typeof dom === "string") {