From 2216b7f32e852611a4692fb033b0a1cafddb0e95 Mon Sep 17 00:00:00 2001 From: Deathamns Date: Sun, 19 Oct 2014 17:36:34 +0200 Subject: [PATCH] Implement popup autoresizing for Safari By default, Safari doesn't resize the popup to its content, but it's possible to set the size pragmatically. The popup will be resized every time when a change happens in the DOM tree. --- src/js/vapi-common.js | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/src/js/vapi-common.js b/src/js/vapi-common.js index d70c449..e2153d5 100644 --- a/src/js/vapi-common.js +++ b/src/js/vapi-common.js @@ -49,12 +49,35 @@ if (window.chrome) { // update popover size to its content if (safari.self.identifier === 'popover' && safari.self) { - vAPI.updatePopoverSize = function() { - safari.self.width = document.body.clientWidth; - safari.self.height = document.body.clientHeight; - }; + (function() { + var upadteTimer = null; + var resizePopover = function() { + if (upadteTimer) { + return; + } - setTimeout(vAPI.updatePopoverSize, 200); + upadteTimer = setTimeout(function() { + safari.self.width = document.body.clientWidth; + safari.self.height = document.body.clientHeight; + upadteTimer = null; + }, 50); + }; + + var mutObs = window.MutationObserver || window.WebkitMutationObserver; + + if (mutObs) { + (new mutObs(resizePopover)).observe(document, { + childList: true, + attributes: true, + characterData: true, + subtree: true + }); + } + else { + // Safari doesn't support DOMAttrModified + document.addEventListener('DOMSubtreeModified', resizePopover); + } + })(); } }