From 7af36eceb959515be2adbc6f1072905d5df3b061 Mon Sep 17 00:00:00 2001 From: Jack Allnutt Date: Mon, 26 Sep 2011 05:00:37 +0100 Subject: [PATCH] Adding a new User object, primarilly used in the UserList object.\n\nAlso JSLinting. --- js/front.events.js | 10 +- js/front.js | 298 ++++++++++++++++++++++++--------------------- js/front.ui.js | 22 ++-- js/util.js | 87 ++++++------- 4 files changed, 222 insertions(+), 195 deletions(-) diff --git a/js/front.events.js b/js/front.events.js index 599aaf0..ad0716a 100644 --- a/js/front.events.js +++ b/js/front.events.js @@ -1,3 +1,5 @@ +/*jslint browser: true, devel: true, sloppy: true, plusplus: true, nomen: true, forin: true, continue: true */ +/*globals kiwi, $, _, Tabview, Userlist, User, Box, init_data */ kiwi.front.events = { bindAll: function () { @@ -89,8 +91,7 @@ kiwi.front.events = { onNotice: function (e, data) { var nick = (data.nick === undefined) ? '' : data.nick, - enick = '[' + nick + ']', - tab; + enick = '[' + nick + ']'; if (Tabview.tabExists(data.target)) { Tabview.getTab(data.target).addMsg(null, enick, data.msg, 'notice'); @@ -217,9 +218,12 @@ kiwi.front.events = { Tabview.getServerTab().addMsg(null, data.server, data.msg, 'motd'); }, onWhois: function (e, data) { + /*globals secondsToTime */ var d, tab, idle_time = ''; - if (data.end) return; + if (data.end) { + return; + } if (typeof data.idle !== 'undefined'){ idle_time = secondsToTime(parseInt(data.idle, 10)); diff --git a/js/front.js b/js/front.js index c481600..47d410c 100644 --- a/js/front.js +++ b/js/front.js @@ -213,7 +213,7 @@ kiwi.front = { run: function (msg) { - var parts, dest, t, pos, textRange, d, plugin_event, msg_sliced, tab; + var parts, dest, t, pos, textRange, plugin_event, msg_sliced, tab; // Run through any plugins plugin_event = {command: msg}; @@ -400,48 +400,13 @@ kiwi.front = { - + sync: function () { kiwi.gateway.sync(); }, - - - - - - nickStripPrefix: function (nick) { - var tmp = nick, i, j, k; - i = 0; - for (j = 0; j < nick.length; j++) { - for (k = 0; k < kiwi.gateway.user_prefixes.length; k++) { - if (nick.charAt(j) === kiwi.gateway.user_prefixes[k].symbol) { - i++; - break; - } - } - } - - return tmp.substr(i); - }, - - nickGetPrefix: function (nick) { - var tmp = nick, i, j, k; - i = 0; - for (j = 0; j < nick.length; j++) { - for (k = 0; k < kiwi.gateway.user_prefixes.length; k++) { - if (nick.charAt(j) === kiwi.gateway.user_prefixes[k].symbol) { - i++; - break; - } - } - } - - return tmp.substr(0, i); - }, - isChannel: function (name) { var prefix, is_chan; prefix = name.charAt(0); @@ -454,8 +419,6 @@ kiwi.front = { return is_chan; }, - - formatIRCMsg: function (msg) { var re, next; @@ -549,7 +512,8 @@ kiwi.front = { var UserList = function (name) { - var userlist, list_html, sortUsers, sortModes, getPrefix; + /*globals User */ + var userlist, list_html, sortUsers; userlist = []; @@ -577,50 +541,8 @@ var UserList = function (name) { } }); - userlist.sort(function (a, b) { - var i, a_idx, b_idx, a_nick, b_nick; - // Try to sort by modes first - if (a.modes.length > 0) { - // a has modes, but b doesn't so a should appear first - if (b.modes.length === 0) { - return -1; - } - a_idx = b_idx = -1; - // Compare the first (highest) mode - for (i = 0; i < kiwi.gateway.user_prefixes.length; i++) { - if (kiwi.gateway.user_prefixes[i].mode === a.modes[0]) { - a_idx = i; - } - } - for (i = 0; i < kiwi.gateway.user_prefixes.length; i++) { - if (kiwi.gateway.user_prefixes[i].mode === b.modes[0]) { - b_idx = i; - } - } - if (a_idx < b_idx) { - return -1; - } else if (a_idx > b_idx) { - return 1; - } - // If we get to here both a and b have the same highest mode so have to resort to lexicographical sorting + userlist.sort(User.compare); - } else if (b.modes.length > 0) { - // b has modes but a doesn't so b should appear first - return 1; - } - a_nick = a.nick.toLocaleUpperCase(); - b_nick = b.nick.toLocaleUpperCase(); - // Lexicographical sorting - if (a_nick < b_nick) { - return -1; - } else if (a_nick > b_nick) { - return 1; - } else { - // This should never happen; both users have the same nick. - console.log('Something\'s gone wrong somewhere - two users have the same nick!'); - return 0; - } - }); _.each(userlist, function (user) { user.html = user.html.appendTo(list_html); }); @@ -628,56 +550,15 @@ var UserList = function (name) { list_html = list_html.appendTo(parent); }; - sortModes = function (modes) { - return modes.sort(function (a, b) { - var a_idx, b_idx, i; - for (i = 0; i < kiwi.gateway.user_prefixes.length; i++) { - if (kiwi.gateway.user_prefixes[i].mode === a) { - a_idx = i; - } - } - for (i = 0; i < kiwi.gateway.user_prefixes.length; i++) { - if (kiwi.gateway.user_prefixes[i].mode === b) { - b_idx = i; - } - } - if (a_idx < b_idx) { - return -1; - } else if (a_idx > b_idx) { - return 1; - } else { - return 0; - } - }); - }; - - getPrefix = function (modes) { - var prefix = ''; - if (typeof modes[0] !== 'undefined') { - prefix = _.detect(kiwi.gateway.user_prefixes, function (prefix) { - return prefix.mode === modes[0]; - }); - prefix = (prefix) ? prefix.symbol : ''; - } - return prefix; - }; - this.addUser = function (users) { if (!_.isArray(users)) { users = [users]; } _.each(users, function (user) { - var html, prefix = ''; - user.nick = kiwi.front.nickStripPrefix(user.nick); - user.modes = sortModes(user.modes); - if (typeof user.modes[0] !== 'undefined') { - prefix = _.detect(kiwi.gateway.user_prefixes, function (prefix) { - return prefix.mode === user.modes[0]; - }); - prefix = (prefix) ? prefix.symbol : ''; - } - html = $('
  • ' + prefix + user.nick + '
  • '); - userlist.push({nick: user.nick, modes: user.modes, html: html}); + user = new User(user.nick, user.modes); + user.html = $('
  • ' + user.prefix + user.nick + '
  • '); + user.html.data('user', user); + userlist.push(user); }); sortUsers(); @@ -710,7 +591,7 @@ var UserList = function (name) { }); if (user) { user.nick = newNick; - user.html.text(getPrefix(user.modes) + newNick); + user.html.text(User.getPrefix(user.modes) + newNick); } sortUsers(); @@ -757,6 +638,16 @@ var UserList = function (name) { }); }; + this.getUser = function (nick) { + if (this.hasUser(nick)) { + return _.detect(userlist, function (user) { + return user.nick === nick; + }); + } else { + return null; + } + }; + this.active = function (active) { if ((arguments.length === 0) || (active)) { list_html.addClass('active'); @@ -770,21 +661,21 @@ var UserList = function (name) { }; this.changeUserMode = function (nick, mode, add) { - var user; + var user, prefix; if (this.hasUser(nick)) { - user = _.detect(userlist, function (u) { + user = _.detect(userlist, function (u) { return u.nick === nick; }); + prefix = user.prefix; if ((arguments.length < 3) || (add)) { - user.modes.push(mode); + user.addMode(mode); } else { - user.modes = _.reject(user.modes, function (m) { - return m === mode; - }); + user.removeMode(mode); + } + if (prefix !== user.prefix) { + user.html.children('a:first').text(user.prefix + user.nick); } - user.modes = sortModes(user.modes); - user.html.children('a:first').text(getPrefix(user.modes) + user.nick); sortUsers(); } @@ -807,7 +698,8 @@ UserList.prototype.setWidth = function (newWidth) { }; UserList.prototype.clickHandler = function () { - var li = $(this).parent(); + var li = $(this).parent(), + user = li.data('user'); // Remove any existing userboxes $('#kiwi .userbox').remove(); @@ -815,7 +707,7 @@ UserList.prototype.clickHandler = function () { if ($(li).data('userbox') === this) { $(li).removeData('userbox'); } else { - $('#tmpl_user_box').tmpl({nick: kiwi.front.nickStripPrefix($(this).text())}).appendTo(li); + $('#tmpl_user_box').tmpl({nick: user.nick}).appendTo(li); $('#kiwi .userbox .userbox_query').click(function (ev) { var nick = $('#kiwi .userbox_nick').val(); @@ -833,6 +725,130 @@ UserList.prototype.clickHandler = function () { +var User = function (nick, modes) { + var sortModes; + + sortModes = function (modes) { + return modes.sort(function (a, b) { + var a_idx, b_idx, i; + for (i = 0; i < kiwi.gateway.user_prefixes.length; i++) { + if (kiwi.gateway.user_prefixes[i].mode === a) { + a_idx = i; + } + } + for (i = 0; i < kiwi.gateway.user_prefixes.length; i++) { + if (kiwi.gateway.user_prefixes[i].mode === b) { + b_idx = i; + } + } + if (a_idx < b_idx) { + return -1; + } else if (a_idx > b_idx) { + return 1; + } else { + return 0; + } + }); + }; + + this.nick = User.stripPrefix(nick); + this.modes = modes || []; + this.modes = sortModes(this.modes); + this.prefix = User.getPrefix(this.modes); + + this.addMode = function (mode) { + this.modes.push(mode); + this.modes = sortModes(this.modes); + this.prefix = User.getPrefix(this.modes); + return this; + }; +}; + +User.prototype.removeMode = function (mode) { + this.modes = _.reject(this.modes, function (m) { + return m === mode; + }); + this.prefix = User.getPrefix(this.modes); + return this; +}; + +User.prototype.isOp = function () { + // return true if this.mode[0] > o + return false; +}; + +User.getPrefix = function (modes) { + var prefix = ''; + if (typeof modes[0] !== 'undefined') { + prefix = _.detect(kiwi.gateway.user_prefixes, function (prefix) { + return prefix.mode === modes[0]; + }); + prefix = (prefix) ? prefix.symbol : ''; + } + return prefix; +}; + +User.stripPrefix = function (nick) { + var tmp = nick, i, j, k; + i = 0; + for (j = 0; j < nick.length; j++) { + for (k = 0; k < kiwi.gateway.user_prefixes.length; k++) { + if (nick.charAt(j) === kiwi.gateway.user_prefixes[k].symbol) { + i++; + break; + } + } + } + + return tmp.substr(i); +}; + +User.compare = function (a, b) { + var i, a_idx, b_idx, a_nick, b_nick; + // Try to sort by modes first + if (a.modes.length > 0) { + // a has modes, but b doesn't so a should appear first + if (b.modes.length === 0) { + return -1; + } + a_idx = b_idx = -1; + // Compare the first (highest) mode + for (i = 0; i < kiwi.gateway.user_prefixes.length; i++) { + if (kiwi.gateway.user_prefixes[i].mode === a.modes[0]) { + a_idx = i; + } + } + for (i = 0; i < kiwi.gateway.user_prefixes.length; i++) { + if (kiwi.gateway.user_prefixes[i].mode === b.modes[0]) { + b_idx = i; + } + } + if (a_idx < b_idx) { + return -1; + } else if (a_idx > b_idx) { + return 1; + } + // If we get to here both a and b have the same highest mode so have to resort to lexicographical sorting + + } else if (b.modes.length > 0) { + // b has modes but a doesn't so b should appear first + return 1; + } + a_nick = a.nick.toLocaleUpperCase(); + b_nick = b.nick.toLocaleUpperCase(); + // Lexicographical sorting + if (a_nick < b_nick) { + return -1; + } else if (a_nick > b_nick) { + return 1; + } else { + // This should never happen; both users have the same nick. + console.log('Something\'s gone wrong somewhere - two users have the same nick!'); + return 0; + } +}; + + /* * MISC VIEW @@ -1091,11 +1107,11 @@ Tabview.prototype.clearPartImage = function () { Tabview.prototype.setIcon = function (url) { this.tab.prepend(''); this.tab.css('padding-left', '33px'); -} +}; Tabview.prototype.setTabText = function (text) { $('span', this.tab).text(text); -} +}; Tabview.prototype.addMsg = function (time, nick, msg, type, style) { var self, tmp, d, re, line_msg; diff --git a/js/front.ui.js b/js/front.ui.js index d5fc351..a3b932b 100644 --- a/js/front.ui.js +++ b/js/front.ui.js @@ -1,5 +1,7 @@ +/*jslint browser: true, devel: true, sloppy: true, plusplus: true, nomen: true, forin: true, continue: true */ +/*globals kiwi, $, _, Tabview, Userlist, User, Box */ kiwi.front.ui = { - + doLayoutSize: function () { var kiwi, toolbars, ul, n_top, n_bottom, nl; kiwi = $('#kiwi'); @@ -112,7 +114,7 @@ kiwi.front.ui = { data = []; Tabview.getCurrentTab().userlist.listUsers(false).each(function () { var nick; - nick = kiwi.front.nickStripPrefix($('a.nick', this).text()); + nick = User.stripPrefix($('a.nick', this).text()); data.push(nick); }); data = _.sortBy(data, function (nick) { @@ -366,20 +368,24 @@ kiwi.front.ui = { tutorial: function () { - var b = $('
    '); + var b = $('
    '), + bounds, + s, + current_s, + next_s; b.css({display: 'block', position: 'absolute', height:'100%', width:'100%'}); $('#kiwi').append(b); - var bounds = function (el) { + bounds = function (el) { var b = 3, ret = {}; ret.top = el.offset().top; ret.left = el.offset().left; ret.width = parseInt(el.outerWidth(true), 10) - (b*2); ret.height = parseInt(el.outerHeight(true), 10) - (b*2); return ret; - } + }; - var s = [ + s = [ function(){ b.animate(bounds($('#kiwi .msginput')), 2000, '', next_s); }, @@ -402,8 +408,8 @@ kiwi.front.ui = { ]; - var current_s = -1; - var next_s = function () { + current_s = -1; + next_s = function () { current_s++; if (typeof s[current_s] === 'function') { console.log('Animating ' + current_s.toString()); diff --git a/js/util.js b/js/util.js index 419c4a4..54584f8 100644 --- a/js/util.js +++ b/js/util.js @@ -1,4 +1,4 @@ -/*jslint devel: true, browser: true, continue: true, sloppy: true, forin: true, plusplus: true, maxerr: 50, indent: 4 */ +/*jslint devel: true, browser: true, continue: true, sloppy: true, forin: true, plusplus: true, maxerr: 50, indent: 4, nomen: true, regexp: true*/ /*globals $, front, gateway, Utilityview */ // Holds anything kiwi client specific (ie. front, gateway, kiwi.plugs..) @@ -9,22 +9,22 @@ var kiwi = {}; function manageDebug(debug) { var log, consoleBackUp; - if (window.console) { - consoleBackUp = window.console.log; - window.console.log = function () { - if (debug) { - consoleBackUp.apply(console, arguments); + if (window.console) { + consoleBackUp = window.console.log; + window.console.log = function () { + if (debug) { + consoleBackUp.apply(console, arguments); } - }; - } else { - log = window.opera ? window.opera.postError : alert; - window.console = {}; - window.console.log = function (str) { - if (debug) { - log(str); - } }; - } + } else { + log = window.opera ? window.opera.postError : alert; + window.console = {}; + window.console.log = function (str) { + if (debug) { + log(str); + } + }; + } } @@ -59,16 +59,17 @@ if (typeof String.prototype.lpad === 'undefined') { -function secondsToTime(secs){ - var hours = Math.floor(secs / (60 * 60)); - - var divisor_for_minutes = secs % (60 * 60); - var minutes = Math.floor(divisor_for_minutes / 60); - - var divisor_for_seconds = divisor_for_minutes % 60; - var seconds = Math.ceil(divisor_for_seconds); - - var obj = { +function secondsToTime(secs) { + var hours, minutes, seconds, divisor_for_minutes, divisor_for_seconds, obj; + hours = Math.floor(secs / (60 * 60)); + + divisor_for_minutes = secs % (60 * 60); + minutes = Math.floor(divisor_for_minutes / 60); + + divisor_for_seconds = divisor_for_minutes % 60; + seconds = Math.ceil(divisor_for_seconds); + + obj = { "h": hours, "m": minutes, "s": seconds @@ -97,7 +98,7 @@ var plugins = [ var img = ''; return '' + img + '
    '; }); - + return event; } }, @@ -122,7 +123,7 @@ var plugins = [ return event; } }, - + { name: "highlight", onaddmsg: function (event, opts) { @@ -134,7 +135,7 @@ var plugins = [ event.msg = '' + event.msg + ''; } } - + if ( !kiwi.front.isChannel(kiwi.front.tabviews[event.tabview].name) && kiwi.front.tabviews[event.tabview].name !== "server" && kiwi.front.cur_channel.name.toLowerCase() !== kiwi.front.tabviews[event.tabview.toLowerCase()].name @@ -144,10 +145,10 @@ var plugins = [ return event; } - }, - - - + }, + + + { //Following method taken from: http://snipplr.com/view/13533/convert-text-urls-into-links/ name: "linkify_plain", @@ -155,7 +156,7 @@ var plugins = [ 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 @@ -170,10 +171,10 @@ var plugins = [ } else { url = 'http://' + url; } - + return '' + nice + '
    '; }); - + return event; } }, @@ -184,11 +185,11 @@ var plugins = [ if (!event.msg) { return event; } - + event.msg = event.msg.replace(/\n/gi, function (txt) { return '
    '; }); - + return event; } }, @@ -233,7 +234,7 @@ var plugins = [ mouseclick: function (e) { var a = $(this), t; - + switch (e.target.className) { case 'link_ext': case 'link_img_a': @@ -268,10 +269,10 @@ var plugins = [ if (typeof kiwi.front.tabviews[event.tabview].nick_colours[event.nick] === 'undefined') { kiwi.front.tabviews[event.tabview].nick_colours[event.nick] = this.randColour(); } - + var c = kiwi.front.tabviews[event.tabview].nick_colours[event.nick]; event.nick = '' + event.nick + ''; - + return event; }, @@ -345,11 +346,11 @@ kiwi.plugs.run = function (event_name, event_data, opts) { var ret = event_data, 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 kiwi.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) { @@ -363,7 +364,7 @@ kiwi.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; -- 2.25.1