]> jfr.im git - irc/quakenet/qwebirc.git/commitdiff
Tidy up menu code and add op/deop/voice/devoice along with function composition and...
authorChris Porter <redacted>
Thu, 4 Jun 2009 23:51:48 +0000 (00:51 +0100)
committerChris Porter <redacted>
Thu, 4 Jun 2009 23:51:48 +0000 (00:51 +0100)
js/irc/ircclient.js
js/jslib.js
js/ui/frontends/qui.js
js/ui/menuitems.js

index 5939c35bf074b39b65b117c1dce4442caf371a8b..155a55237791ed6bcce935c5ce87b3f1a9587bdd 100644 (file)
@@ -497,6 +497,13 @@ qwebirc.irc.IRCClient = new Class({
     
     this.newServerLine("DISCONNECT", {"m": message});
   },
+  nickOnChanHasPrefix: function(nick, channel, prefix) {
+    var entry = this.tracker.getNickOnChannel(nick, channel);
+    if(!$defined(entry))
+      return false; /* shouldn't happen */
+   
+    return entry.prefixes.indexOf(prefix) != -1;
+  },
   supported: function(key, value) {
     if(key == "PREFIX") {
       var l = (value.length - 2) / 2;
index e58bd954257941a924e91539a421cbe097766260..b07fba134c6e3b0b0ab000249975dba4486b7cd7 100644 (file)
@@ -312,3 +312,21 @@ qwebirc.util.b64Decode = function(data) {
 
   return output.join("");
 }
+
+qwebirc.util.composeAnd = function() {
+ var xargs = arguments;
+
+  return function() {
+    for(var i=0;i<xargs.length;i++)
+      if(!xargs[i].apply(this, arguments))
+        return false;
+        
+    return true;
+  }
+}
+
+qwebirc.util.invertFn = function(fn) {
+  return function() {
+    return !fn.apply(this, arguments);
+  }
+}
index f89fda4888e99c317667027af924fd766d243f18..4efc974ccd7e4e9ae9b405f18f07ab7c114b5988 100644 (file)
@@ -423,19 +423,19 @@ qwebirc.ui.QUI.Window = new Class({
     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;
   },
@@ -485,7 +485,7 @@ qwebirc.ui.QUI.Window = new Class({
       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));
     
index 3f9ee89c52fee57ac38e8da894034302a3979e21..05bc92157346578eca98897736be0a2a7e40909f 100644 (file)
@@ -1,45 +1,3 @@
-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"],
@@ -48,3 +6,78 @@ qwebirc.ui.UI_COMMANDS = [
   ["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)
+    }
+  ];
+}();