-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathapp.js
executable file
·224 lines (178 loc) · 5.66 KB
/
app.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
// Override Meteor._debug to filter for custom msgs
Meteor._debug = (function (super_meteor_debug) {
return function (error, info) {
if (!(info && _.has(info, 'msg')))
super_meteor_debug(error, info);
}
})(Meteor._debug);
var nick = new ReactiveVar();
var room = new ReactiveVar('lobby');
// Add a local only collection to manage messages
Messages = new Mongo.Collection(null);
// -------------------------------------------------------------------------- //
// -------------------------------- Handlers -------------------------------- //
// -------------------------------------------------------------------------- //
/**
* Try to retrieve a client by its nickname
* @param {String} nick Nickname to look for
* @return {Client} Client object or null|undefined if not found
*/
function findClient(nick) {
return Clients.findOne({ 'nick': nick});
}
/**
* Generic method to insert a message in the chat panel
* @param {String} room Room name concerned
* @param {String} body Body message
* @param {String} from Session id of the sender
*/
function insertMessage(room, body, from) {
// Do nothing if not logged in
if(!nick.get())
return;
var c = from ? Clients.findOne({ 'sid': from }): null;
if(from && !c)
c = { 'nick': from };
Messages.insert({
'room': room,
'body': body,
'from': c && c.nick
});
$('.chat__messages').scrollTo($('li.chat__messages__item:last'));
}
// On connected, subscribe to collections
Streamy.onConnect(function() {
Meteor.subscribe('clients');
Meteor.subscribe('rooms', Streamy.id());
});
// On disconnect, reset nick name
Streamy.onDisconnect(function() {
nick.set('');
Messages.remove({});
});
Streamy.on('nick_ack', function(data) {
nick.set(data.nick);
});
// On a lobby message, insert the message
Streamy.on('lobby', function(data) {
insertMessage('lobby', data.body, data.__from);
});
// More generic, when receiving from a room this message, insert it
Streamy.on('text', function(data) {
insertMessage(data.__in.toLowerCase(), data.body, data.__from);
});
// On private message
Streamy.on('private', function(data) {
insertMessage(null, data.body, data.__from);
});
// Someone has joined
Streamy.on('__join__', function(data) {
// Dismiss if self
if(data.sid === Streamy.id())
return;
var c = Clients.findOne({ 'sid': data.sid });
var msg = ((c && c.nick) || "Someone") + " has joined";
insertMessage(data.room.toLowerCase(), msg);
});
// Someone has left
Streamy.on('__leave__', function(data) {
var c = Clients.findOne({ 'sid': data.sid });
var msg = ((c && c.nick) || 'Someone') + " has left";
insertMessage(data.room.toLowerCase(), msg);
});
Template.NickChoice.events({
'submit': function(evt, tpl) {
if(evt.preventDefault) evt.preventDefault();
var val = tpl.$('#nickname').val();
if(val)
Streamy.emit('nick_set', { 'handle': val });
}
});
function resizeChatZone() {
$('.chat__messages').css('height', $(window).outerHeight() - ($('.chat__input').outerHeight() * 1.5));
}
Template.App.rendered = function() {
this.$('.chat__message').focus();
$(window).resize(resizeChatZone);
$(window).resize();
};
Template.App.events({
'click .chat__messages__nick': function(evt) {
var to = $(evt.target).text();
$('.chat__message').val('to:' + to + ': ');
$('.chat__message').focus();
},
'submit .chat__input': function(evt, tpl) {
if(evt.preventDefault) evt.preventDefault();
var $ele = tpl.$('.chat__message');
var val = $ele.val();
if(!val)
return;
// Check if its a direct message
if(val.indexOf('to:') === 0) {
var end = val.indexOf(':', 3);
to = findClient(val.substring(3, end));
val = val.substring(end + 1).trim();
if(!to)
return;
Streamy.sessions(to && to.sid).emit('private', {
body: val
});
// And insert the local message
insertMessage(null, val, 'to: ' + to.nick);
}
else {
var current_room = room.get();
// Sends the message, using the broadcast or rooms feature
if(current_room === 'lobby')
Streamy.broadcast('lobby', { 'body': val });
else
Streamy.rooms(current_room).emit('text', { 'body': val });
}
$ele.val('');
},
'submit .create_or_join': function(evt, tpl) {
if(evt.preventDefault) evt.preventDefault();
var $ele = tpl.$('#room__input');
var val = $ele.val();
if(!val)
return;
// Join the room
Streamy.join(val.toLowerCase());
// And switch to it
room.set(val.toLowerCase());
$ele.val('');
},
'click .rooms__list__joinable': function(evt) {
room.set(evt.target.innerText.toLowerCase());
},
'click .rooms__list__item__leave': function(evt) {
if(evt.preventDefault) evt.preventDefault();
var room_name = $(evt.target).prev().text();
Streamy.leave(room_name);
Messages.remove({ 'room': room_name }); // Remove messages from this room
room.set('lobby');
return false;
}
});
Template.App.helpers({
selectedClass: function(room_name) {
var current_room = room.get();
return (current_room === room_name.toLowerCase()) && 'rooms__list__item_active';
},
messages: function() {
var current_room = room.get();
return Messages.find({
$or: [
{ 'room': current_room },
{ 'room': null } // Direct messages
]
});
},
rooms: function() {
return Streamy.rooms();
}
});
Template.registerHelper('nick', function() {
return nick.get();
});