mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-24 15:46:10 +08:00
76 lines
2.0 KiB
TypeScript
76 lines
2.0 KiB
TypeScript
|
import addEventListener from 'rc-util/lib/Dom/addEventListener';
|
||
|
import Affix from './';
|
||
|
|
||
|
// ======================== Observer ========================
|
||
|
const TRIGGER_EVENTS = [
|
||
|
'resize',
|
||
|
'scroll',
|
||
|
'touchstart',
|
||
|
'touchmove',
|
||
|
'touchend',
|
||
|
'pageshow',
|
||
|
'load',
|
||
|
];
|
||
|
|
||
|
interface ObserverEntity {
|
||
|
target: HTMLElement | Window;
|
||
|
affixList: Affix[];
|
||
|
eventHandlers: { [eventName: string]: any };
|
||
|
}
|
||
|
|
||
|
let observerEntities: ObserverEntity[] = [];
|
||
|
|
||
|
export function addObserveTarget(target: HTMLElement | Window | null, affix: Affix): void {
|
||
|
if (!target) return;
|
||
|
|
||
|
let entity: ObserverEntity | undefined = observerEntities.find(item => item.target === target);
|
||
|
|
||
|
if (entity) {
|
||
|
entity.affixList.push(affix);
|
||
|
} else {
|
||
|
entity = {
|
||
|
target,
|
||
|
affixList: [affix],
|
||
|
eventHandlers: {},
|
||
|
};
|
||
|
observerEntities.push(entity);
|
||
|
|
||
|
// Add listener
|
||
|
TRIGGER_EVENTS.forEach(eventName => {
|
||
|
entity!.eventHandlers[eventName] = addEventListener(target, eventName, (event: Event) => {
|
||
|
entity!.affixList.forEach(affix => {
|
||
|
affix.updatePosition(event);
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export function removeObserveTarget(affix: Affix): void {
|
||
|
const observerEntity = observerEntities.find(oriObserverEntity => {
|
||
|
const hasAffix = oriObserverEntity.affixList.some(item => item === affix);
|
||
|
if (hasAffix) {
|
||
|
oriObserverEntity.affixList = oriObserverEntity.affixList.filter(item => item !== affix);
|
||
|
}
|
||
|
return hasAffix;
|
||
|
});
|
||
|
|
||
|
if (observerEntity && observerEntity.affixList.length === 0) {
|
||
|
observerEntities = observerEntities.filter(item => item !== observerEntity);
|
||
|
|
||
|
// Remove listener
|
||
|
TRIGGER_EVENTS.forEach(eventName => {
|
||
|
const handler = observerEntity.eventHandlers[eventName];
|
||
|
if (handler && handler.remove) {
|
||
|
handler.remove();
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export function getTargetRect(target: HTMLElement | Window | null): ClientRect {
|
||
|
return target !== window
|
||
|
? (target as HTMLElement).getBoundingClientRect()
|
||
|
: ({ top: 0, bottom: window.innerHeight } as ClientRect);
|
||
|
}
|