import {iPopupRefreshBuildParams} from "../utils/popup.utils"; import {handleDirection} from "../utils/handleDirection"; import {MaxMinTopLeft} from "./MaxMinTopLeft"; /** * 计算popup根节点定位信息 * @author 韦胜健 * @date 2023/5/16 16:16 */ export function calcPosition( buildParams: iPopupRefreshBuildParams ): { top: number, left: number } { const { direction, align, rects, option } = buildParams; let top: number | null = null; let left: number | null = null; const handleVerticalAlign = () => { switch (align) { case 'start': left = rects.alignReference.left + option.offsetAlign; break; case 'center': left = rects.alignReference.left + rects.alignReference.width / 2 - rects.popupEl.width / 2 + option.offsetAlign; break; case 'end': left = rects.alignReference.left + rects.alignReference.width - rects.popupEl.width + option.offsetAlign; break; } }; const handleHorizontalAlign = () => { switch (align) { case 'start': top = rects.alignReference.top + option.offsetAlign; break; case 'center': top = rects.alignReference.top + rects.alignReference.height / 2 - rects.popupEl.height / 2 + option.offsetAlign; break; case 'end': top = rects.alignReference.top + rects.alignReference.height - rects.popupEl.height + option.offsetAlign; break; } }; handleDirection(direction, { top: () => { top = rects.directionReference.top - rects.popupEl.height - option.offset; handleVerticalAlign(); }, bottom: () => { top = rects.directionReference.top + rects.directionReference.height + option.offset; handleVerticalAlign(); }, left: () => { left = rects.directionReference.left - rects.popupEl.width - option.offset; handleHorizontalAlign(); }, right: () => { left = rects.directionReference.left + rects.directionReference.width + option.offset; handleHorizontalAlign(); }, }); if (!!rects.boundary) { /*一定有值,这里做类型保护,避免后面的代码写类型断言*/ if (top == null || left == null) {return { top: 0, left: 0 };} const minTop = rects.boundary.top; const maxTop = rects.boundary.top + rects.boundary.height - rects.popupEl.height; const minLeft = rects.boundary.left; const maxLeft = rects.boundary.left + rects.boundary.width - rects.popupEl.width; if (top < minTop) {top = minTop;} if (top > maxTop) {top = maxTop;} if (left < minLeft) {left = minLeft;} if (left > maxLeft) {left = maxLeft;} } return MaxMinTopLeft({ top: top!, left: left!, buildParams, option, }); }