forked from akavlie/web-irc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
542 lines (458 loc) · 16 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
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
$(function() {
// Our global object
window.irc = window.irc || {};
// socket.io init
var socket = io.connect('http://localhost');
// MODELS & COLLECTIONS
// ====================
var Message = Backbone.Model.extend({
defaults: {
// expected properties:
// - sender
// - text
'type': 'message'
},
// Set output text for status messages
setText: function() {
var text = '';
switch (this.get('type')) {
case 'join':
text = this.get('nick') + ' joined the channel';
break;
case 'part':
text = this.get('nick') + ' left the channel';
break;
case 'nick':
text = this.get('oldNick') + ' is now known as ' + this.get('newNick');
break;
}
this.set({text: text});
}
});
var Stream = Backbone.Collection.extend({
model: Message
});
var Person = Backbone.Model.extend({
defaults: {
opStatus: ''
}
});
var Participants = Backbone.Collection.extend({
model: Person,
getByNick: function(nick) {
return this.detect(function(person) {
return person.get('nick') == nick;
});
}
});
var Frame = Backbone.Model.extend({
// expected properties:
// - name
defaults: {
'type': 'channel',
'active': true
},
initialize: function() {
this.stream = new Stream;
this.participants = new Participants;
},
part: function() {
console.log('Leaving ' + this.get('name'));
this.destroy();
}
});
var FrameList = Backbone.Collection.extend({
model: Frame,
getByName: function(name) {
return this.detect(function(frame) {
return frame.get('name') == name;
});
},
getActive: function() {
return this.detect(function(frame) {
return frame.get('active') == true;
});
},
setActive: function(frame) {
this.each(function(frm) {
frm.set({active: false});
});
frame.set({active: true});
},
getChannels: function() {
return this.filter(function(frame) {
return frame.get('type') == 'channel';
});
}
});
// hoisted to window for now, for ease of debugging
window.frames = new FrameList;
// VIEWS
// =====
var MessageView = Backbone.View.extend({
tmpl: $('#message-tmpl').html(),
initialize: function() {
this.render();
},
render: function() {
var context = {
sender: this.model.get('sender'),
text: this.model.get('text')
};
var html = Mustache.to_html(this.tmpl, context);
$(this.el).addClass(this.model.get('type'))
.html(html);
return this;
}
});
// Nick in the sidebar
var NickListView = Backbone.View.extend({
el: $('.nicks'),
initialize: function() {
_.bindAll(this);
},
// this is a temp. hack
tmpl: function(opStatus, nick) {
return '<div>' + opStatus + ' ' + nick + '</div>'
},
switchChannel: function(ch) {
ch.participants.bind('add', this.addOne, this);
ch.participants.bind('change', this.changeNick, this);
},
addOne: function(p) {
var text = this.tmpl(p.get('opStatus'), p.get('nick'));
$(this.el).append(text);
},
addAll: function(participants) {
var self = this;
var nicks = [];
participants.each(function(p) {
var text = self.tmpl(p.get('opStatus'), p.get('nick'));
nicks.push(text);
});
$(this.el).html(nicks.join('\n'));
},
changeNick: function() {
console.log('Change of nick seen');
console.log(arguments);
}
});
var nickList = new NickListView;
var FrameView = Backbone.View.extend({
el: $('#frame'),
// to track scroll position
position: {},
initialize: function() {
_.bindAll(this);
},
addMessage: function(message, single) {
// Expensive -- only do this on single message additions
if (single) {
var position = $('#output').scrollTop();
atBottom = $('#output')[0].scrollHeight - position
== $('#output').innerHeight();
var position = this.$('#output').scrollTop();
}
var view = new MessageView({model: message});
$('#output').append(view.el);
// Scroll to bottom on new message if already at bottom
if (atBottom) {
$('#output').scrollTop(position + 100);
}
},
// Switch focus to a different frame
focus: function(frame) {
// Save scroll position for frame before switching
if (this.focused) {
this.position[this.focused.get('name')] = this.$('#output').scrollTop();
}
this.focused = frame;
frames.setActive(this.focused);
this.$('#output').empty();
var self = this;
frame.stream.each(function(message) {
self.addMessage(message, false);
});
nickList.addAll(frame.participants);
if (frame.get('type') == 'channel')
this.$('.nicks').show();
else
this.$('.nicks').hide();
$(this.el).removeClass().addClass(frame.get('type'));
this.$('#output').scrollTop(this.position[frame.get('name')] || 0);
// Only the selected frame should send messages
frames.each(function(frm) {
frm.stream.unbind('add');
frm.participants.unbind();
});
frame.stream.bind('add', this.addMessage, this);
nickList.switchChannel(frame);
},
updateNicks: function(model, nicks) {
console.log('Nicks rendered');
}
});
var FrameTabView = Backbone.View.extend({
tagName: 'li',
tmpl: $('#tab-tmpl').html(),
initialize: function() {
this.model.bind('destroy', this.close, this);
this.render();
},
events: {
'click': 'setActive',
'click .close-frame': 'close'
},
// Send PART command to server
part: function() {
if (this.model.get('type') === 'channel') {
socket.emit('part', this.model.get('name'));
} else {
// PMs don't need an explicit PART
this.model.destroy();
}
},
// Close frame
close: function() {
// Focus on next frame if this one has the focus
if ($(this.el).hasClass('active')) {
// Go to previous frame unless it's status
if ($(this.el).prev().text().trim() !== 'status') {
$(this.el).prev().click();
} else {
$(this.el).next().click();
}
}
$(this.el).remove();
},
// Set as active tab; focus window on frame
setActive: function() {
console.log('View setting active status');
$(this.el).addClass('active')
.siblings().removeClass('active');
irc.frameWindow.focus(this.model);
},
render: function() {
console.log(this.model);
var self = this;
var context = {
text: this.model.get('name'),
type: this.model.get('type'),
isStatus: function() {
return self.model.get('type') == 'status';
}
};
var html = Mustache.to_html(this.tmpl, context);
$(this.el).html(html);
return this;
}
});
var AppView = Backbone.View.extend({
el: $('#content'),
testFrames: $('#sidebar .frames'),
frameList: $('header .frames'),
initialize: function() {
frames.bind('add', this.addTab, this);
this.input = this.$('#prime-input');
this.render();
},
events: {
'keypress #prime-input': 'sendInput',
},
addTab: function(frame) {
var tab = new FrameTabView({model: frame});
this.frameList.append(tab.el);
tab.setActive();
},
joinChannel: function(name) {
socket.emit('join', name);
},
// Map common IRC commands to standard (RFC 1459)
parse: function(text) {
var command = text.split(' ')[0];
console.log(command);
var revised = '';
switch (command) {
case 'msg':
revised = 'privmsg';
break;
default:
revised = command;
break;
}
return irc.utils.swapCommand(command, revised, text);
},
sendInput: function(e) {
if (e.keyCode != 13) return;
var frame = irc.frameWindow.focused,
input = this.input.val();
if (input.indexOf('/') === 0) {
console.log('IRC command detected -- sending to server');
var parsed = this.parse(input.substr(1))
socket.emit('command', parsed);
// special case -- no output emitted, yet we want a new frame
var msgParts = parsed.split(' ');
if (msgParts[0].toLowerCase() === 'privmsg') {
pm = frames.getByName(msgParts[1]) || new Frame({type: 'pm', name: msg.nick});
pm.stream.add({sender: msg.nick, text: msg.text})
frames.add(pm);
}
} else {
socket.emit('say', {
target: frame.get('name'),
message: input
});
frame.stream.add({sender: irc.me.get('nick'), text: input});
}
this.input.val('');
},
render: function() {
// Dynamically assign height
this.el.show();
$(window).resize(function() {
sizeContent($('#frame #output'));
sizeContent($('#frame .nicks'));
});
}
});
var ConnectView = Backbone.View.extend({
el: $('#connect'),
events: {
'click .btn': 'connect',
'keypress': 'connectOnEnter'
},
initialize: function() {
_.bindAll(this);
this.render();
},
render: function() {
this.el.modal({backdrop: true, show: true});
$('#connect-nick').focus();
},
connectOnEnter: function(e) {
if (e.keyCode != 13) return;
this.connect();
},
connect: function(e) {
e && e.preventDefault();
var connectInfo = {
nick: $('#connect-nick').val(),
server: $('#connect-server').val(),
channels: $('#connect-channels').val().split(' ')
};
socket.emit('connect', connectInfo);
$('#connect').modal('hide');
irc.me = new Person({nick: connectInfo.nick});
irc.frameWindow = new FrameView;
irc.app = new AppView;
// Create the status "frame"
frames.add({name: 'status', type: 'status'});
sizeContent($('#frame #output'));
sizeContent($('#frame .nicks'));
}
});
var connect = new ConnectView;
// UTILS
// =====
function humanizeError(message) {
var text = '';
switch (message.command) {
case 'err_unknowncommand':
text = 'That is not a known IRC command.';
break;
}
return text;
}
// Set output window to full height, minus other elements
function sizeContent(sel) {
var newHeight = $('html').height() - $('header').outerHeight(true) -
$('#prime-input').outerHeight(true) -
(sel.outerHeight(true) - sel.height()) - 10;
// (10 = #content padding)
sel.height(newHeight);
}
// VERY TEMPORARY -- JUST FOR TESTING
$('#sidebar #frames li').click(function() {
var name = $(this).text();
irc.app.joinChannel(name);
});
// SOCKET EVENTS
// =============
socket.on('message', function(msg) {
// Filter out messages not aimed at a channel or status (i.e. PMs)
if (msg.to.indexOf('#') !== 0 &&
msg.to.indexOf('&') !== 0 &&
msg.to !== 'status') return;
frame = frames.getByName(msg.to);
if (frame) {
frame.stream.add({sender: msg.from, text: msg.text});
}
});
socket.on('pm', function(msg) {
pm = frames.getByName(msg.nick) || new Frame({type: 'pm', name: msg.nick});
pm.stream.add({sender: msg.nick, text: msg.text})
frames.add(pm);
})
socket.on('motd', function(data) {
data.motd.split('\n').forEach(function(line) {
frames.getByName('status').stream.add({sender: '', text: line});
});
});
socket.on('join', function(data) {
console.log('Join event received for ' + data.channel + ' - ' + data.nick);
if (data.nick == irc.me.get('nick')) {
frames.add({name: data.channel});
} else {
channel = frames.getByName(data.channel);
channel.participants.add({nick: data.nick});
var joinMessage = new Message({type: 'join', nick: data.nick});
joinMessage.setText();
channel.stream.add(joinMessage);
}
});
socket.on('part', function(data) {
console.log('Part event received for ' + data.channel + ' - ' + data.nick);
if (data.nick == irc.me.get('nick')) {
frames.getByName(data.channel).part();
} else {
channel = frames.getByName(data.channel);
channel.participants.getByNick(data.nick).destroy();
var partMessage = new Message({type: 'part', nick: data.nick});
partMessage.setText();
channel.stream.add(partMessage);
}
});
socket.on('nick', function(data) {
// Update my info, if it's me
if (data.oldNick == irc.me.get('nick')) {
irc.me.set({nick: data.newNick});
}
// Set new name in all channels
data.channels.forEach(function(ch) {
var channel = frames.getByName(ch);
// Change nick in user list
channel.participants.getByNick(data.oldNick).set({nick: data.newNick});
// Send nick change message to channel stream
var nickMessage = new Message({
type: 'nick',
oldNick: data.oldNick,
newNick: data.newNick
});
nickMessage.setText();
channel.stream.add(nickMessage);
});
});
socket.on('names', function(data) {
var frame = frames.getByName(data.channel);
console.log(data);
for (var nick in data.nicks) {
frame.participants.add({nick: nick, opStatus: data.nicks[nick]});
}
});
socket.on('error', function(data) {
console.log(data.message);
frame = frames.getActive();
error = humanizeError(data.message);
frame.stream.add({text: error, type: 'error'})
});
});