-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
VueDisqus.vue
115 lines (105 loc) · 2.42 KB
/
VueDisqus.vue
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
<template>
<div id="disqus_thread" />
</template>
<script>
export default {
name: 'VueDisqus',
props: {
shortname: {
type: String,
required: true
},
identifier: {
type: String,
required: false
},
url: {
type: String,
required: false
},
title: {
type: String,
required: false
},
remote_auth_s3: {
type: String,
required: false
},
api_key: {
type: String,
required: false
},
sso_config: {
type: Object,
required: false
},
language: {
type: String,
required: false
}
},
mounted () {
if (window.DISQUS) return this.reset()
this.init()
},
methods: {
reset (dsq = window.DISQUS) {
const setBaseConfig = this.setBaseConfig
dsq.reset({
reload: true,
config: function () {
setBaseConfig(this)
}
})
},
init () {
const setBaseConfig = this.setBaseConfig
window.disqus_config = function () {
setBaseConfig(this)
}
this.makeEmbedScript()
if (this.$route) this.watchUrl()
},
watchUrl () {
this.$watch('$route.path', () => this.reset())
},
makeEmbedScript () {
setTimeout(() => {
const d = document
const s = d.createElement('script')
s.setAttribute('id', 'embed-disqus')
s.setAttribute('data-timestamp', +new Date())
s.type = 'text/javascript'
s.async = true
s.src = `//${this.shortname}.disqus.com/embed.js`
;(d.head || d.body).appendChild(s)
}, 0)
},
setBaseConfig (disqusConfig) {
disqusConfig.page.identifier = (this.identifier || this.$route.path || window.location.pathname)
disqusConfig.page.url = (this.url || this.$el.baseURI)
if (this.title) {
disqusConfig.page.title = this.title
}
if (this.remote_auth_s3) {
disqusConfig.page.remote_auth_s3 = this.remote_auth_s3
}
if (this.api_key) {
disqusConfig.page.api_key = this.api_key
}
if (this.sso_config) {
disqusConfig.sso = this.sso_config
}
if (this.language) {
disqusConfig.language = this.language
}
disqusConfig.callbacks.onReady = [() => {
this.$emit('ready')
}]
disqusConfig.callbacks.onNewComment = [(comment) => {
this.$emit('new-comment', comment)
}]
}
}
}
</script>