import {iAllRefs, iPlainPopperConfig, iPlainPopperCustomConfig, iPlainPopperDefaultConfig, iPublicHookParam, logError, plainAttrs} from "../utils/type.base"; import {processScrollHandler} from "./processScrollHandler"; import {getBoundary} from "../utils/getBoundary"; import {createPopperDebounceHandler} from "./createPopperDebounceHandler"; import {createPopperHooks} from "./createPopperHooks"; import {processInitialize} from "./processInitialize"; import {processSizeEqual} from "./processSizeEqual"; import {refreshPopper} from "./refreshPopper"; import {decodePlacement} from "../utils/decodePlacement"; export function createPlainPopperCreator(defaultConfig: iPlainPopperDefaultConfig) { return (customConfig: iPlainPopperCustomConfig) => { /*配置信息*/ const config: iPlainPopperConfig = { show: true, ...defaultConfig, ...customConfig }; /*dom对象引用*/ const refs: iAllRefs = { popper: config.popper, reference: config.reference, body: config.popper.querySelector(`[${plainAttrs.body}]`) as HTMLDivElement, content: config.popper.querySelector(`[${plainAttrs.content}]`) as HTMLDivElement, arrow: config.popper.querySelector(`[${plainAttrs.arrow}]`) as HTMLDivElement | null, boundary: getBoundary(config.boundary), }; /*内部钩子*/ const hooks = createPopperHooks(); /*检查必须的dom对象*/ if (!refs.popper) {throw logError('popper element is empty!');} if (!refs.reference) {throw logError('reference element is empty!');} if (!refs.body) {throw logError('body element is empty!');} const methods = { /** * 销毁实例 * @author 韦胜健 * @date 2022/4/3 20:04 */ destroy: () => {hooks.onDestroy.exec({ isVertical: decodePlacement(config.placement).isVertical, ...publicHookParams });}, /** * 刷新位置信息 * @author 韦胜健 * @date 2022/4/3 20:13 * @param transformFlag 是否使用transform来定位 */ refresh: (() => { const innerRefresh = (transformFlag = false) => {refreshPopper({ refs, config, hooks, publicHookParams, transformFlag });}; /** * 防抖刷新位置信息,平时用transform定位,最后一次用fixed定位 * @author 韦胜健 * @date 2022/4/4 20:56 */ const debounceRefresh = createPopperDebounceHandler({ normal: () => {innerRefresh(true);}, last: () => {innerRefresh(false);}, duration: 100, }); return (debounce = true) => {debounce ? debounceRefresh() : innerRefresh(false);}; })(), /** * 设置是否显示 * @author 韦胜健 * @date 2022/4/6 9:54 */ show: (flag = true, emit = true) => { if (flag) { const onBeforeShowResult = hooks.onBeforeShow.exec(undefined); if (onBeforeShowResult === false) { return; } } else { const onBeforeCloseResult = hooks.onBeforeClose.exec(undefined); if (onBeforeCloseResult === false) { return; } } /*开启或者关闭的瞬间,移除moving属性启用动画*/ refs.popper.removeAttribute(plainAttrs.moving); config.show = flag; refs.popper.setAttribute(plainAttrs.show, String(config.show)); if (emit) { hooks.onShowChange.exec(config.show); } }, /** * 切换显示 * @author 韦胜健 * @date 2022/4/6 9:55 */ toggle: () => {methods.show(!config.show);}, }; const publicHookParams: iPublicHookParam = { refs, config, refresh: methods.refresh }; processInitialize({ hooks, refs, config, show: methods.show, toggle: methods.toggle }); processSizeEqual({ hooks }); processScrollHandler({ reference: refs.reference, onParentScroll: () => methods.refresh(), onWindowResize: () => methods.refresh(), hooks }); hooks.onRefresh.use(({ direction, align }) => { refs.popper.setAttribute(plainAttrs.direction, direction); refs.popper.setAttribute(plainAttrs.align, align); }); hooks.onInitialize.exec(publicHookParams); methods.show(config.show); return { config, hooks, ...methods, }; }; } export const createPlainPopper = createPlainPopperCreator({ boundary: null, placement: 'bottom-start', arrowSize: 10, offset: 10, arrowScale: 0.5, backgroundColor: 'white', shadowColor: '#c8c8c8', color: 'black', autoSwitchPlacement: true, }); export type iPlainPopper = ReturnType>