From c3c953363b178ca2b0ee270b9c081858658693f6 Mon Sep 17 00:00:00 2001 From: Andrew Dupont Date: Mon, 15 Dec 2008 20:40:15 -0600 Subject: [PATCH] Optimize document.viewport.get(Dimensions|Width|Height). [#336 state:resolved] --- CHANGELOG | 2 ++ src/dom/dom.js | 54 +++++++++++++++++++++++++++++--------------------- 2 files changed, 33 insertions(+), 23 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 4e35e60..00da43c 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,5 @@ +* Optimize document.viewport.get(Dimensions|Width|Height). (Nick Stackenburg, Andrew Dupont) + * Fix issue where Object#isString and Object#isNumber return false for String and Number "wrapper" objects. (atrepp, Samuel Lebeau, Andrew Dupont) * Set document.loaded = true before firing dom:loaded custom event. (Andrew Dupont) diff --git a/src/dom/dom.js b/src/dom/dom.js index 18e0ffc..fd5a186 100644 --- a/src/dom/dom.js +++ b/src/dom/dom.js @@ -1175,37 +1175,45 @@ Element.addMethods = function(methods) { document.viewport = { getDimensions: function() { - var dimensions = { }, B = Prototype.Browser; - $w('width height').each(function(d) { - var D = d.capitalize(); - if (B.WebKit && !document.evaluate) { - // Safari <3.0 needs self.innerWidth/Height - dimensions[d] = self['inner' + D]; - } else if (B.Opera && parseFloat(window.opera.version()) < 9.5) { - // Opera <9.5 needs document.body.clientWidth/Height - dimensions[d] = document.body['client' + D] - } else { - dimensions[d] = document.documentElement['client' + D]; - } - }); - return dimensions; + return { width: this.getWidth(), height: this.getHeight() }; }, - getWidth: function() { - return this.getDimensions().width; - }, - - getHeight: function() { - return this.getDimensions().height; - }, - getScrollOffsets: function() { return Element._returnOffset( window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft, - window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop); + window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop); } }; +(function(viewport) { + var B = Prototype.Browser, doc = document, element, property = {}; + + function getRootElement() { + // Older versions of Safari. + if (B.WebKit && !doc.evaluate) + return document; + + // Older versions of Opera. + if (B.Opera && window.parseFloat(window.opera.version()) < 9.5) + return document.body; + + return document.documentElement; + } + + function define(D) { + if (!element) element = getRootElement(); + + property[D] = 'client' + D; + + viewport['get' + D] = function() { return element[property[D]] }; + return viewport['get' + D](); + } + + viewport.getWidth = define.curry('Width'); + viewport.getHeight = define.curry('Height'); +})(document.viewport); + + Element.Storage = { UID: 1 };