-
Notifications
You must be signed in to change notification settings - Fork 2
/
chatbox.js
103 lines (79 loc) · 2.72 KB
/
chatbox.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
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
const textarea = document.querySelector('.chatbox-message-input')
const chatboxForm = document.querySelector('.chatbox-message-form')
textarea.addEventListener('input', function () {
let line = textarea.value.split('\n').length
if(textarea.rows < 6 || line < 6) {
textarea.rows = line
}
if(textarea.rows > 1) {
chatboxForm.style.alignItems = 'flex-end'
} else {
chatboxForm.style.alignItems = 'center'
}
})
const chatboxToggle = document.querySelector('.chatbox-toggle')
const chatboxMessage = document.querySelector('.chatbox-message-wrapper')
chatboxToggle.addEventListener('click', function () {
chatboxMessage.classList.toggle('show')
})
const dropdownToggle = document.querySelector('.chatbox-message-dropdown-toggle')
const dropdownMenu = document.querySelector('.chatbox-message-dropdown-menu')
dropdownToggle.addEventListener('click', function () {
dropdownMenu.classList.toggle('show')
})
document.addEventListener('click', function (e) {
if(!e.target.matches('.chatbox-message-dropdown, .chatbox-message-dropdown *')) {
dropdownMenu.classList.remove('show')
}
})
const chatboxMessageWrapper = document.querySelector('.chatbox-message-content')
const chatboxNoMessage = document.querySelector('.chatbox-message-no-message')
chatboxForm.addEventListener('submit', function (e) {
e.preventDefault()
if(isValid(textarea.value)) {
writeMessage()
setTimeout(autoReply, 1000)
}
})
function addZero(num) {
return num < 10 ? '0'+num : num
}
function writeMessage() {
const today = new Date()
let message = `
<div class="chatbox-message-item sent">
<span class="chatbox-message-item-text">
${textarea.value.trim().replace(/\n/g, '<br>\n')}
</span>
<span class="chatbox-message-item-time">${addZero(today.getHours())}:${addZero(today.getMinutes())}</span>
</div>
`
chatboxMessageWrapper.insertAdjacentHTML('beforeend', message)
chatboxForm.style.alignItems = 'center'
textarea.rows = 1
textarea.focus()
textarea.value = ''
chatboxNoMessage.style.display = 'none'
scrollBottom()
}
function autoReply() {
const today = new Date()
let message = `
<div class="chatbox-message-item received">
<span class="chatbox-message-item-text">
Thank you for your awesome support!
</span>
<span class="chatbox-message-item-time">${addZero(today.getHours())}:${addZero(today.getMinutes())}</span>
</div>
`
chatboxMessageWrapper.insertAdjacentHTML('beforeend', message)
scrollBottom()
}
function scrollBottom() {
chatboxMessageWrapper.scrollTo(0, chatboxMessageWrapper.scrollHeight)
}
function isValid(value) {
let text = value.replace(/\n/g, '')
text = text.replace(/\s/g, '')
return text.length > 0
}