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
|
var max_length = 50;
var default_channel = "#channel1"
if (window.location.hash.length == 0 && default_channel.length > 0) {
window.location.replace(default_channel);
}
var channel = "";
var i;
if ((i = window.location.hash.indexOf("#")) >= 0) {
channel = window.location.hash.substring(i + 1);
}
function scrollDown() {
$('html, body').animate({scrollTop: $('html, body').height()}, 1);
}
function cleanOld() {
var elems = $("#chat li");
if (elems.length > max_length) {
var i = elems.length - max_length;
elems.each(function() {
if (i > 0) {
$(this).remove();
i--;
return true;
} else {
return false;
}
});
}
}
function printMessage(msg) {
$("#chat").append("<li>"+msg+"</li>");
cleanOld();
scrollDown();
}
function setTopic(newTopic) {
$("#topic").children("h3").html(newTopic);
document.title = newTopic + " - IRC-SSE";
}
function getUser(user) {
return user.substring(0, user.indexOf('!'));
}
function privmsg(msg) {
printMessage(msg.time + " <" + getUser(msg.user) + "> " + msg.message);
}
function action(msg) {
printMessage(msg.time + " * " + getUser(msg.user) + " " + msg.action);
}
function join(msg) {
printMessage(msg.time + " -!- " + msg.user + " joined");
}
function leave(msg) {
printMessage(msg.time + " -!- " + msg.user + " left");
}
function kick(msg) {
printMessage(msg.time + " -!- " + getUser(msg.kickee) + " was kicked by " + getUser(msg.kicker) + " reason: " + msg.message);
}
function rename(msg) {
printMessage(msg.time + " -!- " + getUser(msg.oldname) + " is now known as " + getUser(msg.newname));
}
function mode(msg) {
var output = msg.time + " -!- mode/" + msg.channel +
" [" + ((msg.set == true)?"+":"-") +
msg.modes;
$.each(msg.args, function(i, v) {
if(v != null) {
output += " " + v;
}
});
output += "] by " + getUser(msg.user);
printMessage(output);
}
function topic(msg) {
var output = msg.time + " -!- " + msg.user + " changed topic to " + msg.topic;
printMessage(output);
}
function daychange(msg) {
var output = msg.time + " Day changed to " + msg.date;
printMessage(output);
}
function parseMsg(msg) {
switch(msg.type) {
case 'action':
action(msg);
break;
case 'topic':
topic(msg);
setTopic(msg.topic);
break;
case 'privmsg':
privmsg(msg);
break;
case 'mode':
mode(msg);
break;
case 'leave':
leave(msg);
break;
case 'join':
join(msg);
break;
case 'kick':
kick(msg);
break;
case 'rename':
rename(msg);
break;
case 'daychange':
daychange(msg);
break;
case 'refresh':
location.reload();
break;
default:
break;
}
}
$(document).ready(function() {
if (channel.length > 0) {
$.ajax({
url: "/backlog?" + channel,
// crossDomain: true,
dataType: "json",
success: function(data) {
setTopic(data.topic);
$.each(data.msg, function(i, msg) {
parseMsg(msg);
});
}
});
}
$(window).on('hashchange', function() {
location.reload();
});
});
;(function($) {
$.fn.textfill = function(options) {
var fontSize = options.maxFontPixels;
var ourText = $('#chat', this);
var maxHeight = $(this).height();
var maxWidth = $(this).width();
var textHeight;
var textWidth;
do {
ourText.css('font-size', fontSize);
textHeight = ourText.height();
textWidth = ourText.width();
fontSize = fontSize - 1;
} while ((textHeight > maxHeight || textWidth > maxWidth) && fontSize > 3);
return this;
}
})(jQuery);
//$(document).ready(function() {
// $("#chat-container").textfill({ maxFontPixels: 100 });
//});
var evtSrc = new EventSource("/subscribe?" + channel);
evtSrc.onmessage = function(e) {
parseMsg($.parseJSON(e.data))
}
|