-
Notifications
You must be signed in to change notification settings - Fork 0
/
scratchat.js
90 lines (75 loc) · 2.66 KB
/
scratchat.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
var statusFlag = 0;
var workspace = Blockly.inject('blocklyDiv',{toolbox: document.getElementById('toolbox')});
workspace.addChangeListener(function(event) {
document.getElementById('textarea').innerHTML = Blockly.JavaScript.workspaceToCode(workspace);
});
var me = {};
me.avatar = "res/avatar.png";
var you = {};
you.avatar = "res/avatar.png";
function formatAMPM(date) {
var hours = date.getHours();
var minutes = date.getMinutes();
var ampm = hours >= 12 ? 'PM' : 'AM';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0'+minutes : minutes;
var strTime = hours + ':' + minutes + ' ' + ampm;
return strTime;
}
//-- No use time. It is a javaScript effect.
function insertChat(who, text, time = 0){
var control = "";
var date = formatAMPM(new Date());
if (who == "you"){
control = '<li style="width:100%">' +
'<div class="msj macro">' +
'<div class="avatar"><img class="img-circle" style="width:100%;" src="'+ me.avatar +'" /></div>' +
'<div class="text text-l">' +
'<p>'+ text +'</p>' +
'<p><small>'+date+'</small></p>' +
'</div>' +
'</div>' +
'</li>';
}else{
control = '<li style="width:100%;">' +
'<div class="msj-rta macro">' +
'<div class="text text-r">' +
'<p>'+text+'</p>' +
'<p><small>'+date+'</small></p>' +
'</div>' +
'<div class="avatar" style="padding:0px 0px 0px 10px !important"><img class="img-circle" style="width:100%;" src="'+you.avatar+'" /></div>' +
'</li>';
}
setTimeout(
function(){
$("ul").append(control);
}, time);
}
function testScript() {
resetChat();
window.alert("test ready");
$("#test").html('<script>\
var controlLogic = function(text) {'
+ document.getElementById('textarea').innerHTML +
'}</script>');
}
function resetChat(){
$("ul").empty();
}
$(".mytext").on("keyup", function(e){
if (e.which == 13){
var text = $(this).val();
if (text !== ""){
insertChat("me", text);
controlLogic(text);
$(this).val('');
}
}
});
//-- Clear Chat
resetChat();
// //-- Print Messages
insertChat("you", "Hello, welcome to Scratchat", 2000);
insertChat("you", "How can i help you today?", 2700);
//-- NOTE: No use time on insertChat.