-
Notifications
You must be signed in to change notification settings - Fork 11
/
did-intersect.js
188 lines (147 loc) · 4.31 KB
/
did-intersect.js
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import Modifier from 'ember-modifier';
import { registerDestructor } from '@ember/destroyable';
import { inject as service } from '@ember/service';
import { tracked } from '@glimmer/tracking';
import { assert } from '@ember/debug';
export const DEFAULT_OBSERVER_OPTIONS = {};
function cleanup(instance) {
instance.unobserve.call(instance);
}
export default class DidIntersectModifier extends Modifier {
@service('ember-scroll-modifiers@observer-manager') observerManager;
onEnter;
onExit;
// A flag that determines if we should use intersection observer or use no-op, e.g IE11, no polyfill.
_isObservable = 'IntersectionObserver' in window;
// Observer options need to be specified, so the intersection observer admin service can use it as a key to unobserve, in case there is multiple observers on the same element. By default, we're using the same options that intersection observer uses.
_options = DEFAULT_OBSERVER_OPTIONS;
// Used to track number of enter and exit intersections that have happened.
@tracked
_numOfEnters = 0;
@tracked
_numOfExits = 0;
@tracked
_maxEnterIntersections;
@tracked
_maxExitIntersections;
// Track if we have already initialized `addEnterCallback` or `addExitCallback`
_hasSetupEnterCallback = false;
_hasSetupExitCallback = false;
// the element
element;
constructor(owner, args) {
super(owner, args);
registerDestructor(this, cleanup);
}
get _isExceedingMaxEnters() {
if (!Number.isInteger(this._maxEnterIntersections)) {
return false;
}
return this._numOfEnters > this._maxEnterIntersections;
}
get _isExceedingMaxExits() {
if (!Number.isInteger(this._maxExitIntersections)) {
return false;
}
return this._numOfExits > this._maxExitIntersections;
}
observe() {
if (!this._isObservable) {
return;
}
this.observerManager.observe(this.element, this._options);
}
unobserve() {
if (!this._isObservable) {
return;
}
this.observerManager.unobserve(this.element, this._options);
}
modify(element, positional, named) {
this.element = element;
if (!this._isObservable) {
return;
}
let {
onEnter,
onExit,
maxEnter,
maxExit,
options,
isObserving = true,
} = named;
assert('onEnter or/and onExit is required', onEnter || onExit);
if (onEnter) {
assert('onEnter must be a function', typeof onEnter === 'function');
this.onEnter = onEnter;
}
if (onExit) {
assert('onExit must be a function', typeof onExit === 'function');
this.onExit = onExit;
}
// We should technically accept 0, in case of programmatic changes
if (maxEnter || maxEnter === 0) {
assert('maxEnter must be an integer', Number.isInteger(maxEnter));
this._maxEnterIntersections = maxEnter;
}
// We should accept 0 here too.
if (maxExit || maxExit === 0) {
assert('maxExit must be an integer', Number.isInteger(maxExit));
this._maxExitIntersections = maxExit;
}
if (
!this._hasSetupEnterCallback &&
this.onEnter &&
!this._isExceedingMaxEnters
) {
this.observerManager.addEnterCallback(this.element, (...args) => {
if (this.isDestroying) {
return;
}
this._numOfEnters++;
if (
this._isExceedingMaxEnters &&
(!this.onExit || this._isExceedingMaxExits)
) {
this.unobserve();
return;
}
if (!this._isExceedingMaxEnters) {
this.onEnter(...args);
}
});
this._hasSetupEnterCallback = true;
}
if (
!this._hasSetupExitCallback &&
this.onExit &&
!this._isExceedingMaxExits
) {
this.observerManager.addExitCallback(this.element, (...args) => {
if (this.isDestroying) {
return;
}
this._numOfExits++;
if (
(!this.onEnter || this._isExceedingMaxEnters) &&
this._isExceedingMaxExits
) {
this.unobserve();
return;
}
if (!this._isExceedingMaxExits) {
this.onExit(...args);
}
});
this._hasSetupExitCallback = true;
}
if (options) {
this._options = options;
}
if (isObserving) {
this.observe();
} else {
this.unobserve();
}
}
}