-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapp.js
313 lines (261 loc) · 10.8 KB
/
app.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
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
// SELECT DOCUMENT
const contentText = document.querySelector('.content-text');
const contentImg = document.querySelector('.content-img');
const settings = document.querySelector('.settings');
const settingsBtn = document.querySelector('.settings-btn');
const yourNameInput = document.querySelector('.your-name-input');
const herNameInput = document.querySelector('.her-name-input');
const dataTab = document.querySelector('.data-tab');
const customTab = document.querySelector('.custom-tab');
const questionInput = document.querySelector('.custom-question-input');
const ansInput = document.querySelector('.custom-ans-input');
const addBtn = document.querySelector('.add-btn');
const talkBackSwitch = document.querySelector('.talkBackSwitch');
// DATA
let yourName = (localStorage.getItem('yourName') != null)? localStorage.getItem('yourName') : 'Tom';
yourNameInput.value = yourName;
let herName = (localStorage.getItem('herName') != null)? localStorage.getItem('herName') : 'Jerry';
herNameInput.value = herName;
let data = [];
let savedData = (localStorage.getItem('userData') != null)? true : false;
let userData = (savedData)? JSON.parse(localStorage.getItem("userData")) : [];
let talkBack = false;
let recognizing = false;
let interVal = '';
// VOICE RECOGNITION
const speechRecognition = window.speechRecognition || window.webkitSpeechRecognition;
const recognition = new speechRecognition();
recognition.onstart = () => {
recognition.continuous = true;
recognizing = true;
}
recognition.onspeechstart = () => {
contentText.innerHTML = 'I am Listening';
}
recognition.onend = () => {
recognizing = false;
recognition.start()
contentText.innerHTML= 'Say Something More';
}
recognition.onresult = (event) => {
let resultIndex = event.resultIndex;
let transcript = event.results[resultIndex][0].transcript;
transcript = transcript.replace(`${yourName}`, '');
transcript = transcript.replace(`${herName}`, '');
transcript = transcript.trim();
if (talkBack){
readData(transcript)
}else{
readData(findData(transcript))
}
}
// FUNCTIONS
// ASSIGNING THE DEFAULT DATA
function assignData(yourName, herName) {
data = [
{
title : 'Introduction',
questions : ['hi', 'hello', 'hey', 'whatsup'],
ans : ['Hi', 'Hi Babe', 'Hi Sweetheart', 'Hi Babu', `Hi ${yourName}`, 'Hello' , 'Hello Babe', 'Hello Sweetheart', 'Hello Babu', 'Hey', 'Hey Babe', 'Hey Sweetheart', 'Hey Babu', 'Whatsup', 'Whatsup Babe', 'Whatsup Sweetheart', 'Whatsup Babu'],
},
{
title : 'Propose',
questions : ['I Love You', 'Love You'],
ans : ['I Love You Two', 'Love You', 'Amio Tumake ValoBashi', 'Love You Sweetheart'],
},
{
title : 'Her Name',
questions : ['what is your name', 'your name', 'who are you'],
ans : [herName, `I am ${herName}`, `My Name Is ${herName}`]
},
{
title : 'Your Name',
questions : ['what is my name', 'my name', 'Do You Know Me', 'Who I am'],
ans : [yourName, `You Are ${yourName}`, `Your Name Is ${yourName}`]
},
{
title : 'About Developer',
questions : ['Who Is Your Owner', 'Your Owner', 'Who Makes You', 'Who Is Your Developer'],
ans : ['DevTom', 'Tom', 'Tonmoy']
}
]
}
// TRANSITION FOR SETTING TAB
function showHide() {
settings.classList.add('animation');
settings.classList.toggle('hide');
settingsBtn.classList.toggle('bg-danger');
settingsBtn.classList.toggle('text-light');
}
// FUNCTION FOR MAKING TABLES
function loadDataTable(data, target, IsSavedData) {
var title, tableData, tableIndex, html;
tableIndex = 0;
html ='';
data.forEach(item => {
title = (item.title == undefined)? `<thead><tr><th scope="col">#</th><th scope="col">Noname</th></tr></thead>` : `<thead><tr><th scope="col">#</th><th scope="col">${item.title}</th></tr></thead>`;
tableData= '';
// VALIDATING THAT IF USER HAVE ANY SAVED DATA IN HIS LOCALSTORAGE THEN IT WILL EXECUTE
if(IsSavedData){
// ASSIGNING QUESTION AND ANSWER IN A ROW
tableData = `<tr data-index="${++tableIndex}" class="table-row" ><th class='deleteTableData'><i class="bi bi-trash"></i></th><td data-target="question" >${item.question}</td><td data-target="ans" >${item.ans}</td></tr>`;
// ASSIGNING ALL ROW IN A SINGLE VARRIABLE
html += tableData;
}else{
// MAKING TABLE WITH DEFAULT DATA
item.questions.forEach((question, index) => {
// ASSIGNING QUESTION IN A ROW
tableData += `<tr><th scope="row">${++index}</th><td>${question}</td></tr>`;
});
// MAKING TABLE STRUCTURE AND STORING ALL QUESTION ROW
html += `${title}<tbody>${tableData}</tbody>`;
}
});
// ADDING TABLE TO THE TARGETED ELEMENT
target.innerHTML = html;
}
// FUNCTION FOR ADDING NEW DATA TO THE CUSTOM TABLE
function addNewData() {
// MAKING A OBJECT TO STORE USER DATA
let userAddedData = {};
if(questionInput.value.length > 0 && ansInput.value.length > 0){
// STORING DATA TO THE OBJECT
userAddedData.question =questionInput.value;
userAddedData.ans =ansInput.value;
// ASSIGNING THE NEW USER DATA OBJECT
userData.push(userAddedData);
// CLEARING THE INPUTS
questionInput.value = '';
ansInput.value = '';
// STORING DATA TO THE LOCALSTORAGE
localStorage.setItem('userData', JSON.stringify(userData));
savedData = true;
// LOADING TABLE WITH NEW ASSIGNED DATA
loadDataTable(userData, customTab, savedData);
}else{
alert('Please Input A Valid Data');
}
}
// FINDING DATA FOR ANSWERING THE QUESTIONS
function findData(transcript) {
let text, notMatched;
notMatched = true;
// CHECKING DATA FROM USERSAVED DATA
if(savedData){
userData.forEach(dataItem => {
if(dataItem.question.toLowerCase() == transcript){
text = dataItem.ans;
notMatched = false;
return;
}
})
}
// CHECKING DATA FROM DEFAULT DATA
if (notMatched) {
let dataObj = data.find(dataItem => {
let x = dataItem.questions.some(question => {
return question.toLowerCase() == transcript.toLowerCase();
})
return x;
})
// CHOOSING A RANDOM ANSWER
text = (dataObj != undefined)? dataObj.ans[Math.floor(Math.random() * dataObj.ans.length)] : false;
}
return text;
}
// READ THE THE GIVEN ANSWER
function readData(message) {
// VALIDATING THAT THE ANSWER IS AVAILABLE
message = (message == false)? "Uff Bujlam Na" : message;
const speech = new SpeechSynthesisUtterance();
speech.text = message;
speech.volume = 1;
speech.rate = 1;
speech.pitch = 1;
window.speechSynthesis.speak(speech);
// ADDING A ANIMATION IN IMAGE WHILE SHE IS SPEAKING
interVal = setInterval(() => {
readAnim(window.speechSynthesis.speaking)
}, 100);
}
// ANIMATION FOR THE IMAGE BUTTON
function readAnim(speaking) {
if(speaking){
if(!(contentImg.classList.contains('readAnim'))){
contentImg.classList.add('readAnim');
}else{
return;
}
}else{
contentImg.classList.remove('readAnim');
clearInterval(interVal);
recognition.abort()
}
}
// DELETING A DATA FROM USER SAVED DATA
function deleteData(event) {
// VALIDATING THE RIGHT BUTTON
if(event.target.classList.contains('deleteTableData')){
// SELECTING THE PARENT ELEMENT
const tableParent = event.target.parentElement;
// STORING THE PARENT ELEMENT DATA INDEX VALUE TO SELECT THE CHILD ELEMENT
const tableParentIndex = tableParent.dataset.index;
if(tableParent.classList.contains('table-row')){
// SELECTING THE CHILD ELEMENTS
const tableQuestion = document.querySelector(`tr[data-index="${tableParentIndex}"] td[data-target="question"]`);
const tableAns = document.querySelector(`tr[data-index="${tableParentIndex}"] td[data-target="ans"]`);
// SELECTING THE CORRECT OBJECT FROM USER DATA BY MATCHING THE DATA WITH SELECTED TABLE ELEMENT
let targetedTable = userData.find(tableRow => {
return (tableRow.question.toLowerCase() == tableQuestion.innerText.toLowerCase() && tableRow.ans.toLowerCase() == tableAns.innerText.toLowerCase());
})
// BASIC VALIDATION IF THE DATA OBJECT NOT MATCHED
if(targetedTable != undefined){
// FILTERING USERDATA AND REMOVING THE TARGETED OBJECT
userData = userData.filter(value => {
return value != targetedTable;
});
// SAVING NEW USER DATA TO THE LOCAL STORAGE
localStorage.setItem('userData', JSON.stringify(userData));
// LOADING THE CUSTOM TABLE WITH NEW FILTERD USER DATA
loadDataTable(userData, customTab, savedData);
}
}
}else{
return;
}
}
// HANDLING INPUT DATA
function handleInput(event) {
let target = event.target;
if(target.dataset.target == "yourName"){
yourName = target.value;
localStorage.setItem('yourName', yourName);
}else if(target.dataset.target == "herName"){
herName = target.value;
localStorage.setItem('herName', herName);
}
assignData(yourName, herName)
}
// HANDLING TALKBACK SWITCH
function handleSwitch() {
talkBack = talkBackSwitch.checked;
}
// EVENT LISTENER
settingsBtn.addEventListener('click', showHide);
addBtn.addEventListener('click', addNewData);
contentImg.addEventListener('click', () => {
if(recognizing){
recognition.abort()
}else{
recognition.start()
contentText.innerHTML= 'Say Something';
}
})
yourNameInput.addEventListener('input', handleInput);
herNameInput.addEventListener('input', handleInput);
customTab.addEventListener('click', deleteData);
talkBackSwitch.addEventListener('click', handleSwitch);
// CALL FUNCTION
assignData(yourName, herName)
loadDataTable(data,dataTab);
if(savedData)loadDataTable(userData,customTab, savedData);