-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
52 lines (44 loc) · 1.18 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
var Nanocomponent = require('nanocomponent')
var html = require('bel')
var onIdle = require('on-idle')
var url = require('url')
var twitterWidgetsLoader = require('./loader')
class TwitterComponent extends Nanocomponent {
constructor (opts) {
super()
opts = Object.assign({
placeholder: true
}, opts)
this.opts = opts
}
createElement (tweetURL) {
this.tweetURL = tweetURL
if (this.opts.placeholder) return html`<div>loading tweet: ${tweetURL}</div>`
return html`<div></div>`
}
update (tweetURL) {
return this.tweetURL !== tweetURL
}
loadTweet (el) {
var tweetID = url.parse(this.tweetURL).pathname.split('/').pop()
if (!el) return console.warn(`cant render ${tweetID} on unmounted component`)
onIdle(function () {
window.requestAnimationFrame(function () {
twitterWidgetsLoader.load(function (twttr) {
while (el.hasChildNodes()) el.removeChild(el.lastChild)
twttr.widgets.createTweet(tweetID, el)
})
})
})
}
load (el) {
this.loadTweet(el)
}
afterupdate (el) {
this.loadTweet(el)
}
afterreorder (el) {
this.loadTweet(el)
}
}
module.exports = TwitterComponent