1 | /*
|
---|
2 | * file: Viewport.js
|
---|
3 | *
|
---|
4 | * @BEGINLICENSE
|
---|
5 | * Copyright 2010 Brook Novak (email : [email protected])
|
---|
6 | * This program is free software; you can redistribute it and/or modify
|
---|
7 | * it under the terms of the GNU General Public License as published by
|
---|
8 | * the Free Software Foundation; either version 2 of the License, or
|
---|
9 | * (at your option) any later version.
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | * You should have received a copy of the GNU General Public License
|
---|
15 | * along with this program; if not, write to the Free Software
|
---|
16 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
17 | * @ENDLICENSE
|
---|
18 | */
|
---|
19 |
|
---|
20 | bootstrap.provides("viewport");
|
---|
21 |
|
---|
22 | // Internal declarations
|
---|
23 | var _getViewportSize, _getScrollbarThickness, _getBodyOffset, _getDocumentScrollPos;
|
---|
24 |
|
---|
25 | (function(){
|
---|
26 |
|
---|
27 | var
|
---|
28 |
|
---|
29 | recalcViewportPending = 1,
|
---|
30 | cachedVPWidth,
|
---|
31 | cachedVPHeight;
|
---|
32 |
|
---|
33 | $enqueueInit("viewport", function(){
|
---|
34 |
|
---|
35 | // On resize invalidate scrollbars and body offset measurements
|
---|
36 | _addHandler(window, "resize", function(){
|
---|
37 | recalcViewportPending = 1;
|
---|
38 | });
|
---|
39 |
|
---|
40 | });
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * @param {Boolean} forceReCalc True to force a relcalculation of the viewport dimension.
|
---|
44 | * Otherwised it may return a cached version. Only need to set to true
|
---|
45 | * if calling within a onresize event (since cached values are re-evaluated
|
---|
46 | * on resize events).
|
---|
47 | *
|
---|
48 | * @return {Object} The size of the documents viewport - excluding scrollbars. {width, height}
|
---|
49 | */
|
---|
50 | de.getViewPortSize = _getViewPortSize = function(forceRecalc){
|
---|
51 |
|
---|
52 | if (recalcViewportPending || forceRecalc)
|
---|
53 | reCalcViewport();
|
---|
54 |
|
---|
55 | return {
|
---|
56 | width: cachedVPWidth,
|
---|
57 | height: cachedVPHeight
|
---|
58 | };
|
---|
59 |
|
---|
60 | };
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Recalculates the viewport dimensions
|
---|
64 | */
|
---|
65 | function reCalcViewport() {
|
---|
66 |
|
---|
67 | if (_browser == _Platform.IE && _browserVersion < 7) {
|
---|
68 | // IE 6 and below does not support fixed positioned elements and has troubles with
|
---|
69 | // 100% heights .. can just query doc element client area
|
---|
70 | cachedVPWidth = document.documentElement.clientWidth;
|
---|
71 | cachedVPHeight = document.documentElement.clientHeight; // TODO: For all IE's?
|
---|
72 |
|
---|
73 | } else {
|
---|
74 |
|
---|
75 | var measureDiv = $createElement("div");
|
---|
76 |
|
---|
77 | _setFullStyle(measureDiv, "position:fixed;top:0;left:0;width:100%;height:100%;border-style:none;margin:0");
|
---|
78 |
|
---|
79 | docBody.appendChild(measureDiv);
|
---|
80 |
|
---|
81 | cachedVPWidth = measureDiv.offsetWidth;
|
---|
82 | cachedVPHeight = measureDiv.offsetHeight;
|
---|
83 |
|
---|
84 | docBody.removeChild(measureDiv);
|
---|
85 | }
|
---|
86 |
|
---|
87 | recalcViewportPending = 0;
|
---|
88 |
|
---|
89 | }
|
---|
90 |
|
---|
91 | })();
|
---|
92 |
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * @return {Object} The current scroll position of the document {top, left}.
|
---|
96 | */
|
---|
97 | de.getDocumentScrollPos = _getDocumentScrollPos = function(){
|
---|
98 |
|
---|
99 | var left = 0, top = 0;
|
---|
100 |
|
---|
101 | // DOM Compliant?
|
---|
102 | if (docBody.scrollLeft || docBody.scrollTop) {
|
---|
103 |
|
---|
104 | left = docBody.scrollLeft;
|
---|
105 | top = docBody.scrollTop;
|
---|
106 |
|
---|
107 | } else if (window.pageYOffset || window.pageXOffset) {
|
---|
108 |
|
---|
109 | left = window.pageXOffset;
|
---|
110 | top = window.pageYOffset;
|
---|
111 |
|
---|
112 | } else if (document.documentElement.scrollLeft || document.documentElement.scrollTop) {
|
---|
113 |
|
---|
114 | left = document.documentElement.scrollLeft;
|
---|
115 | top = document.documentElement.scrollTop;
|
---|
116 |
|
---|
117 | }
|
---|
118 |
|
---|
119 | return {
|
---|
120 | top: top,
|
---|
121 | left: left
|
---|
122 | };
|
---|
123 | }
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * Note: this does not include the browser toolbars/status bars etc.. just the document viewing area...
|
---|
127 | * @return {Object} the size of the documents window - i.e. including scrollbars. {width, height}
|
---|
128 | */
|
---|
129 | function _getWindowSize(){
|
---|
130 |
|
---|
131 | var width = 0, height = 0;
|
---|
132 |
|
---|
133 | if (window.innerWidth || window.innerHeight) { // Most Browsers
|
---|
134 |
|
---|
135 | width = window.innerWidth;
|
---|
136 | height = window.innerHeight;
|
---|
137 |
|
---|
138 | } else if (document.documentElement.offsetWidth || document.documentElement.offsetWidth) { // IE
|
---|
139 |
|
---|
140 | width = document.documentElement.offsetWidth;
|
---|
141 | height = document.documentElement.offsetHeight;
|
---|
142 |
|
---|
143 | } else if (docBody.offsetWidth || docBody.offsetWidth) {
|
---|
144 |
|
---|
145 | width = docBody.offsetWidth;
|
---|
146 | height = docBody.offsetWidth;
|
---|
147 |
|
---|
148 | }
|
---|
149 |
|
---|
150 | return {
|
---|
151 | width: width,
|
---|
152 | height: height
|
---|
153 | };
|
---|
154 | };
|
---|
155 |
|
---|
156 |
|
---|