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");
33 addNote: function (elem, text, type) {
37 text = text.replace(/\n/g, "<br/>");
38 $(elem).append("<p class='" + type + "Note'>" + text + "</p>");
41 addForm: function (elem, x) {
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();
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");
57 $("<label/>").text(label).attr("for", $(this).attr("var")).appendTo(fieldset);
58 $("<br/>").appendTo(fieldset);
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/>");
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'/>",
82 var type = field.attr('type');
83 var input = $(html[type] || "<input/>");
84 var name = field.attr("var");
86 input.addClass("df-item");
88 input.attr("name", name);
89 input.attr("id", name);
92 if (field.find("required").length > 0)
93 input.attr("required", "required");
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);
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");
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") {
136 st.c("value").t("1");
138 st.c("value").t("0");
141 /* if this has value then */
142 st.c("value").t($(this).val()).up();
150 displayResult: function (elem, result) {
152 var status = $(result).find("command").attr("status");
153 var kinds = {'prev': 'Prev', 'next': 'Next', 'complete': 'Complete'};
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);
163 if (status === "executing") {
164 for (kind in kinds) {
165 input = $("<input type='button' disabled='disabled' value='" + kinds[kind] + "'/>").click(function() {
166 self.executeCommand(kind, (kind != 'prev') && self.serializeToDataform('form'), function(e) { self.displayResult(elem, e) });
168 if ($(result).find('actions ' + kind).length > 0)
169 input.removeAttr("disabled");
170 $(elem).append(input);
173 $("<input type='button' id='executeButton' value='Execute'/>").click(function() {
174 self.executeCommand("execute", self.serializeToDataform('form'), function(e) { self.displayResult(elem, e) });
177 $("<input type='button' value='Cancel'/>").click(function() {
178 self.cancelCommand(function(e) { self.displayResult(elem, e) });
181 input = $("<input type='button' value='Start over'/>").bind("click", function() {
183 self.status.sessionid = null;
184 self.status.cmdNode = null;
185 self.getCommandNodes(elem);
187 $(elem).append(input);
191 runCommand: function (item, callback) {
193 this.status.cmdNode = $(item).attr("id"); /* Save node of executed command (in global var) */
194 cb = function(result) {
195 this.status.sessionid = $(result).find("command").attr("sessionid");
198 this.executeCommand("execute", false, cb.bind(this));
201 executeCommand: function (type, childs, callback) {
202 if (this.status.sessionid)
203 var execIQ = $iq({ type: "set", to: this.status.queryJID, id: connection.getUniqueId() })
204 .c("command", { xmlns: Strophe.NS.ADHOC, node: this.status.cmdNode, sessionid: this.status.sessionid, action: type });
206 var execIQ = $iq({ type: "set", to: this.status.queryJID, id: connection.getUniqueId() })
207 .c("command", { xmlns: Strophe.NS.ADHOC, node: this.status.cmdNode, action: type });
209 execIQ.cnode(childs);
210 connection.sendIQ(execIQ, callback);
213 cancelCommand: function (callback) {
214 this.executeCommand("cancel", false, callback);
215 this.status.cmdNode = null
216 this.status.sessionid = null;
219 getCommandNodes: function (elem) {
221 var nodesIQ = $iq({ type: "get", to: self.status.queryJID, id: connection.getUniqueId() }).c("query", {xmlns: Strophe.NS.DISCO_ITEMS, node: Strophe.NS.ADHOC});
222 connection.sendIQ(nodesIQ, function(result) {
223 var items = $("<ul></ul>");
224 $(elem).append(items);
225 $(result).find("item").each(function(index, e) {
226 $("<li></li>").append($("<a href='#' id='" + $(e).attr("node") + "'>" + $(e).attr("name") + "</a>").click(function (event) {
227 self.runCommand(this, function (result) { self.displayResult(elem, result); });
228 event.preventDefault();
234 checkFeatures: function (elem, jid) {
236 if (this.status.sessionid)
237 this.cancelCommand();
238 this.status.queryJID = jid;
239 var featureIQ = $iq({ type: "get", to: this.status.queryJID, id: connection.getUniqueId() }).c("query", {xmlns: Strophe.NS.DISCO_INFO});
241 cb = function(result) { /* Callback */
242 if ($(result).find("feature[var='" + Strophe.NS.ADHOC + "']").length > 0) {
243 this.getCommandNodes(elem);
245 $(elem).append("<p>" + this.status.queryJID + " does NOT support AdHoc commands</p>");
248 ecb = function(result) { /* Errback */
249 $(elem).append("<p>Couldn't get list of supported features</p>");
251 connection.sendIQ(featureIQ, cb.bind(this), ecb.bind(this));