2019-03-03 10:04:21 +08:00
|
|
|
import addEventListener from 'rc-util/lib/Dom/addEventListener';
|
|
|
|
|
2019-04-24 19:53:53 +08:00
|
|
|
export type BindElement = HTMLElement | Window | null | undefined;
|
|
|
|
|
2021-10-25 19:43:17 +08:00
|
|
|
export function getTargetRect(target: BindElement): DOMRect {
|
2019-04-24 19:53:53 +08:00
|
|
|
return target !== window
|
|
|
|
? (target as HTMLElement).getBoundingClientRect()
|
2021-10-25 19:43:17 +08:00
|
|
|
: ({ top: 0, bottom: window.innerHeight } as DOMRect);
|
2019-04-24 19:53:53 +08:00
|
|
|
}
|
|
|
|
|
2021-10-25 19:43:17 +08:00
|
|
|
export function getFixedTop(placeholderReact: DOMRect, targetRect: DOMRect, offsetTop?: number) {
|
2019-04-24 19:53:53 +08:00
|
|
|
if (offsetTop !== undefined && targetRect.top > placeholderReact.top - offsetTop) {
|
|
|
|
return offsetTop + targetRect.top;
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2019-05-07 14:57:32 +08:00
|
|
|
export function getFixedBottom(
|
2021-10-25 19:43:17 +08:00
|
|
|
placeholderReact: DOMRect,
|
|
|
|
targetRect: DOMRect,
|
|
|
|
offsetBottom?: number,
|
2019-05-07 14:57:32 +08:00
|
|
|
) {
|
|
|
|
if (offsetBottom !== undefined && targetRect.bottom < placeholderReact.bottom + offsetBottom) {
|
2019-04-24 19:53:53 +08:00
|
|
|
const targetBottomOffset = window.innerHeight - targetRect.bottom;
|
|
|
|
return offsetBottom + targetBottomOffset;
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2019-03-03 10:04:21 +08:00
|
|
|
// ======================== Observer ========================
|
|
|
|
const TRIGGER_EVENTS = [
|
|
|
|
'resize',
|
|
|
|
'scroll',
|
|
|
|
'touchstart',
|
|
|
|
'touchmove',
|
|
|
|
'touchend',
|
|
|
|
'pageshow',
|
|
|
|
'load',
|
|
|
|
];
|
|
|
|
|
|
|
|
interface ObserverEntity {
|
|
|
|
target: HTMLElement | Window;
|
2022-03-02 17:05:59 +08:00
|
|
|
affixList: any[];
|
2019-03-03 10:04:21 +08:00
|
|
|
eventHandlers: { [eventName: string]: any };
|
|
|
|
}
|
|
|
|
|
|
|
|
let observerEntities: ObserverEntity[] = [];
|
|
|
|
|
2019-04-18 01:33:05 +08:00
|
|
|
export function getObserverEntities() {
|
|
|
|
// Only used in test env. Can be removed if refactor.
|
|
|
|
return observerEntities;
|
|
|
|
}
|
|
|
|
|
2022-10-14 14:03:01 +08:00
|
|
|
export function addObserveTarget<T>(target: HTMLElement | Window | null, affix?: T): void {
|
|
|
|
if (!target) {
|
|
|
|
return;
|
|
|
|
}
|
2019-03-03 10:04:21 +08:00
|
|
|
|
2022-11-19 13:47:33 +08:00
|
|
|
let entity: ObserverEntity | undefined = observerEntities.find((item) => item.target === target);
|
2019-03-03 10:04:21 +08:00
|
|
|
|
|
|
|
if (entity) {
|
|
|
|
entity.affixList.push(affix);
|
|
|
|
} else {
|
|
|
|
entity = {
|
|
|
|
target,
|
|
|
|
affixList: [affix],
|
|
|
|
eventHandlers: {},
|
|
|
|
};
|
|
|
|
observerEntities.push(entity);
|
|
|
|
|
|
|
|
// Add listener
|
2022-11-19 13:47:33 +08:00
|
|
|
TRIGGER_EVENTS.forEach((eventName) => {
|
2019-08-05 18:38:10 +08:00
|
|
|
entity!.eventHandlers[eventName] = addEventListener(target, eventName, () => {
|
2022-11-19 13:47:33 +08:00
|
|
|
entity!.affixList.forEach((targetAffix) => {
|
2019-08-05 18:38:10 +08:00
|
|
|
targetAffix.lazyUpdatePosition();
|
2019-03-03 10:04:21 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-02 17:05:59 +08:00
|
|
|
export function removeObserveTarget<T>(affix: T): void {
|
2022-11-19 13:47:33 +08:00
|
|
|
const observerEntity = observerEntities.find((oriObserverEntity) => {
|
|
|
|
const hasAffix = oriObserverEntity.affixList.some((item) => item === affix);
|
2019-03-03 10:04:21 +08:00
|
|
|
if (hasAffix) {
|
2022-11-19 13:47:33 +08:00
|
|
|
oriObserverEntity.affixList = oriObserverEntity.affixList.filter((item) => item !== affix);
|
2019-03-03 10:04:21 +08:00
|
|
|
}
|
|
|
|
return hasAffix;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (observerEntity && observerEntity.affixList.length === 0) {
|
2022-11-19 13:47:33 +08:00
|
|
|
observerEntities = observerEntities.filter((item) => item !== observerEntity);
|
2019-03-03 10:04:21 +08:00
|
|
|
|
|
|
|
// Remove listener
|
2022-11-19 13:47:33 +08:00
|
|
|
TRIGGER_EVENTS.forEach((eventName) => {
|
2019-03-03 10:04:21 +08:00
|
|
|
const handler = observerEntity.eventHandlers[eventName];
|
|
|
|
if (handler && handler.remove) {
|
|
|
|
handler.remove();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|