From 79d92251838a0a1b01ad9653492cb351a9391065 Mon Sep 17 00:00:00 2001 From: Darren Date: Wed, 2 Apr 2014 14:25:21 +0100 Subject: [PATCH] client/src/models/member.js formatting + comments --- client/src/models/member.js | 76 ++++++++++++++++++++++++++++++------- 1 file changed, 63 insertions(+), 13 deletions(-) diff --git a/client/src/models/member.js b/client/src/models/member.js index ce02f5e..618fd78 100644 --- a/client/src/models/member.js +++ b/client/src/models/member.js @@ -1,4 +1,26 @@ _kiwi.model.Member = Backbone.Model.extend({ + initialize: function (attributes) { + var nick, modes, prefix; + + // The nick may have a mode prefix, we don't want this + nick = this.stripPrefix(this.get("nick")); + + // Make sure we have a mode array, and that it's sorted + modes = this.get("modes"); + modes = modes || []; + this.sortModes(modes); + + this.set({"nick": nick, "modes": modes, "prefix": this.getPrefix(modes)}, {silent: true}); + + this.updateOpStatus(); + + this.view = new _kiwi.view.Member({"model": this}); + }, + + + /** + * Sort modes in order of importance + */ sortModes: function (modes) { var that = this; @@ -11,11 +33,13 @@ _kiwi.model.Member = Backbone.Model.extend({ a_idx = i; } } + for (i = 0; i < user_prefixes.length; i++) { if (user_prefixes[i].mode === b) { b_idx = i; } } + if (a_idx < b_idx) { return -1; } else if (a_idx > b_idx) { @@ -25,17 +49,8 @@ _kiwi.model.Member = Backbone.Model.extend({ } }); }, - initialize: function (attributes) { - var nick, modes, prefix; - nick = this.stripPrefix(this.get("nick")); - modes = this.get("modes"); - modes = modes || []; - this.sortModes(modes); - this.set({"nick": nick, "modes": modes, "prefix": this.getPrefix(modes)}, {silent: true}); - this.isOp(); - this.view = new _kiwi.view.Member({"model": this}); - }, + addMode: function (mode) { var modes_to_add = mode.split(''), modes, prefix; @@ -47,10 +62,13 @@ _kiwi.model.Member = Backbone.Model.extend({ modes = this.sortModes(modes); this.set({"prefix": this.getPrefix(modes), "modes": modes}); - this.isOp(); + + this.updateOpStatus(); this.view.render(); }, + + removeMode: function (mode) { var modes_to_remove = mode.split(''), modes, prefix; @@ -61,10 +79,18 @@ _kiwi.model.Member = Backbone.Model.extend({ }); this.set({"prefix": this.getPrefix(modes), "modes": modes}); - this.isOp(); + + this.updateOpStatus(); this.view.render(); }, + + + /** + * Figure out a valid prefix given modes. + * If a user is an op but also has voice, the prefix + * should be the op as it is more important. + */ getPrefix: function (modes) { var prefix = ''; var user_prefixes = this.get('user_prefixes'); @@ -73,29 +99,44 @@ _kiwi.model.Member = Backbone.Model.extend({ prefix = _.detect(user_prefixes, function (prefix) { return prefix.mode === modes[0]; }); + prefix = (prefix) ? prefix.symbol : ''; } + return prefix; }, + + + /** + * Remove any recognised prefix from a nick + */ stripPrefix: function (nick) { var tmp = nick, i, j, k, nick_char; var user_prefixes = this.get('user_prefixes'); + i = 0; nick_character_loop: for (j = 0; j < nick.length; j++) { nick_char = nick.charAt(j); + for (k = 0; k < user_prefixes.length; k++) { if (nick_char === user_prefixes[k].symbol) { i++; continue nick_character_loop; } } + break; } return tmp.substr(i); }, + + + /** + * Format this nick into readable format (eg. nick [ident@hostname]) + */ displayNick: function (full) { var display = this.get('nick'); @@ -107,22 +148,31 @@ _kiwi.model.Member = Backbone.Model.extend({ return display; }, - isOp: function () { + + + /** + * With the modes set on the user, make note if we have some sort of op status + */ + updateOpStatus: function () { var user_prefixes = this.get('user_prefixes'), modes = this.get('modes'), o, max_mode; + if (modes.length > 0) { o = _.indexOf(user_prefixes, _.find(user_prefixes, function (prefix) { return prefix.mode === 'o'; })); + max_mode = _.indexOf(user_prefixes, _.find(user_prefixes, function (prefix) { return prefix.mode === modes[0]; })); + if ((max_mode === -1) || (max_mode > o)) { this.set({"is_op": false}, {silent: true}); } else { this.set({"is_op": true}, {silent: true}); } + } else { this.set({"is_op": false}, {silent: true}); } -- 2.25.1