Debian Codegraph @

closure-library/closure/goog/style/bidi.js

raw
1 // Copyright 2012 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 /**
16 * @fileoverview Bidi utility functions.
17 *
18 */
19
20 goog.provide('goog.style.bidi');
21
22 goog.require('goog.dom');
23 goog.require('goog.style');
24 goog.require('goog.userAgent');
25 goog.require('goog.userAgent.platform');
26 goog.require('goog.userAgent.product');
27 goog.require('goog.userAgent.product.isVersion');
28
29
30 /**
31 * Returns the normalized scrollLeft position for a scrolled element.
32 * @param {Element} element The scrolled element.
33 * @return {number} The number of pixels the element is scrolled. 0 indicates
34 * that the element is not scrolled at all (which, in general, is the
35 * left-most position in ltr and the right-most position in rtl).
36 */
37 goog.style.bidi.getScrollLeft = function(element) {
38 var isRtl = goog.style.isRightToLeft(element);
39 if (isRtl && goog.style.bidi.usesNegativeScrollLeftInRtl_()) {
40 return -element.scrollLeft;
41 } else if (
42 isRtl &&
43 !(goog.userAgent.EDGE_OR_IE && goog.userAgent.isVersionOrHigher('8'))) {
44 // ScrollLeft starts at the maximum positive value and decreases towards
45 // 0 as the element is scrolled towards the left. However, for overflow
46 // visible, there is no scrollLeft and the value always stays correctly at 0
47 var overflowX = goog.style.getComputedOverflowX(element);
48 if (overflowX == 'visible') {
49 return element.scrollLeft;
50 } else {
51 return element.scrollWidth - element.clientWidth - element.scrollLeft;
52 }
53 }
54 // ScrollLeft behavior is identical in rtl and ltr, it starts at 0 and
55 // increases as the element is scrolled away from the start.
56 return element.scrollLeft;
57 };
58
59
60 /**
61 * Returns the "offsetStart" of an element, analogous to offsetLeft but
62 * normalized for right-to-left environments and various browser
63 * inconsistencies. This value returned can always be passed to setScrollOffset
64 * to scroll to an element's left edge in a left-to-right offsetParent or
65 * right edge in a right-to-left offsetParent.
66 *
67 * For example, here offsetStart is 10px in an LTR environment and 5px in RTL:
68 *
69 * <pre>
70 * | xxxxxxxxxx |
71 * ^^^^^^^^^^ ^^^^ ^^^^^
72 * 10px elem 5px
73 * </pre>
74 *
75 * If an element is positioned before the start of its offsetParent, the
76 * startOffset may be negative. This can be used with setScrollOffset to
77 * reliably scroll to an element:
78 *
79 * <pre>
80 * var scrollOffset = goog.style.bidi.getOffsetStart(element);
81 * goog.style.bidi.setScrollOffset(element.offsetParent, scrollOffset);
82 * </pre>
83 *
84 * @see setScrollOffset
85 *
86 * @param {Element} element The element for which we need to determine the
87 * offsetStart position.
88 * @return {number} The offsetStart for that element.
89 */
90 goog.style.bidi.getOffsetStart = function(element) {
91 element = /** @type {!HTMLElement} */ (element);
92 var offsetLeftForReal = element.offsetLeft;
93
94 // The element might not have an offsetParent.
95 // For example, the node might not be attached to the DOM tree,
96 // and position:fixed children do not have an offset parent.
97 // Just try to do the best we can with what we have.
98 var bestParent = element.offsetParent;
99
100 if (!bestParent && goog.style.getComputedPosition(element) == 'fixed') {
101 bestParent = goog.dom.getOwnerDocument(element).documentElement;
102 }
103
104 // Just give up in this case.
105 if (!bestParent) {
106 return offsetLeftForReal;
107 }
108
109 if (goog.userAgent.GECKO) {
110 // When calculating an element's offsetLeft, Firefox erroneously subtracts
111 // the border width from the actual distance. So we need to add it back.
112 var borderWidths = goog.style.getBorderBox(bestParent);
113 offsetLeftForReal += borderWidths.left;
114 } else if (
115 goog.userAgent.isDocumentModeOrHigher(8) &&
116 !goog.userAgent.isDocumentModeOrHigher(9)) {
117 // When calculating an element's offsetLeft, IE8/9-Standards Mode
118 // erroneously adds the border width to the actual distance. So we need to
119 // subtract it.
120 var borderWidths = goog.style.getBorderBox(bestParent);
121 offsetLeftForReal -= borderWidths.left;
122 }
123
124 if (goog.style.isRightToLeft(bestParent)) {
125 // Right edge of the element relative to the left edge of its parent.
126 var elementRightOffset = offsetLeftForReal + element.offsetWidth;
127
128 // Distance from the parent's right edge to the element's right edge.
129 return bestParent.clientWidth - elementRightOffset;
130 }
131
132 return offsetLeftForReal;
133 };
134
135
136 /**
137 * Sets the element's scrollLeft attribute so it is correctly scrolled by
138 * offsetStart pixels. This takes into account whether the element is RTL and
139 * the nuances of different browsers. To scroll to the "beginning" of an
140 * element use getOffsetStart to obtain the element's offsetStart value and then
141 * pass the value to setScrollOffset.
142 * @see getOffsetStart
143 * @param {Element} element The element to set scrollLeft on.
144 * @param {number} offsetStart The number of pixels to scroll the element.
145 * If this value is < 0, 0 is used.
146 */
147 goog.style.bidi.setScrollOffset = function(element, offsetStart) {
148 offsetStart = Math.max(offsetStart, 0);
149 // In LTR and in "mirrored" browser RTL (such as IE), we set scrollLeft to
150 // the number of pixels to scroll.
151 // Otherwise, in RTL, we need to account for different browser behavior.
152 if (!goog.style.isRightToLeft(element)) {
153 element.scrollLeft = offsetStart;
154 } else if (goog.style.bidi.usesNegativeScrollLeftInRtl_()) {
155 element.scrollLeft = -offsetStart;
156 } else if (
157 !(goog.userAgent.EDGE_OR_IE && goog.userAgent.isVersionOrHigher('8'))) {
158 // Take the current scrollLeft value and move to the right by the
159 // offsetStart to get to the left edge of the element, and then by
160 // the clientWidth of the element to get to the right edge.
161 element.scrollLeft =
162 element.scrollWidth - offsetStart - element.clientWidth;
163 } else {
164 element.scrollLeft = offsetStart;
165 }
166 };
167
168
169 /**
170 * @return {boolean} Whether the current browser returns negative scrollLeft
171 * values for RTL elements. If true, then scrollLeft starts at 0 and then
172 * becomes more negative as the element is scrolled towards the left.
173 * @private
174 */
175 goog.style.bidi.usesNegativeScrollLeftInRtl_ = function() {
176 var isSafari10Plus =
177 goog.userAgent.product.SAFARI && goog.userAgent.product.isVersion(10);
178 var isIOS10Plus = goog.userAgent.IOS && goog.userAgent.platform.isVersion(10);
179 return goog.userAgent.GECKO || isSafari10Plus || isIOS10Plus;
180 };
181
182
183 /**
184 * Sets the element's left style attribute in LTR or right style attribute in
185 * RTL. Also clears the left attribute in RTL and the right attribute in LTR.
186 * @param {Element} elem The element to position.
187 * @param {number} left The left position in LTR; will be set as right in RTL.
188 * @param {?number} top The top position. If null only the left/right is set.
189 * @param {boolean} isRtl Whether we are in RTL mode.
190 */
191 goog.style.bidi.setPosition = function(elem, left, top, isRtl) {
192 if (!goog.isNull(top)) {
193 elem.style.top = top + 'px';
194 }
195 if (isRtl) {
196 elem.style.right = left + 'px';
197 elem.style.left = '';
198 } else {
199 elem.style.left = left + 'px';
200 elem.style.right = '';
201 }
202 };