1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-22 18:32:45 +01:00

Abstract out platform-specific popup UI JavaScript

This commit is contained in:
Chris 2015-01-11 19:07:25 -07:00
parent b57d04231d
commit 0bd245803f
2 changed files with 45 additions and 21 deletions

View File

@ -104,27 +104,6 @@ vAPI.i18n = function(s) {
return this.i18nData[s] || s;
};
/******************************************************************************/
// update popover size to its content
if (safari.self.identifier === 'popover') {
var whenSizeChanges = function(d,l){(function(a,d){function h(a,b){a.addEventListener("scroll",b)}function e(){k.style.width=b.offsetWidth+10+"px";k.style.height=b.offsetHeight+10+"px";b.scrollLeft=b.scrollWidth;b.scrollTop=b.scrollHeight;c.scrollLeft=c.scrollWidth;c.scrollTop=c.scrollHeight;f=a.offsetWidth;g=a.offsetHeight}a.b=d;a.a=document.createElement("div");a.a.style.cssText="position:absolute;left:0;top:0;right:0;bottom:0;overflow:scroll;z-index:-1;visibility:hidden";a.a.innerHTML='<div style="position:absolute;left:0;top:0;right:0;bottom:0;overflow:scroll;z-index:-1;visibility:hidden"><div style="position:absolute;left:0;top:0;"></div></div><div style="position:absolute;left:0;top:0;right:0;bottom:0;overflow:scroll;z-index:-1;visibility:hidden"><div style="position:absolute;left:0;top:0;width:200%;height:200%"></div></div>';
a.appendChild(a.a);var b=a.a.childNodes[0],k=b.childNodes[0],c=a.a.childNodes[1],f,g;e();h(b,function(){(a.offsetWidth>f||a.offsetHeight>g)&&a.b();e()});h(c,function(){(a.offsetWidth<f||a.offsetHeight<g)&&a.b();e()})})(d,l)};
var onLoaded = function() {
var body = document.body, popover = safari.self;
var updateSize = function() {
popover.width = body.offsetWidth;
popover.height = body.offsetHeight;
};
body.style.position = "relative"; // Necessary for size change detection
whenSizeChanges(body, updateSize);
updateSize();
};
window.addEventListener('load', onLoaded);
}
/******************************************************************************/
})();
/******************************************************************************/

View File

@ -0,0 +1,45 @@
var whenSizeChanges = function(elm, callback) {
var reset = function() {
k.style.width = grow.offsetWidth + 10 + "px";
k.style.height = grow.offsetHeight + 10 + "px";
grow.scrollLeft = grow.scrollWidth;
grow.scrollTop = grow.scrollHeight;
shrink.scrollLeft = shrink.scrollWidth;
shrink.scrollTop = shrink.scrollHeight;
w = elm.offsetWidth;
h = elm.offsetHeight;
}
var aux = document.createElement("div");
aux.style.cssText = "position:absolute;left:0;top:0;right:0;bottom:0;overflow:scroll;z-index:-1;visibility:hidden";
aux.innerHTML = '<div style="position:absolute;left:0;top:0;right:0;bottom:0;overflow:scroll;z-index:-1;visibility:hidden">\
<div style="position:absolute;left:0;top:0;"></div>\
</div>\
<div style="position:absolute;left:0;top:0;right:0;bottom:0;overflow:scroll;z-index:-1;visibility:hidden">\
<div style="position:absolute;left:0;top:0;width:200%;height:200%"></div>\
</div>';
elm.appendChild(aux);
var grow = aux.childNodes[0],
k = grow.childNodes[0],
shrink = aux.childNodes[1];
var w, h;
reset();
grow.addEventListener("scroll", function() {
(elm.offsetWidth > w || elm.offsetHeight > h) && callback();
reset();
});
shrink.addEventListener("scroll", function() {
(elm.offsetWidth < w || elm.offsetHeight < h) && callback();
reset();
});
};
var onLoaded = function() {
var body = document.body, popover = safari.self;
var updateSize = function() {
popover.width = body.offsetWidth;
popover.height = body.offsetHeight;
};
updateSize();
body.style.position = "relative"; // Necessary for size change detection
whenSizeChanges(body, updateSize);
};
window.addEventListener('load', onLoaded);