UNPKG

4.63 kBJavaScriptView Raw
1"use strict";
2/*-----------------------------------------------------------------------------
3| Copyright (c) 2014-2017, PhosphorJS Contributors
4|
5| Distributed under the terms of the BSD 3-Clause License.
6|
7| The full license is in the file LICENSE, distributed with this software.
8|----------------------------------------------------------------------------*/
9Object.defineProperty(exports, "__esModule", { value: true });
10/**
11 * The namespace for element related utilities.
12 */
13var ElementExt;
14(function (ElementExt) {
15 /**
16 * Compute the box sizing for an element.
17 *
18 * @param element - The element of interest.
19 *
20 * @returns The box sizing data for the specified element.
21 */
22 function boxSizing(element) {
23 var style = window.getComputedStyle(element);
24 var bt = parseFloat(style.borderTopWidth) || 0;
25 var bl = parseFloat(style.borderLeftWidth) || 0;
26 var br = parseFloat(style.borderRightWidth) || 0;
27 var bb = parseFloat(style.borderBottomWidth) || 0;
28 var pt = parseFloat(style.paddingTop) || 0;
29 var pl = parseFloat(style.paddingLeft) || 0;
30 var pr = parseFloat(style.paddingRight) || 0;
31 var pb = parseFloat(style.paddingBottom) || 0;
32 var hs = bl + pl + pr + br;
33 var vs = bt + pt + pb + bb;
34 return {
35 borderTop: bt,
36 borderLeft: bl,
37 borderRight: br,
38 borderBottom: bb,
39 paddingTop: pt,
40 paddingLeft: pl,
41 paddingRight: pr,
42 paddingBottom: pb,
43 horizontalSum: hs,
44 verticalSum: vs
45 };
46 }
47 ElementExt.boxSizing = boxSizing;
48 /**
49 * Compute the size limits for an element.
50 *
51 * @param element - The element of interest.
52 *
53 * @returns The size limit data for the specified element.
54 */
55 function sizeLimits(element) {
56 var style = window.getComputedStyle(element);
57 var minWidth = parseFloat(style.minWidth) || 0;
58 var minHeight = parseFloat(style.minHeight) || 0;
59 var maxWidth = parseFloat(style.maxWidth) || Infinity;
60 var maxHeight = parseFloat(style.maxHeight) || Infinity;
61 maxWidth = Math.max(minWidth, maxWidth);
62 maxHeight = Math.max(minHeight, maxHeight);
63 return { minWidth: minWidth, minHeight: minHeight, maxWidth: maxWidth, maxHeight: maxHeight };
64 }
65 ElementExt.sizeLimits = sizeLimits;
66 /**
67 * Test whether a client position lies within an element.
68 *
69 * @param element - The DOM element of interest.
70 *
71 * @param clientX - The client X coordinate of interest.
72 *
73 * @param clientY - The client Y coordinate of interest.
74 *
75 * @returns Whether the point is within the given element.
76 */
77 function hitTest(element, clientX, clientY) {
78 var rect = element.getBoundingClientRect();
79 return (clientX >= rect.left &&
80 clientX < rect.right &&
81 clientY >= rect.top &&
82 clientY < rect.bottom);
83 }
84 ElementExt.hitTest = hitTest;
85 /**
86 * Vertically scroll an element into view if needed.
87 *
88 * @param area - The scroll area element.
89 *
90 * @param element - The element of interest.
91 *
92 * #### Notes
93 * This follows the "nearest" behavior of the native `scrollIntoView`
94 * method, which is not supported by all browsers.
95 * https://drafts.csswg.org/cssom-view/#element-scrolling-members
96 *
97 * If the element fully covers the visible area or is fully contained
98 * within the visible area, no scrolling will take place. Otherwise,
99 * the nearest edges of the area and element are aligned.
100 */
101 function scrollIntoViewIfNeeded(area, element) {
102 var ar = area.getBoundingClientRect();
103 var er = element.getBoundingClientRect();
104 if (er.top <= ar.top && er.bottom >= ar.bottom) {
105 return;
106 }
107 if (er.top < ar.top && er.height <= ar.height) {
108 area.scrollTop -= ar.top - er.top;
109 return;
110 }
111 if (er.bottom > ar.bottom && er.height >= ar.height) {
112 area.scrollTop -= ar.top - er.top;
113 return;
114 }
115 if (er.top < ar.top && er.height > ar.height) {
116 area.scrollTop -= ar.bottom - er.bottom;
117 return;
118 }
119 if (er.bottom > ar.bottom && er.height < ar.height) {
120 area.scrollTop -= ar.bottom - er.bottom;
121 return;
122 }
123 }
124 ElementExt.scrollIntoViewIfNeeded = scrollIntoViewIfNeeded;
125})(ElementExt = exports.ElementExt || (exports.ElementExt = {}));