|
| 1 | +import { select } from './dom'; |
| 2 | +import { IPosition } from './draggable'; |
| 3 | +import { isNullOrUndefined } from './util'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Position coordinates |
| 7 | + */ |
| 8 | +export interface PositionCoordinates { |
| 9 | + left?: number; |
| 10 | + top?: number; |
| 11 | + bottom?: number; |
| 12 | + right?: number; |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * Coordinates for element position |
| 17 | + * |
| 18 | + * @private |
| 19 | + */ |
| 20 | +export interface Coordinates { |
| 21 | + /** |
| 22 | + * Defines the x Coordinate of page. |
| 23 | + */ |
| 24 | + pageX?: number; |
| 25 | + /** |
| 26 | + * Defines the y Coordinate of page. |
| 27 | + */ |
| 28 | + pageY?: number; |
| 29 | + /** |
| 30 | + * Defines the x Coordinate of client. |
| 31 | + */ |
| 32 | + clientX?: number; |
| 33 | + /** |
| 34 | + * Defines the y Coordinate of client. |
| 35 | + */ |
| 36 | + clientY?: number; |
| 37 | +} |
| 38 | + |
| 39 | +let left: number; |
| 40 | +let top: number; |
| 41 | +let width: number; |
| 42 | +let height: number; |
| 43 | + |
| 44 | +/** |
| 45 | + * Sets the permitted drag area boundaries based on the defined dragArea. |
| 46 | + * |
| 47 | + * @private |
| 48 | + * @param {HTMLElement | string} dragArea - The element or selector string defining the drag area |
| 49 | + * @param {Element} helperElement - The helper element used in dragging |
| 50 | + * @param {PositionCoordinates} borderWidth - The border width of the drag area |
| 51 | + * @param {PositionCoordinates} padding - The padding of the drag area |
| 52 | + * @param {PositionCoordinates} dragLimit - The object to store the calculated drag limits |
| 53 | + * @returns {void} |
| 54 | + */ |
| 55 | +export function setDragArea( |
| 56 | + dragArea: HTMLElement | string, helperElement: Element, |
| 57 | + borderWidth: PositionCoordinates, padding: PositionCoordinates, |
| 58 | + dragLimit: PositionCoordinates |
| 59 | +): void { |
| 60 | + let eleWidthBound: number; |
| 61 | + let eleHeightBound: number; |
| 62 | + let top: number = 0; |
| 63 | + let left: number = 0; |
| 64 | + let ele: HTMLElement; |
| 65 | + const type: string = typeof dragArea; |
| 66 | + if (type === 'string') { |
| 67 | + ele = select(dragArea as string) as HTMLElement; |
| 68 | + } else { |
| 69 | + ele = dragArea as HTMLElement; |
| 70 | + } |
| 71 | + if (ele) { |
| 72 | + const elementArea: ClientRect | DOMRect = ele.getBoundingClientRect(); |
| 73 | + eleWidthBound = ele.scrollWidth ? ele.scrollWidth : elementArea.right - elementArea.left; |
| 74 | + eleHeightBound = ele.scrollHeight ? (dragArea && !isNullOrUndefined(helperElement) && helperElement.classList.contains('sf-treeview')) ? ele.clientHeight : ele.scrollHeight : elementArea.bottom - elementArea.top; |
| 75 | + const keys: string[] = ['Top', 'Left', 'Bottom', 'Right']; |
| 76 | + const styles: CSSStyleDeclaration = getComputedStyle(ele); |
| 77 | + for (let i: number = 0; i < keys.length; i++) { |
| 78 | + const key: string = keys[parseInt(i.toString(), 10)]; |
| 79 | + const tborder: string = styles['border' + key + 'Width']; |
| 80 | + const tpadding: string = styles['padding' + key]; |
| 81 | + const lowerKey: string = key.toLowerCase(); |
| 82 | + (borderWidth as Record<string, number>)[`${lowerKey}`] = isNaN(parseFloat(tborder)) ? 0 : parseFloat(tborder); |
| 83 | + (padding as Record<string, number>)[`${lowerKey}`] = isNaN(parseFloat(tpadding)) ? 0 : parseFloat(tpadding); |
| 84 | + } |
| 85 | + if (dragArea && !isNullOrUndefined(helperElement) && helperElement.classList.contains('sf-treeview')) { |
| 86 | + top = elementArea.top + document.scrollingElement.scrollTop; |
| 87 | + } else { |
| 88 | + top = elementArea.top; |
| 89 | + } |
| 90 | + left = elementArea.left; |
| 91 | + dragLimit.left = left + borderWidth.left + padding.left; |
| 92 | + dragLimit.top = ele.offsetTop + borderWidth.top + padding.top; |
| 93 | + dragLimit.right = left + eleWidthBound - (borderWidth.right + padding.right); |
| 94 | + dragLimit.bottom = top + eleHeightBound - (borderWidth.bottom + padding.bottom); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * Retrieves the document's full height or width, considering the scroll and offset values. |
| 100 | + * |
| 101 | + * @private |
| 102 | + * @param {string} str - The dimension type ('Height' or 'Width') to calculate. |
| 103 | + * @returns {number} - The maximum value across scroll, offset, and client dimensions. |
| 104 | + */ |
| 105 | +export function getDocumentWidthHeight(str: 'Height' | 'Width'): number { |
| 106 | + const docBody: HTMLElement = document.body; |
| 107 | + const docEle: HTMLElement = document.documentElement; |
| 108 | + return Math.max( |
| 109 | + docBody['scroll' + str], docEle['scroll' + str], |
| 110 | + docBody['offset' + str], docEle['offset' + str], |
| 111 | + docEle['client' + str] |
| 112 | + ); |
| 113 | +} |
| 114 | + |
| 115 | +/** |
| 116 | + * Determines if a given element is within the bounds of the viewport. |
| 117 | + * |
| 118 | + * @private |
| 119 | + * @param {HTMLElement} el - The element to check. |
| 120 | + * @returns {boolean} - True if the element is in the viewport, false otherwise. |
| 121 | + */ |
| 122 | +export function elementInViewport(el: HTMLElement): boolean { |
| 123 | + top = el.offsetTop; |
| 124 | + left = el.offsetLeft; |
| 125 | + width = el.offsetWidth; |
| 126 | + height = el.offsetHeight; |
| 127 | + while (el.offsetParent) { |
| 128 | + el = el.offsetParent as HTMLElement; |
| 129 | + top += el.offsetTop; |
| 130 | + left += el.offsetLeft; |
| 131 | + } |
| 132 | + return ( |
| 133 | + top >= window.pageYOffset && |
| 134 | + left >= window.pageXOffset && |
| 135 | + (top + height) <= (window.pageYOffset + window.innerHeight) && |
| 136 | + (left + width) <= (window.pageXOffset + window.innerWidth) |
| 137 | + ); |
| 138 | +} |
| 139 | + |
| 140 | +/** |
| 141 | + * Gets the coordinates of a mouse or touch event. |
| 142 | + * |
| 143 | + * @private |
| 144 | + * @param {MouseEvent | TouchEvent} evt - The event object. |
| 145 | + * @returns {Coordinates} - The x and y coordinates of the page and client. |
| 146 | + */ |
| 147 | +export function getCoordinates(evt: MouseEvent & TouchEvent): Coordinates { |
| 148 | + if (evt.type.indexOf('touch') > -1) { |
| 149 | + return evt.changedTouches[0]; |
| 150 | + } |
| 151 | + return evt as Coordinates; |
| 152 | +} |
| 153 | + |
| 154 | +/** |
| 155 | + * Calculates the parent position of the element relative to the document. |
| 156 | + * |
| 157 | + * @private |
| 158 | + * @param {Element} ele - The element for which the parent position is calculated. |
| 159 | + * @returns {IPosition} - The calculated left and top position. |
| 160 | + */ |
| 161 | +export function calculateParentPosition(ele: Element): IPosition { |
| 162 | + if (isNullOrUndefined(ele)) { |
| 163 | + return { left: 0, top: 0 }; |
| 164 | + } |
| 165 | + const rect: ClientRect | DOMRect = ele.getBoundingClientRect(); |
| 166 | + const style: CSSStyleDeclaration = getComputedStyle(ele); |
| 167 | + return { |
| 168 | + left: (rect.left + window.pageXOffset) - parseInt(style.marginLeft, 10), |
| 169 | + top: (rect.top + window.pageYOffset) - parseInt(style.marginTop, 10) |
| 170 | + }; |
| 171 | +} |
| 172 | + |
| 173 | +/** |
| 174 | + * Retrieves all elements from a point defined by event coordinates. |
| 175 | + * |
| 176 | + * @private |
| 177 | + * @param {MouseEvent | TouchEvent} evt - The event object containing coordinates. |
| 178 | + * @returns {Element[]} - An array of elements located at the event's point. |
| 179 | + */ |
| 180 | +export function getPathElements(evt: MouseEvent & TouchEvent): Element[] { |
| 181 | + const elementTop: number = evt.clientX > 0 ? evt.clientX : 0; |
| 182 | + const elementLeft: number = evt.clientY > 0 ? evt.clientY : 0; |
| 183 | + return document.elementsFromPoint(elementTop, elementLeft); |
| 184 | +} |
| 185 | + |
| 186 | +/** |
| 187 | + * Identifies the scrollable parent of the current node element. |
| 188 | + * |
| 189 | + * @private |
| 190 | + * @param {Element[]} nodes - The path of elements to check. |
| 191 | + * @param {boolean} reverse - Whether to reverse the array to check from bottom to top. |
| 192 | + * @returns {Element | null} - The first scrollable parent element or null. |
| 193 | + */ |
| 194 | +export function getScrollParent(nodes: Element[], reverse: boolean): Element | null { |
| 195 | + const nodeList: Element[] = reverse ? [...nodes].reverse() : nodes; |
| 196 | + for (const node of nodeList) { |
| 197 | + const computedStyle: CSSStyleDeclaration = window.getComputedStyle(node); |
| 198 | + const overflowY: string = computedStyle.overflowY; |
| 199 | + if ((overflowY === 'auto' || overflowY === 'scroll') && |
| 200 | + node.scrollHeight > node.clientHeight) { |
| 201 | + return node; |
| 202 | + } |
| 203 | + } |
| 204 | + const scrollingElement: HTMLElement = document.scrollingElement as HTMLElement; |
| 205 | + const docOverflowY: string = window.getComputedStyle(scrollingElement).overflowY; |
| 206 | + if (docOverflowY === 'visible') { |
| 207 | + scrollingElement.style.overflow = 'auto'; |
| 208 | + return scrollingElement; |
| 209 | + } |
| 210 | + return null; |
| 211 | +} |
0 commit comments