1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.getTargetRect = getTargetRect;
|
7 | exports.getFixedTop = getFixedTop;
|
8 | exports.getFixedBottom = getFixedBottom;
|
9 | exports.getObserverEntities = getObserverEntities;
|
10 | exports.addObserveTarget = addObserveTarget;
|
11 | exports.removeObserveTarget = removeObserveTarget;
|
12 |
|
13 | var _addEventListener = _interopRequireDefault(require("../vc-util/Dom/addEventListener"));
|
14 |
|
15 | var _supportsPassive = _interopRequireDefault(require("../_util/supportsPassive"));
|
16 |
|
17 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
18 |
|
19 | function getTargetRect(target) {
|
20 | return target !== window ? target.getBoundingClientRect() : {
|
21 | top: 0,
|
22 | bottom: window.innerHeight
|
23 | };
|
24 | }
|
25 |
|
26 | function getFixedTop(placeholderReact, targetRect, offsetTop) {
|
27 | if (offsetTop !== undefined && targetRect.top > placeholderReact.top - offsetTop) {
|
28 | return "".concat(offsetTop + targetRect.top, "px");
|
29 | }
|
30 |
|
31 | return undefined;
|
32 | }
|
33 |
|
34 | function getFixedBottom(placeholderReact, targetRect, offsetBottom) {
|
35 | if (offsetBottom !== undefined && targetRect.bottom < placeholderReact.bottom + offsetBottom) {
|
36 | var targetBottomOffset = window.innerHeight - targetRect.bottom;
|
37 | return "".concat(offsetBottom + targetBottomOffset, "px");
|
38 | }
|
39 |
|
40 | return undefined;
|
41 | }
|
42 |
|
43 |
|
44 | var TRIGGER_EVENTS = ['resize', 'scroll', 'touchstart', 'touchmove', 'touchend', 'pageshow', 'load'];
|
45 | var observerEntities = [];
|
46 |
|
47 | function getObserverEntities() {
|
48 |
|
49 | return observerEntities;
|
50 | }
|
51 |
|
52 | function addObserveTarget(target, affix) {
|
53 | if (!target) return;
|
54 | var entity = observerEntities.find(function (item) {
|
55 | return item.target === target;
|
56 | });
|
57 |
|
58 | if (entity) {
|
59 | entity.affixList.push(affix);
|
60 | } else {
|
61 | entity = {
|
62 | target: target,
|
63 | affixList: [affix],
|
64 | eventHandlers: {}
|
65 | };
|
66 | observerEntities.push(entity);
|
67 |
|
68 | TRIGGER_EVENTS.forEach(function (eventName) {
|
69 | entity.eventHandlers[eventName] = (0, _addEventListener.default)(target, eventName, function () {
|
70 | entity.affixList.forEach(function (targetAffix) {
|
71 | var lazyUpdatePosition = targetAffix.exposed.lazyUpdatePosition;
|
72 | lazyUpdatePosition();
|
73 | }, (eventName === 'touchstart' || eventName === 'touchmove') && _supportsPassive.default ? {
|
74 | passive: true
|
75 | } : false);
|
76 | });
|
77 | });
|
78 | }
|
79 | }
|
80 |
|
81 | function removeObserveTarget(affix) {
|
82 | var observerEntity = observerEntities.find(function (oriObserverEntity) {
|
83 | var hasAffix = oriObserverEntity.affixList.some(function (item) {
|
84 | return item === affix;
|
85 | });
|
86 |
|
87 | if (hasAffix) {
|
88 | oriObserverEntity.affixList = oriObserverEntity.affixList.filter(function (item) {
|
89 | return item !== affix;
|
90 | });
|
91 | }
|
92 |
|
93 | return hasAffix;
|
94 | });
|
95 |
|
96 | if (observerEntity && observerEntity.affixList.length === 0) {
|
97 | observerEntities = observerEntities.filter(function (item) {
|
98 | return item !== observerEntity;
|
99 | });
|
100 |
|
101 | TRIGGER_EVENTS.forEach(function (eventName) {
|
102 | var handler = observerEntity.eventHandlers[eventName];
|
103 |
|
104 | if (handler && handler.remove) {
|
105 | handler.remove();
|
106 | }
|
107 | });
|
108 | }
|
109 | } |
\ | No newline at end of file |