OSDN Git Service

Regular updates
[twpd/master.git] / _parcel / behaviors / disqus.js
1 import onmount from 'onmount'
2 import injectDisqus from '../helpers/inject_disqus'
3
4 /**
5  * Delay disqus by some time. It's at the bottom of the page, there's no need
6  * for it to load fast. This will give more time to load more critical assets.
7  */
8
9 const DISQUS_DELAY = 100
10
11 /**
12  * Injects Disqus onto the page.
13  */
14
15 onmount('[data-js-disqus]', function () {
16   const data = JSON.parse(this.getAttribute('data-js-disqus'))
17   const $parent = this.parentNode
18   $parent.setAttribute('hidden', true)
19
20   window.disqus_config = function () {
21     this.page.url = data.url
22     this.page.identifier = data.identifier
23   }
24
25   // Disqus takes a while to load, don't do it so eagerly.
26   window.addEventListener('load', () => {
27     setTimeout(() => {
28       injectDisqus(data.host)
29       $parent.removeAttribute('hidden')
30     }, DISQUS_DELAY)
31   })
32 })