Skip to content

Commit

Permalink
Optimize document.viewport.get(Dimensions|Width|Height). [#336 state:…
Browse files Browse the repository at this point in the history
…resolved]
  • Loading branch information
savetheclocktower committed Dec 16, 2008
1 parent 07506e6 commit c3c9533
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 23 deletions.
2 changes: 2 additions & 0 deletions 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)
Expand Down
54 changes: 31 additions & 23 deletions src/dom/dom.js
Expand Up @@ -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;
},

getWidth: function() {
return this.getDimensions().width;
return { width: this.getWidth(), height: this.getHeight() };
},

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
};
Expand Down

3 comments on commit c3c9533

@staaky
Copy link

@staaky staaky commented on c3c9533 Dec 16, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noticed my name is written as Stackenburg throughout the changelog, there’s no c in my last name :/

@savetheclocktower
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SO SUBMIT A BUG ALREADY

(kidding; I’ll fix it)

@staaky
Copy link

@staaky staaky commented on c3c9533 Dec 16, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

heh, thanks :)

Please sign in to comment.