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