-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
1375 lines (1202 loc) · 46.7 KB
/
index.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
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var sentinel_id = document.getElementById("app-interact").getAttribute("data-sentinelid");
var server = document.getElementById("app-interact").getAttribute("data-server");
var walker = document.getElementById("app-interact").getAttribute("data-walkername");
var token = document.getElementById("app-interact").getAttribute("data-token");
var last_jid = null;
// Javascript objects containing emotions. Each key has a value of type array. - [icon,]
emotions = {
sad: ["fa-frown-o", "blue"],
happy: ["fa-smile-o", "orange"],
fear: ["fa-frown-open", "green"],
anger: ["fa-angry", "red"],
surprised: ["fa-surprise", "yellow"],
default: ["fa-meh-blank", "grey"],
};
//say = "Arianna is growing up so fast. Today she's been trying to stand on her own. It fills me up with such a sense of joy to see my little girl blossom before me.";
// list of emotions to be used in select list
emotions_select_values = Object.keys(emotions);
emotion_select_options = ``;
for (i = 0; i < emotions_select_values.length - 1; i++) {
emotion_select_options = emotion_select_options + `<option value="${emotions_select_values[i]}">${emotions_select_values[i]}</option>`;
}
// Initialize new SpeechSynthesisUtterance object
let speech = new SpeechSynthesisUtterance();
speech.lang = "en";
let voices = [];
window.speechSynthesis.onvoiceschanged = () => {
// Get List of Voices
voices = window.speechSynthesis.getVoices();
//set the voice.
if (voices.length > 49) speech.voice = voices[49]; //US Female
speech.volume = 1;
speech.rate = 0.85;
speech.pitch = 1;
};
// var query_textbox = $("#query__inputField");
// var chat_textbox = $("#chatio__inputField");
// const artyom = new Artyom();
// // // Add a single command
// var commandHello = {
// indexes:["I'd like to add a memory"], // These spoken words will trigger the execution of the command
// action:function(){ // Action to be executed when a index match with spoken word
// //artyom.say("I'm Tobu, I can help you remember.");
// //console.log("hi");
// display_capture_modal();
// artyom.fatality();
// }
// };
// artyom.addCommands(commandHello);
// // // This function activates artyom and will listen all that you say forever (requires https conection, otherwise a dialog will request if you allow the use of the microphone)
// function startContinuousArtyom() {
// artyom.fatality();// use this to stop
// setTimeout(function(){// if you use artyom.fatality , wait 250 ms to initialize again.
// artyom.initialize({
// lang:"en-US",// A lot of languages are supported. Read the docs !
// continuous:true,// Artyom will listen forever
// listen:true, // Start recognizing
// debug:true, // Show everything in the console
// speed:1, // talk normally
// name: "hey there"
// }).then(function(){
// console.log("Ready to work !");
// });
// },250);
// }
// var Chat_UserDictation = chat_artyom.newDictation({
// continuous:true, // Enable continuous if HTTPS connection
// onResult:function(chat_text){
// // Do something with the text
// console.log(chat_text);
// chat_textbox.val(chat_text);
// },
// onStart:function(){
// console.log("chat started");
// },
// onEnd:function(){
// alert("Dictation stopped by the user");
// }
// });
// Stop whenever you want
// Chat_UserDictation.stop();
/*const query_artyom = new Artyom();
// This function activates artyom and will listen all that you say forever (requires https conection, otherwise a dialog will request if you allow the use of the microphone)
function query_startContinuousArtyom(){
query_artyom.fatality();// use this to stop any of
setTimeout(function(){// if you use artyom.fatality , wait 250 ms to initialize again.
query_artyom.initialize({
lang:"en-GB",// A lot of languages are supported. Read the docs !
continuous:true,// Artyom will listen forever
listen:true, // Start recognizing
debug:true, // Show everything in the console
speed:1 // talk normally
}).then(function(){
console.log("Ready to work !");
});
},250);
}
query_startContinuousArtyom();
var Query_UserDictation = query_artyom.newDictation({
continuous:true, // Enable continuous if HTTPS connection
onResult:function(query_text){
// Do something with the text
console.log(query_text);
query_textbox.val(query_text);
},
onStart:function(){
console.log("query started");
},
onEnd:function(){
alert("Dictation stopped by the user");
}
});
Query_UserDictation.start();
// Stop whenever you want
// Query_UserDictation.stop();
query_artyom.redirectRecognizedTextOutput(function(query_recognized,query_isFinal){
if(query_isFinal){
query_textbox.val(query_recognized);
}else{
query_textbox.val(query_recognized);
}
});*/
// // Replace the script tag with the app
document.getElementById("app-interact").parentNode.innerHTML = `
<!-- NAVBAR--><nav class="navbar navbar-expand-sm navbar-dark">
<div class="container">
<a onclick="location.reload()" class="navbar-brand">
<!-- Logo Image -->
<img src="./images/tobu_logo_w.png" width="70" height="30" alt="" class="d-inline-block align-middle mr-2">
</a>
<button type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation" class="navbar-toggler"><span class="navbar-toggler-icon"></span></button>
<div id="navbarSupportedContent" class="collapse navbar-collapse">
<ul class="navbar-nav ml-auto">
<button type="button" class="btn btn-outline-secondary" onclick="display_capture_modal()" style="background-color: #feb248;"><i class="fa fa-picture-o"></i> Capture a memory</button>
<button type="button" id="ask_tobu_btn" class="btn btn-outline-secondary" data-bs-toggle="modal" data-bs-target="#ask_tobu_modal"><i class="fa fa-microphone"></i> Ask Tobu</button>
</ul>
</div>
</div>
</nav><!-- NAVBAR-->
<div style="padding-bottom: 20%"></div>
<div>
<div class="btn-group" style="padding-bottom: 10px;">
<div class="modal fade" id="ask_tobu_modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document" style="height: 30%;">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Ask Tobu</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
</div>
<div class="modal-footer" style="padding-right: 12%">
<!-- User input box -->
<span class="fa-stack fa-1x"><i id="query_mic-bg" class="fa fa-circle fa-stack-2x icon-background" style="margin-left: -10px; color: #ffffff;"></i><i id="query_mic-btn" class="fa fa-microphone fa-stack-1x" style="margin-left: -7px;" onclick="query_mic_click()"></i></span>
<input id="query__inputField" style="width: 500px;float: left;border-color: whitesmoke;height: 47px;border-width: 0px;"" type="text" name="msg" placeholder="ask Tobu about your memories">
<div style="display: inline;float: left;height: 47px; width: 25px;"><button type="button" class="btn btn-outline-secondary" onclick="ask_tobu()">Send</button></div>
</div>
</div>
</div>
</div>
<!-- Capture Memory Modal -->
<div class="modal fade" id="createMemoryModal" tabindex="-1" aria-labelledby="createMemoryModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<p class="modal-title" id="createMemoryModalLabel"> Capture a memory</p>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close" onClick="display_memory_feed();reset_capture_modal();"><span aria-hidden="true">×</span></button>
</div>
<div class="modal-body">
<!-- <button type="button" class="btn btn-outline-secondary">Add Photo/Video</button> -->
<div id="create_image_input"><input type="file" name="" id="create_fileId" onchange="imageUploaded()"></div>
<div id="photos"></div>
<div id="chatbox"></div>
</div>
<div class="modal-footer" style="padding-right: 10%">
<!-- User input box -->
<span class="fa-stack fa-1x"><i id="chat_mic-bg" class="fa fa-circle fa-stack-2x icon-background" style="margin-left: -13px; color: #ffffff;"></i><i id="chat_mic-btn" class="fa fa-microphone fa-stack-1x" style="margin-left: -5px;" onclick="chat_mic_click()"></i></span>
<input id="chatio__inputField" style="width: 950px;float: left;border-color: whitesmoke;height: 47px;border-width: 0px;" "="" type="text" name="msg" placeholder="describe your memory"a>
<div style="display: inline;float: left;height: 47px; width: 25px;"><button type="button" class="btn btn-outline-secondary" onclick="sendMessage()">Send</button></div>
</div>
</div>
</div>
</div>
</div>
<!-- Memory Modal -->
<div class="modal fade" id="memoryModal" tabindex="-1" aria-labelledby="memoryModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<p class="modal-title" id="memoryModal_title"></p>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close" onclick="close_edit_modal(); shutUp()"><span aria-hidden="true">×</span></button>
</div>
<div class="modal-body">
<div id="memoryModal_details">
<div class="card mb-3">
<div id="memoryModal_image"></div>
<div class="card-body">
<div style="display: inline;float: right;">
<a type="text" class="dropdown" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><i class="fa fa-ellipsis-v"></i></a>
<div class="dropdown-menu">
<a id="memoryModal_btn_narrate" class="dropdown-item" href="#">Narrate</a>
<a id="memoryModal_btn_edit" class="dropdown-item" href="#">Edit</a>
<a id="memoryModal_btn_delete" class="dropdown-item" href="#">Delete</a>
</div>
</div>
<h5 id="memoryModal_subject" class="card-title" style="margin-bottom: 0px;"></h5>
<p class="card-text"><small class="text-muted"><span id="memoryModal_how"></span><span id="memoryModal_date"></span><span><i class="fas fa-map-marker-alt" style="padding-left: 2%;"></i></span><span id="memoryModal_where"></span><span id="memoryModal_people"></span></small></p>
<p class="card-text"></p>
<p id="memoryModal_description" class="card-text"></p>
<p class="card-text"><small class="text-muted"><span id="memoryModal_lastUpdated"></span></small></p>
</div>
<div id="memoryModal_related_memories"></div>
</div>
</div>
<div id="memoryModal_edit" style="display: none">
<div id="edit_image_input"><input type="file" name="" id="edit_fileId" onchange="editImageUploaded()"></div>
<div id="edit_photos"></div>
<form>
<div class="form-group">
<label for="memory_subject">Subject</label>
<input type="text" class="form-control" id="memory_subject" placeholder="Subject">
</div>
<!-- <div class="form-group">
<label for="memory_category">Category</label>
<input type="text" class="form-control" id="memory_category" placeholder="Category">
</div> -->
<div class="form-group">
<label for="memory_when">When</label>
<input type="text" class="form-control" id="memory_when" placeholder="When did this happen?">
</div>
<div class="form-group">
<label for="memory_where">Where</label>
<input type="text" class="form-control" id="memory_where" placeholder="Where did this happen?">
</div>
<div class="form-group">
<label for="memory_how">How:</label>
<select class="form-control" id="memory_how">${emotion_select_options}</select>
</div>
<div class="form-group">
<label for="memory_who">Who:</label>
<input type="text" class="form-control" id="memory_who" name="memory_who">
</div>
<div class="form-group">
<label for="memory_description">Description</label>
<textarea class="form-control" id="memory_description" rows="3"></textarea>
</div>
<div class="form-group">
<label for="memory_summary">Summary</label>
<textarea class="form-control" id="memory_summary" rows="3"></textarea>
</div>
</form>
<button class="btn btn-primary" onclick="save_memory_details()"><i class="fa fa-check-circle" aria-hidden="true"></i>Save Changes</button>
</div>
</div>
</div>
</div>
</div>
</div>
<div id="ask_tobu_alert" class="alert alert-primary alert-dismissible fade show mb-3" role="alert" style="display:none;"></div>
<div class="loader"></div>
<div id="all_memories"></div>
</div>
`;
var chat_messages = [];
var create_memory_images = [];
var upload_ids = [];
var edit_memory_images = [];
var edit_memory_ids = [];
var extension = "";
var current_file_url = "";
var file_upload = false;
var current_memory_photos = [];
var current_memory_id = "";
// get input from query field
var query_inputField = document.getElementById("query__inputField");
//fetches and renders memories in the feed area
async function display_memory_feed() {
// clear memory feed again before displaying anything
$("#all_memories").html("");
var memories = [];
if (query_inputField.value) {
walker_get_memories(query_inputField.value).then(async (result) => {
if (result.success){
memories = result.report[0];
if (memories.length > 0) {
display_askTobu_alert(memories.length, query_inputField.value);
await render_memories(memories);
}
else {
walker_get_memories().then(async (result) => {
memories = result.report[0];
display_askTobu_alert(0, query_inputField.value);
await render_memories(memories);
})
.catch(function (error) {
console.log(error);
});
}
} else{
$("#all_memories").html(`<div style="height:75vh; margin:auto; text-align:center; padding: 100px 0;"><i class="fa fa-exclamation-triangle"></i><br><div>We are currently experiencing technical difficulties with the Ask tobu feature. Please try again later. Thank you for your patience</div></div>`);
}
})
.catch(function (error) {
console.log(error);
});
} else {
walker_get_memories()
.then(async (result) => {
if(!(result.success)){
$("#all_memories").html(`<div style="height:75vh; margin:auto; text-align:center; padding: 100px 0;"><i class="fa fa-exclamation-triangle"></i><br><div>We are currently experiencing technical difficulties at the moment. Please try again later. Thank you for your patience</div></div>`);
}
else{
memories = result.report[0];
await render_memories(memories);
}
})
.catch(function (error) {
console.log(error);
});
}
}
function isValidTimestamp(_timestamp) {
const newTimestamp = new Date(_timestamp).getTime();
return isNumeric(newTimestamp);
}
function isNumeric(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
//takes an array of memories and renders memory posts in the memory feed.
async function render_memories(memories) {
//clear memory feed
$("#all_memories").html("");
// console.log(memories);
if (!memories || memories.length == 0) {
return;
}
for (let i = 0; i < memories.length; i++) {
// for each memory
console.log(memories[i]);
m_keys = Object.keys(memories[i]);
$("#all_memories").append(`<div id="memory_${memories[i]["id"]}" class="card mb-3"></div>`); // append card to feed
// checking to see if image_url exists; making sure it's not null or an empty string
if (m_keys.includes("image_urls") && (memories[i]["image_urls"] != null || memories[i]["image_urls"] != "")) {
var imageUrl = null;
var memory_card_image = null;
if (Array.isArray(memories[i]["image_urls"]) && memories[i]["image_urls"].length > 0) memory_card_image = memories[i]["image_urls"][0];
if (typeof memories[i]["image_urls"] === "string") memory_card_image = memories[i]["image_urls"].split(",")[0]; // this safeguards against comma separated string (until I'm able to store it correctly)
if (memory_card_image) {
$("#memory_" + memories[i]["id"]).prepend(
`<img src="${memory_card_image}" class="card-img-top" alt="..." onclick=display_memory_modal('${memories[i]["id"]}')>`
);
}
}
memory_subject = memories[i]["subject"];
memory_description = memories[i]["description"];
memory_summary = memories[i]["summary"];
memory_where = memories[i]["where"];
memory_when = memories[i]["when"];
memory_date_created = memories[i]["date_created"];
memory_date_modified = memories[i]["date_modified"];
memory_emotion = [];
memory_who = [];
memory_subject = memory_subject == "" ? "Subject N/A" : memory_subject;
memory_description = memory_description == "" ? "Description N/A" : memory_description;
memory_summary = memory_summary == "" ? "Summary N/A" : memory_summary;
memory_where = memory_where == "" ? "Unknown Location" : memory_where;
memory_who = Array.isArray(memories[i]["who"]) ? memories[i]["who"] : [];
var display_memory_people = ``;
if(memory_who.length > 0) display_memory_people = `<span><i class="fas fa-user" style="padding-left: 2%;"></i></span>${memory_who.toString()}`;
memory_emotion = memories[i]["how"] == "" ? emotions["default"] : emotions[memories[i]["how"]];
memory_when = memory_when == "" ? "Date Unknown" : memory_when;
memory_date_created = memory_date_created == "" ? memory_when : memory_date_created;
memory_date_modified = memory_date_modified == "" ? memory_date_created : memory_date_modified;
memory_date_created = isValidTimestamp(memory_date_created) ? memory_date_created.replace("T", " ").substring(0, memory_date_created.lastIndexOf(".")): memory_date_created;
memory_date_modified = isValidTimestamp(memory_date_modified) ? memory_date_modified.replace("T", " ").substring(0, memory_date_modified.lastIndexOf(".")): memory_date_modified;
memory_date_created = memory_date_created == "" ? memory_when : memory_date_created;
memory_date_modified = memory_date_modified == "" ? memory_date_created : memory_date_modified;
$("#memory_" + memories[i]["id"]).append(`
<div class="card-body">
<h5 class="card-title truncated" style="margin-bottom: 0px;"><a href="javascript:display_memory_modal('${memories[i]["id"]}')">${memory_subject}</a></h5>
<p class="card-text"><small class="text-muted"><span><i class="fa ${memory_emotion[0]}" style="color: ${memory_emotion[1]};"></i></span>${memory_when}<span><i class="fas fa-map-marker-alt" style="padding-left: 2%;"></i></span>${memory_where}${display_memory_people}</small></p>
<p class="card-text"></p>
<p class="card-text">${memory_summary}</p>
<p class="card-text"><small class="text-muted">Last updated on ${memory_date_modified}</small></p>
</div>
`);
if (m_keys.includes("relatedMemories") && memories[i]["relatedMemories"] != null && Array.isArray(memories[i]["relatedMemories"]) && memories[i]["relatedMemories"].length > 0) {
related_memories = memories[i]["relatedMemories"];
$("#memory_" + memories[i]["id"]).append(await render_related_memories(related_memories, 3));
}
} // end for each memory
}
async function render_related_memories(related_memories, limit) {
var output = `
<div class="card-footer" style="margin-bottom: 1%;">
<p class="card-text"><small class="text-muted"><span><i class="fa fa-picture-o"></i></span>Related Memories</small></p>
<div class="related_memories">
<div class="tb">
<div class="tr">
`;
for (let r = 0; r < related_memories.length; r++) {
if(r < limit) {
var rm_subject = related_memories[r]["subject"];
var rm_imageData = null;
var rm_card_image = null;
rm_keys = Object.keys(related_memories[r]);
if (rm_keys.includes("image_urls") && (related_memories[r]["image_urls"] != null || related_memories[r]["image_urls"] != "")) {
if (Array.isArray(related_memories[r]["image_urls"]) && related_memories[r]["image_urls"].length > 0) rm_card_image = related_memories[r]["image_urls"][0];
if (typeof related_memories[r]["image_urls"] === "string") rm_card_image = related_memories[r]["image_urls"].split(",")[0]; // this safeguards against comma separated string (until I'm able to store it correctly)
if (rm_card_image) {
rm_subject = related_memories[r]["subject"];
if (related_memories[r]["id"]) {
output +=
`<div class="td" style="background-image: url('${rm_card_image}');position:relative" onclick="display_memory_modal('${related_memories[r]["id"]}')">
<div style="top:0;left:0;width:100%; height:100%; background-color:#000000;opacity:0.6;position:absolute;color:#fff;padding: 2em;font-size: 0.8em;text-align: center;cursor:pointer">
<div class="truncated-v">${rm_subject}</div>...
</div>
</div>`
}
}
}
if (related_memories[r]["image_urls"] == null || related_memories[r]["image_urls"] == "" || (Array.isArray(related_memories[r]["image_urls"]) && related_memories[r]["image_urls"].length == 0)) {
if (related_memories[r]["id"]) {
output +=
`<div class="td" style="position:relative">
<div style="top:0;left:0;width:100%; height:100%; background-color:#000000;opacity:0.6;position:absolute;color:#fff;padding: 2em;font-size: 0.8em;text-align: center;cursor:pointer" onclick="display_memory_modal('${related_memories[r]["id"]}')">
<div class="truncated-v">${rm_subject}</div>...
</div>
</div>`;
}
}
}
}
output += `</div></div></div></div></div>`;
return output;
}
async function ask_tobu() {
//close this modal
$("#ask_tobu_modal").modal("hide");
//clear memory feed when ask_tobu modal closes so that it doesn't append to all memories
$("#all_memories").html("");
await display_memory_feed();
}
function display_askTobu_alert(num_memories, query_value) {
if (num_memories > 0) {
// display alert
$("#ask_tobu_alert").html(`
Showing results for: '${query_value}'
<button type="button" class="close" data-dismiss="alert" aria-label="Close" onclick="display_memory_feed()">
<span aria-hidden="true">×</span>
</button>
`);
//document.getElementById("ask_tobu_alert").style.display = "block";
} else {
$("#ask_tobu_alert").html(`
No results found for: '${query_value}'; displaying all memories
<button type="button" class="close" data-dismiss="alert" aria-label="Close" onclick="display_memory_feed()">
<span aria-hidden="true">×</span>
</button>
`);
//document.getElementById("ask_tobu_alert").style.display = "block";
}
// clear query field after displaying alert
var query_inputField = document.getElementById("query__inputField");
query_inputField.value = "";
}
//displays the capture a memory modal
async function display_capture_modal() {
await walker_yield_clear().then((result) => {
$("#createMemoryModal").modal({backdrop: 'static', keyboard: false});
$("#createMemoryModal").modal("show");
walker_run_talk("talk", "Document a memory", create_memory_images, current_file_url).then((result) => {
chat_messages.push(["bot", result.report[0]["response"]]);
readOutLoud(result.report[0]["response"]);
update_messages();
})
.catch(function (error) {
console.log(error);
});
})
.catch(function (error) {
console.log(error);
});
}
function close_capture_modal_after_speaking() {
setTimeout(function () {
if(!window.speechSynthesis.speaking) {
$("#createMemoryModal").modal("hide");
reset_capture_modal();
display_memory_feed();
} else close_capture_modal_after_speaking();
}, 1000);
}
function reset_capture_modal() {
shutUp();
//reset the conver.sation
chat_messages = [];
document.getElementById("chatbox").innerHTML = ""
$("#photos").html(``); // resets photos
$("#photos").hide();
$("#create_fileId").val(""); // clear input field
create_memory_images = [];
upload_ids = [];
}
async function get_photo_url(memory_photo_ids){
$("#edit_photos").html(``);
current_memory_photos = [];
for (let p = 0; p < memory_photo_ids.length; p++) {
console.log(memory_photo_ids[p]);
await walker_get_file(memory_photo_ids[p]).then((result) => {
current_memory_photos.push(result.report[0][0]["context"]["url"]);
display_memory_photos(memory.id, memory_photo_ids, current_memory_photos);
$("#edit_fileId").val(""); // clear input field
});
}
}
//displays the detailed modal of the memory
async function display_memory_modal(id) {
memory = [];
current_memory_photos = [];
file_upload = false;
edit_memory_ids = [];
// always show detailed view; hide edit view when memory modal is being displayed
$("#memoryModal_edit").hide();
$("#memoryModal_details").show();
//clear
$("#edit_photos").html(``);
$("#memoryModal_title").text("");
$("#memoryModal_subject").text("");
$("#memoryModal_date").text("");
$("#memoryModal_where").text("");
$("#memoryModal_description").text("");
$("#memoryModal_how").html("");
$("#memoryModal_related_memories").html("");
$("#memoryModal_lastUpdated").text("");
$("#memoryModal_image").html(" ");
$("#memoryModal_people").html(``);
$("#memoryModal_btn_narrate").off("click");
$("#memoryModal_btn_edit").off("click");
$("#memoryModal_btn_delete").off("click");
showLoader(true);
$("#memoryModal").modal({backdrop: 'static', keyboard: false});
$("#memoryModal").modal("show");
await walker_get_memory(id).then(async (result) => {
memory = result.report[0];
current_memory_id = id;
if ((memory["image_urls"] != null || memory["image_urls"] != "")) {
var imageUrl = null;
var memory_card_image = null;
if (Array.isArray(memory["image_urls"]) && memory["image_urls"].length > 0) memory_card_image = memory["image_urls"][0];
if (typeof memory["image_urls"] === "string") memory_card_image = memory["image_urls"].split(",")[0]; // this safeguards against comma separated string (until I'm able to store it correctly)
if (memory_card_image) {
$("#memoryModal_image").html(
`<img src="${memory_card_image}" class="card-img-top" alt="...">`
);
}
//edit_memory_ids = memory.file_ids;
get_photo_url(edit_memory_ids);
} else {
$("#memoryModal_image").html(" ");
}
console.log(memory);
$("#memoryModal_title").text(memory.when);
$("#memoryModal_subject").text(memory.subject);
$("#memoryModal_date").text(memory.when);
$("#memoryModal_where").text(memory.where);
$("#memoryModal_description").text(memory.description);
if (memory.how == "") {
$("#memoryModal_how").html(`<i class="fa ${emotions["default"][0]}" style="color: ${emotions["default"][1]};"></i>`);
} else {
$("#memoryModal_how").html(`<i class="fa ${emotions[memory.how][0]}" style="color: ${emotions[memory.how][1]};"></i>`);
}
persons = [];
for (let p = 0; p < memory.who.length; p++) {
persons.push(memory.who[p]["context"]["name"]);
}
if(persons.length > 0){
$("#memoryModal_people").html(`<span><i class="fas fa-user" style="padding-left: 2%;"></i></span>${persons.toString()}`);
} else{
$("#memoryModal_people").html(``);
}
$("#memoryModal_lastUpdated").text(`Last updated on ${memory.date_modified.replace("T", " ").substring(0, memory.date_modified.lastIndexOf("."))}`);
if(memory.relatedMemories.length > 0) $("#memoryModal_related_memories").html(await render_related_memories(memory.relatedMemories, 10));
$("#memoryModal_btn_narrate").on("click", function () {
readOutLoud(memory.summary);
});
$("#memoryModal_btn_edit").on("click", function () {
$("#edit_fileId").val(""); // clear input field
$("#memoryModal_title").text("Edit Memory");
$('input[name="memory_who"]').amsifySuggestags({
suggestions: persons,
});
document.getElementById("memory_subject").value = memory.subject;
// document.getElementById("memory_category").value = memory.category;
document.getElementById("memory_description").value = memory.description;
document.getElementById("memory_summary").value = memory.summary;
document.getElementById("memory_when").value = memory.when;
document.getElementById("memory_where").value = memory.where;
document.getElementById("memory_how").value = memory.how;
document.getElementById("memory_who").value = persons.toString();
$('input[name="memory_who"]').amsifySuggestags();
$("#memoryModal_edit").show();
$("#memoryModal_details").hide();
});
$("#memoryModal_btn_delete").on("click", function () {
walker_delete_memory(memory.id).then((result) => {
//close this modal
$("#memoryModal").modal("hide");
setTimeout(function () {
//resets feed and hide ask_tobu alert
display_memory_feed();
document.getElementById("ask_tobu_alert").style.display = "none";
}, 1500);
})
.catch(function (error) {
console.log(error);
});
});
showLoader(false);
})
.catch(function (error) {
showLoader(false);
console.log(error);
});
}
function display_memory_photos(memory_id, memory_file_ids, memory_photos) {
console.log(memory_file_ids);
d_memory_photos = ``;
for (let ph = 0; ph < memory_photos.length; ph++) {
d_memory_photos =
d_memory_photos +
`<div><i class="fas fa-times" style="padding-right: 20px; margin-bottom: 10px;" onclick="delete_memory_photo('${memory_id}', '${memory_file_ids[ph]}', '${memory_file_ids}')"></i><img src="${memory_photos[ph]}" style="height: 200px;"></div>`;
// display photos above edit form
$("#edit_photos").html(`<div class="tb"><div class="tr">${d_memory_photos}</div></div>`);
}
}
function delete_memory_photo(memory_id, photo_id, memory_file_ids) {
console.log(memory_file_ids);
memory_file_ids = memory_file_ids.split(",");
console.log(memory_file_ids);
if (Array.isArray(memory_file_ids)) {
const delete_index = memory_file_ids.indexOf(photo_id);
if (delete_index > -1) memory_file_ids.splice(delete_index, 1);
}
console.log(memory_file_ids);
edit_memory_ids = memory_file_ids;
update_file_id(memory_id, edit_memory_ids).then((result) => {
get_photo_url(edit_memory_ids);
})
.catch(function (error) {
console.log(error);
});
// walker_delete_file(photo_id).then((result) => {})
// .catch(function (error) {
// console.log(error);
// });
}
function close_edit_modal() {
document.getElementById("memoryModal_edit").style.display = "none";
document.getElementById("memoryModal_details").style.display = "block";
$("#memoryModal").modal("hide");
// hides ask_tobu alert
document.getElementById("ask_tobu_alert").style.display = "none";
// display_memory_feed();
}
function save_memory_details() {
walker_update_memory(memory.id, edit_memory_ids, current_file_url);
console.log(who_selected);
setTimeout(function () {
close_edit_modal();
display_memory_feed();
}, 1500);
}
function readOutLoud(message) {
speech.text = message;
window.speechSynthesis.speak(speech);
}
function shutUp() {
window.speechSynthesis.cancel();
}
function showLoader(show = true) {
if(show){
$("#glass_case").show();
$('.loader').show();
}
else{
$("#glass_case").hide();
$('.loader').hide();
}
}
var speechRecognition = window.webkitSpeechRecognition;
// ----------------- Chat Microphone --------------------------
var chat_inputField = document.getElementById("chatio__inputField");
var chat_recognition = new speechRecognition();
var chat_textbox = $("#chatio__inputField");
var chat_content = "";
var chat_finalTranscripts = "";
chat_recognition.continuous = true;
chat_recognition.interimResults = true;
chat_stat = true;
chat_textbox.keyup(function(event) {
if (event.which === 13) {
event.preventDefault();
sendMessage();
}
});
chat_recognition.onstart = function () {
console.log("Recording start");
chat_finalTranscripts = "";
document.getElementById("chat_mic-btn").style.color = "#ffffff";
document.getElementById("chat_mic-bg").style.color = "#365d96";
};
chat_recognition.onend = function () {
console.log("Recording end");
sendMessage();
document.getElementById("chat_mic-btn").style.color = "#000000";
document.getElementById("chat_mic-bg").style.color = "#ffffff";
chat_content = "";
chat_textbox.val(chat_content);
chat_stat = true;
};
chat_recognition.onresult = function (event) {
// var chat_current = event.resultIndex;
// var chat_transcript = event.results[chat_current][0].transcript;
// chat_content += chat_transcript;
// chat_textbox.val(chat_content);
var chat_interimTranscripts = "";
for(var i=event.resultIndex; i<event.results.length; i++){
var chat_transcript = event.results[i][0].transcript;
chat_transcript.replace("\n", "<br>");
if(event.results[i].isFinal){
chat_finalTranscripts += chat_transcript;
chat_textbox.val(chat_finalTranscripts);
}
else{
chat_interimTranscripts += chat_transcript;
chat_textbox.val(chat_finalTranscripts + chat_interimTranscripts);
}
}
};
function chat_mic_click() {
if (chat_stat) {
if (chat_content.length) {
chat_content += "";
}
chat_recognition.continuous = true;
chat_recognition.start();
chat_stat = false;
} else {
chat_recognition.stop();
}
}
// ----------------- Chat Microphone --------------------------
// ----------------- Query Microphone --------------------------
var query_inputField = document.getElementById("query__inputField");
var query_recognition = new speechRecognition();
var query_textbox = $("#query__inputField");
var query_content = "";
var query_finalTranscripts = "";
query_recognition.continuous = true;
query_recognition.interimResults = true;
query_stat = true;
query_recognition.onstart = function () {
console.log("Recording start");
query_finalTranscripts = "";
query_textbox.val("");
document.getElementById("query_mic-btn").style.color = "#ffffff";
document.getElementById("query_mic-bg").style.color = "#365d96";
};
query_recognition.onend = async function () {
console.log("Recording end");
await ask_tobu();
document.getElementById("query_mic-btn").style.color = "#000000";
document.getElementById("query_mic-bg").style.color = "#ffffff";
query_content = "";
query_stat = true;
};
query_recognition.onresult = function (event) {
// var query_current = event.resultIndex;
// var query_transcript = event.results[query_current][0].transcript;
// query_content += query_transcript;
// query_textbox.val(query_content);
var query_interimTranscripts = "";
for(var i=event.resultIndex; i<event.results.length; i++){
var query_transcript = event.results[i][0].transcript;
query_transcript.replace("\n", "<br>");
if(event.results[i].isFinal){
query_finalTranscripts += query_transcript;
query_textbox.val(query_finalTranscripts);
}
else{
query_interimTranscripts += query_transcript;
query_textbox.val(query_finalTranscripts + query_interimTranscripts);
}
}
};
function query_mic_click() {
if (query_stat) {
if (query_content.length) {
query_content += "";
}
query_recognition.continuous = true;
query_recognition.start();
query_stat = false;
} else {
query_recognition.stop();
}
}
// ----------------- Query Microphone --------------------------
function update_messages() {
conv = "";
for (let i = 0; i < chat_messages.length; i++) {
if (chat_messages[i][0] == "bot") {
//evaluate whether its the final message to close dialog
if( (chat_messages[i][1]).includes("I've recorded that")) {
console.log("dialog ended");
close_capture_modal_after_speaking();
}
new_message =
'<p class="botText"><span>' + chat_messages[i][1] + "</span></p>";
} else {
new_message =
'<p class="userText"><span>' + chat_messages[i][1] + "</span></p>";
}
conv = conv + new_message;
}
document.getElementById("chatbox").innerHTML = conv;
chat_inputField.value = "";
}
function sendMessage() {
var utterance = chat_inputField.value;
if (utterance.trim()) {
chat_messages.push(["user", utterance]);
update_messages();
walker_run_talk("talk", utterance, create_memory_images, current_file_url)
.then((result) => {
chat_messages.push(["bot", result.report[0]["response"]]);
readOutLoud(result.report[0]["response"]);
update_messages();
})
.catch(function (error) {
console.log(error);
});
}
}
async function uploadImage(file) {
const cloudName = "dlwqeqp9i";
const body = new FormData();
body.append("file", file);
body.append("upload_preset", "tobuapp");
const result = await fetch(
`https://api.cloudinary.com/v1_1/${cloudName}/upload`,
{
method: "POST",
body,
}
).then((res) => res.json());
return result;
}
async function imageUploaded() {
var base64String = "";
var file = document.querySelector("#create_image_input input[type=file]")["files"][0];
const result = await uploadImage(file).catch((err) => console.log(err));
const imageUrl = result.url;
console.log({ imageUrl });
file_upload = true;