parent.appendChild(e);
e.addClass("menu");
+ var nickArray = [nick];
qwebirc.ui.MENU_ITEMS.forEach(function(x) {
- var text = x[0], fn = x[1], visible_fn = x[2];
-
- if(visible_fn == qwebirc.ui.menuitems.fns.always || visible_fn.apply(this)) {
- var e2 = new Element("a");
- e.appendChild(e2);
+ if(!x.predicate || x.predicate !== true && !x.predicate.apply(this, nickArray))
+ return;
+
+ var e2 = new Element("a");
+ e.appendChild(e2);
- e2.href = "#";
- e2.set("text", "- " + text);
+ e2.href = "#";
+ e2.set("text", "- " + x.text);
- e2.addEvent("focus", function() { this.blur() }.bind(e2));
- e2.addEvent("click", function(ev) { new Event(ev.stop()); this.menuClick(fn); }.bind(this));
- }
+ e2.addEvent("focus", function() { this.blur() }.bind(e2));
+ e2.addEvent("click", function(ev) { new Event(ev.stop()); this.menuClick(x.fn); }.bind(this));
}.bind(this));
return e;
},
this.prevNick = e;
e.addClass("selected");
this.moveMenuClass();
- e.menu = this.createMenu(x.realNick, e);
+ e.menu = this.createMenu(e.realNick, e);
new Event(x).stop();
}.bind(this));
-qwebirc.ui.menuitems = {fns: {}};
-
-qwebirc.ui.menuitems.fns.always = function() {
- return true;
-};
-
-qwebirc.ui.menuitems.fns.is_opped = function() {
- var channel = this.name; /* window name */
- var myNick = this.client.nickname;
-
- var entry = this.client.tracker.getNickOnChannel(myNick, channel);
- if(!$defined(entry))
- return false; /* shouldn't happen */
-
- /* TODO: improve (halfops) */
- return entry.prefixes.indexOf("@") != -1;
-};
-
-/*
- [text, command_fn, visible_predicate]
-
- - text is the text shown to the user in the menu
- - command_fn is executed when they click
- (this will be the current window)
- - visible_predicate will be executed to determine whether or not to show the item
- (this will be the current window)
-*/
-qwebirc.ui.MENU_ITEMS = [
- ["whois", function(nick) {
- this.client.exec("/WHOIS " + nick);
- }, qwebirc.ui.menuitems.fns.always],
- ["query", function(nick) {
- this.client.exec("/QUERY " + nick);
- }, qwebirc.ui.menuitems.fns.always],
- ["slap", function(nick) {
- this.client.exec("/ME slaps " + nick + " around a bit with a large fishbot");
- }, qwebirc.ui.menuitems.fns.always],
- ["kick", function(nick) { /* TODO: disappear when we're deopped */
- this.client.exec("/KICK " + nick + " wibble");
- }, qwebirc.ui.menuitems.fns.is_opped]
-];
-
qwebirc.ui.UI_COMMANDS = [
["Options", "options"],
["Add webchat to your site", "embedded"],
["Frequently asked questions", "faq"],
["About qwebirc", "about"]
];
+
+qwebirc.ui.MENU_ITEMS = function() {
+ var isOpped = function(nick) {
+ var channel = this.name; /* window name */
+ var myNick = this.client.nickname;
+
+ return this.client.nickOnChanHasPrefix(myNick, channel, "@");
+ };
+
+ var isVoiced = function(nick) {
+ var channel = this.name;
+ var myNick = this.client.nickname;
+
+ return this.client.nickOnChanHasPrefix(myNick, channel, "+");
+ };
+
+ var targetOpped = function(nick) {
+ var channel = this.name;
+ return this.client.nickOnChanHasPrefix(nick, channel, "@");
+ };
+
+ var targetVoiced = function(nick) {
+ var channel = this.name;
+ return this.client.nickOnChanHasPrefix(nick, channel, "+");
+ };
+
+ var invert = qwebirc.util.invertFn, compose = qwebirc.util.composeAnd;
+
+ var command = function(cmd) {
+ return function(nick) { this.client.exec("/" + cmd + " " + nick); };
+ };
+
+ return [
+ {
+ text: "whois",
+ fn: command("whois"),
+ predicate: true
+ },
+ {
+ text: "query",
+ fn: command("query"),
+ predicate: true
+ },
+ {
+ text: "slap",
+ fn: function(nick) { this.client.exec("/ME slaps " + nick + " around a bit with a large fishbot"); },
+ predicate: true
+ },
+ {
+ text: "kick", /* TODO: disappear when we're deopped */
+ fn: function(nick) { this.client.exec("/KICK " + nick + " wibble"); },
+ predicate: isOpped
+ },
+ {
+ text: "op",
+ fn: command("op"),
+ predicate: compose(isOpped, invert(targetOpped))
+ },
+ {
+ text: "deop",
+ fn: command("deop"),
+ predicate: compose(isOpped, targetOpped)
+ },
+ {
+ text: "voice",
+ fn: command("voice"),
+ predicate: compose(isOpped, invert(targetVoiced))
+ },
+ {
+ text: "devoice",
+ fn: command("devoice"),
+ predicate: compose(isOpped, targetVoiced)
+ }
+ ];
+}();