Commit | Line | Data |
---|---|---|
a3364605 JA |
1 | /*jslint undef: true, browser: true, continue: true, sloppy: true, evil: true, forin: true, newcap: false, plusplus: true, maxerr: 50, indent: 4 */ |
2 | /*global gateway, io, $, iScroll, agent, touchscreen*/ | |
54f4a22e | 3 | var front = { |
a3364605 JA |
4 | revision: 38, |
5 | ||
6 | cur_channel: '', | |
7 | windows: {}, | |
8 | tabviews: {}, | |
604c5174 | 9 | utilityviews: {}, |
a3364605 JA |
10 | boxes: {}, |
11 | ||
12 | buffer: [], | |
13 | buffer_pos: 0, | |
110ce6d4 JA |
14 | |
15 | original_topic: '', | |
a3364605 JA |
16 | |
17 | init: function () { | |
18 | var about_info, supportsOrientationChange, orientationEvent; | |
19 | gateway.nick = 'kiwi_' + Math.ceil(100 * Math.random()) + Math.ceil(100 * Math.random()); | |
20 | gateway.session_id = null; | |
21 | ||
22 | $(gateway).bind("onmsg", front.onMsg); | |
23 | $(gateway).bind("onnotice", front.onNotice); | |
24 | $(gateway).bind("onaction", front.onAction); | |
25 | $(gateway).bind("onmotd", front.onMOTD); | |
26 | $(gateway).bind("onoptions", front.onOptions); | |
27 | $(gateway).bind("onconnect", front.onConnect); | |
b7be4766 | 28 | $(gateway).bind("onconnect_fail", front.onConnectFail); |
a3364605 JA |
29 | $(gateway).bind("ondisconnect", front.onDisconnect); |
30 | $(gateway).bind("onnick", front.onNick); | |
31 | $(gateway).bind("onuserlist", front.onUserList); | |
32 | $(gateway).bind("onuserlist_end", front.onUserListEnd); | |
33 | $(gateway).bind("onjoin", front.onJoin); | |
34 | $(gateway).bind("ontopic", front.onTopic); | |
35 | $(gateway).bind("onpart", front.onPart); | |
36 | $(gateway).bind("onkick", front.onKick); | |
37 | $(gateway).bind("onquit", front.onQuit); | |
38 | $(gateway).bind("onwhois", front.onWhois); | |
39 | $(gateway).bind("onsync", front.onSync); | |
40 | $(gateway).bind("onchannel_redirect", front.onChannelRedirect); | |
41 | $(gateway).bind("ondebug", front.onDebug); | |
d9e91fed D |
42 | $(gateway).bind("onctcp_request", front.onCTCPRequest); |
43 | $(gateway).bind("onctcp_response", front.onCTCPResponse); | |
81122538 | 44 | $(gateway).bind("onirc_error", front.onIRCError); |
a3364605 JA |
45 | |
46 | this.buffer = []; | |
47 | ||
48 | // Build the about box | |
49 | front.boxes.about = new Box("about"); | |
50 | about_info = 'UI adapted for ' + agent; | |
51 | if (touchscreen) { | |
52 | about_info += ' touchscreen '; | |
53 | } | |
54 | about_info += 'usage'; | |
55 | $('#tmpl_about_box').tmpl({ | |
56 | about: about_info, | |
57 | front_revision: front.revision, | |
58 | gateway_revision: gateway.revision | |
59 | }).appendTo(front.boxes.about.content); | |
60 | ||
61 | //$(window).bind("beforeunload", function(){ gateway.quit(); }); | |
62 | ||
63 | if (touchscreen) { | |
64 | $('#kiwi').addClass('touchscreen'); | |
65 | ||
66 | // Single touch scrolling through scrollback for touchscreens | |
67 | scroll_opts = {}; | |
68 | touch_scroll = new iScroll('windows', scroll_opts); | |
69 | } | |
70 | ||
71 | front.registerKeys(); | |
72 | ||
c432172c | 73 | $('#kiwi .toolbars').resize(front.doLayoutSize); |
a3364605 JA |
74 | |
75 | $('#kiwi .formconnectwindow').submit(function () { | |
76 | var netsel = $('#kiwi .formconnectwindow .network'), | |
77 | nick = $('#kiwi .formconnectwindow .nick'), | |
78 | tmp; | |
79 | ||
80 | if (nick.val() === '') { | |
81 | nick.val('Nick please!'); | |
82 | nick.focus(); | |
83 | return false; | |
84 | } | |
85 | ||
86 | tmp = nick.val().split(' '); | |
87 | gateway.nick = tmp[0]; | |
c89b9fdf D |
88 | |
89 | init_data.channel = $('#channel').val(); | |
90 | ||
a3364605 JA |
91 | front.doLayout(); |
92 | try { | |
93 | front.run('/connect ' + netsel.val()); | |
94 | } catch (e) { | |
95 | alert(e); | |
96 | } | |
97 | ||
c432172c | 98 | $('#kiwi .connectwindow').slideUp('', front.barsShow); |
a3364605 JA |
99 | $('#windows').click(function () { $('#kiwi_msginput').focus(); }); |
100 | ||
101 | return false; | |
102 | }); | |
103 | ||
104 | supportsOrientationChange = (typeof window.onorientationchange !== undefined); | |
105 | orientationEvent = supportsOrientationChange ? "orientationchange" : "resize"; | |
d30da6a0 D |
106 | if (window.addEventListener) { |
107 | window.addEventListener(orientationEvent, front.doLayoutSize, false); | |
108 | } else { | |
109 | // < IE9 | |
110 | window.attachEvent(orientationEvent, front.doLayoutSize, false); | |
111 | } | |
a3364605 JA |
112 | //$('#kiwi').bind("resize", front.doLayoutSize, false); |
113 | ||
114 | front.doLayout(); | |
c432172c | 115 | front.barsHide(); |
875d0c71 | 116 | |
a3364605 | 117 | front.tabviewAdd('server'); |
875d0c71 | 118 | front.tabviews.server.userlist_width = 0; // Disable the userlist |
a3364605 JA |
119 | |
120 | // Any pre-defined nick? | |
c89b9fdf | 121 | if (typeof window.init_data.nick === "string") { |
bad1ea63 JA |
122 | $('#kiwi .formconnectwindow .nick').val(init_data.nick); |
123 | } | |
c89b9fdf D |
124 | |
125 | // Any pre-defined channels? | |
126 | if (typeof window.init_data.channel === 'string') { | |
127 | $('#channel').val(init_data.channel); | |
128 | } | |
a3364605 | 129 | |
a3364605 JA |
130 | |
131 | $('#kiwi .cur_topic').keydown(function (e) { | |
aa191535 D |
132 | if (e.which === 13) { |
133 | // enter | |
134 | e.preventDefault(); | |
135 | $(this).change(); | |
136 | $('#kiwi_msginput').focus(); | |
137 | } else if (e.which === 27) { | |
138 | // escape | |
139 | e.preventDefault(); | |
140 | $(this).text(front.original_topic); | |
141 | $('#kiwi_msginput').focus(); | |
142 | } | |
a3364605 | 143 | }); |
aa191535 | 144 | /*$('.cur_topic').live('keypress', function(e) { |
110ce6d4 | 145 | if (e.keyCode === 13) { |
1666d8d9 | 146 | // enter |
110ce6d4 JA |
147 | e.preventDefault(); |
148 | $(this).change(); | |
149 | $('#kiwi_msginput').focus(); | |
1666d8d9 JA |
150 | } else if (e.keyCode === 27) { |
151 | // escape | |
152 | e.preventDefault(); | |
153 | $(this).text(front.original_topic); | |
110ce6d4 | 154 | } |
aa191535 | 155 | });*/ |
a3364605 | 156 | $('.cur_topic').live('change', function () { |
110ce6d4 JA |
157 | var chan, text; |
158 | text = $(this).text(); | |
110ce6d4 | 159 | if (text !== front.original_topic) { |
110ce6d4 | 160 | chan = front.cur_channel.name; |
aa191535 | 161 | gateway.setTopic(chan, text); |
110ce6d4 JA |
162 | } |
163 | }); | |
ff19dc74 D |
164 | |
165 | ||
166 | $('#windows a.chan').live('click', function() { | |
167 | front.joinChannel($(this).text()); | |
168 | return false; | |
169 | }); | |
110ce6d4 | 170 | |
a3364605 JA |
171 | }, |
172 | ||
173 | doLayoutSize: function () { | |
875d0c71 | 174 | var kiwi, toolbars, ul, n_top, n_bottom; |
a3364605 | 175 | kiwi = $('#kiwi'); |
875d0c71 | 176 | |
a3364605 JA |
177 | if (kiwi.width() < 330 && !kiwi.hasClass('small_kiwi')) { |
178 | console.log("switching to small kiwi"); | |
179 | kiwi.removeClass('large_kiwi'); | |
180 | kiwi.addClass('small_kiwi'); | |
181 | } else if (kiwi.width() >= 330 && !kiwi.hasClass('large_kiwi')) { | |
182 | kiwi.removeClass('small_kiwi'); | |
183 | kiwi.addClass('large_kiwi'); | |
184 | } | |
185 | ||
875d0c71 | 186 | toolbars = $('#kiwi .cur_topic'); |
a3364605 JA |
187 | ul = $('#kiwi .userlist'); |
188 | ||
875d0c71 | 189 | n_top = parseInt(toolbars.offset().top, 10) + parseInt(toolbars.outerHeight(true), 10); |
a3364605 JA |
190 | n_bottom = $(document).height() - parseInt($('#kiwi .control').offset().top, 10); |
191 | ||
192 | $('#kiwi .windows').css({top: n_top + 'px', bottom: n_bottom + 'px'}); | |
193 | $('#kiwi .userlist').css({top: n_top + 'px', bottom: n_bottom + 'px'}); | |
194 | }, | |
195 | ||
196 | ||
197 | doLayout: function () { | |
198 | $('#kiwi .msginput .nick a').text(gateway.nick); | |
199 | $('#kiwi_msginput').val(' '); | |
200 | $('#kiwi_msginput').focus(); | |
201 | }, | |
202 | ||
203 | ||
204 | joinChannel: function (chan_name) { | |
205 | var chans = chan_name.split(','), | |
bad1ea63 | 206 | i; |
a3364605 JA |
207 | for (i in chans) { |
208 | chan = chans[i]; | |
209 | if (front.tabviews[chan.toLowerCase()] === undefined || (front.tabviews[chan.toLowerCase()] !== undefined && front.tabviews[chan.toLowerCase()].safe_to_close === true)) { | |
210 | gateway.join(chan); | |
211 | front.tabviewAdd(chan); | |
212 | } else { | |
213 | front.tabviews[chan.toLowerCase()].show(); | |
214 | } | |
215 | } | |
216 | }, | |
217 | ||
218 | ||
219 | run: function (msg) { | |
875d0c71 D |
220 | var parts, dest, t, pos, textRange, d, plugin_event; |
221 | ||
222 | // Run through any plugins | |
223 | plugin_event = {command: msg}; | |
224 | plugin_event = plugs.run('command_run', plugin_event); | |
225 | if (!plugin_event || typeof plugin_event.command === 'undefined') return; | |
226 | ||
227 | // Update msg if it's been changed by any plugins | |
228 | msg = plugin_event.command.toString(); | |
229 | ||
a3364605 JA |
230 | console.log("running " + msg); |
231 | if (msg.substring(0, 1) === '/') { | |
232 | parts = msg.split(' '); | |
233 | switch (parts[0].toLowerCase()) { | |
234 | case '/j': | |
235 | case '/join': | |
236 | front.joinChannel(parts[1]); | |
237 | break; | |
238 | ||
239 | case '/connect': | |
240 | case '/server': | |
241 | if (parts[1] === undefined) { | |
242 | alert('Usage: /connect servername [port]'); | |
243 | break; | |
244 | } | |
245 | ||
246 | if (parts[2] === undefined) { | |
bad1ea63 JA |
247 | parts[2] = 6667; |
248 | } | |
a3364605 JA |
249 | front.cur_channel.addMsg(null, ' ', '=== Connecting to ' + parts[1] + '...', 'status'); |
250 | gateway.connect(parts[1], parts[2], 0); | |
251 | break; | |
252 | ||
253 | case '/nick': | |
254 | console.log("/nick"); | |
255 | if (parts[1] === undefined) { | |
256 | console.log("calling show nick"); | |
257 | front.showChangeNick(); | |
258 | } else { | |
259 | console.log("sending raw"); | |
260 | gateway.raw(msg.substring(1)); | |
261 | } | |
262 | break; | |
263 | ||
264 | case '/part': | |
265 | if (typeof parts[1] === "undefined") { | |
266 | if (front.cur_channel.safe_to_close) { | |
267 | front.cur_channel.close(); | |
268 | } else { | |
269 | gateway.raw(msg.substring(1) + ' ' + front.cur_channel.name); | |
270 | } | |
271 | } else { | |
272 | gateway.raw(msg.substring(1)); | |
273 | } | |
274 | break; | |
275 | ||
276 | case '/names': | |
277 | if (typeof parts[1] !== "undefined") { | |
278 | gateway.raw(msg.substring(1)); | |
279 | } | |
280 | break; | |
281 | ||
282 | case '/debug': | |
283 | gateway.debug(); | |
284 | break; | |
285 | ||
286 | case '/q': | |
287 | case '/query': | |
288 | if (typeof parts[1] !== "undefined") { | |
289 | front.tabviewAdd(parts[1]); | |
290 | } | |
291 | break; | |
323e90f6 D |
292 | |
293 | ||
294 | case '/m': | |
295 | case '/msg': | |
296 | if (typeof parts[1] !== "undefined") { | |
297 | var msg_sliced = msg.split(' ').slice(2).join(' '); | |
298 | gateway.msg(parts[1], msg_sliced); | |
a5ffcf00 D |
299 | |
300 | if (!front.tabviewExists(parts[1])) { | |
301 | front.tabviewAdd(parts[1]); | |
302 | } | |
303 | front.tabviews[parts[1].toLowerCase()].addMsg(null, gateway.nick, msg_sliced); | |
323e90f6 D |
304 | } |
305 | break; | |
306 | ||
a3364605 JA |
307 | |
308 | case '/quote': | |
309 | gateway.raw(msg.replace(/^\/quote /i, '')); | |
310 | break; | |
311 | ||
312 | case '/me': | |
313 | gateway.action(front.cur_channel.name, msg.substring(4)); | |
314 | //front.tabviews[destination.toLowerCase()].addMsg(null, ' ', '* '+data.nick+' '+data.msg, 'color:green;'); | |
315 | front.cur_channel.addMsg(null, ' ', '* ' + gateway.nick + ' ' + msg.substring(4), 'action', 'color:#555;'); | |
316 | break; | |
317 | ||
318 | case '/notice': | |
319 | dest = parts[1]; | |
320 | msg = parts.slice(2).join(' '); | |
321 | ||
322 | gateway.notice(dest, msg); | |
323 | this.onNotice({}, {nick: gateway.nick, channel: dest, msg: msg}); | |
324 | break; | |
325 | ||
326 | case '/win': | |
327 | if (parts[1] !== undefined) { | |
328 | front.windowsShow(parseInt(parts[1], 10)); | |
329 | } | |
330 | break; | |
331 | ||
332 | case '/quit': | |
333 | gateway.quit(msg.split(" ", 2)[1]); | |
f4f7781b JA |
334 | break; |
335 | ||
45c03116 | 336 | case '/topic': |
a3364605 JA |
337 | if (parts[1] === undefined) { |
338 | t = $('.cur_topic'); | |
339 | if (t.createTextRange) { | |
340 | pos = t.text().length(); | |
341 | textRange = t.createTextRange(); | |
342 | textRange.collapse(true); | |
343 | textRange.moveEnd(pos); | |
344 | textRange.moveStart(pos); | |
345 | textRange.select(); | |
346 | } else if (t.setSelectionRange) { | |
347 | t.setSelectionRange(pos, pos); | |
348 | } | |
349 | } else { | |
350 | gateway.setTopic(front.cur_channel.name, msg.split(' ', 2)[1]); | |
351 | //gateway.raw('TOPIC ' + front.cur_channel.name + ' :' + msg.split(' ', 2)[1]); | |
aa191535 | 352 | } |
45c03116 | 353 | break; |
a3364605 JA |
354 | default: |
355 | //front.cur_channel.addMsg(null, ' ', '--> Invalid command: '+parts[0].substring(1)); | |
356 | gateway.raw(msg.substring(1)); | |
357 | } | |
358 | ||
359 | } else { | |
360 | //alert('Sending message: '+msg); | |
361 | if (msg.trim() === '') { | |
bad1ea63 JA |
362 | return; |
363 | } | |
3f4aa6a5 | 364 | if (front.cur_channel.name !== 'server') { |
a3364605 JA |
365 | gateway.msg(front.cur_channel.name, msg); |
366 | d = new Date(); | |
367 | d = d.getHours() + ":" + d.getMinutes(); | |
368 | //front.addMsg(d, gateway.nick, msg); | |
369 | front.cur_channel.addMsg(null, gateway.nick, msg); | |
370 | } | |
371 | } | |
372 | }, | |
373 | ||
374 | ||
375 | onMsg: function (e, data) { | |
875d0c71 | 376 | var destination, plugin_event; |
a3364605 JA |
377 | // Is this message from a user? |
378 | if (data.channel === gateway.nick) { | |
379 | destination = data.nick.toLowerCase(); | |
380 | } else { | |
381 | destination = data.channel.toLowerCase(); | |
382 | } | |
383 | ||
875d0c71 D |
384 | plugin_event = {nick: data.nick, msg:data.msg, destination: destination}; |
385 | plugin_event = plugs.run('msg_recieved', plugin_event); | |
386 | if (!plugin_event) return; | |
387 | ||
388 | if (!front.tabviewExists(plugin_event.destination)) { | |
389 | front.tabviewAdd(plugin_event.destination); | |
bad1ea63 | 390 | } |
875d0c71 | 391 | front.tabviews[plugin_event.destination].addMsg(null, plugin_event.nick, plugin_event.msg); |
a3364605 JA |
392 | }, |
393 | ||
394 | onDebug: function (e, data) { | |
395 | if (!front.tabviewExists('kiwi_debug')) { | |
bad1ea63 JA |
396 | front.tabviewAdd('kiwi_debug'); |
397 | } | |
a3364605 JA |
398 | front.tabviews.kiwi_debug.addMsg(null, ' ', data.msg); |
399 | }, | |
400 | ||
401 | onAction: function (e, data) { | |
bad1ea63 | 402 | var destination; |
a3364605 JA |
403 | // Is this message from a user? |
404 | if (data.channel === gateway.nick) { | |
405 | destination = data.nick; | |
406 | } else { | |
407 | destination = data.channel; | |
408 | } | |
409 | ||
410 | if (!front.tabviewExists(destination)) { | |
bad1ea63 JA |
411 | front.tabviewAdd(destination); |
412 | } | |
a3364605 JA |
413 | front.tabviews[destination.toLowerCase()].addMsg(null, ' ', '* ' + data.nick + ' ' + data.msg, 'action', 'color:#555;'); |
414 | }, | |
415 | ||
416 | onTopic: function (e, data) { | |
417 | if (front.tabviewExists(data.channel)) { | |
418 | front.tabviews[data.channel.toLowerCase()].changeTopic(data.topic); | |
419 | } | |
420 | }, | |
421 | ||
422 | onNotice: function (e, data) { | |
a5ffcf00 D |
423 | var nick = (data.nick === undefined) ? '' : data.nick; |
424 | var enick = '[' + nick + ']'; | |
c89b9fdf | 425 | |
a5ffcf00 D |
426 | if (front.tabviewExists(data.target)) { |
427 | front.tabviews[data.target.toLowerCase()].addMsg(null, enick, data.msg, 'notice'); | |
428 | } else if (front.tabviewExists(nick)) { | |
429 | front.tabviews[nick.toLowerCase()].addMsg(null, enick, data.msg, 'notice'); | |
a3364605 | 430 | } else { |
a5ffcf00 | 431 | front.tabviews.server.addMsg(null, enick, data.msg, 'notice'); |
a3364605 JA |
432 | } |
433 | }, | |
423a590e JA |
434 | |
435 | onCTCPRequest: function (e, data) { | |
436 | var msg = data.msg.split(" ", 2); | |
437 | switch (msg[0]) { | |
438 | case 'PING': | |
a3364605 JA |
439 | if (typeof msg[1] === 'undefined') { |
440 | msg[1] = ''; | |
441 | } | |
442 | gateway.notice(data.nick, String.fromCharCode(1) + 'PING ' + msg[1] + String.fromCharCode(1)); | |
423a590e | 443 | break; |
d9e91fed | 444 | case 'TIME': |
a3364605 | 445 | gateway.notice(data.nick, String.fromCharCode(1) + 'TIME ' + (new Date()).toLocaleString() + String.fromCharCode(1)); |
423a590e JA |
446 | break; |
447 | } | |
a3364605 | 448 | front.tabviews.server.addMsg(null, 'CTCP [' + data.nick + ']', data.msg, 'ctcp'); |
423a590e JA |
449 | }, |
450 | ||
a3364605 | 451 | onCTCPResponse: function (e, data) { |
423a590e JA |
452 | }, |
453 | ||
a3364605 JA |
454 | onConnect: function (e, data) { |
455 | if (data.connected) { | |
337c9866 D |
456 | if (gateway.nick !== data.nick) { |
457 | gateway.nick = data.nick; | |
458 | front.doLayout(); | |
459 | } | |
460 | ||
a3364605 JA |
461 | front.tabviews.server.addMsg(null, ' ', '=== Connected OK :)', 'status'); |
462 | if (typeof init_data.channel === "string") { | |
463 | front.joinChannel(init_data.channel); | |
464 | } | |
875d0c71 | 465 | plugs.run('connect', {success: true}); |
a3364605 JA |
466 | } else { |
467 | front.tabviews.server.addMsg(null, ' ', '=== Failed to connect :(', 'status'); | |
875d0c71 | 468 | plugs.run('connect', {success: false}); |
a3364605 JA |
469 | } |
470 | }, | |
b7be4766 D |
471 | onConnectFail: function (e, data) { |
472 | var reason = (typeof data.reason === 'string') ? data.reason : ''; | |
473 | front.tabviews.server.addMsg(null, '', 'There\'s a problem connecting! (' + reason + ')', 'error'); | |
875d0c71 | 474 | plugs.run('connect', {success: false}); |
b7be4766 | 475 | }, |
a3364605 JA |
476 | onDisconnect: function (e, data) { |
477 | var tab; | |
478 | for (tab in front.tabviews) { | |
479 | front.tabviews[tab].addMsg(null, '', 'Disconnected from server!', 'error'); | |
480 | } | |
875d0c71 | 481 | plugs.run('disconnect', {success: false}); |
a3364605 JA |
482 | }, |
483 | onOptions: function (e, data) { | |
484 | if (typeof gateway.network_name === "string" && gateway.network_name !== "") { | |
485 | front.tabviews.server.tab.text(gateway.network_name); | |
486 | } | |
487 | }, | |
488 | onMOTD: function (e, data) { | |
489 | front.tabviews.server.addMsg(null, data.server, data.msg, 'motd'); | |
490 | }, | |
491 | onWhois: function (e, data) { | |
0c5e2aaa JA |
492 | var d; |
493 | if (data.msg) { | |
a3364605 | 494 | front.cur_channel.addMsg(null, data.nick, data.msg, 'whois'); |
0c5e2aaa JA |
495 | } else if (data.logon) { |
496 | d = new Date(); | |
497 | d.setTime(data.logon * 1000); | |
498 | d = d.toLocaleString(); | |
499 | front.cur_channel.addMsg(null, data.nick, 'idle for ' + data.idle + ' second' + ((data.idle !== 1) ? 's' : '') + ', signed on ' + d, 'whois'); | |
500 | } else { | |
501 | front.cur_channel.addMsg(null, data.nick, 'idle for ' + data.idle + ' seconds', 'whois'); | |
502 | } | |
a3364605 JA |
503 | }, |
504 | onUserList: function (e, data) { | |
505 | var ul, nick, mode; | |
506 | if (front.tabviews[data.channel.toLowerCase()] === undefined) { | |
bad1ea63 | 507 | return; |
a3364605 JA |
508 | } |
509 | ul = front.tabviews[data.channel.toLowerCase()].userlist; | |
510 | ||
511 | if (!document.userlist_updating) { | |
512 | document.userlist_updating = true; | |
513 | ul.empty(); | |
514 | } | |
515 | ||
516 | $.each(data.users, function (i, item) { | |
517 | nick = i; //i.match(/^.+!/g); | |
518 | mode = item; | |
519 | $('<li><a class="nick" onclick="front.userClick(this);">' + mode + nick + '</a></li>').appendTo(ul); | |
520 | }); | |
521 | ||
522 | front.tabviews[data.channel.toLowerCase()].userlistSort(); | |
523 | }, | |
524 | onUserListEnd: function (e, data) { | |
525 | document.userlist_updating = false; | |
526 | }, | |
527 | ||
528 | onJoin: function (e, data) { | |
529 | if (!front.tabviewExists(data.channel)) { | |
530 | front.tabviewAdd(data.channel.toLowerCase()); | |
531 | } | |
532 | ||
533 | if (data.nick === gateway.nick) { | |
bad1ea63 JA |
534 | return; // Not needed as it's already in nicklist |
535 | } | |
a3364605 JA |
536 | front.tabviews[data.channel.toLowerCase()].addMsg(null, ' ', '--> ' + data.nick + ' has joined', 'action', 'color:#009900;'); |
537 | $('<li><a class="nick" onclick="front.userClick(this);">' + data.nick + '</a></li>').appendTo(front.tabviews[data.channel.toLowerCase()].userlist); | |
538 | front.tabviews[data.channel.toLowerCase()].userlistSort(); | |
539 | }, | |
540 | onPart: function (e, data) { | |
541 | if (front.tabviewExists(data.channel)) { | |
542 | // If this is us, close the tabview | |
543 | if (data.nick === gateway.nick) { | |
544 | front.tabviews[data.channel.toLowerCase()].close(); | |
545 | front.tabviews.server.show(); | |
546 | return; | |
547 | } | |
548 | ||
549 | front.tabviews[data.channel.toLowerCase()].addMsg(null, ' ', '<-- ' + data.nick + ' has left (' + data.message + ')', 'action', 'color:#990000;'); | |
550 | front.tabviews[data.channel.toLowerCase()].userlist.children().each(function () { | |
551 | if ($(this).text() === data.nick) { | |
552 | $(this).remove(); | |
553 | } | |
554 | }); | |
555 | } | |
556 | }, | |
557 | onKick: function (e, data) { | |
558 | if (front.tabviewExists(data.channel)) { | |
559 | // If this is us, close the tabview | |
560 | if (data.kicked === gateway.nick) { | |
561 | //front.tabviews[data.channel.toLowerCase()].close(); | |
562 | front.tabviews[data.channel.toLowerCase()].addMsg(null, ' ', '=== You have been kicked from ' + data.channel + '. ' + data.message, 'status'); | |
563 | front.tabviews[data.channel.toLowerCase()].safe_to_close = true; | |
564 | $('li', front.tabviews[data.channel.toLowerCase()].userlist).remove(); | |
565 | return; | |
566 | } | |
567 | ||
568 | front.tabviews[data.channel.toLowerCase()].addMsg(null, ' ', '<-- ' + data.kicked + ' kicked by ' + data.nick + '(' + data.message + ')', 'action', 'color:#990000;'); | |
569 | front.tabviews[data.channel.toLowerCase()].userlist.children().each(function () { | |
570 | if ($(this).text() === data.nick) { | |
571 | $(this).remove(); | |
572 | } | |
573 | }); | |
574 | } | |
575 | }, | |
576 | onNick: function (e, data) { | |
577 | if (data.nick === gateway.nick) { | |
578 | gateway.nick = data.newnick; | |
579 | front.doLayout(); | |
580 | } | |
581 | ||
582 | $.each(front.tabviews, function (i, item) { | |
583 | $.each(front.tabviews, function (i, item) { | |
584 | item.changeNick(data.newnick, data.nick); | |
585 | }); | |
586 | }); | |
587 | }, | |
588 | onQuit: function (e, data) { | |
589 | $.each(front.tabviews, function (i, item) { | |
590 | $.each(front.tabviews, function (i, item) { | |
591 | item.userlist.children().each(function () { | |
592 | if ($(this).text() === data.nick) { | |
593 | $(this).remove(); | |
594 | item.addMsg(null, ' ', '<-- ' + data.nick + ' has quit (' + data.message + ')', 'action', 'color:#990000;'); | |
595 | } | |
596 | }); | |
597 | }); | |
598 | }); | |
599 | }, | |
600 | onChannelRedirect: function (e, data) { | |
601 | front.tabviews[data.from.toLowerCase()].close(); | |
602 | front.tabviewAdd(data.to.toLowerCase()); | |
603 | front.tabviews[data.to.toLowerCase()].addMsg(null, ' ', '=== Redirected from ' + data.from, 'action'); | |
604 | }, | |
605 | ||
81122538 | 606 | onIRCError: function (e, data) { |
a3364605 JA |
607 | var t_view; |
608 | if (data.channel !== undefined && front.tabviewExists(data.channel)) { | |
609 | t_view = data.channel; | |
610 | } else { | |
611 | t_view = 'server'; | |
612 | } | |
613 | ||
614 | switch (data.error) { | |
81122538 | 615 | case 'banned_from_channel': |
aa191535 | 616 | front.tabviews[t_view].addMsg(null, ' ', '=== You are banned from ' + data.channel + '. ' + data.reason, 'status'); |
a3364605 JA |
617 | if (t_view !== 'server') { |
618 | front.tabviews[t_view].safe_to_close = true; | |
619 | } | |
81122538 JA |
620 | break; |
621 | case 'bad_channel_key': | |
aa191535 | 622 | front.tabviews[t_view].addMsg(null, ' ', '=== Bad channel key for ' + data.channel, 'status'); |
a3364605 JA |
623 | if (t_view !== 'server') { |
624 | front.tabviews[t_view].safe_to_close = true; | |
625 | } | |
81122538 JA |
626 | break; |
627 | case 'invite_only_channel': | |
aa191535 | 628 | front.tabviews[t_view].addMsg(null, ' ', '=== ' + data.channel + ' is invite only.', 'status'); |
a3364605 JA |
629 | if (t_view !== 'server') { |
630 | front.tabviews[t_view].safe_to_close = true; | |
631 | } | |
81122538 JA |
632 | break; |
633 | case 'channel_is_full': | |
aa191535 | 634 | front.tabviews[t_view].addMsg(null, ' ', '=== ' + data.channel + ' is full.', 'status'); |
a3364605 JA |
635 | if (t_view !== 'server') { |
636 | front.tabviews[t_view].safe_to_close = true; | |
637 | } | |
81122538 | 638 | break; |
818ef327 JA |
639 | case 'chanop_privs_needed': |
640 | front.tabviews[data.channel].addMsg(null, ' ', '=== ' + data.reason, 'status'); | |
641 | break; | |
642 | case 'no_such_nick': | |
643 | front.tabviews.server.addMsg(null, ' ', '=== ' + data.nick + ': ' + data.reason, 'status'); | |
644 | break; | |
337c9866 D |
645 | case 'nickname_in_use': |
646 | front.tabviews.server.addMsg(null, ' ', '=== The nickname ' + data.nick + ' is already in use. Please select a new nickname', 'status'); | |
647 | front.showChangeNick(); | |
648 | break; | |
81122538 | 649 | default: |
a3364605 | 650 | // We don't know what data contains, so don't do anything with it. |
3f4aa6a5 | 651 | //front.tabviews.server.addMsg(null, ' ', '=== ' + data, 'status'); |
81122538 JA |
652 | } |
653 | }, | |
654 | ||
a3364605 JA |
655 | registerKeys: function () { |
656 | $('#kiwi_msginput').bind('keydown', function (e) { | |
657 | var windows, meta, num, msg, data, candidates, word_pos, word; | |
658 | windows = $('#windows'); | |
659 | //var meta = e.altKey; | |
660 | meta = e.ctrlKey; | |
661 | ||
662 | switch (true) { | |
663 | case (e.which >= 48) && (e.which <= 57): | |
664 | if (meta) { | |
665 | num = e.which - 48; | |
666 | if (num === 0) { | |
667 | num = 10; | |
668 | } | |
669 | num = num - 1; | |
670 | front.windowsShow(num); | |
671 | return false; | |
672 | } | |
673 | break; | |
674 | case e.which === 27: // escape | |
675 | return false; | |
676 | case e.which === 13: // return | |
677 | msg = $('#kiwi_msginput').val(); | |
678 | msg = msg.trim(); | |
679 | ||
680 | front.buffer.push(msg); | |
681 | front.buffer_pos = front.buffer.length; | |
682 | ||
683 | front.run(msg); | |
684 | $('#kiwi_msginput').val(''); | |
685 | ||
686 | break; | |
687 | case e.which === 33: // page up | |
688 | console.log("page up"); | |
689 | windows[0].scrollTop = windows[0].scrollTop - windows.height(); | |
690 | return false; | |
691 | case e.which === 34: // page down | |
692 | windows[0].scrollTop = windows[0].scrollTop + windows.height(); | |
693 | return false; | |
694 | case e.which === 37: // left | |
695 | if (meta) { | |
696 | front.windowsPrevious(); | |
697 | return false; | |
698 | } | |
699 | break; | |
700 | case e.which === 38: // up | |
701 | if (front.buffer_pos > 0) { | |
702 | front.buffer_pos--; | |
703 | $('#kiwi_msginput').val(front.buffer[front.buffer_pos]); | |
704 | } | |
705 | break; | |
706 | case e.which === 39: // right | |
707 | if (meta) { | |
708 | front.windowsNext(); | |
709 | return false; | |
710 | } | |
711 | break; | |
712 | case e.which === 40: // down | |
713 | if (front.buffer_pos < front.buffer.length) { | |
714 | front.buffer_pos++; | |
715 | $('#kiwi_msginput').val(front.buffer[front.buffer_pos]); | |
716 | } | |
717 | break; | |
718 | ||
719 | case e.which === 9: // tab | |
720 | // Get possible autocompletions | |
721 | data = []; | |
722 | front.cur_channel.userlist.children().each(function () { | |
723 | nick = front.nickStripPrefix($('a.nick', this).text()); | |
724 | data.push(nick); | |
725 | }); | |
726 | ||
727 | // Do the autocomplete | |
728 | if (this.value.length === this.selectionStart && this.value.length === this.selectionEnd) { | |
729 | candidates = []; | |
730 | ||
731 | word_pos = this.value.lastIndexOf(' '); | |
732 | word = ""; | |
733 | if (word_pos === -1) { | |
734 | word = this.value; | |
735 | } else { | |
736 | word = this.value.substr(word_pos); | |
737 | } | |
738 | word = word.trim(); | |
739 | ||
740 | // filter data to find only strings that start with existing value | |
741 | for (i = 0; i < data.length; i++) { | |
742 | if (data[i].indexOf(word) === 0 && data[i].length > word.length) { | |
743 | candidates.push(data[i]); | |
bad1ea63 | 744 | } |
a3364605 JA |
745 | } |
746 | ||
747 | if (candidates.length > 0) { | |
748 | // some candidates for autocompletion are found | |
749 | this.value = this.value.substring(0, word_pos) + ' ' + candidates[0] + ': '; | |
750 | this.selectionStart = this.value.length; | |
751 | } | |
752 | } | |
753 | return false; | |
754 | } | |
755 | }); | |
756 | ||
757 | ||
758 | $('#kiwi .control .msginput .nick').click(function () { | |
759 | front.showChangeNick(); | |
760 | }); | |
761 | ||
762 | ||
763 | ||
764 | ||
765 | ||
766 | $('#kiwi .plugins .load_plugin_file').click(function () { | |
767 | var lst, j, txt; | |
768 | if (typeof front.boxes.plugins !== "undefined") { | |
bad1ea63 JA |
769 | return; |
770 | } | |
a3364605 JA |
771 | |
772 | front.boxes.plugins = new Box("plugin_file"); | |
773 | $('#tmpl_plugins').tmpl({}).appendTo(front.boxes.plugins.content); | |
774 | front.boxes.plugins.box.css('top', -(front.boxes.plugins.height + 40)); | |
775 | ||
776 | // Populate the plugin list.. | |
4528138d D |
777 | function enumPlugins () { |
778 | lst = $('#plugin_list'); | |
779 | lst.find('option').remove(); | |
780 | for (j in plugs.loaded) { | |
781 | txt = plugs.loaded[j].name; | |
782 | lst.append('<option value="' + txt + '">' + txt + '</option>'); | |
783 | } | |
a3364605 | 784 | } |
4528138d | 785 | enumPlugins(); |
a3364605 JA |
786 | |
787 | // Event bindings | |
788 | $('#kiwi .plugin_file').submit(function () { | |
4528138d D |
789 | $('<div></div>').load($('.txtpluginfile').val(), function(e){ |
790 | enumPlugins(); | |
a3364605 JA |
791 | }); |
792 | return false; | |
793 | }); | |
794 | $('#kiwi .cancelpluginfile').click(function () { | |
795 | front.boxes.plugins.destroy(); | |
796 | }); | |
797 | ||
798 | $('#kiwi #plugins_list_unload').click(function () { | |
4528138d | 799 | var selected_plugin; |
a3364605 | 800 | selected_plugin = $('#plugin_list').val(); |
4528138d D |
801 | plugs.unloadPlugin(selected_plugin); |
802 | enumPlugins(); | |
a3364605 JA |
803 | }); |
804 | ||
805 | $('#kiwi .txtpluginfile').focus(); | |
806 | ||
807 | }); | |
808 | ||
809 | $('#kiwi .plugins .reload_css').click(function () { | |
810 | var links = document.getElementsByTagName("link"), | |
811 | i; | |
812 | for (i = 0; i < links.length; i++) { | |
813 | if (links[i].rel === "stylesheet") { | |
814 | if (links[i].href.indexOf("?") === -1) { | |
815 | links[i].href += "?"; | |
816 | } | |
817 | links[i].href += "x"; | |
818 | } | |
819 | } | |
820 | }); | |
821 | ||
822 | ||
823 | $('#kiwi .about .about_close').click(function () { | |
824 | $('#kiwi .about').css('display', 'none'); | |
825 | }); | |
826 | ||
827 | ||
828 | $('#kiwi .poweredby').click(function () { | |
829 | $('#kiwi .about').css('display', 'block'); | |
830 | }); | |
831 | ||
832 | }, | |
833 | ||
834 | ||
835 | showChangeNick: function () { | |
836 | $('#kiwi').append($('#tmpl_change_nick').tmpl({})); | |
837 | ||
838 | $('#kiwi .form_newnick').submit(function () { | |
839 | front.run('/NICK ' + $('#kiwi .txtnewnick').val()); | |
840 | $('#kiwi .newnick').remove(); | |
841 | return false; | |
842 | }); | |
843 | ||
844 | $('#kiwi .txtnewnick').keypress(function (ev) { | |
845 | if (!this.first_press) { | |
846 | this.first_press = true; | |
847 | return false; | |
848 | } | |
849 | }); | |
850 | ||
851 | $('#kiwi .txtnewnick').keydown(function (ev) { | |
852 | if (ev.which === 27) { // ESC | |
853 | $('#kiwi_msginput').focus(); | |
854 | $('#kiwi .newnick').remove(); | |
855 | } | |
856 | }); | |
857 | ||
858 | $('#kiwi .cancelnewnick').click(function () { | |
859 | $('#kiwi .newnick').remove(); | |
860 | }); | |
861 | ||
862 | $('#kiwi .txtnewnick').focus(); | |
863 | }, | |
864 | ||
865 | ||
866 | tabviewExists: function (name) { | |
867 | return (typeof front.tabviews[name.toLowerCase()] !== 'undefined'); | |
868 | }, | |
869 | ||
870 | tabviewAdd: function (v_name) { | |
875d0c71 D |
871 | var re, htmlsafe_name, tmp_divname, tmp_userlistname, tmp_tabname, userlist_enabled = true; |
872 | ||
a3364605 JA |
873 | if (v_name.charAt(0) === gateway.channel_prefix) { |
874 | re = new RegExp(gateway.channel_prefix, "g"); | |
875 | htmlsafe_name = v_name.replace(re, 'pre'); | |
876 | htmlsafe_name = "chan_" + htmlsafe_name; | |
877 | } else { | |
878 | htmlsafe_name = 'query_' + v_name; | |
875d0c71 | 879 | userlist_enabled = false; |
a3364605 JA |
880 | } |
881 | ||
882 | tmp_divname = 'kiwi_window_' + htmlsafe_name; | |
883 | tmp_userlistname = 'kiwi_userlist_' + htmlsafe_name; | |
884 | tmp_tabname = 'kiwi_tab_' + htmlsafe_name; | |
885 | ||
886 | if (!front.tabviewExists(v_name)) { | |
887 | $('#kiwi .windows .scroller').append('<div id="' + tmp_divname + '" class="messages"></div>'); | |
888 | $('#kiwi .userlist').append('<ul id="' + tmp_userlistname + '"></ul>'); | |
889 | $('#kiwi .windowlist ul').append('<li id="' + tmp_tabname + '" onclick="front.tabviews[\'' + v_name.toLowerCase() + '\'].show();">' + v_name + '</li>'); | |
890 | } | |
891 | //$('#kiwi .windowlist ul .window_'+v_name).click(function(){ front.windowShow(v_name); }); | |
892 | //front.windowShow(v_name); | |
893 | ||
894 | front.tabviews[v_name.toLowerCase()] = new Tabview(); | |
895 | front.tabviews[v_name.toLowerCase()].name = v_name; | |
896 | front.tabviews[v_name.toLowerCase()].div = $('#' + tmp_divname); | |
897 | front.tabviews[v_name.toLowerCase()].userlist = $('#' + tmp_userlistname); | |
898 | front.tabviews[v_name.toLowerCase()].tab = $('#' + tmp_tabname); | |
875d0c71 | 899 | if (!userlist_enabled) front.tabviews[v_name.toLowerCase()].userlist_width = 0; |
a3364605 JA |
900 | front.tabviews[v_name.toLowerCase()].show(); |
901 | ||
902 | if (typeof registerTouches === "function") { | |
903 | //alert("Registering touch interface"); | |
904 | //registerTouches($('#'+tmp_divname)); | |
905 | registerTouches(document.getElementById(tmp_divname)); | |
906 | } | |
907 | /* | |
908 | front.tabviews[v_name.toLowerCase()].userlist.click(function(){ | |
909 | alert($(this).attr('id')); | |
910 | }); | |
911 | */ | |
912 | ||
913 | front.doLayoutSize(); | |
914 | }, | |
915 | ||
916 | ||
917 | userClick: function (item) { | |
ef885bff JA |
918 | var li = $(item).parent(); |
919 | ||
a3364605 JA |
920 | // Remove any existing userboxes |
921 | $('#kiwi .userbox').remove(); | |
ef885bff JA |
922 | |
923 | if ($(li).data('userbox') === item) { | |
924 | $(li).removeData('userbox'); | |
925 | } else { | |
926 | $('#tmpl_user_box').tmpl({nick: front.nickStripPrefix($(item).text())}).appendTo(li); | |
927 | ||
928 | $('#kiwi .userbox .userbox_query').click(function (ev) { | |
929 | var nick = $('#kiwi .userbox_nick').val(); | |
930 | front.run('/query ' + nick); | |
931 | }); | |
932 | ||
933 | $('#kiwi .userbox .userbox_whois').click(function (ev) { | |
934 | var nick = $('#kiwi .userbox_nick').val(); | |
935 | front.run('/whois ' + nick); | |
936 | }); | |
937 | $(li).data('userbox', item); | |
938 | } | |
a3364605 JA |
939 | }, |
940 | ||
941 | ||
942 | sync: function () { | |
943 | gateway.sync(); | |
944 | }, | |
945 | ||
946 | onSync: function (e, data) { | |
947 | // Set any settings | |
948 | if (data.nick !== undefined) { | |
949 | gateway.nick = data.nick; | |
950 | } | |
951 | ||
952 | // Add the tabviews | |
953 | if (data.tabviews !== undefined) { | |
954 | $.each(data.tabviews, function (i, tab) { | |
955 | if (!front.tabviewExists(tab.name)) { | |
956 | front.tabviewAdd(gateway.channel_prefix + tab.name); | |
957 | ||
958 | if (tab.userlist !== undefined) { | |
959 | front.onUserList({'channel': gateway.channel_prefix + tab.name, 'users': tab.userlist}); | |
960 | } | |
961 | } | |
962 | }); | |
963 | } | |
964 | ||
965 | front.doLayout(); | |
966 | }, | |
967 | ||
968 | ||
969 | setTopicText: function (new_topic) { | |
110ce6d4 | 970 | front.original_topic = new_topic; |
a3364605 JA |
971 | $('#kiwi .cur_topic .topic').text(new_topic); |
972 | front.doLayoutSize(); | |
973 | }, | |
974 | ||
975 | ||
976 | ||
977 | ||
978 | ||
979 | ||
980 | ||
981 | nickStripPrefix: function (nick) { | |
982 | var tmp = nick, i; | |
983 | ||
984 | prefix = tmp.charAt(0); | |
985 | for (i in gateway.user_prefixes) { | |
986 | if (gateway.user_prefixes[i].symbol !== prefix) { | |
987 | continue; | |
988 | } | |
989 | return tmp.substring(1); | |
990 | } | |
991 | ||
992 | return tmp; | |
993 | }, | |
994 | ||
995 | nickGetPrefix: function (nick) { | |
996 | var tmp = nick, i; | |
997 | ||
998 | prefix = tmp.charAt(0); | |
999 | for (i in gateway.user_prefixes) { | |
1000 | if (gateway.user_prefixes[i].symbol === prefix) { | |
1001 | return prefix; | |
1002 | } | |
1003 | } | |
1004 | ||
1005 | return ''; | |
1006 | }, | |
1007 | ||
1008 | isChannel: function (name) { | |
1009 | prefix = name.charAt(0); | |
1010 | if (gateway.channel_prefix.indexOf(prefix) > -1) { | |
1011 | is_chan = true; | |
1012 | } else { | |
1013 | is_chan = false; | |
1014 | } | |
1015 | ||
1016 | return is_chan; | |
1017 | }, | |
1018 | ||
1019 | tabviewsNext: function () { | |
1020 | var wl = $('#kiwi .windowlist ul'), | |
1021 | next_left = parseInt(wl.css('text-indent').replace('px', ''), 10) + 170; | |
1022 | wl.css('text-indent', next_left); | |
1023 | }, | |
1024 | ||
1025 | tabviewsPrevious: function () { | |
1026 | var wl = $('#kiwi .windowlist ul'), | |
1027 | next_left = parseInt(wl.css('text-indent').replace('px', ''), 10) - 170; | |
1028 | wl.css('text-indent', next_left); | |
1029 | }, | |
1030 | ||
1031 | windowsNext: function () { | |
1032 | var tab, next; | |
1033 | next = false; | |
1034 | for (tab in front.tabviews) { | |
1035 | if (!next) { | |
1036 | if (front.tabviews[tab] === front.cur_channel) { | |
1037 | next = true; | |
1038 | continue; | |
1039 | } | |
1040 | } else { | |
1041 | front.tabviews[tab].show(); | |
1042 | return; | |
1043 | } | |
1044 | } | |
1045 | }, | |
1046 | ||
1047 | windowsPrevious: function () { | |
1048 | var tab, prev_tab, next; | |
1049 | next = false; | |
1050 | for (tab in front.tabviews) { | |
1051 | if (front.tabviews[tab] === front.cur_channel) { | |
1052 | if (prev_tab) { | |
1053 | prev_tab.show(); | |
1054 | } | |
1055 | return; | |
1056 | } | |
1057 | prev_tab = front.tabviews[tab]; | |
1058 | } | |
1059 | }, | |
1060 | ||
1061 | windowsShow: function (num) { | |
1062 | num = parseInt(num, 10); | |
1063 | console.log('Showing window ' + num.toString()); | |
1064 | var i = 0, tab; | |
1065 | for (tab in front.tabviews) { | |
1066 | if (i === num) { | |
1067 | front.tabviews[tab].show(); | |
1068 | return; | |
1069 | } | |
1070 | i++; | |
1071 | } | |
c432172c D |
1072 | }, |
1073 | ||
1074 | ||
1075 | ||
1076 | barsShow: function () { | |
1077 | $('#kiwi .toolbars').slideDown(); | |
1078 | $('#kiwi .control').slideDown(); | |
1079 | }, | |
1080 | ||
1081 | barsHide: function () { | |
1082 | $('#kiwi .toolbars').slideUp(); | |
1083 | $('#kiwi .control').slideUp(); | |
a3364605 JA |
1084 | } |
1085 | }; | |
54f4a22e D |
1086 | |
1087 | ||
1088 | ||
1089 | ||
1090 | ||
1091 | ||
1092 | ||
1093 | ||
1094 | ||
1095 | ||
1096 | ||
1097 | ||
1098 | ||
1099 | ||
1100 | ||
1101 | ||
1bb74900 D |
1102 | /* |
1103 | * MISC VIEW | |
1104 | */ | |
1105 | ||
604c5174 D |
1106 | var Utilityview = function (name) { |
1107 | var rand_name = randomString(15); | |
1108 | var tmp_divname = 'kiwi_window_' + rand_name; | |
1109 | var tmp_userlistname = 'kiwi_userlist_' + rand_name; | |
1110 | var tmp_tabname = 'kiwi_tab_' + rand_name; | |
1bb74900 | 1111 | |
604c5174 D |
1112 | this.name = rand_name; |
1113 | this.title = name; | |
1114 | this.topic = ' '; | |
1bb74900 | 1115 | |
604c5174 D |
1116 | $('#kiwi .windows .scroller').append('<div id="' + tmp_divname + '" class="messages"></div>'); |
1117 | ||
1118 | this.tab = $('<li id="' + tmp_tabname + '">' + this.title + '</li>'); | |
1119 | this.tab.click(function(){ | |
1120 | front.utilityviews[rand_name.toLowerCase()].show(); | |
1121 | }); | |
1122 | $('#kiwi .utilityviewlist ul').append(this.tab); | |
1bb74900 D |
1123 | |
1124 | this.div = $('#' + tmp_divname); | |
1125 | this.div.css('overflow', 'hidden'); | |
1126 | ||
604c5174 | 1127 | front.utilityviews[rand_name.toLowerCase()] = this; |
1bb74900 D |
1128 | }; |
1129 | ||
1130 | Utilityview.prototype.name = null; | |
604c5174 | 1131 | Utilityview.prototype.title = null; |
1bb74900 D |
1132 | Utilityview.prototype.div = null; |
1133 | Utilityview.prototype.tab = null; | |
ca7e63ea | 1134 | Utilityview.prototype.topic = ' '; |
1bb74900 D |
1135 | Utilityview.prototype.show = function () { |
1136 | $('#kiwi .messages').removeClass("active"); | |
1137 | $('#kiwi .userlist ul').removeClass("active"); | |
2b9dcc03 | 1138 | $('#kiwi .toolbars ul li').removeClass("active"); |
1bb74900 D |
1139 | |
1140 | $('#windows').css('overflow-y', 'hidden'); | |
2b9dcc03 | 1141 | $('#windows').css('right', 0); |
1bb74900 D |
1142 | // Activate this tab! |
1143 | this.div.addClass('active'); | |
1144 | this.tab.addClass('active'); | |
1145 | ||
1146 | this.addPartImage(); | |
1147 | ||
ca7e63ea | 1148 | front.setTopicText(this.topic); |
1bb74900 D |
1149 | front.cur_channel = this; |
1150 | ||
1151 | // If we're using fancy scrolling, refresh it | |
1152 | if (touch_scroll) { | |
1153 | touch_scroll.refresh(); | |
1154 | } | |
1155 | } | |
1156 | ||
1157 | Utilityview.prototype.close = function () { | |
1158 | this.div.remove(); | |
1159 | this.tab.remove(); | |
1160 | ||
1161 | if (front.cur_channel === this) { | |
1162 | front.tabviews.server.show(); | |
1163 | } | |
604c5174 | 1164 | delete front.utilityviews[this.name.toLowerCase()]; |
1bb74900 D |
1165 | }; |
1166 | ||
1167 | Utilityview.prototype.addPartImage = function () { | |
1168 | this.clearPartImage(); | |
1169 | ||
1170 | // We can't close this tab, so don't have the close image | |
1171 | if (this.name === 'server') { | |
1172 | return; | |
1173 | } | |
1174 | ||
8397d902 | 1175 | var del_html = '<img src="/img/redcross.png" class="tab_part" />'; |
1bb74900 D |
1176 | this.tab.append(del_html); |
1177 | ||
1178 | $('.tab_part', this.tab).click(function () { | |
1179 | if (front.cur_channel.name !== 'server') { | |
1180 | front.cur_channel.close(); | |
1181 | } | |
1182 | }); | |
1183 | }; | |
1184 | ||
1185 | Utilityview.prototype.clearPartImage = function () { | |
2b9dcc03 | 1186 | $('#kiwi .toolbars .tab_part').remove(); |
1bb74900 D |
1187 | }; |
1188 | ||
1189 | ||
1190 | ||
1191 | ||
54f4a22e D |
1192 | |
1193 | /* | |
1194 | * | |
1195 | * TABVIEWS | |
1196 | * | |
1197 | */ | |
1198 | ||
1199 | ||
a3364605 JA |
1200 | var Tabview = function () {}; |
1201 | Tabview.prototype.name = null; | |
1202 | Tabview.prototype.div = null; | |
1203 | Tabview.prototype.userlist = null; | |
875d0c71 | 1204 | Tabview.prototype.userlist_width = 100; // 0 for disabled |
a3364605 JA |
1205 | Tabview.prototype.tab = null; |
1206 | Tabview.prototype.topic = ""; | |
1207 | Tabview.prototype.safe_to_close = false; // If we have been kicked/banned/etc from this channel, don't wait for a part message | |
1208 | ||
1209 | Tabview.prototype.show = function () { | |
875d0c71 D |
1210 | var w, u; |
1211 | ||
a3364605 JA |
1212 | $('#kiwi .messages').removeClass("active"); |
1213 | $('#kiwi .userlist ul').removeClass("active"); | |
2b9dcc03 | 1214 | $('#kiwi .toolbars ul li').removeClass("active"); |
a3364605 | 1215 | |
875d0c71 D |
1216 | w = $('#windows'); |
1217 | u = $('#kiwi .userlist'); | |
1218 | ||
1219 | w.css('overflow-y', 'scroll'); | |
1220 | ||
1221 | // Set the window size accordingly | |
1222 | if (this.userlist_width > 0) { | |
1223 | u.width(this.userlist_width); | |
1224 | w.css('right', u.outerWidth(true)); | |
1225 | } else { | |
1226 | w.css('right', 0); | |
1227 | } | |
1bb74900 | 1228 | |
a3364605 JA |
1229 | // Activate this tab! |
1230 | this.div.addClass('active'); | |
875d0c71 | 1231 | if (this.userlist_width > 0) this.userlist.addClass('active'); |
a3364605 JA |
1232 | this.tab.addClass('active'); |
1233 | ||
a3364605 JA |
1234 | // Add the part image to the tab |
1235 | this.addPartImage(); | |
1236 | ||
1237 | this.clearHighlight(); | |
1238 | front.setTopicText(this.topic); | |
1239 | front.cur_channel = this; | |
1240 | ||
1241 | // If we're using fancy scrolling, refresh it | |
1242 | if (touch_scroll) { | |
1243 | touch_scroll.refresh(); | |
1244 | } | |
1245 | ||
1246 | this.scrollBottom(); | |
1247 | if (!touchscreen) { | |
1248 | $('#kiwi_msginput').focus(); | |
1249 | } | |
1250 | }; | |
1251 | ||
1252 | Tabview.prototype.close = function () { | |
1253 | this.div.remove(); | |
1254 | this.userlist.remove(); | |
1255 | this.tab.remove(); | |
1256 | ||
1257 | if (front.cur_channel === this) { | |
1258 | front.tabviews.server.show(); | |
1259 | } | |
1260 | delete front.tabviews[this.name.toLowerCase()]; | |
1261 | }; | |
1262 | ||
1263 | Tabview.prototype.addPartImage = function () { | |
1264 | this.clearPartImage(); | |
1265 | ||
1266 | // We can't close this tab, so don't have the close image | |
1267 | if (this.name === 'server') { | |
1268 | return; | |
1269 | } | |
1270 | ||
8397d902 | 1271 | var del_html = '<img src="/img/redcross.png" class="tab_part" />'; |
a3364605 JA |
1272 | this.tab.append(del_html); |
1273 | ||
1274 | $('.tab_part', this.tab).click(function () { | |
1275 | if (front.isChannel($(this).parent().text())) { | |
1276 | front.run("/part"); | |
1277 | } else { | |
1278 | // Make sure we don't close the server tab | |
1279 | if (front.cur_channel.name !== 'server') { | |
1280 | front.cur_channel.close(); | |
1281 | } | |
1282 | } | |
1283 | }); | |
1284 | }; | |
1285 | ||
1286 | Tabview.prototype.clearPartImage = function () { | |
2b9dcc03 | 1287 | $('#kiwi .toolbars .tab_part').remove(); |
a3364605 JA |
1288 | }; |
1289 | ||
1290 | Tabview.prototype.addMsg = function (time, nick, msg, type, style) { | |
ca7e63ea | 1291 | var self, tmp, plugin_ret, i, d, re, line_msg; |
a3364605 JA |
1292 | |
1293 | self = this; | |
1294 | ||
ca7e63ea D |
1295 | tmp = {msg: msg, time: time, nick: nick, tabview: this.name}; |
1296 | tmp = plugs.run('addmsg', tmp); | |
1297 | if (!tmp) return; | |
a3364605 | 1298 | |
ca7e63ea D |
1299 | |
1300 | msg = tmp.msg; | |
1301 | time = tmp.time; | |
1302 | nick = tmp.nick; | |
1303 | ||
a3364605 JA |
1304 | if (time === null) { |
1305 | d = new Date(); | |
1306 | time = d.getHours().toString().lpad(2, "0") + ":" + d.getMinutes().toString().lpad(2, "0") + ":" + d.getSeconds().toString().lpad(2, "0"); | |
1307 | } | |
1308 | ||
1309 | // The CSS class (action, topic, notice, etc) | |
1310 | if (typeof type !== "string") { | |
1311 | type = ''; | |
1312 | } | |
1313 | ||
1314 | // Make sure we don't have NaN or something | |
1315 | if (typeof msg !== "string") { | |
1316 | msg = ''; | |
1317 | } | |
1318 | ||
1319 | // Text formatting | |
1320 | // bold | |
1321 | if (msg.indexOf(String.fromCharCode(2)) !== -1) { | |
1322 | next = '<b>'; | |
1323 | while (msg.indexOf(String.fromCharCode(2)) !== -1) { | |
1324 | msg = msg.replace(String.fromCharCode(2), next); | |
1325 | next = (next === '<b>') ? '</b>' : '<b>'; | |
1326 | } | |
1327 | if (next === '</b>') { | |
1328 | msg = msg + '</b>'; | |
1329 | } | |
1330 | } | |
1331 | ||
1332 | // Wierd thing noticed by Dux0r on the irc.esper.net server | |
1333 | if (typeof msg !== "string") { | |
1334 | msg = ''; | |
1335 | } | |
1336 | ||
1337 | // underline | |
1338 | if (msg.indexOf(String.fromCharCode(31)) !== -1) { | |
1339 | next = '<u>'; | |
1340 | while (msg.indexOf(String.fromCharCode(31)) !== -1) { | |
1341 | msg = msg.replace(String.fromCharCode(31), next); | |
1342 | next = (next === '<u>') ? '</u>' : '<u>'; | |
1343 | } | |
1344 | if (next === '</u>') { | |
1345 | msg = msg + '</u>'; | |
1346 | } | |
1347 | } | |
1348 | ||
ca7e63ea | 1349 | // Make the channels clickable |
a3364605 | 1350 | re = new RegExp('\\B(' + gateway.channel_prefix + '[^ ,.\\007]+)', 'g'); |
a3364605 | 1351 | msg = msg.replace(re, function (match) { |
ca7e63ea | 1352 | return '<a class="chan">' + match + '</a>'; |
9545f343 JA |
1353 | }); |
1354 | ||
ca7e63ea D |
1355 | // Build up and add the line |
1356 | line_msg = $('<div class="msg ' + type + '"><div class="time">' + time + '</div><div class="nick">' + nick + '</div><div class="text" style="' + style + '">' + msg + ' </div></div>'); | |
a3364605 JA |
1357 | this.div.append(line_msg); |
1358 | ||
1359 | if (!touchscreen) { | |
1360 | this.scrollBottom(); | |
1361 | } else { | |
1362 | touch_scroll.refresh(); | |
1363 | //console.log(this.div.attr("scrollHeight") +" - "+ $('#windows').height()); | |
1364 | this.scrollBottom(); | |
1365 | //if(this.div.attr("scrollHeight") > $('#windows').height()){ | |
1366 | // touch_scroll.scrollTo(0, this.div.height()); | |
1367 | //} | |
1368 | } | |
1369 | }; | |
1370 | ||
1371 | Tabview.prototype.scrollBottom = function () { | |
1372 | var w = $('#windows'); | |
1373 | w[0].scrollTop = w[0].scrollHeight; | |
1374 | }; | |
1375 | ||
1376 | Tabview.prototype.changeNick = function (newNick, oldNick) { | |
1377 | this.userlist.children().each(function () { | |
1378 | var item = $('a.nick', this); | |
1379 | if (front.nickStripPrefix(item.text()) === oldNick) { | |
1380 | item.text(front.nickGetPrefix(item.text()) + newNick); | |
1381 | document.temp_chan = 1; | |
1382 | } | |
1383 | }); | |
1384 | ||
1385 | if (typeof document.temp_chan !== "undefined") { | |
1386 | this.addMsg(null, ' ', '=== ' + oldNick + ' is now known as ' + newNick, 'action'); | |
1387 | delete document.temp_chan; | |
1388 | this.userlistSort(); | |
1389 | } | |
1390 | }; | |
1391 | ||
1392 | Tabview.prototype.userlistSort = function () { | |
1393 | var ul = this.userlist, | |
1394 | listitems = ul.children('li').get(); | |
1395 | listitems.sort(function (a, b) { | |
1396 | var compA = $(a).text().toUpperCase(), | |
1397 | compB = $(b).text().toUpperCase(), | |
1398 | i; | |
1399 | ||
1400 | // Sort by prefixes first | |
1401 | for (i in gateway.user_prefixes) { | |
1402 | prefix = gateway.user_prefixes[i].symbol; | |
1403 | ||
1404 | if (compA.charAt(0) === prefix && compB.charAt(0) === prefix) { | |
1405 | // Both have the same prefix, string compare time | |
1406 | return 0; | |
1407 | } | |
1408 | ||
1409 | if (compA.charAt(0) === prefix && compB.charAt(0) !== prefix) { | |
1410 | return -1; | |
1411 | } | |
1412 | ||
1413 | if (compA.charAt(0) !== prefix && compB.charAt(0) === prefix) { | |
1414 | return 1; | |
1415 | } | |
1416 | } | |
1417 | ||
1418 | // No prefixes, compare by string | |
1419 | return (compA < compB) ? -1 : (compA > compB) ? 1 : 0; | |
1420 | }); | |
1421 | $.each(listitems, function (idx, itm) { ul.append(itm); }); | |
1422 | }; | |
1423 | ||
1424 | Tabview.prototype.highlight = function () { | |
1425 | this.tab.addClass('highlight'); | |
1426 | }; | |
1427 | Tabview.prototype.activity = function () { | |
1428 | this.tab.addClass('activity'); | |
1429 | }; | |
1430 | Tabview.prototype.clearHighlight = function () { | |
1431 | this.tab.removeClass('highlight'); | |
1432 | this.tab.removeClass('activity'); | |
1433 | }; | |
1434 | Tabview.prototype.changeTopic = function (new_topic) { | |
1435 | this.topic = new_topic; | |
1436 | this.addMsg(null, ' ', '=== Topic for ' + this.name + ' is: ' + new_topic, 'topic'); | |
1437 | if (front.cur_channel.name === this.name) { | |
1438 | front.setTopicText(new_topic); | |
1439 | } | |
1440 | }; | |
1441 | ||
1442 | ||
1443 | ||
1444 | ||
1445 | ||
1446 | var Box = function (classname) { | |
1447 | this.id = randomString(10); | |
1448 | var tmp = $('<div id="' + this.id + '" class="box ' + classname + '"><div class="boxarea"></div></div>'); | |
1449 | $('#kiwi').append(tmp); | |
1450 | this.box = $('#' + this.id); | |
1451 | this.content = $('#' + this.id + ' .boxarea'); | |
1452 | ||
1453 | this.box.draggable({ stack: ".box" }); | |
1454 | this.content.click(function () {}); | |
1455 | //this.box.click(function(){ $(this)..css }); | |
1456 | }; | |
1457 | Box.prototype.create = function (name, classname) { | |
1458 | ||
1459 | }; | |
1460 | Box.prototype.id = null; | |
1461 | Box.prototype.box = null; | |
1462 | Box.prototype.content = null; | |
1463 | Box.prototype.destroy = function () { | |
1464 | var name; | |
1465 | this.box.remove(); | |
1466 | for (name in front.boxes) { | |
1467 | if (front.boxes[name].id === this.id) { | |
1468 | delete front.boxes[name]; | |
1469 | } | |
1470 | } | |
1471 | }; | |
1472 | Box.prototype.height = function () { | |
1473 | return this.box.height(); | |
1474 | }; |