-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
55 lines (39 loc) · 1.64 KB
/
index.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
module.exports = Endless;
function Endless() {};
Endless.prototype.view = __dirname;
Endless.prototype.style = __dirname;
Endless.prototype.name = 'd-endless';
Endless.prototype.create = function(model, dom) {
var filterPath = model.get('filterPath');
var elementId = model.get('elementId');
if(!filterPath || !elementId) return;
this.stepSize = parseInt(model.get('stepSize')) || 10;
this.elementId = elementId;
this.segments = filterPath.split('.');
dom.on('scroll', this.endless.bind(this));
};
Endless.prototype.endless = function(e) {
var el = document.getElementById(this.elementId);
var last = el && el.lastChild.previousSibling;
if(this.model.get('updating') || !last || !this.reachedBottom(el)) return;
this.model.set('updating', true);
var filter = this.pathIndex(this.app, this.segments);
// If we're already including all results, we just abort
if(filter.ids().length < filter.limit) return this.model.set('updating', false);
// Otherwise we add to the limit and update the filter
filter.limit += this.stepSize;
filter.update();
this.model.set('updating', false);
};
Endless.prototype.reachedBottom = function(el) {
if(!el || !el.getBoundingClientRect || (!window && !document)) return false;
var rect = el.getBoundingClientRect();
return rect.bottom > 0 && rect.bottom <= (window.innerHeight || document.documentElement.clientHeight);
};
Endless.prototype.pathIndex = function(app, segments) {
var segments = segments.slice(0);
if(segments.length <= 0) return app;
if(segments[0].toLowerCase() === 'app') segments.shift();
app = app[segments.shift()];
return this.pathIndex(app, segments);
};