2 * Implementation of ECMA Script 5 like bind from:
3 * https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind
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");
11 var fSlice = Array.prototype.slice,
12 aArgs = fSlice.call(arguments, 1),
14 fNOP = function () {},
15 fBound = function () {
16 return fToBind.apply(this instanceof fNOP ? this : oThis || window, Args.concat(fSlice.call(arguments)));
18 fNOP.prototype = this.prototype;
19 fBound.prototype = new fNOP();
24 Strophe.addNamespace("ADHOC", "http://jabber.org/protocol/commands");
26 function Adhoc(view, readycb) {
39 addNote: function (text, type) {
43 text = text.replace(/\n/g, "<br/>");
44 $(this.status.view).append("<p class='" + type + "Note'>" + text + "</p>");
47 addForm: function (x) {
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();
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");
63 $("<label/>").text(label).attr("for", $(this).attr("var")).appendTo(fieldset);
64 $("<br/>").appendTo(fieldset);
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/>");
72 $(self.status.view).append(form);
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'/>",
88 var type = field.attr('type');
89 var input = $(html[type] || "<input/>");
90 var name = field.attr("var");
92 input.addClass("df-item");
94 input.attr("name", name);
95 input.attr("id", name);
98 if (field.find("required").length > 0)
99 input.attr("required", "required");
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);
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");
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") {
142 st.c("value").t("1");
144 st.c("value").t("0");
147 /* if this has value then */
148 st.c("value").t($(this).val()).up();
156 displayResult: function (result) {
158 var status = $(result).find("command").attr("status");
159 var kinds = {'prev': 'Prev', 'next': 'Next', 'complete': 'Complete'};
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]")) {
169 if (status === "executing") {
170 for (kind in kinds) {
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) });
176 if ($(result).find('actions ' + kind).length > 0)
177 input.removeAttr("disabled");
178 $(self.status.view).append(input);
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);
185 $("<input type='button' value='Cancel'/>").click(function() {
186 self.cancelCommand(function(e) { self.displayResult(e) });
187 }).appendTo(self.status.view);
189 self.status.sessionid = null;
190 self.status.cmdNode = null;
191 self.status.readycb();
195 runCommand: function (item, callback) {
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");
202 this.executeCommand("execute", false, cb.bind(this));
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 });
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 });
213 execIQ.cnode(childs);
214 connection.sendIQ(execIQ, callback);
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;
224 getCommandNodes: function (callback) {
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();
240 checkFeatures: function (jid, cb, ecb) {
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();
248 function callback(result) {
249 if ($(result).find("feature[var='" + Strophe.NS.ADHOC + "']").length > 0) {
256 connection.sendIQ(featureIQ, callback, ecb);