if-Statement -> &&-Operator "magic
[adhocweb.git] / js / adhoc.js
1 var Adhoc = {
2     status: {
3         sessionid: null,
4         cmdNode: null,
5         queryJID: null
6     },
7
8     addNote: function (elem, text, type) {
9         if (!type) {
10            type = "info";
11         }
12         text = text.replace(/\n/g, "<br/>");
13         $(elem).append("<p class='" + type + "Note'>" + text + "</p>");
14     },
15
16     addForm: function (elem, x) {
17         var form = $("<form action='#'/>");
18         form.submit(function(event) {
19             Adhoc.executeCommand("execute", Adhoc.serializeToDataform('form'),
20                 function(e) { Adhoc.displayResult(elem, e) });
21             event.preventDefault();
22         });
23         var fieldset = $("<fieldset/>");
24         form.append(fieldset);
25         if ($(x).find("title").length > 0)
26             $("<legend/>").text($(x).find("title").text()).appendTo(fieldset);
27         if ($(x).find("instructions").length > 0)
28             $("<p/>").text($(x).find("instructions").text()).appendTo(fieldset);
29         $(x).find("field").each(function() {
30             var item = null;
31             var type = $(this).attr("type");
32             if($(this).attr("label")) {
33                 $("<label/>").text($(this).attr("label")).attr("for", $(this).attr("var")).appendTo(fieldset);
34                 $("<br/>").appendTo(fieldset);
35             }
36             switch(type) {
37             case "hidden":
38                 item = $("<input type='hidden'/>");
39                 break;
40             case "boolean":
41                 item = $("<input type='checkbox'/>");
42                 break;
43             case "text-multi":
44                 item = $("<textarea rows='10' cols='70'/>");
45                 break;
46             case "text-single":
47                 item = $("<input type='text'/>");
48                 break;
49             case "fixed":
50                 item = $("<input type='text'/>").attr("readonly",true);
51                 break;
52             case "jid-multi":
53                 item = $("<textarea rows='10' cols='70'/>");
54                 break;
55             case "jid-single":
56                 item = $("<input type='text'/>");
57                 break;
58             case "list-multi":
59                 item = $("<select multiple='multiple'/>");
60                 $(this).find("option").each(function() {
61                     $("<option/>").val($(this).find("value").text()).text($(this).attr("label")).appendTo(item);
62                 });
63                 break;
64             case "list-single":
65                 item = $("<select/>");
66                 $(this).find("option").each(function() {
67                     $("<option/>").val($(this).find("value").text()).text($(this).attr("label")).appendTo(item);
68                 });
69                 break;
70             case "text-private":
71                 item = $("<input type='password'/>");
72                 break;
73             default:
74                 item = $("<input/>");
75             }
76             item.addClass("df-item");
77             if ($(this).find("value").length > 0) {
78                 var value = null;
79                 if ((type == "text-multi") || (type == "jid-multi")) {
80                     value = "";
81                     $(this).find("value").each(function() {
82                         value = value + $(this).text() + "\n";
83                     });
84                     item.val(value);
85                 } else if (type == "list-multi") {
86                     $(this).children("value").each(function() {
87                         item.children('option[value="' + $(this).text() + '"]').each(function() {
88                             $(this).attr("selected", "selected");
89                 });
90                     });
91                 } else {
92                     item.val($(this).find("value").text());
93                 }
94             }
95             if ($(x).attr("type") == "result")
96                 item.attr("readonly", true);
97             if ($(this).attr("var")) {
98                 item.attr("name", $(this).attr("var"));
99                 item.attr("id", $(this).attr("var"));
100             }
101             fieldset.append(item);
102             if (type != "hidden")
103                 fieldset.append("<br/>");
104         });
105         $(elem).append(form);
106     },
107
108     serializeToDataform: function (form) {
109         st = $build("x", {"xmlns": "jabber:x:data", "type": "submit"});
110         $(form).find(".df-item").each(function(){
111             st.c("field", {"var": $(this).attr("name")});
112             if (this.nodeName.toLowerCase() == "select" && this.multiple) {
113                 for (var i = 0; i < this.options.length; i++)
114                     if (this.options[i].selected)
115                         st.c("value").t(this.options[i].text).up();
116             } else if (this.nodeName.toLowerCase() == "textarea") {
117                 var sp_value = this.value.split(/\r?\n|\r/g);
118                 for(var i = 0; i < sp_value.length; i++)
119                     st.c("value").t(sp_value[i]).up();
120             } else if (this.nodeName.toLowerCase() == "input" && this.type == "checkbox") {
121                 if (this.checked) {
122                     st.c("value").t("1");
123                 } else {
124                     st.c("value").t("0");
125                 }
126             } else {
127                 // if this has value then
128                 st.c("value").t($(this).val()).up();
129             }
130             st.up();
131         });
132         st.up();
133         return st.tree();
134     },
135
136     displayResult: function (elem, result) {
137         var status = $(result).find("command").attr("status");
138         var kinds = {'prev': 'Prev', 'next': 'Next', 'complete': 'Complete'};
139
140         $(elem).empty();
141         $(result).find("command > *").each(function(index, e) {
142             if ($(e).is("note")) {
143                 Adhoc.addNote(elem, $(e).text(), $(e).attr("type"));
144             } else if ($(e).is("x[xmlns=jabber:x:data]")) {
145                 Adhoc.addForm(elem, e);
146             }
147         });
148         if (status == "executing") {
149             for (kind in kinds) {
150                 input = $("<input type='button' disabled='disabled' value='" + kinds[kind] + "'/>").click(function() {
151                     Adhoc.executeCommand(kind, (kind != 'prev') && Adhoc.serializeToDataform('form'), function(e) { Adhoc.displayResult(elem, e) });
152                 });
153                 if ($(result).find('actions ' + kind).length > 0)
154                     input.removeAttr("disabled");
155                 $(elem).append(input);
156             }
157
158             $("<input type='button' id='executeButton' value='Execute'/>").click(function() {
159                 Adhoc.executeCommand("execute", Adhoc.serializeToDataform('form'), function(e) { Adhoc.displayResult(elem, e) });
160             }).appendTo(elem);
161
162             $("<input type='button' value='Cancel'/>").click(function() {
163                 Adhoc.cancelCommand(function(e) { Adhoc.displayResult(elem, e) });
164             }).appendTo(elem);
165         } else {
166             input = $("<input type='button' value='Start over'/>").bind("click", function() {
167                 $(elem).empty();
168                 Adhoc.status.sessionid = null;
169                 Adhoc.status.cmdNode = null;
170                 Adhoc.getCommandNodes(elem);
171             });
172             $(elem).append(input);
173         }
174     },
175
176     runCommand: function (item, callback) {
177         Adhoc.status.cmdNode = $(item).attr("id"); // Save node of executed command (in global var)
178         Adhoc.executeCommand("execute", false, function(result) {
179             Adhoc.status.sessionid = $(result).find("command").attr("sessionid");
180             callback(result);
181         });
182     },
183
184     executeCommand: function (type, childs, callback) {
185         if (Adhoc.status.sessionid)
186             var execIQ = $iq({ type: "set", to: Adhoc.status.queryJID, id: connection.getUniqueId() })
187                 .c("command", { xmlns: Strophe.NS.ADHOC, node: Adhoc.status.cmdNode, sessionid: Adhoc.status.sessionid, action: type });
188         else
189             var execIQ = $iq({ type: "set", to: Adhoc.status.queryJID, id: connection.getUniqueId() })
190                 .c("command", { xmlns: Strophe.NS.ADHOC, node: Adhoc.status.cmdNode, action: type });
191         if (childs)
192             execIQ.cnode(childs);
193             connection.sendIQ(execIQ, callback);
194     },
195
196     cancelCommand: function (callback) {
197         Adhoc.executeCommand("cancel", false, callback);
198         Adhoc.status.cmdNode = null
199         Adhoc.status.sessionid = null;
200     },
201
202     getCommandNodes: function (elem) {
203         var nodesIQ = $iq({ type: "get", to: Adhoc.status.queryJID, id: connection.getUniqueId() }).c("query", {xmlns: Strophe.NS.DISCO_ITEMS, node: Strophe.NS.ADHOC});
204         connection.sendIQ(nodesIQ, function(result) {
205             var items = $("<ul></ul>");
206             $(elem).append(items);
207             $(result).find("item").each(function(index, e) {
208                 $("<li></li>").append($("<a href='#' id='" + $(e).attr("node") + "'>" + $(e).attr("name") + "</a>").click(function (event) {
209                     Adhoc.runCommand(this, function (result) { Adhoc.displayResult(elem, result); });
210                     event.preventDefault();
211                 })).appendTo(items);
212             });
213         });
214     },
215
216     checkFeatures: function (elem, jid) {
217         if (Adhoc.status.sessionid)
218             Adhoc.cancelCommand();
219         Adhoc.status.queryJID = jid;
220         var featureIQ = $iq({ type: "get", to: Adhoc.status.queryJID, id: connection.getUniqueId() }).c("query", {xmlns: Strophe.NS.DISCO_INFO});
221         $(elem).empty();
222         connection.sendIQ(featureIQ,
223             function(result) { /* Callback */
224                 if ($(result).find("feature[var='" + Strophe.NS.ADHOC + "']").length > 0) {
225                     Adhoc.getCommandNodes(elem);
226                 } else {
227                     $(elem).append("<p>" + Adhoc.status.queryJID + " does NOT support AdHoc commands</p>");
228                 }
229             },
230             function(result) { /* Errback */
231                 $(elem).append("<p>Couldn't get list of supported features</p>");
232             }
233         );
234     }
235
236 }