Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 /* | |
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 | |
4 * http://mozilla.org/MPL/2.0/. | |
5 */ | |
6 | |
7 "use strict"; | |
8 | |
9 /** | |
10 * Element creation helper, allows defining attributes and child elements in one | |
11 * go. | |
12 * @param {Document} doc | |
13 * Document to create the element in | |
14 * @param {string} tagName | |
15 * Tag name of the new element | |
16 * @param {Object.<string, string>} [attrs] | |
17 * Attributes to set on the element | |
18 * @param {Array.<Node>} [children] | |
19 * Child nodes to add to the element | |
20 * @return {Element} | |
21 * Element that was created | |
22 */ | |
23 function createElement(doc, tagName, attrs, children) | |
24 { | |
25 let el = doc.createElement(tagName); | |
26 if (attrs) | |
27 for (let key in attrs) | |
28 el.setAttribute(key, attrs[key]); | |
29 if (children) | |
30 for (let child of children) | |
31 el.appendChild(child) | |
32 return el; | |
33 } | |
34 exports.createElement = createElement; | |
35 | |
36 /** | |
37 * Calculates the document size for a window. | |
38 * @return {Array.<number>} | |
39 * Width and height of the document loaded into the window | |
40 */ | |
41 function getWindowSize(/**Window*/ wnd) | |
42 { | |
43 return [wnd.innerWidth, wnd.document.documentElement.clientHeight]; | |
44 } | |
45 exports.getWindowSize = getWindowSize; | |
46 | |
47 /** | |
48 * Determines the parent element for a document node, if any. Will ascend into | |
49 * parent frames if necessary. | |
50 */ | |
51 function getParentElement(/**Node*/ elem) /**Element*/ | |
52 { | |
53 let result = elem.parentNode; | |
54 if (result && result.nodeType == result.DOCUMENT_NODE && result.defaultView && result.defaultView.frameElement) | |
55 result = result.defaultView.frameElement; | |
56 | |
57 if (result && result.nodeType != result.ELEMENT_NODE) | |
58 return null; | |
59 | |
60 return result; | |
61 } | |
62 exports.getParentElement = getParentElement; | |
63 | |
64 /** | |
65 * Calculates the element's position within the top frame. This will consider | |
66 * the element being clipped by frame boundaries. | |
67 * @return {Object} | |
68 * Object with properties left, top, width, height denoting the element's | |
69 * position and size within the top frame. | |
70 */ | |
71 function getElementPosition(/**Element*/ element) | |
72 { | |
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(); | |
84 let wnd = element.ownerDocument.defaultView; | |
85 | |
86 rect = {left: rect.left, top: rect.top, | |
87 right: rect.right, bottom: rect.bottom}; | |
88 while (true) | |
89 { | |
90 intersectRect(rect, wnd); | |
91 | |
92 if (!wnd.frameElement) | |
93 break; | |
94 | |
95 // Recalculate coordinates to be relative to frame's parent window | |
96 let frameElement = wnd.frameElement; | |
97 wnd = frameElement.ownerDocument.defaultView; | |
98 | |
99 let frameRect = frameElement.getBoundingClientRect(); | |
100 let frameStyle = wnd.getComputedStyle(frameElement, null); | |
101 let relLeft = frameRect.left + parseFloat(frameStyle.borderLeftWidth) + pars eFloat(frameStyle.paddingLeft); | |
102 let relTop = frameRect.top + parseFloat(frameStyle.borderTopWidth) + parseFl oat(frameStyle.paddingTop); | |
103 | |
104 rect.left += relLeft; | |
105 rect.right += relLeft; | |
106 rect.top += relTop; | |
107 rect.bottom += relTop; | |
108 } | |
109 | |
110 return rect; | |
111 } | |
112 exports.getElementPosition = getElementPosition; | |
OLD | NEW |