forked from thedjpetersen/subway
/
chat_application.js
91 lines (79 loc) · 2.59 KB
/
chat_application.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
var ChatApplicationView = Backbone.View.extend({
className: 'container-fluid',
originalTitle: document.title,
initialize: function() {
irc.chatWindows.bind('change:unread', this.showUnread, this)
.bind('change:unreadMentions', this.showUnread, this)
.bind('forMe', this.playSound, this);
// Preload sound files
if (this._supportedFormat) {
this.sounds = {
newPm: this._loadSound('new-pm'),
message: this._loadSound('msg')
};
}
// Detect window focus so new message alerts come in
// when window is not focused, even on current tab
var blurTimer, activeChat;
$(window).blur(function() {
blurTimer = setTimeout(function() {
// Only assign if there's currently an active window
// Guards against losing activeChat if there's a second blur event
activeChat = irc.chatWindows.getActive() ?
irc.chatWindows.getActive() :
activeChat;
if (activeChat && activeChat.set) { activeChat.set('active', false); }
}, 1000);
}).focus(function() {
clearTimeout(blurTimer);
if(activeChat && activeChat.set) { activeChat.set('active', true); }
});
this.render();
},
render: function() {
$('body').html($(this.el).html(ich.chat_application()));
if (!irc.connected) {
var overview = new OverviewView;
} else {
this.channelList = new ChannelListView;
}
return this;
},
// Net connection error
showError: function(text) {
$('#loading_image').remove();
$('.btn').removeClass('disabled');
$('#home_parent').after(ich.alert({
type: 'alert-error',
content: text
}).alert());
},
renderUserBox: function() {
$('#user-box').html(ich.user_box(irc.me.toJSON()));
},
// Show number of unread mentions in title
showUnread: function() {
var unreads = irc.chatWindows.unreadCount();
if (unreads > 0)
document.title = '(' + unreads + ') ' + this.originalTitle;
else
document.title = this.originalTitle;
},
playSound: function(type) {
this.sounds && this.sounds[type].play();
},
_loadSound: function(name) {
var a = new Audio();
a.src = '/assets/sounds/' + name + '.' + this._supportedFormat();
return a;
},
// Detect supported HTML5 audio format
_supportedFormat: function() {
var a = document.createElement('audio');
if (!a.canPlayType) return false;
else if (!!(a.canPlayType('audio/ogg; codecs="vorbis"').replace(/no/, '')))
return 'ogg'
else if (!!(a.canPlayType('audio/mpeg;').replace(/no/, '')))
return 'mp3'
}
});