]> cgit.babelmonkeys.de Git - adhocweb.git/blobdiff - js/adhoc.js
Fix all buttons performing the "complete" action
[adhocweb.git] / js / adhoc.js
index 7e2ba521bc30e910dcc9a258bc973493d2e6855b..cbe27523ab1ab905719fa82ca2fdce232d15c503 100644 (file)
@@ -23,27 +23,33 @@ if (!Function.prototype.bind) {
 
 Strophe.addNamespace("ADHOC", "http://jabber.org/protocol/commands");
 
-var Adhoc = {
-    status: {
+function Adhoc(view, readycb) {
+    this.status = {
         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/>");
-        $(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='#'/>");
+        var form = $("<form class='form-stacked' 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/>");
@@ -51,19 +57,18 @@ var Adhoc = {
         $(x).find("title").each(function() { $("<legend/>").text($(this).text()).appendTo(fieldset); });
         $(x).find("instructions").each(function() { $("<p/>").text($(this).text()).appendTo(fieldset); });
         $(x).find("field").each(function() {
+           var clearfix = $("<div class='clearfix'/>");
             var item = self.buildHTMLField(this);
             var label = $(this).attr("label");
             if(label) {
-                $("<label/>").text(label).attr("for", $(this).attr("var")).appendTo(fieldset);
-                $("<br/>").appendTo(fieldset);
+                $("<label/>").text(label).attr("for", $(this).attr("var")).appendTo(clearfix);
             }
             if ($(x).attr("type") === "result")
                 item.attr("readonly", true);
-            fieldset.append(item);
-            if ($(this).attr("type") !== "hidden")
-                fieldset.append("<br/>");
+            clearfix.append(item);
+            fieldset.append(clearfix);
         });
-        $(elem).append(form);
+        $(self.status.view).append(form);
     },
 
     buildHTMLField: function(fld) {
@@ -147,52 +152,49 @@ var Adhoc = {
         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 actions = $(result).find("actions:first");
 
-        $(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") {
+           var controls = $("<div class='actions'/>");
             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) });
-                    });
-                })(kind);
-                if ($(result).find('actions ' + kind).length > 0)
+               var input;
+               (function (type) {
+                   input = $("<input type='button' disabled='disabled' class='btn' value='" + kinds[type] + "'/>").click(function() {
+                       self.executeCommand(type, (type != 'prev') && self.serializeToDataform('form'), function(e) { self.displayResult(e) });
+                   }).appendTo(controls);
+               })(kind);
+                if (actions.find(kind).length > 0)
                     input.removeAttr("disabled");
-                $(elem).append(input);
+               if (actions.attr("execute") == kind)
+                   input.addClass("primary");
             }
 
-            $("<input type='button' id='executeButton' value='Execute'/>").click(function() {
-                self.executeCommand("execute", self.serializeToDataform('form'), function(e) { self.displayResult(elem, e) });
-            }).appendTo(elem);
-
-            $("<input type='button' value='Cancel'/>").click(function() {
-                self.cancelCommand(function(e) { self.displayResult(elem, e) });
-            }).appendTo(elem);
+            $("<input type='button' class='btn' value='Cancel'/>").click(function() {
+                self.cancelCommand(function(e) { self.displayResult(e) });
+            }).appendTo(controls);
+           $(self.status.view + " fieldset").append(controls);
         } 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;
-        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);
@@ -213,43 +215,44 @@ var Adhoc = {
     },
 
     cancelCommand: function (callback) {
+       if (this.status.cmdNode == null) return;
         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>");
-            $(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);
             });
+           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});
-        $(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);
     }
 }