| 1 |
// Copyright (c) Jupyter Development Team. |
| 2 |
// Distributed under the terms of the Modified BSD License. |
| 3 |
/*----------------------------------------------------------------------------- |
| 4 |
| Copyright (c) 2014-2017, PhosphorJS Contributors |
| 5 |
| |
| 6 |
| Distributed under the terms of the BSD 3-Clause License. |
| 7 |
| |
| 8 |
| The full license is in the file LICENSE, distributed with this software. |
| 9 |
|----------------------------------------------------------------------------*/ |
| 10 |
|
| 11 |
/** |
| 12 |
* The namespace for element related utilities. |
| 13 |
*/ |
| 14 |
export namespace ElementExt { |
| 15 |
/** |
| 16 |
* An object which holds the border and padding data for an element. |
| 17 |
*/ |
| 18 |
export interface IBoxSizing { |
| 19 |
/** |
| 20 |
* The top border width, in pixels. |
| 21 |
*/ |
| 22 |
borderTop: number; |
| 23 |
|
| 24 |
/** |
| 25 |
* The left border width, in pixels. |
| 26 |
*/ |
| 27 |
borderLeft: number; |
| 28 |
|
| 29 |
/** |
| 30 |
* The right border width, in pixels. |
| 31 |
*/ |
| 32 |
borderRight: number; |
| 33 |
|
| 34 |
/** |
| 35 |
* The bottom border width, in pixels. |
| 36 |
*/ |
| 37 |
borderBottom: number; |
| 38 |
|
| 39 |
/** |
| 40 |
* The top padding width, in pixels. |
| 41 |
*/ |
| 42 |
paddingTop: number; |
| 43 |
|
| 44 |
/** |
| 45 |
* The left padding width, in pixels. |
| 46 |
*/ |
| 47 |
paddingLeft: number; |
| 48 |
|
| 49 |
/** |
| 50 |
* The right padding width, in pixels. |
| 51 |
*/ |
| 52 |
paddingRight: number; |
| 53 |
|
| 54 |
/** |
| 55 |
* The bottom padding width, in pixels. |
| 56 |
*/ |
| 57 |
paddingBottom: number; |
| 58 |
|
| 59 |
/** |
| 60 |
* The sum of horizontal border and padding. |
| 61 |
*/ |
| 62 |
horizontalSum: number; |
| 63 |
|
| 64 |
/** |
| 65 |
* The sum of vertical border and padding. |
| 66 |
*/ |
| 67 |
verticalSum: number; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Compute the box sizing for an element. |
| 72 |
* |
| 73 |
* @param element - The element of interest. |
| 74 |
* |
| 75 |
* @returns The box sizing data for the specified element. |
| 76 |
*/ |
| 77 |
export function boxSizing(element: Element): IBoxSizing { |
| 78 |
let style = window.getComputedStyle(element); |
| 79 |
let bt = parseFloat(style.borderTopWidth!) || 0; |
| 80 |
let bl = parseFloat(style.borderLeftWidth!) || 0; |
| 81 |
let br = parseFloat(style.borderRightWidth!) || 0; |
| 82 |
let bb = parseFloat(style.borderBottomWidth!) || 0; |
| 83 |
let pt = parseFloat(style.paddingTop!) || 0; |
| 84 |
let pl = parseFloat(style.paddingLeft!) || 0; |
| 85 |
let pr = parseFloat(style.paddingRight!) || 0; |
| 86 |
let pb = parseFloat(style.paddingBottom!) || 0; |
| 87 |
let hs = bl + pl + pr + br; |
| 88 |
let vs = bt + pt + pb + bb; |
| 89 |
return { |
| 90 |
borderTop: bt, |
| 91 |
borderLeft: bl, |
| 92 |
borderRight: br, |
| 93 |
borderBottom: bb, |
| 94 |
paddingTop: pt, |
| 95 |
paddingLeft: pl, |
| 96 |
paddingRight: pr, |
| 97 |
paddingBottom: pb, |
| 98 |
horizontalSum: hs, |
| 99 |
verticalSum: vs |
| 100 |
}; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* An object which holds the min and max size data for an element. |
| 105 |
*/ |
| 106 |
export interface ISizeLimits { |
| 107 |
/** |
| 108 |
* The minimum width, in pixels. |
| 109 |
*/ |
| 110 |
minWidth: number; |
| 111 |
|
| 112 |
/** |
| 113 |
* The minimum height, in pixels. |
| 114 |
*/ |
| 115 |
minHeight: number; |
| 116 |
|
| 117 |
/** |
| 118 |
* The maximum width, in pixels. |
| 119 |
*/ |
| 120 |
maxWidth: number; |
| 121 |
|
| 122 |
/** |
| 123 |
* The maximum height, in pixels. |
| 124 |
*/ |
| 125 |
maxHeight: number; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Compute the size limits for an element. |
| 130 |
* |
| 131 |
* @param element - The element of interest. |
| 132 |
* |
| 133 |
* @returns The size limit data for the specified element. |
| 134 |
*/ |
| 135 |
export function sizeLimits(element: Element): ISizeLimits { |
| 136 |
let style = window.getComputedStyle(element); |
| 137 |
let minWidth = parseFloat(style.minWidth!) || 0; |
| 138 |
let minHeight = parseFloat(style.minHeight!) || 0; |
| 139 |
let maxWidth = parseFloat(style.maxWidth!) || Infinity; |
| 140 |
let maxHeight = parseFloat(style.maxHeight!) || Infinity; |
| 141 |
maxWidth = Math.max(minWidth, maxWidth); |
| 142 |
maxHeight = Math.max(minHeight, maxHeight); |
| 143 |
return { minWidth, minHeight, maxWidth, maxHeight }; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Test whether a client position lies within an element. |
| 148 |
* |
| 149 |
* @param element - The DOM element of interest. |
| 150 |
* |
| 151 |
* @param clientX - The client X coordinate of interest. |
| 152 |
* |
| 153 |
* @param clientY - The client Y coordinate of interest. |
| 154 |
* |
| 155 |
* @returns Whether the point is within the given element. |
| 156 |
*/ |
| 157 |
export function hitTest( |
| 158 |
element: Element, |
| 159 |
clientX: number, |
| 160 |
clientY: number |
| 161 |
): boolean { |
| 162 |
let rect = element.getBoundingClientRect(); |
| 163 |
return ( |
| 164 |
clientX >= rect.left && |
| 165 |
clientX < rect.right && |
| 166 |
clientY >= rect.top && |
| 167 |
clientY < rect.bottom |
| 168 |
); |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Vertically scroll an element into view if needed. |
| 173 |
* |
| 174 |
* @param area - The scroll area element. |
| 175 |
* |
| 176 |
* @param element - The element of interest. |
| 177 |
* |
| 178 |
* #### Notes |
| 179 |
* This follows the "nearest" behavior of the native `scrollIntoView` |
| 180 |
* method, which is not supported by all browsers. |
| 181 |
* https://drafts.csswg.org/cssom-view/#element-scrolling-members |
| 182 |
* |
| 183 |
* If the element fully covers the visible area or is fully contained |
| 184 |
* within the visible area, no scrolling will take place. Otherwise, |
| 185 |
* the nearest edges of the area and element are aligned. |
| 186 |
*/ |
| 187 |
export function scrollIntoViewIfNeeded( |
| 188 |
area: Element, |
| 189 |
element: Element |
| 190 |
): void { |
| 191 |
let ar = area.getBoundingClientRect(); |
| 192 |
let er = element.getBoundingClientRect(); |
| 193 |
if (er.top <= ar.top && er.bottom >= ar.bottom) { |
| 194 |
return; |
| 195 |
} |
| 196 |
if (er.top < ar.top && er.height <= ar.height) { |
| 197 |
area.scrollTop -= ar.top - er.top; |
| 198 |
return; |
| 199 |
} |
| 200 |
if (er.bottom > ar.bottom && er.height >= ar.height) { |
| 201 |
area.scrollTop -= ar.top - er.top; |
| 202 |
return; |
| 203 |
} |
| 204 |
if (er.top < ar.top && er.height > ar.height) { |
| 205 |
area.scrollTop -= ar.bottom - er.bottom; |
| 206 |
return; |
| 207 |
} |
| 208 |
if (er.bottom > ar.bottom && er.height < ar.height) { |
| 209 |
area.scrollTop -= ar.bottom - er.bottom; |
| 210 |
return; |
| 211 |
} |
| 212 |
} |
| 213 |
} |