-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathhelpers.js
More file actions
76 lines (70 loc) · 2.29 KB
/
helpers.js
File metadata and controls
76 lines (70 loc) · 2.29 KB
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
import log from './helpers-log'
const defaultLocale = 'en_us'
export function fetchFbSdk(options) {
const locale = options.locale ? options.locale : defaultLocale
return new Promise((resolve, reject) => {
;(function(d, s, id) {
const fjs = d.getElementsByTagName(s)[0]
if (d.getElementById(id)) {
return
}
const js = d.createElement(s)
js.id = id
js.src = 'https://connect.facebook.net/' + locale + '/sdk/xfbml.customerchat.js'
fjs.parentNode.insertBefore(js, fjs)
js.onload = function() {
console.log('%cVueFbCustomerChat:: loaded', log.info)
resolve()
}
js.onerror = function() {
reject()
console.error('%cVueFbCustomerChat:: NOT loaded', log.error)
}
})(document, 'script', 'facebook-jssdk')
})
}
export function initFbSdk(options) {
return new Promise(resolve => {
window.fbAsyncInit = function() {
const defaults = { cookie: true, xfbml: true, version: 'v5.0' }
options = { ...defaults, ...options }
window.FB.init(options)
resolve()
}
})
}
export function getFbSdk(options) {
return new Promise(resolve => {
if (window.FB) {
resolve(window.FB)
} else {
fetchFbSdk(options).then(() => {
initFbSdk(options).then(() => {
resolve(window.FB)
})
})
}
})
}
export function mountFbCustomerChat(options) {
const elem = document.createElement('div')
elem.setAttribute('class', 'fb-customerchat')
elem.setAttribute('attribution', 'setup_tool')
// set attributes
Object.entries(options).forEach(attr => {
elem.setAttribute(attr[0], attr[1])
})
document.body.appendChild(elem)
}
// TODO:
// // events to emit
// FB.Event.subscribe('customerchat.load', () => console.log('customerchat.load'));
// FB.Event.subscribe('customerchat.show', () => console.log('customerchat.show'));
// FB.Event.subscribe('customerchat.hide', () => console.log('customerchat.hide'));
// FB.Event.subscribe('customerchat.dialogShow', () => console.log('customerchat.dialogShow'));
// FB.Event.subscribe('customerchat.dialogHide', () => console.log('customerchat.dialogHide'));
// // triggers
// FB.CustomerChat.show(shouldShowDialog: boolean);
// FB.CustomerChat.hide();
// FB.CustomerChat.hideDialog();
// FB.CustomerChat.showDialog();