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) {
166 input = $("<input type='button' disabled='disabled' value='" + kinds[type] + "'/>").click(function() {
167 self.executeCommand(type, (type!= 'prev') && self.serializeToDataform('form'), function(e) { self.displayResult(elem, e) });
170 if ($(result).find('actions ' + kind).length > 0)
171 input.removeAttr("disabled");
172 $(elem).append(input);
175 $("<input type='button' id='executeButton' value='Execute'/>").click(function() {
176 self.executeCommand("execute", self.serializeToDataform('form'), function(e) { self.displayResult(elem, e) });
179 $("<input type='button' value='Cancel'/>").click(function() {
180 self.cancelCommand(function(e) { self.displayResult(elem, e) });
183 input = $("<input type='button' value='Start over'/>").bind("click", function() {
185 self.status.sessionid = null;
186 self.status.cmdNode = null;
187 self.getCommandNodes(elem);
189 $(elem).append(input);
193 runCommand: function (item, callback) {
195 this.status.cmdNode = $(item).attr("id"); /* Save node of executed command (in global var) */
196 cb = function(result) {
197 this.status.sessionid = $(result).find("command").attr("sessionid");
200 this.executeCommand("execute", false, cb.bind(this));
203 executeCommand: function (type, childs, callback) {
204 if (this.status.sessionid)
205 var execIQ = $iq({ type: "set", to: this.status.queryJID, id: connection.getUniqueId() })
206 .c("command", { xmlns: Strophe.NS.ADHOC, node: this.status.cmdNode, sessionid: this.status.sessionid, action: type });
208 var execIQ = $iq({ type: "set", to: this.status.queryJID, id: connection.getUniqueId() })
209 .c("command", { xmlns: Strophe.NS.ADHOC, node: this.status.cmdNode, action: type });
211 execIQ.cnode(childs);
212 connection.sendIQ(execIQ, callback);
215 cancelCommand: function (callback) {
216 this.executeCommand("cancel", false, callback);
217 this.status.cmdNode = null
218 this.status.sessionid = null;
221 getCommandNodes: function (elem) {
223 var nodesIQ = $iq({ type: "get", to: self.status.queryJID, id: connection.getUniqueId() }).c("query", {xmlns: Strophe.NS.DISCO_ITEMS, node: Strophe.NS.ADHOC});
224 connection.sendIQ(nodesIQ, function(result) {
225 var items = $("<ul></ul>");
226 $(elem).append(items);
227 $(result).find("item").each(function(index, e) {
228 $("<li></li>").append($("<a href='#' id='" + $(e).attr("node") + "'>" + $(e).attr("name") + "</a>").click(function (event) {
229 self.runCommand(this, function (result) { self.displayResult(elem, result); });
230 event.preventDefault();
236 checkFeatures: function (elem, jid) {
238 if (this.status.sessionid)
239 this.cancelCommand();
240 this.status.queryJID = jid;
241 var featureIQ = $iq({ type: "get", to: this.status.queryJID, id: connection.getUniqueId() }).c("query", {xmlns: Strophe.NS.DISCO_INFO});
243 cb = function(result) { /* Callback */
244 if ($(result).find("feature[var='" + Strophe.NS.ADHOC + "']").length > 0) {
245 this.getCommandNodes(elem);
247 $(elem).append("<p>" + this.status.queryJID + " does NOT support AdHoc commands</p>");
250 ecb = function(result) { /* Errback */
251 $(elem).append("<p>Couldn't get list of supported features</p>");
253 connection.sendIQ(featureIQ, cb.bind(this), ecb.bind(this));