_kiwi.model.Member = Backbone.Model.extend({\r
+ initialize: function (attributes) {\r
+ var nick, modes, prefix;\r
+\r
+ // The nick may have a mode prefix, we don't want this\r
+ nick = this.stripPrefix(this.get("nick"));\r
+\r
+ // Make sure we have a mode array, and that it's sorted\r
+ modes = this.get("modes");\r
+ modes = modes || [];\r
+ this.sortModes(modes);\r
+\r
+ this.set({"nick": nick, "modes": modes, "prefix": this.getPrefix(modes)}, {silent: true});\r
+\r
+ this.updateOpStatus();\r
+\r
+ this.view = new _kiwi.view.Member({"model": this});\r
+ },\r
+\r
+\r
+ /**\r
+ * Sort modes in order of importance\r
+ */\r
sortModes: function (modes) {\r
var that = this;\r
\r
a_idx = i;\r
}\r
}\r
+\r
for (i = 0; i < user_prefixes.length; i++) {\r
if (user_prefixes[i].mode === b) {\r
b_idx = i;\r
}\r
}\r
+\r
if (a_idx < b_idx) {\r
return -1;\r
} else if (a_idx > b_idx) {\r
}\r
});\r
},\r
- initialize: function (attributes) {\r
- var nick, modes, prefix;\r
- nick = this.stripPrefix(this.get("nick"));\r
\r
- modes = this.get("modes");\r
- modes = modes || [];\r
- this.sortModes(modes);\r
- this.set({"nick": nick, "modes": modes, "prefix": this.getPrefix(modes)}, {silent: true});\r
- this.isOp();\r
- this.view = new _kiwi.view.Member({"model": this});\r
- },\r
+\r
addMode: function (mode) {\r
var modes_to_add = mode.split(''),\r
modes, prefix;\r
\r
modes = this.sortModes(modes);\r
this.set({"prefix": this.getPrefix(modes), "modes": modes});\r
- this.isOp();\r
+\r
+ this.updateOpStatus();\r
\r
this.view.render();\r
},\r
+\r
+\r
removeMode: function (mode) {\r
var modes_to_remove = mode.split(''),\r
modes, prefix;\r
});\r
\r
this.set({"prefix": this.getPrefix(modes), "modes": modes});\r
- this.isOp();\r
+\r
+ this.updateOpStatus();\r
\r
this.view.render();\r
},\r
+\r
+\r
+ /**\r
+ * Figure out a valid prefix given modes.\r
+ * If a user is an op but also has voice, the prefix\r
+ * should be the op as it is more important.\r
+ */\r
getPrefix: function (modes) {\r
var prefix = '';\r
var user_prefixes = this.get('user_prefixes');\r
prefix = _.detect(user_prefixes, function (prefix) {\r
return prefix.mode === modes[0];\r
});\r
+\r
prefix = (prefix) ? prefix.symbol : '';\r
}\r
+\r
return prefix;\r
},\r
+\r
+\r
+ /**\r
+ * Remove any recognised prefix from a nick\r
+ */\r
stripPrefix: function (nick) {\r
var tmp = nick, i, j, k, nick_char;\r
var user_prefixes = this.get('user_prefixes');\r
+\r
i = 0;\r
\r
nick_character_loop:\r
for (j = 0; j < nick.length; j++) {\r
nick_char = nick.charAt(j);\r
+\r
for (k = 0; k < user_prefixes.length; k++) {\r
if (nick_char === user_prefixes[k].symbol) {\r
i++;\r
continue nick_character_loop;\r
}\r
}\r
+\r
break;\r
}\r
\r
return tmp.substr(i);\r
},\r
+\r
+\r
+ /**\r
+ * Format this nick into readable format (eg. nick [ident@hostname])\r
+ */\r
displayNick: function (full) {\r
var display = this.get('nick');\r
\r
\r
return display;\r
},\r
- isOp: function () {\r
+\r
+\r
+ /**\r
+ * With the modes set on the user, make note if we have some sort of op status\r
+ */\r
+ updateOpStatus: function () {\r
var user_prefixes = this.get('user_prefixes'),\r
modes = this.get('modes'),\r
o, max_mode;\r
+\r
if (modes.length > 0) {\r
o = _.indexOf(user_prefixes, _.find(user_prefixes, function (prefix) {\r
return prefix.mode === 'o';\r
}));\r
+\r
max_mode = _.indexOf(user_prefixes, _.find(user_prefixes, function (prefix) {\r
return prefix.mode === modes[0];\r
}));\r
+\r
if ((max_mode === -1) || (max_mode > o)) {\r
this.set({"is_op": false}, {silent: true});\r
} else {\r
this.set({"is_op": true}, {silent: true});\r
}\r
+\r
} else {\r
this.set({"is_op": false}, {silent: true});\r
}\r