File size: 7,738 Bytes
8eb2cb0 |
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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
var sendForm = document.querySelector('#chatform'),
textInput = document.querySelector('.chatbox'),
chatList = document.querySelector('.chatlist'),
userBubble = document.querySelectorAll('.userInput'),
botBubble = document.querySelectorAll('.bot__output'),
animateBotBubble = document.querySelectorAll('.bot__input--animation'),
overview = document.querySelector('.chatbot__overview'),
hasCorrectInput,
imgLoader = false,
animationCounter = 1,
animationBubbleDelay = 600,
input,
previousInput,
isReaction = false,
unkwnCommReaction = "I didn't quite get that.",
chatbotButton = document.querySelector(".submit-button")
let printVoice = false;
let bar = document.getElementById("loadding_bar")
sendForm.onkeydown = function(e){
if(e.keyCode == 13){
e.preventDefault();
//No mix ups with upper and lowercases
var input = textInput.value.toLowerCase();
//Empty textarea fix
if(input.length > 0) {
createBubble(input)
}
}
};
sendForm.addEventListener('submit', function(e) {
//so form doesnt submit page (no page refresh)
e.preventDefault();
//No mix ups with upper and lowercases
var input = textInput.value.toLowerCase();
//Empty textarea fix
if(input.length > 0) {
createBubble(input)
}
}) //end of eventlistener
var createBubble = function(input) {
//create input bubble
var chatBubble = document.createElement('li');
chatBubble.classList.add('userInput');
//adds input of textarea to chatbubble list item
chatBubble.innerHTML = input;
//adds chatBubble to chatlist
chatList.appendChild(chatBubble)
// checkInput(input);
checkInput(input)
}
// ํ๋ก๋ฏธ์ค๋ฅผ ์ฌ์ฉํ ๋น๋๊ธฐ ํจ์
function asyncGetAnswer(input) {
return new Promise((resolve, reject) => {
let state = getAnswer(input)
resolve(state)
});
}
// ํ๋ก๋ฏธ์ค๋ฅผ ์ฌ์ฉํ ๋น๋๊ธฐ ํจ์
function asyncGetVoice(input) {
return new Promise((resolve, reject) => {
let audio_src = getVoice(input)
resolve(audio_src)
});
}
// ์คํ ํจ์ ์ ์
async function executeGetAnswer(input) {
if (!input){
return false
}
console.log("asyncGetAnswer ๋๊ธฐ ํจ์ ์คํ ์์");
// ๋๊ธฐ ํจ์ ์คํ
await asyncGetAnswer(input);
console.log("asyncGetAnswer ๋๊ธฐ ํจ์ ์คํ ์ข
๋ฃ");
}
// ์คํ ํจ์ ์ ์
async function executeGetVoice(input) {
if (!input){
return false
}
console.log("asyncGetVoice ๋๊ธฐ ํจ์ ์คํ ์์");
// ๋๊ธฐ ํจ์ ์คํ
await asyncGetVoice(input);
console.log("asyncGetVoice ๋๊ธฐ ํจ์ ์คํ ์ข
๋ฃ");
}
// ํ
์คํธ ์
๋ ฅ ํ ์ ์ก ์ ํธ์ถ๋จ
async function checkInput(input) {
isReaction = true;
await executeGetAnswer(input)
console.log("answer: ", answer);
console.log("jeju_answer: ", jeju_answer);
// ์์ฑ ์ถ๋ ฅ ์ฌ๋ถ ํ์ธ
// voice toggle button
let voice_check = document.getElementById("voice_yn");
let is_checked = voice_check.checked
console.log("is_checked:", is_checked)
if (is_checked) {
console.log("์๋ฆฌO")
await executeGetVoice(jeju_answer)
}else{
console.log("์๋ฆฌX")
}
if (answer == ""){
responseCommand("๋ค์ ์
๋ ฅํด์ฃผ์ธ์.", "๋ค์ ์
๋ ฅํด์ฃผ์ธ์.");
}else{
// ํ๋ฉด์ ์ถ๋ ฅํ๊ธฐ
responseCommand(answer, jeju_answer);
}
}
// ๋ต๋ณ ๋ฐ์์ค๊ธฐ
function getAnswer(input){
$.ajax({
type:"get", // fetch์ method ๊ธฐ๋ฅ
url: "/process_input/"+input,
timeout:100000,
async:false,
// ์ฑ๊ณต
success:function(result){
console.log("success getAnswer() ํจ์ ์ฑ๊ณต" + input);
answer = result.answer
jeju_answer = result.jeju_answer
console.log("answer " + answer);
console.log("jeju_answer " + jeju_answer);
},
error:function(request,error){
alert("fail getAnswer() ํจ์ ์คํจ " + input);
}
})
}
// ์์ฑ ๋ง๋ค๊ธฐ
function getVoice(sentence){
$.ajax({
type:"get", // fetch์ method ๊ธฐ๋ฅ
url: "/voice/"+sentence,
timeout:100000,
async:false,
// ์ฑ๊ณต
success:function(audio_src){
console.log("success " + audio_src);
print_voice(audio_src)
return audio_src
},
error:function(request,error){
alert("fail " + sentence);
return "error"
}
})
}
// ์์ฑ ์๋ฆฌ ์ถ๋ ฅํ๊ธฐ
function print_voice(audio_src){
let hidden_area = document.querySelector("#hidden_area");
voice_tag_html = `
<audio src="../${audio_src}" autoplay></audio>`;
hidden_area.insertAdjacentHTML("beforeend", voice_tag_html);
printVoice = true;
}
function responseCommand(comm, jeju_comm) {
// animationCounter = 1;
//create response bubble
var successResponse = document.createElement('li');
successResponse.classList.add('bot__output');
successResponse.classList.add('bot__output--failed');
response_html = `<p>${comm}</p>
<p style="font-size:11px;color:lightslategray">${jeju_comm}</p>`
//Add text to successResponse
successResponse.innerHTML = response_html; //adds input of textarea to chatbubble list item
//add list item to chatlist
chatList.appendChild(successResponse) //adds chatBubble to chatlist
animateBotOutput();
// reset text area input
textInput.value = "";
//Sets chatlist scroll to bottom
chatList.scrollTop = chatList.scrollHeight;
animationCounter = 1;
}
function responseText(e) {
var response = document.createElement('li');
response.classList.add('bot__output');
//Adds whatever is given to responseText() to response bubble
response.innerHTML = e;
chatList.appendChild(response);
animateBotOutput();
console.log(response.clientHeight);
//Sets chatlist scroll to bottom
setTimeout(function(){
chatList.scrollTop = chatList.scrollHeight;
console.log(response.clientHeight);
}, 0)
}
function responseImg(e) {
var image = new Image();
image.classList.add('bot__output');
//Custom class for styling
image.classList.add('bot__outputImage');
//Gets the image
image.src = "/images/"+e;
chatList.appendChild(image);
animateBotOutput()
if(image.completed) {
chatList.scrollTop = chatList.scrollTop + image.scrollHeight;
}
else {
image.addEventListener('load', function(){
chatList.scrollTop = chatList.scrollTop + image.scrollHeight;
})
}
}
//change to SCSS loop
function animateBotOutput() {
chatList.lastElementChild.style.animationDelay= (animationCounter * animationBubbleDelay)+"ms";
animationCounter++;
chatList.lastElementChild.style.animationPlayState = "running";
}
function commandReset(e){
animationCounter = 1;
previousInput = Object.keys(possibleInput)[e];
}
var reactionInput = {
"best work" : function(){
//Redirects you to a different page after 3 secs
responseText("On this GitHub page you'll find everything about Navvy");
responseText("<a href='https://github.com/meesrutten/chatbot'>Navvy on GitHub</a>")
animationCounter = 1;
return
},
"about" : function(){
responseText("Things I want to learn or do:");
responseText("Get great at CSS & JS animation");
responseText("Create 3D browser experiences");
responseText("Learn Three.js and WebGL");
responseText("Combine Motion Design with Front-End");
animationCounter = 1;
return
}
}
|