-
Notifications
You must be signed in to change notification settings - Fork 2
/
jQuery.openTokChat.js
151 lines (117 loc) · 3.8 KB
/
jQuery.openTokChat.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
(function ( $ ) {
var $self;
var $chatBox;
var $inputBox;
var $sendButton;
var $chatMessages;
var $chatInput;
var $chatSendButton;
var openSession;
var openTokChatSettings;
$.fn.openTokChat = function( command, options ) {
if(command == "init")
{
$self = this;
// save default settings.
openTokChatSettings = $.extend({
width: "640",
height:"480",
user:'Anonymous',
session: null,
onStart: function () {}
}, options );
openSession = openTokChatSettings.session;
openSession.on('sessionConnected', sessionConnectedHandler);
openSession.on("signal:chat", signalReceived)
openSession.on("sessionDisconnected", sessionDisconnectedHandler);
drawChat(); //start up all the other js we need
addSystemMessage("Component initialized");
openTokChatSettings.onStart(this);
return this;
}
};
function drawChat () {
// $self.text("esta");
$self.addClass("openToKChatMain");
$chatBox = $("<div>").addClass("messageContainer");
$chatBox.css("width",openTokChatSettings.width);
$chatBox.css("height",openTokChatSettings.height);
$chatMessages = $("<ul>").addClass("chatMessages");
$chatMessages.css("height",$chatBox.height());
$chatMessages.css("max-height",$chatBox.height());
$chatMessages.css("min-height",$chatBox.height());
// $chatMessages.css("height",)
$chatMessages.css("scroll","overwflow");
$chatBox.append($chatMessages);
$chatInput = $("<input>").attr("type","text").attr("class","messageInput");
$chatInput.css("width", $chatBox.width() - 50);
$chatInput.css("height","30px");
$chatInput.css("padding-top","0px");
$chatSendButton = $("<input>").attr("type","button").attr("value","Send").addClass('btn btn-success btn-sm');
$chatSendButton.click(function () {
sendMessage();
})
$chatInput.keypress(function(e) {
if(e.which == 13) {
sendMessage();
}
});
$chatInput.attr("disabled",true);
$chatSendButton.attr("disabled",true)
$self.append($chatBox);
$self.append($chatInput);
$self.append($chatSendButton);
}
function sendMessage () {
var msg = $chatInput.val();
if(msg == "")
return;
else {
$chatInput.val("");
}
var data = new Object();
data.msg = msg;
data.user = openTokChatSettings.user;
openSession.signal({type:"chat",data:JSON.stringify(data)})
}
function addUserLine(data) {
main = $("<span>");
obj = $("<span>").addClass("userName");
obj.text(data.user + ": ");
main.append(obj);
obj2 = $("<span>").addClass("userMessage");
obj2.text(strip(data.msg));
main.append(obj2);
addNewLine(main.html());
}
function addNewLine(html) {
new_il = $("<li></li>");
new_il.html(html);
$chatMessages.append(new_il);
$chatMessages.scrollTop(999999*9999999999);
}
function signalReceived(event) {
if(event.type == "signal:chat") {
data = $.parseJSON(event.data);
addUserLine(data);
}
}
function sessionConnectedHandler (event) {
$("#id_open_myllamada").text("Transmitir").removeClass("disabled");
addSystemMessage("Chat session connected.");
$chatInput.removeAttr("disabled");
$chatSendButton.removeAttr("disabled");
}
function sessionDisconnectedHandler (event) {
addSystemMessage("You have been disconected from the server.");
$chatInput.attr("disabled",true);
$chatSendButton.attr("disabled",true)
}
function addSystemMessage(text) {
addNewLine("<span class='systemMessage'>"+strip(text)+"</span>");
}
function strip(html)
{
return $("<div>").text(html).html();
}
}( jQuery ));