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