-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (47 loc) · 1.33 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
var h = require('hyperscript')
module.exports = function (content, onMore) {
var bottom, top
function onMore (ev) {
if(ev.target == bottom) {
bottom.disabled = true
bottom.textContent = 'loading...'
content.dispatchEvent(new CustomEvent(
'readymore', {target: content, detail: {bottom: true}}
))
}
else if(ev.target == top) {
top.disabled = true
top.textContent = 'loading...'
content.dispatchEvent(new CustomEvent(
'readymore', {target: content, detail: {top: true}}
))
}
}
function text (count) {
return 'load more' + (count ? ' ('+count+')' : '')
}
function createButton (text, onClick) {
return h('button', {onclick: onClick}, text)
}
content.addEventListener('hasmore', function (ev) {
if(ev.detail.bottom) {
if(bottom) {
bottom.textContent = text(ev.detail.count)
bottom.disabled = false
}
else
scroller.appendChild(bottom = createButton(text(ev.count), onMore))
}
else if(ev.detail.top) {
if(top) {
top.textContent = text(ev.detail.count)
top.disabled = false
}
else {
scroller.insertBefore(top = createButton(text(ev.count), onMore), content)
}
}
})
var scroller = h('div.hyperloadmore__scroller', content)
return scroller
}