-
Notifications
You must be signed in to change notification settings - Fork 8
/
webchat.js
71 lines (57 loc) · 1.97 KB
/
webchat.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
function Webchat () {
this.container = document.getElementById('app-web-chat')
this.configureWidget()
this.setupElements()
this.addButtonListener()
this.addStatusChangeListener()
}
Webchat.prototype.configureWidget = function () {
window.zESettings = {
webWidget: {
chat: {
suppress: true
}
}
}
}
Webchat.prototype.setupElements = function () {
this.enabledContainer = document.createElement('span')
this.enabledContainer.className = 'moj-hidden'
this.defaultContainer = document.querySelector('.app-web-chat__unavailable')
this.button = document.createElement('a')
this.button.href = '#'
this.button.className = 'govuk-link govuk-footer__link app-web-chat__button'
this.button.innerHTML = 'Speak to an advisor now (opens in new window)'
this.enabledContainer.append(this.button)
this.disabledContainer = document.createElement('span')
this.disabledContainer.className = 'app-web-chat__offline moj-hidden'
this.disabledContainer.innerHTML = 'Available Monday to Friday, 10am to midday (except public holidays).'
this.container.append(this.enabledContainer)
this.container.append(this.disabledContainer)
}
Webchat.prototype.addButtonListener = function () {
this.button.addEventListener('click', function (e) {
e.preventDefault()
zE('webWidget', 'popout')
}, false)
}
Webchat.prototype.addStatusChangeListener = function () {
zE('webWidget:on', 'chat:status', function (status) {
this.defaultContainer.classList.add('moj-hidden')
if (status === 'online') {
this.enableChat()
} else {
this.disableChat()
}
}.bind(this))
}
Webchat.prototype.enableChat = function () {
this.disabledContainer.classList.add('moj-hidden')
this.enabledContainer.classList.remove('moj-hidden')
}
Webchat.prototype.disableChat = function () {
this.disabledContainer.classList.remove('moj-hidden')
this.enabledContainer.classList.add('moj-hidden')
}
const webchat = () => new Webchat()
export default webchat