-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
148 lines (116 loc) · 3.22 KB
/
main.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
(function($){
$(function(){
var network = Clank.connect(_CLANK_URI);
network.on("socket/connect", function(session){
$("#connected").html("Connected");
bindUi(session);
});
network.on("socket/disconnect", function(session){
$("#connected").html("Disconnected");
unbindUi();
});
})
})(jQuery)
/**
* Below is the Clank relevant functions, the rest of this file is UI Bindings etc.
*/
/**
* This will do a RPC to change this connections nickname
* @param session
*/
function changeNickname(session)
{
session.call("chat/change_nickname", {nickname: $("#nickname > input").val()})
.then(function(result){
$("#nickname > a").html("Done");
setTimeout(function(){
$("#nickname > a").html("Change");
}, 5000);
}, function(error){
$("#nickname > a").html("Failed");
setTimeout(function(){
$("#nickname > a").html("Change");
}, 5000);
});
}
function subscribeToRoom(session, room)
{
session.subscribe(_room, function(uri, payload){
appendChat(payload.from, payload.msg);
});
}
function publishChat(session)
{
if (!_room)
return;
var msg = $("#chat-input").val();
$("#chat-input").val("");
session.publish(_room, msg);
}
/**
* This would be better off written with a proper javascript framework, like BackBone.js
*
*
*
*/
var _room = null;
function joinChatRoom(session)
{
if (_room)
{
session.unsubscribe(_room); //ensure they are only in 1 room at a time
}
var roomName = $("#chatroom").val();
$("#chat-title").html(roomName);
$("#chat-pane").find("ul").html("");
_room = "chat/" + $("#chatroom").val(); //set the room URI
subscribeToRoom(session, _room);
}
function appendChat(from, msg)
{
$("#chat-pane").find("ul").append("<li>"+from+": " + msg + "</li>");
$("#chat-pane").scrollTop($('#chat-pane').get(0).scrollHeight);
}
function bindUi(session)
{
$("#join-chat").bind("click", function(e){
e.preventDefault();
joinChatRoom(session);
});
$("#chatroom").bind("keypress", function(e){
var code = (e.keyCode ? e.keyCode : e.which);
if(code == 13) { //Enter keycode
joinChatRoom(session);
}
});
$("#send-chat").bind("click", function(e){
e.preventDefault();
publishChat(session)
});
$("#chat-input").bind("keypress", function(e){
var code = (e.keyCode ? e.keyCode : e.which);
if(code == 13) { //Enter keycode
publishChat(session);
}
});
$("#nickname > input").bind("keypress", function(e){
var code = (e.keyCode ? e.keyCode : e.which);
if(code == 13) { //Enter keycode
changeNickname(session);
}
});
$("#nickname > a").bind("click", function(e){
e.preventDefault();
changeNickname(session);
});
}
// attempt to clean up on disconnect.
function unbindUi()
{
$("#join-chat").unbind();
$("#chatroom").unbind();
$("#send-chat").unbind();
$("#chat-input").unbind();
$("#nickname > input").unbind();
$("#nickname > a").unbind();
}