2020-03-07 11:45:13 +00:00
|
|
|
import addEventListener from '../vc-util/Dom/addEventListener';
|
2020-10-12 07:45:52 +00:00
|
|
|
import { ComponentPublicInstance } from 'vue';
|
2020-12-10 09:40:46 +00:00
|
|
|
import supportsPassive from '../_util/supportsPassive';
|
2020-03-07 11:45:13 +00:00
|
|
|
|
2020-10-12 07:45:52 +00:00
|
|
|
export type BindElement = HTMLElement | Window | null | undefined;
|
|
|
|
export type Rect = ClientRect | DOMRect;
|
|
|
|
|
|
|
|
export function getTargetRect(target: BindElement): ClientRect {
|
2020-03-07 11:45:13 +00:00
|
|
|
return target !== window
|
2020-10-12 07:45:52 +00:00
|
|
|
? (target as HTMLElement).getBoundingClientRect()
|
|
|
|
: ({ top: 0, bottom: window.innerHeight } as ClientRect);
|
2020-03-07 11:45:13 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 07:45:52 +00:00
|
|
|
export function getFixedTop(
|
|
|
|
placeholderReact: Rect,
|
|
|
|
targetRect: Rect,
|
|
|
|
offsetTop: number | undefined,
|
|
|
|
) {
|
2020-03-07 11:45:13 +00:00
|
|
|
if (offsetTop !== undefined && targetRect.top > placeholderReact.top - offsetTop) {
|
2020-10-12 07:45:52 +00:00
|
|
|
return `${offsetTop + targetRect.top}px`;
|
2020-03-07 11:45:13 +00:00
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2020-10-12 07:45:52 +00:00
|
|
|
export function getFixedBottom(
|
|
|
|
placeholderReact: Rect,
|
|
|
|
targetRect: Rect,
|
|
|
|
offsetBottom: number | undefined,
|
|
|
|
) {
|
2020-03-07 11:45:13 +00:00
|
|
|
if (offsetBottom !== undefined && targetRect.bottom < placeholderReact.bottom + offsetBottom) {
|
|
|
|
const targetBottomOffset = window.innerHeight - targetRect.bottom;
|
2020-10-12 07:45:52 +00:00
|
|
|
return `${offsetBottom + targetBottomOffset}px`;
|
2020-03-07 11:45:13 +00:00
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ======================== Observer ========================
|
|
|
|
const TRIGGER_EVENTS = [
|
|
|
|
'resize',
|
|
|
|
'scroll',
|
|
|
|
'touchstart',
|
|
|
|
'touchmove',
|
|
|
|
'touchend',
|
|
|
|
'pageshow',
|
|
|
|
'load',
|
|
|
|
];
|
|
|
|
|
2020-10-12 07:45:52 +00:00
|
|
|
interface ObserverEntity {
|
|
|
|
target: HTMLElement | Window;
|
|
|
|
affixList: ComponentPublicInstance<any>[];
|
|
|
|
eventHandlers: { [eventName: string]: any };
|
|
|
|
}
|
|
|
|
|
|
|
|
let observerEntities: ObserverEntity[] = [];
|
2020-03-07 11:45:13 +00:00
|
|
|
|
|
|
|
export function getObserverEntities() {
|
|
|
|
// Only used in test env. Can be removed if refactor.
|
|
|
|
return observerEntities;
|
|
|
|
}
|
|
|
|
|
2020-10-12 07:45:52 +00:00
|
|
|
export function addObserveTarget(
|
|
|
|
target: HTMLElement | Window | null,
|
|
|
|
affix: ComponentPublicInstance<any>,
|
|
|
|
): void {
|
2020-03-07 11:45:13 +00:00
|
|
|
if (!target) return;
|
|
|
|
|
2020-10-12 07:45:52 +00:00
|
|
|
let entity: ObserverEntity | undefined = observerEntities.find(item => item.target === target);
|
2020-03-07 11:45:13 +00:00
|
|
|
|
|
|
|
if (entity) {
|
|
|
|
entity.affixList.push(affix);
|
|
|
|
} else {
|
|
|
|
entity = {
|
|
|
|
target,
|
|
|
|
affixList: [affix],
|
|
|
|
eventHandlers: {},
|
|
|
|
};
|
|
|
|
observerEntities.push(entity);
|
|
|
|
|
|
|
|
// Add listener
|
|
|
|
TRIGGER_EVENTS.forEach(eventName => {
|
2020-10-12 07:45:52 +00:00
|
|
|
entity!.eventHandlers[eventName] = addEventListener(target, eventName, () => {
|
2020-12-10 09:40:46 +00:00
|
|
|
entity!.affixList.forEach(
|
|
|
|
targetAffix => {
|
|
|
|
(targetAffix as any).lazyUpdatePosition();
|
|
|
|
},
|
|
|
|
(eventName === 'touchstart' || eventName === 'touchmove') && supportsPassive
|
|
|
|
? ({ passive: true } as EventListenerOptions)
|
|
|
|
: false,
|
|
|
|
);
|
2020-03-07 11:45:13 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-12 07:45:52 +00:00
|
|
|
export function removeObserveTarget(affix: ComponentPublicInstance<any>): void {
|
2020-03-07 11:45:13 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|