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 class='form-stacked' 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 clearfix = $("<div class='clearfix'/>");
61 var item = self.buildHTMLField(this);
62 var label = $(this).attr("label");
64 $("<label/>").text(label).attr("for", $(this).attr("var")).appendTo(clearfix);
66 if ($(x).attr("type") === "result")
67 item.attr("readonly", true);
68 clearfix.append(item);
69 fieldset.append(clearfix);
71 $(self.status.view).append(form);
74 buildHTMLField: function(fld) {
75 var field = $(fld), html = {
76 "hidden" : "<input type='hidden'/>",
77 "boolean" : "<input type='checkbox'/>",
78 "fixed" : "<input type='text' readonly='true'/>",
79 "text-single" : "<input type='text'/>",
80 "text-private": "<input type='password'/>",
81 "text-multi" : "<textarea rows='10' cols='70'/>",
82 "jid-single" : "<input type='text'/>",
83 "jid-multi" : "<textarea rows='10' cols='70'/>",
84 "list-single" : "<select/>",
85 "list-multi" : "<select multiple='multiple'/>",
87 var type = field.attr('type');
88 var input = $(html[type] || "<input/>");
89 var name = field.attr("var");
91 input.addClass("df-item");
93 input.attr("name", name);
94 input.attr("id", name);
97 if (field.find("required").length > 0)
98 input.attr("required", "required");
100 /* Add possible values to the lists */
101 if (type === 'list-multi' || type==='list-single') {
102 field.find("option").each(function() {
103 var option = $("<option/>");
104 option.text($(this).attr("label"));
105 option.val($(this).find("value").text());
106 input.append(option);
110 /* Add/select default values */
111 field.children("value").each(function() {
112 var value = $(this).text();
113 if ((type === "text-multi") || (type === "jid-multi")) {
114 input.text(input.text() + value + "\n"); /* .append() would work, but doesn't escape */
115 } else if (type === "list-multi") {
116 input.children('option[value="' + value + '"]').each(function() {
117 $(this).attr("selected", "selected");
127 serializeToDataform: function (form) {
128 st = $build("x", {"xmlns": "jabber:x:data", "type": "submit"});
129 $(form).find(".df-item").each(function(){
130 st.c("field", {"var": $(this).attr("name")});
131 if (this.nodeName.toLowerCase() === "select" && this.multiple) {
132 for (var i = 0; i < this.options.length; i++)
133 if (this.options[i].selected)
134 st.c("value").t(this.options[i].text).up();
135 } else if (this.nodeName.toLowerCase() === "textarea") {
136 var sp_value = this.value.split(/\r?\n|\r/g);
137 for(var i = 0; i < sp_value.length; i++)
138 st.c("value").t(sp_value[i]).up();
139 } else if (this.nodeName.toLowerCase() === "input" && this.type === "checkbox") {
141 st.c("value").t("1");
143 st.c("value").t("0");
146 /* if this has value then */
147 st.c("value").t($(this).val()).up();
155 displayResult: function (result) {
157 var status = $(result).find("command").attr("status");
158 var kinds = {'prev': 'Prev', 'next': 'Next', 'complete': 'Complete'};
159 var actions = $(result).find("actions:first");
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 var controls = $("<div class='actions'/>");
171 for (kind in kinds) {
174 input = $("<input type='button' disabled='disabled' class='btn' value='" + kinds[type] + "'/>").click(function() {
175 self.executeCommand(type, (type != 'prev') && self.serializeToDataform('form'), function(e) { self.displayResult(e) });
176 }).appendTo(controls);
178 if (actions.find(kind).length > 0)
179 input.removeAttr("disabled");
180 if (actions.attr("execute") == kind)
181 input.addClass("primary");
184 $("<input type='button' class='btn' value='Cancel'/>").click(function() {
185 self.cancelCommand(function(e) { self.displayResult(e) });
186 }).appendTo(controls);
187 $(self.status.view + " fieldset").append(controls);
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);