2 // Copyright (c) 2010-2013 Florian Zeitz
4 // This project is MIT licensed.
5 // Please see the COPYING file for more information.
8 * Implementation of ECMA Script 5 like bind from:
9 * https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind
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");
17 var fSlice = Array.prototype.slice,
18 aArgs = fSlice.call(arguments, 1),
20 fNOP = function () {},
21 fBound = function () {
22 return fToBind.apply(this instanceof fNOP ? this : oThis || window, Args.concat(fSlice.call(arguments)));
24 fNOP.prototype = this.prototype;
25 fBound.prototype = new fNOP();
30 Strophe.addNamespace("ADHOC", "http://jabber.org/protocol/commands");
32 function Adhoc(view, readycb) {
45 addNote: function (text, type) {
49 text = text.replace(/\n/g, "<br/>");
50 $(this.status.view).append("<p class='" + type + "Note'>" + text + "</p>");
53 addForm: function (x) {
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();
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");
70 $("<label/>").text(label).attr("for", $(this).attr("var")).appendTo(clearfix);
72 if ($(x).attr("type") === "result")
73 item.attr("readonly", true);
74 clearfix.append(item);
75 fieldset.append(clearfix);
77 $(self.status.view).append(form);
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'/>",
93 var type = field.attr('type');
94 var input = $(html[type] || "<input/>");
95 var name = field.attr("var");
97 input.addClass("df-item");
99 input.attr("name", name);
100 input.attr("id", name);
103 if (field.find("required").length > 0)
104 input.attr("required", "required");
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);
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");
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") {
147 st.c("value").t("1");
149 st.c("value").t("0");
152 /* if this has value then */
153 st.c("value").t($(this).val()).up();
161 displayResult: function (result) {
163 var status = $(result).find("command").attr("status");
164 var kinds = {'prev': 'Prev', 'next': 'Next', 'complete': 'Complete'};
165 var actions = $(result).find("actions:first");
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]")) {
175 if (status === "executing") {
176 var controls = $("<div class='actions'/>");
177 for (kind in kinds) {
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);
184 if (actions.find(kind).length > 0)
185 input.removeAttr("disabled");
186 if (actions.attr("execute") == kind)
187 input.addClass("primary");
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);
195 self.status.sessionid = null;
196 self.status.cmdNode = null;
197 self.status.readycb();
201 runCommand: function (item, callback) {
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");
208 this.executeCommand("execute", false, cb.bind(this));
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 });
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 });
219 execIQ.cnode(childs);
220 connection.sendIQ(execIQ, callback);
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;
230 getCommandNodes: function (callback) {
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();
246 checkFeatures: function (jid, cb, ecb) {
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();
254 function callback(result) {
255 if ($(result).find("feature[var='" + Strophe.NS.ADHOC + "']").length > 0) {
262 connection.sendIQ(featureIQ, callback, ecb);