import {iPopupRects, iPopupRefreshBuildParams, iPopupRefreshParams} from "../utils/popup.utils"; import {decodePlacement} from "../utils/decodePlacement"; import {getAvailableDirection} from "../utils/getAvailableDirection"; import {calcPosition} from "./calcPosition"; import {applyPosition} from "./applyPosition"; import {refreshArrow} from "./refreshArrow"; import {getRects} from "../utils/getRects"; import {MaxMinTopLeft} from "./MaxMinTopLeft"; /** * 刷新popup定位 * @author 韦胜健 * @date 2023/5/16 16:18 */ export function refreshPopup(params: iPopupRefreshParams) { const { option, refs, } = params; /*禁用 popperEl的动画,避免因为过度动画导致的arrow定位计算问题*/ refs.popupEl.setAttribute('data-start-refresh', ''); let { direction, align, isVertical } = decodePlacement(option.placement); if (option.sizeEqual) { if (isVertical) { refs.popupContent.style.width = option.reference.offsetWidth + 'px'; } else { refs.popupContent.style.height = option.reference.offsetHeight + 'px'; } } /*所有的元素rect信息*/ const rects: iPopupRects = getRects(refs); /*获取合适的direction*/ if (option.autoSwitchDirection) { direction = getAvailableDirection({ direction, rects, option }); } const buildParams: iPopupRefreshBuildParams = { direction, align, isVertical, rects, ...params, }; /*重置popper的padding*/ refs.popupEl.style.padding = `0 0 0 0`; refs.popupEl.setAttribute('data-direction', direction); refs.popupEl.setAttribute('data-align', align); refs.popupEl.setAttribute('data-pos-type', params.transformLocation ? 'transform' : 'fixed'); refs.popupEl.style.top = ''; refs.popupEl.style.left = ''; refs.popupEl.style.right = ''; refs.popupEl.style.bottom = ''; refs.popupEl.style.transform = ''; if (!option.cover) { const { top, left } = calcPosition(buildParams); applyPosition({ top, left, buildParams }); buildParams.rects = getRects(refs); refreshArrow(buildParams); } else { const { top: _top, left: _left, height, width } = rects.alignReference; const { top, left } = MaxMinTopLeft({ top: _top!, left: _left!, buildParams, option, }); Object.assign(refs.popupEl.style, { ...params.transformLocation ? { transform: `translate3d(${left}px,${top}px,0)` } : { top: `${top}px`, left: `${left}px`, }, height: `${height}px`, width: `${width}px`, }); } /*禁用 popperEl的动画,避免因为过度动画导致的arrow定位计算问题*/ refs.popupEl.removeAttribute('data-start-refresh'); }