-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.html
403 lines (372 loc) · 20.1 KB
/
index.html
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Chatbot Settings</title>
<link rel="icon" href="data:,">
<link rel="stylesheet" media="all" href="./style/client.css">
<style>
body{
background:#ffffff;
}
#setting-container {
background:#e6e6e6;
position: absolute;
top: 50px;
bottom: 100px;
width: 100%;
max-width: 1000px;
border: solid black 1px;
left: 50%; -webkit-transform: translate(-50%, 0); -moz-transform: translate(-50%, 0); -ms-transform: translate(-50%, 0); -o-transform: translate(-50%, 0); transform: translate(-50%, 0);
}
.bubble-message {
width: 300px;
max-width:100%;
height: 50px;
border: solid 1px #666666;
font-family: Helvetica, Arial, sans-serif;
}
.settings-button {
display:inline-block;
-webkit-box-sizing:content-box;
-moz-box-sizing:content-box;
box-sizing:content-box;
padding:6px 12px;
margin-left: 5px;
margin-bottom: 5px;
line-height:22px;
max-width:calc(80% - 50px);
min-width:50px;
font-size:12px;
font-family: Helvetica, Arial, sans-serif;
letter-spacing:1px;
text-align:center;
vertical-align:top;
background: #333333;
color: #ffffff;
border-radius: 5px;
cursor: pointer;
transition:.2s ease-out;
user-select: none;
}.settings-button:hover {
background: #666666;
}
#head{
position: absolute;
top: 15px;
height: 30px;
width: 100%;
text-align: center;
font-family: Helvetica, Arial, sans-serif;
}
#foot{
position: absolute;
bottom: 30px;
height: 60px;
width: 100%;
max-width: 1000px;
text-align: center;
vertical-align: top;
left: 50%; -webkit-transform: translate(-50%, 0); -moz-transform: translate(-50%, 0); -ms-transform: translate(-50%, 0); -o-transform: translate(-50%, 0); transform: translate(-50%, 0);
}
#foot2{
position: absolute;
bottom: 10px;
height: 20px;
width: 100%;
max-width: 1000px;
text-align: center;
vertical-align: top;
font-family: Helvetica, Arial, sans-serif;
left: 50%; -webkit-transform: translate(-50%, 0); -moz-transform: translate(-50%, 0); -ms-transform: translate(-50%, 0); -o-transform: translate(-50%, 0); transform: translate(-50%, 0);
}
#preview{
display: none;
position: absolute;
top: 0px;
bottom: 0px;
left: 0px;
right: 0px;
}
</style>
<meta content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" name="viewport" />
</head>
<body>
<div id="head">
<div style="display:inline-block">Chatbot's name: <textarea id="chatbot-name" rows="1" style="width:100px;resize:none;vertical-align:middle"></textarea></div>
<div style="display:inline-block">Chatbot's avatar: <textarea id="chatbot-avatar" rows="1" style="width:150px;resize:none;vertical-align:middle" placeholder="Image URL"></textarea></div>
<div style="display:inline-block">User's avatar: <textarea id="user-avatar" rows="1" style="width:150px;resize:none;vertical-align:middle" placeholder="Image URL"></textarea></div>
</div>
<div id="setting-container">
<div id="history-container">
<table id="chat-history">
</table>
</div>
<div id="message-container"><textarea id="message" name="message" rows="1" disabled></textarea></div>
<div id="send-button" style="cursor:auto;"><img src="./style/send.png" width="20px" style="vertical-align:middle"/></div>
</div>
<div id="foot">
<div class="button" style="float:left;margin-left:0px" onclick="set_example()">Example</div>
<div class="button" style="float:left" onclick="clear_all()">Clear All</div>
<div class="button" style="float:right;" onclick="download_code()">Generate Code</div>
<div class="button" style="float:right" onclick="preview()">Preview</div>
</div>
<div id="foot2">Github Repository: <a href="https://github.com/qiusihang/ticktalkturk">https://github.com/qiusihang/ticktalkturk</a></div>
<div id="preview">
<div style="position:absolute;top:10px;left:0px;right:0px;height:50px;text-align:center;z-index:10">
<div class="button" style="border-radius: 20px;" onclick="close_preview()">CLOSE PREVIEW</div>
</div>
</div>
<script>
var chatbot_name = "test";
window.onload = function() {clear_all();}
var to_bottom = function() {
var div = document.getElementById("history-container");
div.scrollTop = div.scrollHeight; // go to the bottom
}
var create_bot_bubble = function(index, del=true) {
var content = "\
<div class=\"left-arrow\" style=\"float:none;vertical-align:top\"></div><div class=\"bubble\" style=\"float: none;\">\
Chatbot Message Type: <select name=\"type\" oninput=\"select_type(this)\">\
<option value=\"text\">text</option>\
<option value=\"image\">image</option>\
<option value=\"buttons\">radio buttons</option>\
<option value=\"checkboxes\">checkboxes</option>\
</select><br/>\
Content:<br/><textarea class=\"bubble-message\" placeholder=\"Fill in your message here...\"></textarea>\
</div>";
if (del) content += "<div class=\"settings-button\" onclick=\"del_button(this)\">Delete</div>";
content += "<br/><div class=\"settings-button\" style=\"";
content += "margin-top:5px;border-radius:0px 15px 0px 15px;background:#ffffff;color:#000000;box-shadow:1px 1px 1px rgba(0,0,0,0.1);min-width:20px";
content += "\" onclick=\"add_button(this)\" onmouseover=\"change_html(this,'Add Utterance')\" onmouseout=\"change_html(this,'+')\">+</div>";
var table = document.getElementById("chat-history");
var row = table.insertRow(index);
var cell = row.insertCell();
cell.innerHTML = content;
// to_bottom();
};
var create_bot_bubble_me = function(index) {
var content = "\
<div class=\"right-arrow\"></div><div class=\"bubble-me\">\
User Input Validation: <select name=\"validation\" oninput=\"select_validation(this)\">\
<option value=\"none\">None</option>\
<option value=\"yes\">Keyword-based</option>\
</select><br/>\
<div class=\"keywords\" style=\"display:none\" >\
Keywords/phrases for validation:<br/><textarea class=\"bubble-message\" placeholder=\"Fill in the keywords/phrases (seperated by semicolons ';'). If a keyword/phrase is found in user's response, the conversation moves on.\"></textarea>\
</div>\
</div>";
var table = document.getElementById("chat-history");
var row = table.insertRow(index);
var cell = row.insertCell();
cell.innerHTML = content;
// to_bottom();
}
var create_new_turn = function(index) {
create_bot_bubble(index,false);
create_bot_bubble_me(index+1,false);
var table = document.getElementById("chat-history");
var row = table.insertRow(index+2);
var cell = row.insertCell();
cell.style = "text-align:center"
cell.innerHTML = "<hr><div class=\"settings-button\" style=\"border-radius:20px;background:#999999\" onclick=\"add_turn(this)\">New Turn</div>";
// to_bottom();
}
var select_type = function(e) {
var ta = e.parentNode.querySelector("textarea.bubble-message");
switch (e.value) {
case "text": ta.placeholder = "Fill in your message here..."; break;
case "image": ta.placeholder = "Fill in the image URL here..."; break;
case "buttons":
case "checkboxes": ta.placeholder = "Fill in the options here (seperated by semicolons ';')..."; break;
}
};
var select_validation = function(e) {
switch (e.value) {
case "none": e.parentNode.querySelector("div.keywords").style.display = "none"; break;
default: e.parentNode.querySelector("div.keywords").style.display = "block"; break;
}
};
var add_turn = function(e) {
var row = e.parentNode.parentNode;
e.innerHTML = "Delete Turn";
e.setAttribute( "onClick", "javascript: del_turn(this);" );
create_new_turn(row.rowIndex+1);
}
var del_turn = function(e) {
var row = e.parentNode.parentNode;
var ri = row.rowIndex;
var table = document.getElementById("chat-history");
while (true) {
table.deleteRow(ri);
if ( table.rows[ri].cells[0].firstChild.tagName == "HR" ) break;
}
}
var add_button = function(e) {
var row = e.parentNode.parentNode;
create_bot_bubble(row.rowIndex+1);
}
var del_button = function(e) {
var row = e.parentNode.parentNode;
var table = document.getElementById("chat-history");
table.deleteRow(row.rowIndex);
}
var change_html = function(e, text) {
e.innerHTML = text;
}
var generate_conversation = function() {
var table = document.getElementById("chat-history");
var survey = [];
var ri = 0;
var t = 0
var l = table.rows.length;
var flag = true;
while ( ri < l ) {
var q = {"messages":[]};
while ( ri < l ) {
if ( table.rows[ri].cells[0].firstChild.tagName == "HR" ) break; // next turn
var bb = table.rows[ri].cells[0].querySelector("div.bubble");
if ( bb == null ) bb = table.rows[ri].cells[0].querySelector("div.bubble-me");
var sl = bb.querySelector("select");
var ta = sl.parentNode.querySelector("textarea.bubble-message");
ta.style.border = "solid 1px #666666";
switch (sl.value) {
case "text":
if ( ta.value.length < 1 ) { flag = false; ta.style.border = "solid 1px red"; }
q.messages.push(ta.value);
break;
case "image":
if ( ta.value.length < 1 ) { flag = false; ta.style.border = "solid 1px red"; }
q.messages.push("<img style=\"max-width:500px;width:100%\" src=\""+ta.value+"\"/>");
break;
case "buttons":
if ( ta.value.length < 1 ) { flag = false; ta.style.border = "solid 1px red"; }
ops = ta.value.split("#").join("").split(";");
for ( var i = 0 ; i < ops.length ; i ++ ) ops[i] = ops[i].trim();
q.messages.push("buttons:#"+ops.join("#"));
break;
case "checkboxes":
if ( ta.value.length < 1 ) { flag = false; ta.style.border = "solid 1px red"; }
ops = ta.value.split("#").join("").split(";");
for ( var i = 0 ; i < ops.length ; i ++ ) ops[i] = ops[i].trim();
q.messages.push("checkbox:#"+ops.join("#"));
break;
case "yes":
if ( ta.value.length < 1 ) { flag = false; ta.style.border = "solid 1px red"; }
ops = ta.value.split("#").join("").split(";");
for ( var i = 0 ; i < ops.length ; i ++ ) ops[i] = ops[i].trim();
q["validation"] = "#"+ops.join("#");
break;
default: break;
}
ri += 1;
}
survey.push(q);
ri += 1;
t += 1;
}
if (!flag) {
alert("Please fill in your message(s).");
return "[]";
} else {
return JSON.stringify(survey);
}
}
var generate_html = function() {
var template = "<!DOCTYPE html>\
<html lang=\"en\">\
<head>\
<meta charset=\"UTF-8\">\
<title>Chatbot</title>\
<link rel=\"icon\" href=\"data:,\">\
<link rel=\"stylesheet\" media=\"all\" href=\"https://qiusihang.github.io/ticktalkturk/style/client.css\">\
<meta content=\"width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0\" name=\"viewport\" />\
</head>\
<body>\
<div id=\"container\">\
<div id=\"history-container\">\
<table id=\"chat-history\">\
</table>\
</div>\
<div id=\"message-container\">\
<textarea id=\"message\" name=\"message\" onkeydown=\"onKeyDown(event)\" placeholder=\"Type your message here\" rows=\"1\"></textarea>\
</div>\
<div id=\"send-button\" onclick=\"click_send()\"><img src=\"https://qiusihang.github.io/ticktalkturk/style/send.png\" width=\"20px\" style=\"vertical-align:middle\"/></div>\
<div id=\"message-cover\">Please reply the chatbot by selecting the option(s).</div>\
<div id=\"submit\" style=\"cursor:pointer\" onclick=\"submit()\">\
<div style=\"font-size:16px;border:0px;background:rgba(0,0,0,0);color:#666666\"><b>SUBMIT</b></div>\
<textarea id=\"chat-answer\" name=\"chat-answer\" style=\"display:none\"></textarea>\
</div>\
</div>\
<sssss src=\"https://qiusihang.github.io/ticktalkturk/js/jquery.js\"></sssss>\
<sssss src=\"https://qiusihang.github.io/ticktalkturk/js/chatbot.js\"></sssss>\
<sssss src=\"https://qiusihang.github.io/ticktalkturk/js/client.js\"></sssss>\
<sssss> var survey = SURVEY; window.onload = init(); var submit = function() { var res = {answers: answers}; console.log(res); /*Do what you want to do with the survey results here!*/ }</sssss>\
</body>\
</html>";
chatbot_name = document.getElementById("chatbot-name").value;
chatbot_image = document.getElementById("chatbot-avatar").value;
user_image = document.getElementById("user-avatar").value;
var survey_html = generate_conversation()
+(chatbot_name.length>0?"; var chatbot_name=\""+chatbot_name+"\"":"")
+(chatbot_image.length>0?"; var chatbot_image=\""+chatbot_image+"\"":"")
+(user_image.length>0?"; var user_image=\""+user_image+"\"":"");
template = template.split("sssss").join("script");
template = template.replace("SURVEY",survey_html);
template = template.split(" ").join("");
return template;
}
var preview = function() {
if ( generate_conversation() == "[]" ) return;
var iframe = document.createElement("iframe");
iframe.style = "height:100%;width:100%";
document.getElementById("preview").appendChild(iframe);
iframe.contentWindow.document.open();
iframe.contentWindow.document.write(generate_html());
iframe.contentWindow.document.close();
document.getElementById("preview").style.display = "block";
}
var close_preview = function() {
document.getElementById("preview").style.display = "none";
document.getElementById("preview").removeChild(document.getElementById("preview").querySelector("iframe"));
}
var download_code = function() {
if ( generate_conversation() == "[]" ) return;
var element = document.createElement('a');
element.setAttribute("href", "data:text/plain;charset=utf-8," + encodeURIComponent(generate_html()));
element.setAttribute('download', "chatbot.html");
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
var set_example = function() {
document.getElementById("chatbot-name").value = "Andrea";
document.getElementById("chatbot-avatar").value = "https://qiusihang.github.io/ticktalkturk/res/andrea.png";
document.getElementById("user-avatar").value = "https://qiusihang.github.io/ticktalkturk/res/user_example.png";
var table = document.getElementById("chat-history");
while (table.rows.length > 0) table.deleteRow(0);
create_new_turn(0); table.rows[0].cells[0].querySelector("textarea").value = "Hi, I'm Andrea. How are you doing? Can you help me complete this task?";
create_bot_bubble(1); table.rows[1].cells[0].querySelector("select").value = "buttons"; table.rows[1].cells[0].querySelector("textarea").value = "No problem.; I would love to!";
create_new_turn(4); table.rows[4].cells[0].querySelector("select").value = "image"; table.rows[4].cells[0].querySelector("textarea").value = "https://qiusihang.github.io/ticktalkturk/logo.png";
create_bot_bubble(5); table.rows[5].cells[0].querySelector("textarea").value = "Is this the logo of TickTalkTurk?";
create_bot_bubble(6); table.rows[6].cells[0].querySelector("select").value = "buttons"; table.rows[6].cells[0].querySelector("textarea").value = "Yes; No";
table.rows[7].cells[0].querySelector("select").value = "yes"; table.rows[7].cells[0].querySelector("textarea").value = "Yes"; table.rows[7].cells[0].querySelector("textarea").parentNode.style.display = "block";
table.rows[3].cells[0].querySelector("div.settings-button").innerHTML = "Delete Turn"; table.rows[3].cells[0].querySelector("div.settings-button").setAttribute( "onClick", "javascript: del_turn(this);" );
create_new_turn(9); table.rows[9].cells[0].querySelector("textarea").value = "Please check the statement(s) that you agree with.";
create_bot_bubble(10); table.rows[10].cells[0].querySelector("select").value = "checkboxes"; table.rows[10].cells[0].querySelector("textarea").value = "This chatbot is interesting; This chatbot is easy-to-use; This chatbot is funny";
table.rows[8].cells[0].querySelector("div.settings-button").innerHTML = "Delete Turn"; table.rows[8].cells[0].querySelector("div.settings-button").setAttribute( "onClick", "javascript: del_turn(this);" );
var div = document.getElementById("history-container");
div.scrollTop = 0; // go to the top
}
var clear_all = function() {
document.getElementById("chatbot-name").value = "";
var table = document.getElementById("chat-history");
while (table.rows.length > 0) table.deleteRow(0);
create_new_turn(0);
var div = document.getElementById("history-container");
div.scrollTop = 0; // go to the top
}
</script>
</body>
</html>