]> cgit.babelmonkeys.de Git - adhocweb.git/blob - js/adhoc.js
574c5a65fb24949b4e052ea71640669afceaffba
[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                     if (kind == 'prev')
152                         Adhoc.executeCommand(kind, false, function(e) { Adhoc.displayResult(elem, e) });
153                     else
154                         Adhoc.executeCommand(kind, Adhoc.serializeToDataform('form'), function(e) { Adhoc.displayResult(elem, e) });
155                 });
156                 if ($(result).find('actions ' + kind).length > 0)
157                     input.removeAttr("disabled");
158                 $(elem).append(input);
159             }
160
161             $("<input type='button' id='executeButton' value='Execute'/>").click(function() {
162                 Adhoc.executeCommand("execute", Adhoc.serializeToDataform('form'), function(e) { Adhoc.displayResult(elem, e) });
163             }).appendTo(elem);
164
165             $("<input type='button' value='Cancel'/>").click(function() {
166                 Adhoc.cancelCommand(function(e) { Adhoc.displayResult(elem, e) });
167             }).appendTo(elem);
168         } else {
169             input = $("<input type='button' value='Start over'/>").bind("click", function() {
170                 $(elem).empty();
171                 Adhoc.status.sessionid = null;
172                 Adhoc.status.cmdNode = null;
173                 Adhoc.getCommandNodes(elem);
174             });
175             $(elem).append(input);
176         }
177     },
178
179     runCommand: function (item, callback) {
180         Adhoc.status.cmdNode = $(item).attr("id"); // Save node of executed command (in global var)
181         Adhoc.executeCommand("execute", false, function(result) {
182             Adhoc.status.sessionid = $(result).find("command").attr("sessionid");
183             callback(result);
184         });
185     },
186
187     executeCommand: function (type, childs, callback) {
188         if (Adhoc.status.sessionid)
189             var execIQ = $iq({ type: "set", to: Adhoc.status.queryJID, id: connection.getUniqueId() })
190                 .c("command", { xmlns: Strophe.NS.ADHOC, node: Adhoc.status.cmdNode, sessionid: Adhoc.status.sessionid, action: type });
191         else
192             var execIQ = $iq({ type: "set", to: Adhoc.status.queryJID, id: connection.getUniqueId() })
193                 .c("command", { xmlns: Strophe.NS.ADHOC, node: Adhoc.status.cmdNode, action: type });
194         if (childs)
195             execIQ.cnode(childs);
196             connection.sendIQ(execIQ, callback);
197     },
198
199     cancelCommand: function (callback) {
200         Adhoc.executeCommand("cancel", false, callback);
201         Adhoc.status.cmdNode = null
202         Adhoc.status.sessionid = null;
203     },
204
205     getCommandNodes: function (elem) {
206         var nodesIQ = $iq({ type: "get", to: Adhoc.status.queryJID, id: connection.getUniqueId() }).c("query", {xmlns: Strophe.NS.DISCO_ITEMS, node: Strophe.NS.ADHOC});
207         connection.sendIQ(nodesIQ, function(result) {
208             var items = $("<ul></ul>");
209             $(elem).append(items);
210             $(result).find("item").each(function(index, e) {
211                 $("<li></li>").append($("<a href='#' id='" + $(e).attr("node") + "'>" + $(e).attr("name") + "</a>").click(function (event) {
212                     Adhoc.runCommand(this, function (result) { Adhoc.displayResult(elem, result); });
213                     event.preventDefault();
214                 })).appendTo(items);
215             });
216         });
217     },
218
219     checkFeatures: function (elem, jid) {
220         if (Adhoc.status.sessionid)
221             Adhoc.cancelCommand();
222         Adhoc.status.queryJID = jid;
223         var featureIQ = $iq({ type: "get", to: Adhoc.status.queryJID, id: connection.getUniqueId() }).c("query", {xmlns: Strophe.NS.DISCO_INFO});
224         $(elem).empty();
225         connection.sendIQ(featureIQ,
226             function(result) { /* Callback */
227                 if ($(result).find("feature[var='" + Strophe.NS.ADHOC + "']").length > 0) {
228                     Adhoc.getCommandNodes(elem);
229                 } else {
230                     $(elem).append("<p>" + Adhoc.status.queryJID + " does NOT support AdHoc commands</p>");
231                 }
232             },
233             function(result) { /* Errback */
234                 $(elem).append("<p>Couldn't get list of supported features</p>");
235             }
236         );
237     }
238
239 }