| Left: | ||
| Right: |
| LEFT | RIGHT |
|---|---|
| 1 /* | 1 /* |
| 2 * This Source Code is subject to the terms of the Mozilla Public License | 2 * This Source Code is subject to the terms of the Mozilla Public License |
| 3 * version 2.0 (the "License"). You can obtain a copy of the License at | 3 * version 2.0 (the "License"). You can obtain a copy of the License at |
| 4 * http://mozilla.org/MPL/2.0/. | 4 * http://mozilla.org/MPL/2.0/. |
| 5 */ | 5 */ |
| 6 | 6 |
| 7 "use strict"; | 7 "use strict"; |
| 8 | 8 |
| 9 /** | 9 /** |
| 10 * Element creation helper, allows defining attributes and child elements in one | 10 * Element creation helper, allows defining attributes and child elements in one |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 55 result = result.defaultView.frameElement; | 55 result = result.defaultView.frameElement; |
| 56 | 56 |
| 57 if (result && result.nodeType != result.ELEMENT_NODE) | 57 if (result && result.nodeType != result.ELEMENT_NODE) |
| 58 return null; | 58 return null; |
| 59 | 59 |
| 60 return result; | 60 return result; |
| 61 } | 61 } |
| 62 exports.getParentElement = getParentElement; | 62 exports.getParentElement = getParentElement; |
| 63 | 63 |
| 64 /** | 64 /** |
| 65 * Modifies a rectangle with coordinates relative to a window's client area | |
| 66 * to make sure it doesn't exceed that client area. | |
| 67 * @param {Object} rect | |
| 68 * Rectangle with properties left, top, right, bottom. | |
| 69 * @param {Window} wnd | |
| 70 * Window to restrict the rectangle to. | |
| 71 */ | |
| 72 function intersectRect(rect, wnd) | |
| 73 { | |
| 74 let [wndWidth, wndHeight] = getWindowSize(wnd); | |
| 75 rect.left = Math.max(rect.left, 0); | |
| 76 rect.top = Math.max(rect.top, 0); | |
| 77 rect.right = Math.min(rect.right, wndWidth); | |
| 78 rect.bottom = Math.min(rect.bottom, wndHeight); | |
| 79 } | |
| 80 | |
| 81 /** | |
| 65 * Calculates the element's position within the top frame. This will consider | 82 * Calculates the element's position within the top frame. This will consider |
| 66 * the element being clipped by frame boundaries. | 83 * the element being clipped by frame boundaries. |
| 67 * @return {Object} | 84 * @return {Object} |
| 68 * Object with properties left, top, width, height denoting the element's | 85 * Object with properties left, top, width, height denoting the element's |
| 69 * position and size within the top frame. | 86 * position and size within the top frame. |
| 70 */ | 87 */ |
| 71 function getElementPosition(/**Element*/ element) | 88 function getElementPosition(/**Element*/ element) |
| 72 { | 89 { |
| 73 // Restrict rectangle coordinates by the boundaries of a window's client area | |
| 74 function intersectRect(rect, wnd) | |
|
saroyanm
2016/11/23 17:44:39
Suggestion: This function will be created each tim
Wladimir Palant
2016/11/24 14:02:02
Done.
| |
| 75 { | |
| 76 let [wndWidth, wndHeight] = getWindowSize(wnd); | |
| 77 rect.left = Math.max(rect.left, 0); | |
| 78 rect.top = Math.max(rect.top, 0); | |
| 79 rect.right = Math.min(rect.right, wndWidth); | |
| 80 rect.bottom = Math.min(rect.bottom, wndHeight); | |
| 81 } | |
| 82 | |
| 83 let rect = element.getBoundingClientRect(); | 90 let rect = element.getBoundingClientRect(); |
| 84 let wnd = element.ownerDocument.defaultView; | 91 let wnd = element.ownerDocument.defaultView; |
| 85 | 92 |
| 86 rect = {left: rect.left, top: rect.top, | 93 rect = {left: rect.left, top: rect.top, |
| 87 right: rect.right, bottom: rect.bottom}; | 94 right: rect.right, bottom: rect.bottom}; |
| 88 while (true) | 95 while (true) |
| 89 { | 96 { |
| 90 intersectRect(rect, wnd); | 97 intersectRect(rect, wnd); |
| 91 | 98 |
| 92 if (!wnd.frameElement) | 99 if (!wnd.frameElement) |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 103 | 110 |
| 104 rect.left += relLeft; | 111 rect.left += relLeft; |
| 105 rect.right += relLeft; | 112 rect.right += relLeft; |
| 106 rect.top += relTop; | 113 rect.top += relTop; |
| 107 rect.bottom += relTop; | 114 rect.bottom += relTop; |
| 108 } | 115 } |
| 109 | 116 |
| 110 return rect; | 117 return rect; |
| 111 } | 118 } |
| 112 exports.getElementPosition = getElementPosition; | 119 exports.getElementPosition = getElementPosition; |
| LEFT | RIGHT |