-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfinitescroll.js
132 lines (106 loc) · 3.16 KB
/
infinitescroll.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
define([
'jquery',
'log'
], function($, log) {
'use strict';
var $window = $(window),
$document = $(document),
scrollCache = {},
registry = {};
function callIt(fn) { fn(); }
// Check if the user's viewport has scrolled based a defined breakpoint
function scrolled($context) {
var elementHeight, scrollBottom;
if ($context.is($window)) {
elementHeight = (window.innerHeight || $window.height());
scrollBottom = $document.height() - elementHeight - $window.scrollTop();
}
else {
elementHeight = $context.prop('clientHeight');
scrollBottom = $context.prop('scrollHeight') - elementHeight - $context.scrollTop();
}
return (scrollBottom / elementHeight);
}
function scroll(context) {
var $context = context === 'window' ? $window : $(context);
return function() {
var breakpoint, s = scrolled($context);
for (breakpoint in registry[context]) {
if (s <= Number(breakpoint)) {
registry[context][breakpoint].wrapped.forEach(callIt);
}
}
};
}
function infinitescroll(breakpoint, callback, context) {
if (typeof breakpoint === "function") {
context = callback;
callback = breakpoint;
breakpoint = 1;
}
context = context || 'window';
breakpoint = Number(breakpoint).toString();
var cb, $context = context === 'window' ? $window : $(context);
if (!registry[context]) {
registry[context] = {};
scrollCache[context] = scroll(context);
$context.on('scroll', scrollCache[context]);
}
cb = registry[context][breakpoint];
if (!cb) {
cb = registry[context][breakpoint] = {
wrapped: [],
original: []
};
}
function onHit() {
if (onHit.blocking) { return; }
onHit.blocking = true;
var retval = callback.apply(null, arguments);
if (retval && typeof retval.then === 'function') {
retval.then(function() {
onHit.blocking = false;
scrollCache[context]();
}, function(err) {
if (err instanceof Error) {
log.warn(err);
}
});
}
else {
onHit.blocking = false;
}
}
// store both the original and wrapped so it can be removed
cb.original.push(callback);
cb.wrapped.push(onHit);
// First check
scrollCache[context]();
}
infinitescroll.remove = function(fn, context) {
context = context || 'window';
var breakpoint, cb, i,
$context = context === 'window' ? $window : $(context);
for (breakpoint in registry[context]) {
cb = registry[context][breakpoint];
// search for the original function
i = cb.original.indexOf(fn);
if (~i) {
cb.original.splice(i, 1);
cb.wrapped.splice(i, 1);
if (!cb.original.length) {
delete registry[context][breakpoint];
}
}
}
if (!Object.keys(registry[context]).length) {
$context.off('scroll', scrollCache[context]);
delete registry[context];
}
};
infinitescroll.check = function(context) {
context = context || 'window';
scrollCache[context]();
};
return infinitescroll;
});