From c6ef62a3bf2f08342e67e27a7a40a594423dcb90 Mon Sep 17 00:00:00 2001 From: Jack Allnutt Date: Thu, 8 Sep 2011 01:40:10 +0100 Subject: [PATCH] Cleaning up client-side files, including one or two variables leaking into the global scope --- js/front.js | 79 ++++++---- js/gateway.js | 2 +- js/util.js | 396 +++++++++++++++++++++++++++----------------------- 3 files changed, 262 insertions(+), 215 deletions(-) diff --git a/js/front.js b/js/front.js index fd5c279..25ba676 100644 --- a/js/front.js +++ b/js/front.js @@ -1,5 +1,5 @@ -/*jslint undef: true, browser: true, continue: true, sloppy: true, evil: true, forin: true, newcap: false, plusplus: true, maxerr: 50, indent: 4 */ -/*global gateway, io, $, iScroll, agent, touchscreen*/ +/*jslint devel: true, undef: true, browser: true, continue: true, sloppy: true, forin: true, newcap: true, plusplus: true, maxerr: 50, indent: 4 */ +/*global gateway, io, $, iScroll, agent, touchscreen, init_data, plugs, registerTouches, randomString */ var front = { revision: 38, @@ -15,7 +15,8 @@ var front = { original_topic: '', init: function () { - var about_info, supportsOrientationChange, orientationEvent; + /*global Box, touch_scroll:true */ + var about_info, supportsOrientationChange, orientationEvent, scroll_opts; gateway.nick = 'kiwi_' + Math.ceil(100 * Math.random()) + Math.ceil(100 * Math.random()); gateway.session_id = null; @@ -163,7 +164,7 @@ var front = { }); - $('#windows a.chan').live('click', function() { + $('#windows a.chan').live('click', function () { front.joinChannel($(this).text()); return false; }); @@ -203,7 +204,8 @@ var front = { joinChannel: function (chan_name) { var chans = chan_name.split(','), - i; + i, + chan; for (i in chans) { chan = chans[i]; if (front.tabviews[chan.toLowerCase()] === undefined || (front.tabviews[chan.toLowerCase()] !== undefined && front.tabviews[chan.toLowerCase()].safe_to_close === true)) { @@ -217,12 +219,14 @@ var front = { run: function (msg) { - var parts, dest, t, pos, textRange, d, plugin_event; + var parts, dest, t, pos, textRange, d, plugin_event, msg_sliced; // 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; + if (!plugin_event || typeof plugin_event.command === 'undefined') { + return; + } // Update msg if it's been changed by any plugins msg = plugin_event.command.toString(); @@ -294,7 +298,7 @@ var front = { case '/m': case '/msg': if (typeof parts[1] !== "undefined") { - var msg_sliced = msg.split(' ').slice(2).join(' '); + msg_sliced = msg.split(' ').slice(2).join(' '); gateway.msg(parts[1], msg_sliced); if (!front.tabviewExists(parts[1])) { @@ -381,9 +385,11 @@ var front = { destination = data.channel.toLowerCase(); } - plugin_event = {nick: data.nick, msg:data.msg, destination: destination}; + plugin_event = {nick: data.nick, msg: data.msg, destination: destination}; plugin_event = plugs.run('msg_recieved', plugin_event); - if (!plugin_event) return; + if (!plugin_event) { + return; + } if (!front.tabviewExists(plugin_event.destination)) { front.tabviewAdd(plugin_event.destination); @@ -420,8 +426,8 @@ var front = { }, onNotice: function (e, data) { - var nick = (data.nick === undefined) ? '' : data.nick; - var enick = '[' + nick + ']'; + var nick = (data.nick === undefined) ? '' : data.nick, + enick = '[' + nick + ']'; if (front.tabviewExists(data.target)) { front.tabviews[data.target.toLowerCase()].addMsg(null, enick, data.msg, 'notice'); @@ -654,7 +660,7 @@ var front = { registerKeys: function () { $('#kiwi_msginput').bind('keydown', function (e) { - var windows, meta, num, msg, data, candidates, word_pos, word; + var windows, meta, num, msg, data, candidates, word_pos, word, i; windows = $('#windows'); //var meta = e.altKey; meta = e.ctrlKey; @@ -720,6 +726,7 @@ var front = { // Get possible autocompletions data = []; front.cur_channel.userlist.children().each(function () { + var nick; nick = front.nickStripPrefix($('a.nick', this).text()); data.push(nick); }); @@ -764,7 +771,6 @@ var front = { $('#kiwi .plugins .load_plugin_file').click(function () { - var lst, j, txt; if (typeof front.boxes.plugins !== "undefined") { return; } @@ -774,7 +780,8 @@ var front = { front.boxes.plugins.box.css('top', -(front.boxes.plugins.height + 40)); // Populate the plugin list.. - function enumPlugins () { + function enumPlugins() { + var lst, j, txt; lst = $('#plugin_list'); lst.find('option').remove(); for (j in plugs.loaded) { @@ -786,7 +793,7 @@ var front = { // Event bindings $('#kiwi .plugin_file').submit(function () { - $('
').load($('.txtpluginfile').val(), function(e){ + $('
').load($('.txtpluginfile').val(), function (e) { enumPlugins(); }); return false; @@ -868,6 +875,7 @@ var front = { }, tabviewAdd: function (v_name) { + /*global Tabview */ var re, htmlsafe_name, tmp_divname, tmp_userlistname, tmp_tabname, userlist_enabled = true; if (v_name.charAt(0) === gateway.channel_prefix) { @@ -896,7 +904,9 @@ 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; + if (!userlist_enabled) { + front.tabviews[v_name.toLowerCase()].userlist_width = 0; + } front.tabviews[v_name.toLowerCase()].show(); if (typeof registerTouches === "function") { @@ -979,21 +989,20 @@ var front = { nickStripPrefix: function (nick) { - var tmp = nick, i; + var tmp = nick, i, prefix; prefix = tmp.charAt(0); for (i in gateway.user_prefixes) { - if (gateway.user_prefixes[i].symbol !== prefix) { - continue; + if (gateway.user_prefixes[i].symbol === prefix) { + return tmp.substring(1); } - return tmp.substring(1); } return tmp; }, nickGetPrefix: function (nick) { - var tmp = nick, i; + var tmp = nick, i, prefix; prefix = tmp.charAt(0); for (i in gateway.user_prefixes) { @@ -1006,6 +1015,7 @@ var front = { }, isChannel: function (name) { + var prefix, is_chan; prefix = name.charAt(0); if (gateway.channel_prefix.indexOf(prefix) > -1) { is_chan = true; @@ -1104,10 +1114,10 @@ var front = { */ var Utilityview = function (name) { - var rand_name = randomString(15); - var tmp_divname = 'kiwi_window_' + rand_name; - var tmp_userlistname = 'kiwi_userlist_' + rand_name; - var tmp_tabname = 'kiwi_tab_' + rand_name; + var rand_name = randomString(15), + tmp_divname = 'kiwi_window_' + rand_name, + tmp_userlistname = 'kiwi_userlist_' + rand_name, + tmp_tabname = 'kiwi_tab_' + rand_name; this.name = rand_name; this.title = name; @@ -1116,7 +1126,7 @@ var Utilityview = function (name) { $('#kiwi .windows .scroller').append('
'); this.tab = $('
  • ' + this.title + '
  • '); - this.tab.click(function(){ + this.tab.click(function () { front.utilityviews[rand_name.toLowerCase()].show(); }); $('#kiwi .utilityviewlist ul').append(this.tab); @@ -1152,7 +1162,7 @@ Utilityview.prototype.show = function () { if (touch_scroll) { touch_scroll.refresh(); } -} +}; Utilityview.prototype.close = function () { this.div.remove(); @@ -1228,7 +1238,9 @@ Tabview.prototype.show = function () { // Activate this tab! this.div.addClass('active'); - if (this.userlist_width > 0) this.userlist.addClass('active'); + if (this.userlist_width > 0) { + this.userlist.addClass('active'); + } this.tab.addClass('active'); // Add the part image to the tab @@ -1288,13 +1300,15 @@ Tabview.prototype.clearPartImage = function () { }; Tabview.prototype.addMsg = function (time, nick, msg, type, style) { - var self, tmp, plugin_ret, i, d, re, line_msg; + var self, tmp, plugin_ret, i, d, re, line_msg, next; self = this; tmp = {msg: msg, time: time, nick: nick, tabview: this.name}; tmp = plugs.run('addmsg', tmp); - if (!tmp) return; + if (!tmp) { + return; + } msg = tmp.msg; @@ -1391,7 +1405,8 @@ Tabview.prototype.changeNick = function (newNick, oldNick) { Tabview.prototype.userlistSort = function () { var ul = this.userlist, - listitems = ul.children('li').get(); + listitems = ul.children('li').get(), + prefix; listitems.sort(function (a, b) { var compA = $(a).text().toUpperCase(), compB = $(b).text().toUpperCase(), diff --git a/js/gateway.js b/js/gateway.js index 675d9a3..83d341d 100644 --- a/js/gateway.js +++ b/js/gateway.js @@ -1,5 +1,5 @@ /*jslint browser: true, confusion: true, sloppy: true, maxerr: 50, indent: 4 */ -/*globals io, $ */ +/*globals io, $, kiwi_server */ var gateway = { revision: 16, diff --git a/js/util.js b/js/util.js index 37ac40a..85988b0 100644 --- a/js/util.js +++ b/js/util.js @@ -1,208 +1,228 @@ -function manageDebug(debug){ - if(window.console){ - var consoleBackUp = window.console.log; - window.console.log = function(str){ - if(debug){ - consoleBackUp.call(this,str); +/*jslint devel: true, browser: true, continue: true, sloppy: true, forin: true, plusplus: true, maxerr: 50, indent: 4 */ +/*globals $, front, gateway, Utilityview */ +function manageDebug(debug) { + var log, consoleBackUp; + if (window.console) { + consoleBackUp = window.console.log; + window.console.log = function (str) { + if (debug) { + consoleBackUp.call(this, str); } - } - }else{ - var log = window.opera ? window.opera.postError : alert; + }; + } else { + log = window.opera ? window.opera.postError : alert; window.console = {}; - window.console.log = function(str){ - if(debug){ + window.console.log = function (str) { + if (debug) { log(str); } - } + }; } } function randomString(string_length) { - var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz"; - var randomstring = ''; - for (var i=0; i'; - return ''+ img +'
    '; - }); - - return event; - } - }, - - { - name: "html_safe", - onaddmsg: function(event, opts){ - event.msg = $('
    ').text(event.msg).html(); - event.nick = $('
    ').text(event.nick).html(); - - return event; - } - }, - - { - name: "activity", - onaddmsg: function(event, opts){ - if(front.cur_channel.name.toLowerCase() != front.tabviews[event.tabview.toLowerCase()].name){ - front.tabviews[event.tabview].activity(); - } - - return event; - } - }, - - { - name: "highlight", - onaddmsg: function(event, opts){ - if(event.msg.toLowerCase().indexOf(gateway.nick.toLowerCase()) > -1){ - if(front.cur_channel.name.toLowerCase() != front.tabviews[event.tabview.toLowerCase()].name){ - front.tabviews[event.tabview].highlight(); - } - if(front.isChannel(front.tabviews[event.tabview].name)){ - event.msg = ''+event.msg+''; - } - } - - if( - !front.isChannel(front.tabviews[event.tabview].name) && front.tabviews[event.tabview].name != "server" - && front.cur_channel.name.toLowerCase() != front.tabviews[event.tabview.toLowerCase()].name - ){ - front.tabviews[event.tabview].highlight(); - } - - return event; - } - }, - - - - { - //Following method taken from: http://snipplr.com/view/13533/convert-text-urls-into-links/ - name: "linkify_plain", - onaddmsg: function(event, opts){ - if( !event.msg ) return event; - - event.msg = event.msg.replace(/((https?\:\/\/|ftp\:\/\/)|(www\.))(\S+)(\w{2,4})(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/gi,function(url){ - // If it's any of the supported images in the images plugin, skip it - if(url.match('(\.jpg|\.jpeg|\.gif|\.bmp|\.png)$')) return url; - - nice = url; - if(url.match('^https?:\/\/')){ - //nice = nice.replace(/^https?:\/\//i,'') - } else { - url = 'http://'+url; - } - - return ''+ nice +'
    '; - }); - - return event; - } - }, + { + name: "images", + onaddmsg: function (event, opts) { + if (!event.msg) { + return event; + } + + event.msg = event.msg.replace(/^((https?\:\/\/|ftp\:\/\/)|(www\.))(\S+)(\w{2,4})(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?(\.jpg|\.jpeg|\.gif|\.bmp|\.png)$/gi, function (url) { + // Don't let any future plugins change it (ie. html_safe plugins) + event.event_bubbles = false; + + var img = ''; + return '' + img + '
    '; + }); + + return event; + } + }, { - name: "lftobr", - onaddmsg: function(event, opts){ - if( !event.msg ) return event; - - event.msg = event.msg.replace(/\n/gi,function(txt){ - return '
    '; - }); - - return event; - } - }, - - - { - name: "inBrowser", - oninit: function(event, opts){ - $('#windows a.link_ext').live('mouseover', this.mouseover); - $('#windows a.link_ext').live('mouseout', this.mouseout); - $('#windows a.link_ext').live('click', this.mouseclick); - }, - - onunload: function(event, opts){ - // TODO: make this work - $('#windows a.link_ext').die('mouseover', this.mouseover); - $('#windows a.link_ext').die('mouseout', this.mouseout); - $('#windows a.link_ext').die('click', this.mouseclick); - }, - - - - mouseover: function(e){ - var a = $(this); - var tt = $('.tt', a); + name: "html_safe", + onaddmsg: function (event, opts) { + event.msg = $('
    ').text(event.msg).html(); + event.nick = $('
    ').text(event.nick).html(); + + return event; + } + }, + + { + name: "activity", + onaddmsg: function (event, opts) { + if (front.cur_channel.name.toLowerCase() !== front.tabviews[event.tabview.toLowerCase()].name) { + front.tabviews[event.tabview].activity(); + } + + return event; + } + }, + + { + name: "highlight", + onaddmsg: function (event, opts) { + if (event.msg.toLowerCase().indexOf(gateway.nick.toLowerCase()) > -1) { + if (front.cur_channel.name.toLowerCase() !== front.tabviews[event.tabview.toLowerCase()].name) { + front.tabviews[event.tabview].highlight(); + } + if (front.isChannel(front.tabviews[event.tabview].name)) { + event.msg = '' + event.msg + ''; + } + } + + if ( + !front.isChannel(front.tabviews[event.tabview].name) && front.tabviews[event.tabview].name !== "server" + && front.cur_channel.name.toLowerCase() !== front.tabviews[event.tabview.toLowerCase()].name + ) { + front.tabviews[event.tabview].highlight(); + } + + return event; + } + }, + + + + { + //Following method taken from: http://snipplr.com/view/13533/convert-text-urls-into-links/ + name: "linkify_plain", + onaddmsg: function (event, opts) { + if (!event.msg) { + return event; + } + + event.msg = event.msg.replace(/((https?\:\/\/|ftp\:\/\/)|(www\.))(\S+)(\w{2,4})(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/gi, function (url) { + var nice; + // If it's any of the supported images in the images plugin, skip it + if (url.match(/(\.jpg|\.jpeg|\.gif|\.bmp|\.png)$/)) { + return url; + } + + nice = url; + if (url.match('^https?:\/\/')) { + //nice = nice.replace(/^https?:\/\//i,'') + nice = url; // Shutting up JSLint... + } else { + url = 'http://' + url; + } + + return '' + nice + '
    '; + }); + + return event; + } + }, + + { + name: "lftobr", + onaddmsg: function (event, opts) { + if (!event.msg) { + return event; + } + + event.msg = event.msg.replace(/\n/gi, function (txt) { + return '
    '; + }); + + return event; + } + }, + + + { + name: "inBrowser", + oninit: function (event, opts) { + $('#windows a.link_ext').live('mouseover', this.mouseover); + $('#windows a.link_ext').live('mouseout', this.mouseout); + $('#windows a.link_ext').live('click', this.mouseclick); + }, + + onunload: function (event, opts) { + // TODO: make this work + $('#windows a.link_ext').die('mouseover', this.mouseover); + $('#windows a.link_ext').die('mouseout', this.mouseout); + $('#windows a.link_ext').die('click', this.mouseclick); + }, + + + + mouseover: function (e) { + var a = $(this), + tt = $('.tt', a), + tooltip; if (tt.text() === '') { - var tooltip = $('Open in Kiwi..'); + tooltip = $('Open in Kiwi..'); tt.append(tooltip); } - tt.css('top', -tt.outerHeight()+'px'); + tt.css('top', -tt.outerHeight() + 'px'); tt.css('left', (a.outerWidth() / 2) - (tt.outerWidth() / 2)); - }, + }, - mouseout: function(e){ - var a = $(this); - var tt = $('.tt', a); - }, + mouseout: function (e) { + var a = $(this), + tt = $('.tt', a); + }, - mouseclick: function(e){ - var a = $(this); + mouseclick: function (e) { + var a = $(this), + t; switch (e.target.className) { case 'link_ext': case 'link_img_a': return true; - break; + //break; case 'link_ext_browser': - var t = new Utilityview('Browser'); + t = new Utilityview('Browser'); t.topic = a.attr('href'); - t.iframe = $(''); - t.iframe.attr('src', a.attr('href')); - t.div.append(t.iframe); + t.iframe = $(''); + t.iframe.attr('src', a.attr('href')); + t.div.append(t.iframe); t.show(); break; } return false; - } - } + } + } ]; @@ -213,24 +233,31 @@ var plugins = [ -plugs = {}; +var plugs = {}; plugs.loaded = {}; plugs.loadPlugin = function (plugin) { - if (typeof plugin.name !== 'string') return false; + var plugin_ret; + if (typeof plugin.name !== 'string') { + return false; + } - var plugin_ret = plugs.run('plugin_load', {plugin: plugin}); - if (typeof plugin_ret === 'object') plugs.loaded[plugin_ret.plugin.name] = plugin_ret.plugin; + plugin_ret = plugs.run('plugin_load', {plugin: plugin}); + if (typeof plugin_ret === 'object') { + plugs.loaded[plugin_ret.plugin.name] = plugin_ret.plugin; + } plugs.run('init', {}, {run_only: plugin_ret.plugin.name}); return true; }; plugs.unloadPlugin = function (plugin_name) { - if (typeof plugs.loaded[plugin_name] !== 'object') return; + if (typeof plugs.loaded[plugin_name] !== 'object') { + return; + } - plugs.run('unload', {}, {run_only: plugin_name}); - delete plugs.loaded[plugin_name]; -} + plugs.run('unload', {}, {run_only: plugin_name}); + delete plugs.loaded[plugin_name]; +}; @@ -239,15 +266,18 @@ plugs.unloadPlugin = function (plugin_name) { */ plugs.run = function (event_name, event_data, opts) { var ret = event_data, - ret_tmp, plugin_name; + ret_tmp, + plugin_name; // Set some defaults if not provided event_data = (typeof event_data === 'undefined') ? {} : event_data; opts = (typeof opts === 'undefined') ? {} : opts; for (plugin_name in plugs.loaded) { - // If we're only calling 1 plugin, make sure it's that one - if (typeof opts.run_only === 'string' && opts.run_only !== plugin_name) continue; + // If we're only calling 1 plugin, make sure it's that one + if (typeof opts.run_only === 'string' && opts.run_only !== plugin_name) { + continue; + } if (typeof plugs.loaded[plugin_name]['on' + event_name] === 'function') { try { @@ -256,10 +286,10 @@ plugs.run = function (event_name, event_data, opts) { return null; } ret = ret_tmp; - - if (typeof ret.event_bubbles === 'boolean' && ret.event_bubbles === false){ - delete ret.event_bubbles; - return ret; + + if (typeof ret.event_bubbles === 'boolean' && ret.event_bubbles === false) { + delete ret.event_bubbles; + return ret; } } catch (e) { } @@ -269,10 +299,12 @@ plugs.run = function (event_name, event_data, opts) { return ret; }; - -for(var i in plugins) plugs.loadPlugin(plugins[i]); - - +(function () { + var i; + for (i in plugins) { + plugs.loadPlugin(plugins[i]); + } +}()); -- 2.25.1