import {iElementRect, iPlainPopperConfig, tDirection} from "./type.base"; import {handleDirection} from "./handleDirection"; /** * 获取合适的direction,自动调整方向 * @author 韦胜健 * @date 2022/4/4 17:05 */ export function getAvailableDirection({ direction, rects: { body, reference, boundary }, config }: { direction: tDirection, rects: { body: iElementRect, reference: iElementRect, boundary: null | iElementRect }, config: iPlainPopperConfig },): tDirection { if (!boundary || !!config.autoSwitchPlacement) {return direction;} const args = { body, reference, boundary, config }; return handleDirection(direction, { top: () => { if (isTopAvailable(args)) { return 'top'; } else { return isBottomAvailable(args) ? 'bottom' : 'top'; } }, bottom: () => { if (isBottomAvailable(args)) { return 'bottom'; } else { return isTopAvailable(args) ? 'top' : 'bottom'; } }, left: () => { if (isLeftAvailable(args)) { return 'left'; } else { return isRightAvailable(args) ? 'right' : 'left'; } }, right: () => { if (isRightAvailable(args)) { return 'right'; } else { return isLeftAvailable(args) ? 'left' : 'right'; } }, }); } /** * 是否可以放在上边 * @author 韦胜健 * @date 2022/4/4 17:48 */ function isTopAvailable({ body, reference, boundary, config }: { body: iElementRect, reference: iElementRect, boundary: iElementRect, config: iPlainPopperConfig }): boolean { return (reference.top - body.height - config.offset) > boundary.top; } /** * 是否可以放在下边 * @author 韦胜健 * @date 2022/4/4 17:49 */ function isBottomAvailable({ body, reference, boundary, config }: { body: iElementRect, reference: iElementRect, boundary: iElementRect, config: iPlainPopperConfig }): boolean { return (reference.top + reference.height + body.height + config.offset) < boundary.top + boundary.height; } /** * 是否可以放在左边 * @author 韦胜健 * @date 2022/4/4 17:49 */ function isLeftAvailable({ body, reference, boundary, config }: { body: iElementRect, reference: iElementRect, boundary: iElementRect, config: iPlainPopperConfig }): boolean { return (reference.left - body.width - config.offset) > boundary.left; } /** * 是否可以放在右边 * @author 韦胜健 * @date 2022/4/4 17:50 */ function isRightAvailable({ body, reference, boundary, config }: { body: iElementRect, reference: iElementRect, boundary: iElementRect, config: iPlainPopperConfig }): boolean { return (reference.left + reference.width + body.width + config.offset) < boundary.left + boundary.width; }