Re-clicking a nick will now collapse the userbox
[KiwiIRC.git] / js / front.js
index 5e7f3286e9fe4852c1cdf9d68adedc450bac3632..6b48ecc0d33875c914312e9ff63b6031d17ed350 100644 (file)
@@ -69,7 +69,7 @@ var front = {
 
         front.registerKeys();
         
-        $('#kiwi .cur_topic').resize(front.doLayoutSize);
+        $('#kiwi .toolbars').resize(front.doLayoutSize);
 
         $('#kiwi .formconnectwindow').submit(function () {
             var netsel = $('#kiwi .formconnectwindow .network'),
@@ -84,6 +84,9 @@ var front = {
             
             tmp = nick.val().split(' ');
             gateway.nick = tmp[0];
+
+            init_data.channel = $('#channel').val();
+
             front.doLayout();
             try {
                 front.run('/connect ' + netsel.val());
@@ -91,7 +94,7 @@ var front = {
                 alert(e);
             }
             
-            $('#kiwi .connectwindow').slideUp();
+            $('#kiwi .connectwindow').slideUp('', front.barsShow);
             $('#windows').click(function () { $('#kiwi_msginput').focus(); });
 
             return false;
@@ -99,19 +102,30 @@ var front = {
 
         supportsOrientationChange = (typeof window.onorientationchange !==  undefined);
         orientationEvent = supportsOrientationChange ? "orientationchange" : "resize";
-        window.addEventListener(orientationEvent, front.doLayoutSize, false);
+        if (window.addEventListener) {
+            window.addEventListener(orientationEvent, front.doLayoutSize, false);
+        } else {
+            // < IE9
+            window.attachEvent(orientationEvent, front.doLayoutSize, false);
+        }
         //$('#kiwi').bind("resize", front.doLayoutSize, false);
 
         front.doLayout();
-        //front.windowAdd('server');
+        front.barsHide();
+        
         front.tabviewAdd('server');
+        front.tabviews.server.userlist_width = 0; // Disable the userlist
         
         // Any pre-defined nick?
-        if (typeof init_data.nick === "string") {
+        if (typeof window.init_data.nick === "string") {
             $('#kiwi .formconnectwindow .nick').val(init_data.nick);
         }
+
+        // Any pre-defined channels?
+        if (typeof window.init_data.channel === 'string') {
+            $('#channel').val(init_data.channel);
+        }
         
-        //gateway.session_id = 'testses';
         
         $('#kiwi .cur_topic').keydown(function (e) {
             if (e.which === 13) {
@@ -174,6 +188,7 @@ var front = {
             
             switch (e.target.className) {
             case 'link_ext':
+            case 'link_img_a':
                 return true;
                 break;
             case 'link_ext_browser':
@@ -187,8 +202,9 @@ var front = {
     },
     
     doLayoutSize: function () {
-        var kiwi, ct, ul, n_top, n_bottom;
+        var kiwi, toolbars, ul, n_top, n_bottom;
         kiwi = $('#kiwi');
+
         if (kiwi.width() < 330 && !kiwi.hasClass('small_kiwi')) {
             console.log("switching to small kiwi");
             kiwi.removeClass('large_kiwi');
@@ -198,16 +214,10 @@ var front = {
             kiwi.addClass('large_kiwi');
         }
 
-        ct = $('#kiwi .cur_topic');
+        toolbars = $('#kiwi .cur_topic');
         ul = $('#kiwi .userlist');
 
-        n_top = parseInt(ct.offset().top, 10) + parseInt(ct.height(), 10);
-        n_top = n_top + parseInt(ct.css('border-top-width').replace('px', ''), 10);
-        n_top = n_top + parseInt(ct.css('border-bottom-width').replace('px', ''), 10);
-        n_top = n_top + parseInt(ct.css('padding-top').replace('px', ''), 10);
-        n_top = n_top + parseInt(ct.css('padding-bottom').replace('px', ''), 10);
-        n_top += 1; // Dunno why this is needed.. but it's always 1 px out :/
-
+        n_top = parseInt(toolbars.offset().top, 10) + parseInt(toolbars.outerHeight(true), 10);
         n_bottom = $(document).height() - parseInt($('#kiwi .control').offset().top, 10);
 
         $('#kiwi .windows').css({top: n_top + 'px', bottom: n_bottom + 'px'});
@@ -238,7 +248,16 @@ var front = {
     
     
     run: function (msg) {
-        var parts, dest, t, pos, textRange, d;
+        var parts, dest, t, pos, textRange, d, plugin_event;
+        
+        // Run through any plugins
+        plugin_event = {command: msg};
+        plugin_event = plugs.run('command_run', plugin_event);
+        if (!plugin_event || typeof plugin_event.command === 'undefined') return;
+
+        // Update msg if it's been changed by any plugins
+        msg = plugin_event.command.toString();
+
         console.log("running " + msg);
         if (msg.substring(0, 1) === '/') {
             parts = msg.split(' ');
@@ -301,6 +320,21 @@ var front = {
                     front.tabviewAdd(parts[1]);
                 }
                 break;
+
+            
+            case '/m':
+            case '/msg':
+                if (typeof parts[1] !== "undefined") {
+                    var msg_sliced = msg.split(' ').slice(2).join(' ');
+                    gateway.msg(parts[1], msg_sliced);
+
+                    if (!front.tabviewExists(parts[1])) {
+                        front.tabviewAdd(parts[1]);
+                    }
+                    front.tabviews[parts[1].toLowerCase()].addMsg(null, gateway.nick, msg_sliced);
+                }
+                break;
+            
                 
             case '/quote':
                 gateway.raw(msg.replace(/^\/quote /i, ''));
@@ -370,7 +404,7 @@ var front = {
     
     
     onMsg: function (e, data) {
-        var destination;
+        var destination, plugin_event;
         // Is this message from a user?
         if (data.channel === gateway.nick) {
             destination = data.nick.toLowerCase();
@@ -378,10 +412,14 @@ var front = {
             destination = data.channel.toLowerCase();    
         }
         
-        if (!front.tabviewExists(destination)) {
-            front.tabviewAdd(destination);
+        plugin_event = {nick: data.nick, msg:data.msg, destination: destination};
+        plugin_event = plugs.run('msg_recieved', plugin_event);
+        if (!plugin_event) return;
+
+        if (!front.tabviewExists(plugin_event.destination)) {
+            front.tabviewAdd(plugin_event.destination);
         }
-        front.tabviews[destination].addMsg(null, data.nick, data.msg);
+        front.tabviews[plugin_event.destination].addMsg(null, plugin_event.nick, plugin_event.msg);
     },
     
     onDebug: function (e, data) {
@@ -413,15 +451,15 @@ var front = {
     },
     
     onNotice: function (e, data) {
-        var nick = (data.nick === undefined || data.nick === '') ? '' : '[' + data.nick + ']';
-        if (data.channel !== undefined) {
-            if (front.tabviewExists(data.channel)) {
-                front.tabviews[data.channel.toLowerCase()].addMsg(null, nick, data.msg, 'notice');
-            } else {
-                front.tabviews.server.addMsg(null, nick, data.msg, 'notice');
-            }
+        var nick = (data.nick === undefined) ? '' : data.nick;
+        var enick = '[' + nick + ']';
+
+        if (front.tabviewExists(data.target)) {
+            front.tabviews[data.target.toLowerCase()].addMsg(null, enick, data.msg, 'notice');
+        } else if (front.tabviewExists(nick)) {
+            front.tabviews[nick.toLowerCase()].addMsg(null, enick, data.msg, 'notice');
         } else {
-            front.tabviews.server.addMsg(null, nick, data.msg, 'notice');
+            front.tabviews.server.addMsg(null, enick, data.msg, 'notice');
         }
     },
     
@@ -446,23 +484,32 @@ var front = {
     
     onConnect: function (e, data) {
         if (data.connected) {
+            if (gateway.nick !== data.nick) {
+                gateway.nick = data.nick;
+                front.doLayout();
+            }
+
             front.tabviews.server.addMsg(null, ' ', '=== Connected OK :)', 'status');
             if (typeof init_data.channel === "string") {
                 front.joinChannel(init_data.channel);
             }
+            plugs.run('connect', {success: true});
         } else {
             front.tabviews.server.addMsg(null, ' ', '=== Failed to connect :(', 'status');
+            plugs.run('connect', {success: false});
         }
     },
     onConnectFail: function (e, data) {
         var reason = (typeof data.reason === 'string') ? data.reason : '';
         front.tabviews.server.addMsg(null, '', 'There\'s a problem connecting! (' + reason + ')', 'error');
+        plugs.run('connect', {success: false});
     },
     onDisconnect: function (e, data) {
         var tab;
         for (tab in front.tabviews) {
             front.tabviews[tab].addMsg(null, '', 'Disconnected from server!', 'error');
         }
+        plugs.run('disconnect', {success: false});
     },
     onOptions: function (e, data) {
         if (typeof gateway.network_name === "string" && gateway.network_name !== "") {
@@ -626,6 +673,10 @@ var front = {
         case 'no_such_nick':
             front.tabviews.server.addMsg(null, ' ', '=== ' + data.nick + ': ' + data.reason, 'status'); 
             break;
+        case 'nickname_in_use':
+            front.tabviews.server.addMsg(null, ' ', '=== The nickname ' + data.nick + ' is already in use. Please select a new nickname', 'status');
+            front.showChangeNick();
+            break;
         default:
             // We don't know what data contains, so don't do anything with it.
             //front.tabviews.server.addMsg(null, ' ', '=== ' + data, 'status');
@@ -861,13 +912,15 @@ var front = {
     },
     
     tabviewAdd: function (v_name) {
-        var re, htmlsafe_name, tmp_divname, tmp_userlistname, tmp_tabname;
+        var re, htmlsafe_name, tmp_divname, tmp_userlistname, tmp_tabname, userlist_enabled = true;
+
         if (v_name.charAt(0) === gateway.channel_prefix) {
             re = new RegExp(gateway.channel_prefix, "g");
             htmlsafe_name = v_name.replace(re, 'pre');
             htmlsafe_name = "chan_" + htmlsafe_name;
         } else {
             htmlsafe_name = 'query_' + v_name;
+            userlist_enabled = false;
         }
         
         tmp_divname = 'kiwi_window_' + htmlsafe_name;
@@ -887,6 +940,7 @@ var front = {
         front.tabviews[v_name.toLowerCase()].div = $('#' + tmp_divname);
         front.tabviews[v_name.toLowerCase()].userlist = $('#' + tmp_userlistname);
         front.tabviews[v_name.toLowerCase()].tab = $('#' + tmp_tabname);
+        if (!userlist_enabled) front.tabviews[v_name.toLowerCase()].userlist_width = 0;
         front.tabviews[v_name.toLowerCase()].show();
         
         if (typeof registerTouches === "function") {
@@ -905,27 +959,27 @@ var front = {
     
     
     userClick: function (item) {
+        var li = $(item).parent();
+
         // Remove any existing userboxes
         $('#kiwi .userbox').remove();
-        
-        var li = $(item).parent();
-        /*var html = '<div class="userbox">\
-    <input type="hidden" class="userbox_nick" value="' + front.nickStripPrefix($(item).text()) + '" />\
-    <a href="#" class="userbox_query">Message</a>\
-    <a href="#" class="userbox_whois">Info</a>\
-</div>';
-        li.append(html);*/
-        $('#tmpl_user_box').tmpl({nick: front.nickStripPrefix($(item).text())}).appendTo(li);
-        
-        $('#kiwi .userbox .userbox_query').click(function (ev) {
-            var nick = $('#kiwi .userbox_nick').val();
-            front.run('/query ' + nick);
-        });
-        
-        $('#kiwi .userbox .userbox_whois').click(function (ev) {
-            var nick = $('#kiwi .userbox_nick').val();
-            front.run('/whois ' + nick);
-        });
+
+        if ($(li).data('userbox') === item) {
+            $(li).removeData('userbox');
+        } else {
+            $('#tmpl_user_box').tmpl({nick: front.nickStripPrefix($(item).text())}).appendTo(li);
+            
+            $('#kiwi .userbox .userbox_query').click(function (ev) {
+                var nick = $('#kiwi .userbox_nick').val();
+                front.run('/query ' + nick);
+            });
+            
+            $('#kiwi .userbox .userbox_whois').click(function (ev) {
+                var nick = $('#kiwi .userbox_nick').val();
+                front.run('/whois ' + nick);
+            });
+            $(li).data('userbox', item);
+        }
     },
     
     
@@ -1059,6 +1113,18 @@ var front = {
             }
             i++;
         }
+    },
+
+
+
+    barsShow: function () {
+        $('#kiwi .toolbars').slideDown();
+        $('#kiwi .control').slideDown();
+    },
+
+    barsHide: function () {
+        $('#kiwi .toolbars').slideUp();
+        $('#kiwi .control').slideUp();
     }
 };
 
@@ -1088,10 +1154,11 @@ var Utilityview = function (name, src) {
     var tmp_tabname = 'kiwi_tab_' + name;
     
     this.name = name;
+    this.topic = src;
 
     if (!front.tabviewExists(name)) {
         $('#kiwi .windows .scroller').append('<div id="' + tmp_divname + '" class="messages"></div>');
-        $('#kiwi .windowlist ul').append('<li id="' + tmp_tabname + '" onclick="front.tabviews[\'' + name.toLowerCase() + '\'].show();">' + name + '</li>');
+        $('#kiwi .utilityviewlist ul').append('<li id="' + tmp_tabname + '" onclick="front.tabviews[\'' + name.toLowerCase() + '\'].show();">' + name + '</li>');
     }
     
     this.div = $('#' + tmp_divname);
@@ -1099,7 +1166,7 @@ var Utilityview = function (name, src) {
 
     this.tab = $('#' + tmp_tabname);
 
-    this.iframe = $('<iframe border="0" class="utility_view" src="http://google.com/" style="width:100%;height:100%;border:none;"></iframe>');
+    this.iframe = $('<iframe border="0" class="utility_view" src="" style="width:100%;height:100%;border:none;"></iframe>');
     if(src) this.iframe.attr('src', src);
     this.div.append(this.iframe);
 
@@ -1110,20 +1177,21 @@ Utilityview.prototype.name = null;
 Utilityview.prototype.div = null;
 Utilityview.prototype.tab = null;
 Utilityview.prototype.iframe = null;
+Utilityview.prototype.topic = ' ';
 Utilityview.prototype.show = function () {
     $('#kiwi .messages').removeClass("active");
     $('#kiwi .userlist ul').removeClass("active");
-    $('#kiwi .windowlist ul li').removeClass("active");
+    $('#kiwi .toolbars ul li').removeClass("active");
 
     $('#windows').css('overflow-y', 'hidden');
-
+    $('#windows').css('right', 0);
     // Activate this tab!
     this.div.addClass('active');
     this.tab.addClass('active');
 
     this.addPartImage();
 
-    front.setTopicText(' ');
+    front.setTopicText(this.topic);
     front.cur_channel = this;
 
     // If we're using fancy scrolling, refresh it
@@ -1161,7 +1229,7 @@ Utilityview.prototype.addPartImage = function () {
 };
 
 Utilityview.prototype.clearPartImage = function () {
-    $('#kiwi .windowlist .tab_part').remove();
+    $('#kiwi .toolbars .tab_part').remove();
 };
 
 
@@ -1179,20 +1247,34 @@ var Tabview = function () {};
 Tabview.prototype.name = null;
 Tabview.prototype.div = null;
 Tabview.prototype.userlist = null;
+Tabview.prototype.userlist_width = 100;     // 0 for disabled
 Tabview.prototype.tab = null;
 Tabview.prototype.topic = "";
 Tabview.prototype.safe_to_close = false;                // If we have been kicked/banned/etc from this channel, don't wait for a part message
 
 Tabview.prototype.show = function () {
+    var w, u;
+
     $('#kiwi .messages').removeClass("active");
     $('#kiwi .userlist ul').removeClass("active");
-    $('#kiwi .windowlist ul li').removeClass("active");
+    $('#kiwi .toolbars ul li').removeClass("active");
     
-    $('#windows').css('overflow-y', 'scroll');
+    w = $('#windows');
+    u = $('#kiwi .userlist');
+
+    w.css('overflow-y', 'scroll');
+
+    // Set the window size accordingly
+    if (this.userlist_width > 0) {
+        u.width(this.userlist_width);
+        w.css('right', u.outerWidth(true));
+    } else {
+        w.css('right', 0);
+    }
 
     // Activate this tab!
     this.div.addClass('active');
-    this.userlist.addClass('active');
+    if (this.userlist_width > 0) this.userlist.addClass('active');
     this.tab.addClass('active');
     
     // Add the part image to the tab
@@ -1248,39 +1330,23 @@ Tabview.prototype.addPartImage = function () {
 };
 
 Tabview.prototype.clearPartImage = function () {
-    $('#kiwi .windowlist .tab_part').remove();
+    $('#kiwi .toolbars .tab_part').remove();
 };
 
 Tabview.prototype.addMsg = function (time, nick, msg, type, style) {
-    var html_nick, self, tmp, plugin_ret, i, d, re, line_msg;
-    html_nick = $('<div/>').text(nick).html();
+    var self, tmp, plugin_ret, i, d, re, line_msg;
     
     self = this;
     
-    tmp = msg;
-    plugin_ret = '';
-    for (i in plugins.privmsg) {
-        if ((plugins.privmsg[i].onprivmsg instanceof Function)) {
-            plugin_ret = '';
-            try {
-                plugin_ret = plugins.privmsg[i].onprivmsg(tmp, this.name);
-                
-                // If this plugin has returned false, do not add this message
-                if (plugin_ret === false) {
-                    return;
-                }
-            } catch (e) {
-            }
-            
-            // If we actually have a string from the plugin, use it
-            if (typeof plugin_ret === "string") {
-                tmp = plugin_ret;
-            }
-        }
-    }
-    msg = tmp;
+    tmp = {msg: msg, time: time, nick: nick, tabview: this.name};
+    tmp = plugs.run('addmsg', tmp);
+    if (!tmp) return;
     
-    //var html_msg = $('<div/>').text(msg).html()+' '; // Add the space so the styling always has at least 1 character to go from
+
+    msg = tmp.msg;
+    time = tmp.time;
+    nick = tmp.nick;
+
     if (time === null) {
         d = new Date();
         time = d.getHours().toString().lpad(2, "0") + ":" + d.getMinutes().toString().lpad(2, "0") + ":" + d.getSeconds().toString().lpad(2, "0");
@@ -1326,15 +1392,14 @@ Tabview.prototype.addMsg = function (time, nick, msg, type, style) {
         }
     }
     
+    // Make the channels clickable
     re = new RegExp('\\B(' + gateway.channel_prefix + '[^ ,.\\007]+)', 'g');
-    
     msg = msg.replace(re, function (match) {
-        return '<a class="chan" href="#">' + match + '</a>';
+        return '<a class="chan">' + match + '</a>';
     });
 
-    line_msg = $('<div class="msg ' + type + '"><div class="time">' + time + '</div><div class="nick">' + html_nick + '</div><div class="text" style="' + style + '">' + msg + ' </div></div>');
-    //$('a.link_ext', line_msg).tooltip({ tip : $('#tooltip_link'), effect : 'toggle', offset : [2, 0] });
-
+    // Build up and add the line
+    line_msg = $('<div class="msg ' + type + '"><div class="time">' + time + '</div><div class="nick">' + nick + '</div><div class="text" style="' + style + '">' + msg + ' </div></div>');
     this.div.append(line_msg);
 
     if (!touchscreen) {