You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
110 lines
3.0 KiB
110 lines
3.0 KiB
5 years ago
|
import addEventListener from '../vc-util/Dom/addEventListener';
|
||
4 years ago
|
import { ComponentPublicInstance } from 'vue';
|
||
5 years ago
|
|
||
4 years ago
|
export type BindElement = HTMLElement | Window | null | undefined;
|
||
|
export type Rect = ClientRect | DOMRect;
|
||
|
|
||
|
export function getTargetRect(target: BindElement): ClientRect {
|
||
5 years ago
|
return target !== window
|
||
4 years ago
|
? (target as HTMLElement).getBoundingClientRect()
|
||
|
: ({ top: 0, bottom: window.innerHeight } as ClientRect);
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
export function getFixedTop(
|
||
|
placeholderReact: Rect,
|
||
|
targetRect: Rect,
|
||
|
offsetTop: number | undefined,
|
||
|
) {
|
||
5 years ago
|
if (offsetTop !== undefined && targetRect.top > placeholderReact.top - offsetTop) {
|
||
4 years ago
|
return `${offsetTop + targetRect.top}px`;
|
||
5 years ago
|
}
|
||
|
return undefined;
|
||
|
}
|
||
|
|
||
4 years ago
|
export function getFixedBottom(
|
||
|
placeholderReact: Rect,
|
||
|
targetRect: Rect,
|
||
|
offsetBottom: number | undefined,
|
||
|
) {
|
||
5 years ago
|
if (offsetBottom !== undefined && targetRect.bottom < placeholderReact.bottom + offsetBottom) {
|
||
|
const targetBottomOffset = window.innerHeight - targetRect.bottom;
|
||
4 years ago
|
return `${offsetBottom + targetBottomOffset}px`;
|
||
5 years ago
|
}
|
||
|
return undefined;
|
||
|
}
|
||
|
|
||
|
// ======================== Observer ========================
|
||
|
const TRIGGER_EVENTS = [
|
||
|
'resize',
|
||
|
'scroll',
|
||
|
'touchstart',
|
||
|
'touchmove',
|
||
|
'touchend',
|
||
|
'pageshow',
|
||
|
'load',
|
||
|
];
|
||
|
|
||
4 years ago
|
interface ObserverEntity {
|
||
|
target: HTMLElement | Window;
|
||
|
affixList: ComponentPublicInstance<any>[];
|
||
|
eventHandlers: { [eventName: string]: any };
|
||
|
}
|
||
|
|
||
|
let observerEntities: ObserverEntity[] = [];
|
||
5 years ago
|
|
||
|
export function getObserverEntities() {
|
||
|
// Only used in test env. Can be removed if refactor.
|
||
|
return observerEntities;
|
||
|
}
|
||
|
|
||
4 years ago
|
export function addObserveTarget(
|
||
|
target: HTMLElement | Window | null,
|
||
|
affix: ComponentPublicInstance<any>,
|
||
|
): void {
|
||
5 years ago
|
if (!target) return;
|
||
|
|
||
4 years ago
|
let entity: ObserverEntity | undefined = observerEntities.find(item => item.target === target);
|
||
5 years ago
|
|
||
|
if (entity) {
|
||
|
entity.affixList.push(affix);
|
||
|
} else {
|
||
|
entity = {
|
||
|
target,
|
||
|
affixList: [affix],
|
||
|
eventHandlers: {},
|
||
|
};
|
||
|
observerEntities.push(entity);
|
||
|
|
||
|
// Add listener
|
||
|
TRIGGER_EVENTS.forEach(eventName => {
|
||
4 years ago
|
entity!.eventHandlers[eventName] = addEventListener(target, eventName, () => {
|
||
|
entity!.affixList.forEach(targetAffix => {
|
||
|
(targetAffix as any).lazyUpdatePosition();
|
||
5 years ago
|
});
|
||
|
});
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
export function removeObserveTarget(affix: ComponentPublicInstance<any>): void {
|
||
5 years ago
|
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();
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
}
|