]> git.babelmonkeys.de Git - adhocweb.git/commitdiff
Restructure adhoc "class"
authorFlorian Zeitz <florob@babelmonkeys.de>
Tue, 17 Jan 2012 21:13:45 +0000 (22:13 +0100)
committerFlorian Zeitz <florob@babelmonkeys.de>
Tue, 17 Jan 2012 21:13:45 +0000 (22:13 +0100)
js/adhoc.js
js/main.js

index 7e2ba521bc30e910dcc9a258bc973493d2e6855b..0266318c9e99626dd0b9d0a053ffc2899e54e02d 100644 (file)
@@ -23,27 +23,33 @@ if (!Function.prototype.bind) {
 
 Strophe.addNamespace("ADHOC", "http://jabber.org/protocol/commands");
 
 
 Strophe.addNamespace("ADHOC", "http://jabber.org/protocol/commands");
 
-var Adhoc = {
-    status: {
+function Adhoc(view, readycb) {
+    this.status = {
         sessionid: null,
         cmdNode: null,
         sessionid: null,
         cmdNode: null,
-        queryJID: null
-    },
+        queryJID: null,
+       readycb: readycb,
+       view: view
+    };
+}
+
+Adhoc.prototype = {
+    constructor: Adhoc,
 
 
-    addNote: function (elem, text, type) {
+    addNote: function (text, type) {
         if (!type) {
            type = "info";
         }
         text = text.replace(/\n/g, "<br/>");
         if (!type) {
            type = "info";
         }
         text = text.replace(/\n/g, "<br/>");
-        $(elem).append("<p class='" + type + "Note'>" + text + "</p>");
+        $(this.status.view).append("<p class='" + type + "Note'>" + text + "</p>");
     },
 
     },
 
-    addForm: function (elem, x) {
+    addForm: function (x) {
         var self = this;
         var form = $("<form action='#'/>");
         form.submit(function(event) {
             self.executeCommand("execute", self.serializeToDataform('form'),
         var self = this;
         var form = $("<form action='#'/>");
         form.submit(function(event) {
             self.executeCommand("execute", self.serializeToDataform('form'),
-                function(e) { self.displayResult(elem, e) });
+                function(e) { self.displayResult(e) });
             event.preventDefault();
         });
         var fieldset = $("<fieldset/>");
             event.preventDefault();
         });
         var fieldset = $("<fieldset/>");
@@ -63,7 +69,7 @@ var Adhoc = {
             if ($(this).attr("type") !== "hidden")
                 fieldset.append("<br/>");
         });
             if ($(this).attr("type") !== "hidden")
                 fieldset.append("<br/>");
         });
-        $(elem).append(form);
+        $(self.status.view).append(form);
     },
 
     buildHTMLField: function(fld) {
     },
 
     buildHTMLField: function(fld) {
@@ -147,52 +153,48 @@ var Adhoc = {
         return st.tree();
     },
 
         return st.tree();
     },
 
-    displayResult: function (elem, result) {
+    displayResult: function (result) {
         var self = this;
         var status = $(result).find("command").attr("status");
         var kinds = {'prev': 'Prev', 'next': 'Next', 'complete': 'Complete'};
 
         var self = this;
         var status = $(result).find("command").attr("status");
         var kinds = {'prev': 'Prev', 'next': 'Next', 'complete': 'Complete'};
 
-        $(elem).empty();
-        $(result).find("command > *").each(function(index, e) {
-            if ($(e).is("note")) {
-                self.addNote(elem, $(e).text(), $(e).attr("type"));
-            } else if ($(e).is("x[xmlns=jabber:x:data]")) {
-                self.addForm(elem, e);
+        $(self.status.view).empty();
+        $(result).find("command > *").each(function() {
+            if ($(this).is("note")) {
+                self.addNote($(this).text(), $(this).attr("type"));
+            } else if ($(this).is("x[xmlns=jabber:x:data]")) {
+                self.addForm(this);
             }
         });
         if (status === "executing") {
             for (kind in kinds) {
                 (function(type) {
                     input = $("<input type='button' disabled='disabled' value='" + kinds[type] + "'/>").click(function() {
             }
         });
         if (status === "executing") {
             for (kind in kinds) {
                 (function(type) {
                     input = $("<input type='button' disabled='disabled' value='" + kinds[type] + "'/>").click(function() {
-                        self.executeCommand(type, (type!= 'prev') && self.serializeToDataform('form'), function(e) { self.displayResult(elem, e) });
+                        self.executeCommand(type, (type!= 'prev') && self.serializeToDataform('form'), function(e) { self.displayResult(e) });
                     });
                 })(kind);
                 if ($(result).find('actions ' + kind).length > 0)
                     input.removeAttr("disabled");
                     });
                 })(kind);
                 if ($(result).find('actions ' + kind).length > 0)
                     input.removeAttr("disabled");
-                $(elem).append(input);
+                $(self.status.view).append(input);
             }
 
             $("<input type='button' id='executeButton' value='Execute'/>").click(function() {
             }
 
             $("<input type='button' id='executeButton' value='Execute'/>").click(function() {
-                self.executeCommand("execute", self.serializeToDataform('form'), function(e) { self.displayResult(elem, e) });
-            }).appendTo(elem);
+                self.executeCommand("execute", self.serializeToDataform('form'), function(e) { self.displayResult(e) });
+            }).appendTo(self.status.view);
 
             $("<input type='button' value='Cancel'/>").click(function() {
 
             $("<input type='button' value='Cancel'/>").click(function() {
-                self.cancelCommand(function(e) { self.displayResult(elem, e) });
-            }).appendTo(elem);
+                self.cancelCommand(function(e) { self.displayResult(e) });
+            }).appendTo(self.status.view);
         } else {
         } else {
-            input = $("<input type='button' value='Start over'/>").bind("click", function() {
-                $(elem).empty();
-                self.status.sessionid = null;
-                self.status.cmdNode = null;
-                self.getCommandNodes(elem);
-            });
-            $(elem).append(input);
+           self.status.sessionid = null;
+           self.status.cmdNode = null;
+           self.status.readycb();
         }
     },
 
     runCommand: function (item, callback) {
         var cb;
         }
     },
 
     runCommand: function (item, callback) {
         var cb;
-        this.status.cmdNode = $(item).attr("id"); /* Save node of executed command (in global var) */
+        this.status.cmdNode = $(item).attr("id"); /* Save node of executed command */
         cb = function(result) {
             this.status.sessionid = $(result).find("command").attr("sessionid");
             callback(result);
         cb = function(result) {
             this.status.sessionid = $(result).find("command").attr("sessionid");
             callback(result);
@@ -213,43 +215,44 @@ var Adhoc = {
     },
 
     cancelCommand: function (callback) {
     },
 
     cancelCommand: function (callback) {
+       if (this.status.cmdNode == null) return;
         this.executeCommand("cancel", false, callback);
         this.status.cmdNode = null
         this.status.sessionid = null;
     },
 
         this.executeCommand("cancel", false, callback);
         this.status.cmdNode = null
         this.status.sessionid = null;
     },
 
-    getCommandNodes: function (elem) {
+    getCommandNodes: function (callback) {
         var self = this;
         var nodesIQ = $iq({ type: "get", to: self.status.queryJID, id: connection.getUniqueId() }).c("query", {xmlns: Strophe.NS.DISCO_ITEMS, node: Strophe.NS.ADHOC});
         connection.sendIQ(nodesIQ, function(result) {
             var items = $("<ul></ul>");
         var self = this;
         var nodesIQ = $iq({ type: "get", to: self.status.queryJID, id: connection.getUniqueId() }).c("query", {xmlns: Strophe.NS.DISCO_ITEMS, node: Strophe.NS.ADHOC});
         connection.sendIQ(nodesIQ, function(result) {
             var items = $("<ul></ul>");
-            $(elem).append(items);
-            $(result).find("item").each(function(index, e) {
-                $("<li></li>").append($("<a href='#' id='" + $(e).attr("node") + "'>" + $(e).attr("name") + "</a>").click(function (event) {
-                    self.runCommand(this, function (result) { self.displayResult(elem, result); });
+            $(result).find("item").each(function() {
+                $("<li></li>").append($("<a href='#' id='" + $(this).attr("node") + "'>" + $(this).attr("name") + "</a>").click(function (event) {
+                   self.cancelCommand(function(){});
+                    self.runCommand(this, function (result) { self.displayResult(result); });
                     event.preventDefault();
                 })).appendTo(items);
             });
                     event.preventDefault();
                 })).appendTo(items);
             });
+           callback(items);
         });
     },
 
         });
     },
 
-    checkFeatures: function (elem, jid) {
-        var cb, ecb;
+    checkFeatures: function (jid, cb, ecb) {
+       var callback;
         if (this.status.sessionid)
             this.cancelCommand();
         this.status.queryJID = jid;
         var featureIQ = $iq({ type: "get", to: this.status.queryJID, id: connection.getUniqueId() }).c("query", {xmlns: Strophe.NS.DISCO_INFO});
         if (this.status.sessionid)
             this.cancelCommand();
         this.status.queryJID = jid;
         var featureIQ = $iq({ type: "get", to: this.status.queryJID, id: connection.getUniqueId() }).c("query", {xmlns: Strophe.NS.DISCO_INFO});
-        $(elem).empty();
-        cb = function(result) { /* Callback */
-            if ($(result).find("feature[var='" + Strophe.NS.ADHOC + "']").length > 0) {
-                this.getCommandNodes(elem);
-            } else {
-                $(elem).append("<p>" + this.status.queryJID + " does NOT support AdHoc commands</p>");
-            }
-        }
-        ecb = function(result) { /* Errback */
-            $(elem).append("<p>Couldn't get list of supported features</p>");
-        }
-        connection.sendIQ(featureIQ, cb.bind(this), ecb.bind(this));
+        $(this.status.view).empty();
+
+       function callback(result) {
+           if ($(result).find("feature[var='" + Strophe.NS.ADHOC + "']").length > 0) {
+               cb(result);
+           } else {
+               ecb(result);
+           }
+       }
+
+        connection.sendIQ(featureIQ, callback, ecb);
     }
 }
     }
 }
index 73ace060a8394f96a984b32e9ba61fa9080e287c..621596162d3ce61b9068efa0eed6c07c7fd9605e 100644 (file)
@@ -4,6 +4,8 @@ var show_log = true;
 var localJID = null;
 var connection   = null;
 
 var localJID = null;
 var connection   = null;
 
+var commandCenter = null;
+
 function log(msg) {
     var entry = $('<div></div>').append(document.createTextNode(msg));
     $('#log').append(entry);
 function log(msg) {
     var entry = $('<div></div>').append(document.createTextNode(msg));
     $('#log').append(entry);
@@ -17,6 +19,17 @@ function rawOutput(data) {
     log('SENT: ' + data);
 }
 
     log('SENT: ' + data);
 }
 
+function getFeatures(jid) {
+    var cb, ecb;
+    cb = function(result) { /* Callback */
+       commandCenter.getCommandNodes(function(items) { $('#output').empty(); $('#output').append(items) });
+    }
+    ecb = function(result) { /* Errback */
+       $('#output').append("<p>" + jid + " does NOT support AdHoc commands</p>");
+    }
+    commandCenter.checkFeatures(jid, cb, ecb);
+}
+
 function onConnect(status) {
     if (status == Strophe.Status.CONNECTING) {
         log('Strophe is connecting.');
 function onConnect(status) {
     if (status == Strophe.Status.CONNECTING) {
         log('Strophe is connecting.');
@@ -37,7 +50,13 @@ function onConnect(status) {
         log('Strophe is connected.');
         $('#queryJID').val(connection.domain);
         $('#query').show();
         log('Strophe is connected.');
         $('#queryJID').val(connection.domain);
         $('#query').show();
-        Adhoc.checkFeatures("#output", connection.domain);
+       commandCenter = new Adhoc("#output", function() {
+               $("<input type='button' value='Start over'/>").bind("click", function() {
+                       $('#output').empty();
+                       commandCenter.getCommandNodes(function(items) { $('#output').append(items) });
+               }).appendTo('#output');
+       });
+        getFeatures(connection.domain);
     }
 }
 
     }
 }
 
@@ -98,7 +117,7 @@ $(document).ready(function () {
     });
 
     $('#queryForm').bind('submit', function (event) {
     });
 
     $('#queryForm').bind('submit', function (event) {
-        Adhoc.checkFeatures("#output", $('#queryJID').val());
+        getFeatures($('#queryJID').val());
         event.preventDefault();
     });
 });
         event.preventDefault();
     });
 });