-
Notifications
You must be signed in to change notification settings - Fork 1
/
module.js
132 lines (117 loc) · 4.7 KB
/
module.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
export default function StayDown(opts) {
opts = opts || {};
this.target = opts.target;
this.interval = opts.interval | 1000;
this.max = opts.max || 0;
this.callback = opts.callback;
this.userScroll = true;
this.spinner = opts.spinner;
this.spin_img = new Image();
this.stickyHeight = opts.stickyHeight || 10;
if (this.spinner) {
this.spin_img.src = this.spinner;
}
var staydown = this;
this.intend_down = true;
this.emit('lock');
var wheelevent = "wheel";
if (document.onmousewheel !== undefined) {
wheelevent = 'mousewheel';
}
window.addEventListener('resize', function (event) {
staydown.emit('windowresize');
staydown.checkdown();
});
this.target.addEventListener('scroll', function (event) {
if (staydown.userScroll) {
if (staydown.intend_down && !staydown.isdown()) {
staydown.intend_down = false;
staydown.emit('release');
} else if (!staydown.intend_down && staydown.isdown()) {
staydown.intend_down = true;
staydown.emit('lock');
}
}
staydown.userScroll = true;
});
if (window.MutationObserver) {
//private function for getting images recursively from dom
//mutation observer for whenever the overflow element changes
this.mo = new MutationObserver(function (mutations) {
var mut, idx, nidx, imgs, img, iidx, ilen, parent, spin;
staydown.userScroll = false;
//something changed, check scroll
staydown.checkdown();
//check to see if image was added, and add onload check
for (idx = 0; idx < mutations.length; idx++) {
mut = mutations[idx];
for (nidx = 0; nidx < mut.addedNodes.length; nidx++) {
// Check if we appended a node type that isn't
// an element that we can search for images inside.
if (!mut.addedNodes[nidx].getElementsByTagName) {
continue;
}
imgs = mut.addedNodes[nidx].getElementsByTagName('img');
for (iidx = 0, ilen = imgs.length; iidx < ilen; iidx++) {
img = imgs[iidx];
if (!img.complete) {
parent = img.parentNode;
if (staydown.spinner) {
spin = staydown.spin_img.cloneNode();
parent.replaceChild(spin, img);
}
var onImageLoad = function (event) {
if (spin) {
//image loads later, and isn't a mutation
parent.replaceChild(img, spin);
}
staydown.emit('imageload');
staydown.checkdown();
event.target.removeEventListener('load', onImageLoad);
};
img.addEventListener('load', onImageLoad);
}
}
}
}
});
this.mo.observe(this.target, {attributes: true, childList: true, characterData: true, subtree: true});
} else {
var checkdown = function () {
staydown.checkdown();
window.setTimeout(checkdown, staydown.interval);
};
checkdown();
}
}
(function () {
this.isdown = function () {
var position = this.target.scrollHeight - this.target.scrollTop - this.target.clientHeight;
return position < this.stickyHeight;
};
this.append = function (newel) {
this.emit('append');
this.target.appendChild(newel);
if (this.intend_down) {
this.target.scrollTop = this.target.scrollHeight;
this.emit('scrolldown');
}
while (this.max !== 0 && this.target.children.length > this.max) {
this.target.removeChild(this.target.children[0]);
this.emit('removechild');
}
};
this.emit = function (type, msg) {
if (typeof this.callback === 'function') {
this.callback(type, msg);
}
};
this.checkdown = function () {
var position = this.target.scrollHeight - this.target.scrollTop - this.target.clientHeight;
if (this.intend_down || position < this.stickyHeight) {
this.target.scrollTop = this.target.scrollHeight;
this.userScroll = false;
this.emit('scrolldown');
}
};
}).call(StayDown.prototype);