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