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