]> cgit.babelmonkeys.de Git - adhocweb.git/blob - js/adhoc.js
Restructure adhoc "class"
[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 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 item = self.buildHTMLField(this);
61             var label = $(this).attr("label");
62             if(label) {
63                 $("<label/>").text(label).attr("for", $(this).attr("var")).appendTo(fieldset);
64                 $("<br/>").appendTo(fieldset);
65             }
66             if ($(x).attr("type") === "result")
67                 item.attr("readonly", true);
68             fieldset.append(item);
69             if ($(this).attr("type") !== "hidden")
70                 fieldset.append("<br/>");
71         });
72         $(self.status.view).append(form);
73     },
74
75     buildHTMLField: function(fld) {
76         var field = $(fld), html = {
77             "hidden"      : "<input type='hidden'/>",
78             "boolean"     : "<input type='checkbox'/>",
79             "fixed"       : "<input type='text' readonly='true'/>",
80             "text-single" : "<input type='text'/>",
81             "text-private": "<input type='password'/>",
82             "text-multi"  : "<textarea rows='10' cols='70'/>",
83             "jid-single"  : "<input type='text'/>",
84             "jid-multi"   : "<textarea rows='10' cols='70'/>",
85             "list-single" : "<select/>",
86             "list-multi"  : "<select multiple='multiple'/>",
87         };
88         var type = field.attr('type');
89         var input = $(html[type] || "<input/>");
90         var name = field.attr("var");
91
92         input.addClass("df-item");
93         if (name) {
94             input.attr("name", name);
95             input.attr("id", name);
96         }
97
98         if (field.find("required").length > 0)
99             input.attr("required", "required");
100
101         /* Add possible values to the lists */
102         if (type === 'list-multi' || type==='list-single') {
103             field.find("option").each(function() {
104                 var option = $("<option/>");
105                 option.text($(this).attr("label"));
106                 option.val($(this).find("value").text());
107                 input.append(option);
108             });
109         }
110
111         /* Add/select default values */
112         field.children("value").each(function() {
113             var value = $(this).text();
114             if ((type === "text-multi") || (type === "jid-multi")) {
115                 input.text(input.text() + value + "\n"); /* .append() would work, but doesn't escape */
116             } else if (type === "list-multi") {
117                 input.children('option[value="' + value + '"]').each(function() {
118                     $(this).attr("selected", "selected");
119                 });
120             } else {
121                 input.val(value);
122             }
123         });
124
125         return input;
126     },
127
128     serializeToDataform: function (form) {
129         st = $build("x", {"xmlns": "jabber:x:data", "type": "submit"});
130         $(form).find(".df-item").each(function(){
131             st.c("field", {"var": $(this).attr("name")});
132             if (this.nodeName.toLowerCase() === "select" && this.multiple) {
133                 for (var i = 0; i < this.options.length; i++)
134                     if (this.options[i].selected)
135                         st.c("value").t(this.options[i].text).up();
136             } else if (this.nodeName.toLowerCase() === "textarea") {
137                 var sp_value = this.value.split(/\r?\n|\r/g);
138                 for(var i = 0; i < sp_value.length; i++)
139                     st.c("value").t(sp_value[i]).up();
140             } else if (this.nodeName.toLowerCase() === "input" && this.type === "checkbox") {
141                 if (this.checked) {
142                     st.c("value").t("1");
143                 } else {
144                     st.c("value").t("0");
145                 }
146             } else {
147                 /* if this has value then */
148                 st.c("value").t($(this).val()).up();
149             }
150             st.up();
151         });
152         st.up();
153         return st.tree();
154     },
155
156     displayResult: function (result) {
157         var self = this;
158         var status = $(result).find("command").attr("status");
159         var kinds = {'prev': 'Prev', 'next': 'Next', 'complete': 'Complete'};
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             for (kind in kinds) {
171                 (function(type) {
172                     input = $("<input type='button' disabled='disabled' value='" + kinds[type] + "'/>").click(function() {
173                         self.executeCommand(type, (type!= 'prev') && self.serializeToDataform('form'), function(e) { self.displayResult(e) });
174                     });
175                 })(kind);
176                 if ($(result).find('actions ' + kind).length > 0)
177                     input.removeAttr("disabled");
178                 $(self.status.view).append(input);
179             }
180
181             $("<input type='button' id='executeButton' value='Execute'/>").click(function() {
182                 self.executeCommand("execute", self.serializeToDataform('form'), function(e) { self.displayResult(e) });
183             }).appendTo(self.status.view);
184
185             $("<input type='button' value='Cancel'/>").click(function() {
186                 self.cancelCommand(function(e) { self.displayResult(e) });
187             }).appendTo(self.status.view);
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 }