utils.js
2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import addEventListener from "rc-util/es/Dom/addEventListener";
export function getTargetRect(target) {
return target !== window ? target.getBoundingClientRect() : {
top: 0,
bottom: window.innerHeight
};
}
export function getFixedTop(placeholderReact, targetRect, offsetTop) {
if (offsetTop !== undefined && targetRect.top > placeholderReact.top - offsetTop) {
return offsetTop + targetRect.top;
}
return undefined;
}
export function getFixedBottom(placeholderReact, targetRect, offsetBottom) {
if (offsetBottom !== undefined && targetRect.bottom < placeholderReact.bottom + offsetBottom) {
var targetBottomOffset = window.innerHeight - targetRect.bottom;
return offsetBottom + targetBottomOffset;
}
return undefined;
} // ======================== Observer ========================
var TRIGGER_EVENTS = ['resize', 'scroll', 'touchstart', 'touchmove', 'touchend', 'pageshow', 'load'];
var observerEntities = [];
export function getObserverEntities() {
// Only used in test env. Can be removed if refactor.
return observerEntities;
}
export function addObserveTarget(target, affix) {
if (!target) return;
var entity = observerEntities.find(function (item) {
return item.target === target;
});
if (entity) {
entity.affixList.push(affix);
} else {
entity = {
target: target,
affixList: [affix],
eventHandlers: {}
};
observerEntities.push(entity); // Add listener
TRIGGER_EVENTS.forEach(function (eventName) {
entity.eventHandlers[eventName] = addEventListener(target, eventName, function () {
entity.affixList.forEach(function (targetAffix) {
targetAffix.lazyUpdatePosition();
});
});
});
}
}
export function removeObserveTarget(affix) {
var observerEntity = observerEntities.find(function (oriObserverEntity) {
var hasAffix = oriObserverEntity.affixList.some(function (item) {
return item === affix;
});
if (hasAffix) {
oriObserverEntity.affixList = oriObserverEntity.affixList.filter(function (item) {
return item !== affix;
});
}
return hasAffix;
});
if (observerEntity && observerEntity.affixList.length === 0) {
observerEntities = observerEntities.filter(function (item) {
return item !== observerEntity;
}); // Remove listener
TRIGGER_EVENTS.forEach(function (eventName) {
var handler = observerEntity.eventHandlers[eventName];
if (handler && handler.remove) {
handler.remove();
}
});
}
}