// Generated by CoffeeScript 1.3.3
var $field, $form, $item, $opt, Field, Form, Item, Option, helper,
__slice = [].slice,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
helper = {
fill: function(src, target, klass) {
var f, _i, _len, _results;
_results = [];
for (_i = 0, _len = src.length; _i < _len; _i++) {
f = src[_i];
_results.push(target.push(f instanceof klass ? f : new klass(f)));
}
return _results;
},
createHtmlFieldCouple: function(f) {
var div, id;
div = $("
");
id = "Strophe.x.Field-" + f.type + "-" + f["var"];
div.append("
").append($(f.toHTML()).attr("id", id)).append("
");
return div.children();
},
getHtmlFields: function(html) {
html = $(html);
return __slice.call(html.find("input")).concat(__slice.call(html.find("select")), __slice.call(html.find("textarea")));
}
};
Form = (function() {
Form._types = ["form", "submit", "cancel", "result"];
function Form(opt) {
this.toHTML = __bind(this.toHTML, this);
this.toJSON = __bind(this.toJSON, this);
this.toXML = __bind(this.toXML, this);
var f, i, _i, _j, _len, _len1, _ref, _ref1, _ref2, _ref3;
this.fields = [];
this.items = [];
this.reported = [];
if (opt) {
if (_ref = opt.type, __indexOf.call(Form._types, _ref) >= 0) {
this.type = opt.type;
}
this.title = opt.title;
this.instructions = opt.instructions;
helper.fill = function(src, target, klass) {
var f, _i, _len, _results;
_results = [];
for (_i = 0, _len = src.length; _i < _len; _i++) {
f = src[_i];
_results.push(target.push(f instanceof klass ? f : new klass(f)));
}
return _results;
};
if (opt.fields) {
if (opt.fields) {
helper.fill(opt.fields, this.fields, Field);
}
} else if (opt.items) {
if (opt.items) {
helper.fill(opt.items, this.items, Item);
}
_ref1 = this.items;
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
i = _ref1[_i];
_ref2 = i.fields;
for (_j = 0, _len1 = _ref2.length; _j < _len1; _j++) {
f = _ref2[_j];
if (!(_ref3 = f["var"], __indexOf.call(this.reported, _ref3) >= 0)) {
this.reported.push(f["var"]);
}
}
}
}
}
}
Form.prototype.type = "form";
Form.prototype.title = null;
Form.prototype.instructions = null;
Form.prototype.toXML = function() {
var f, i, r, xml, _i, _j, _k, _len, _len1, _len2, _ref, _ref1, _ref2;
xml = $build("x", {
xmlns: "jabber:x:data",
type: this.type
});
if (this.title) {
xml.c("title").t(this.title.toString()).up();
}
if (this.instructions) {
xml.c("instructions").t(this.instructions.toString()).up();
}
if (this.fields.length > 0) {
_ref = this.fields;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
f = _ref[_i];
xml.cnode(f.toXML()).up();
}
} else if (this.items.length > 0) {
xml.c("reported");
_ref1 = this.reported;
for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
r = _ref1[_j];
xml.c("field", {
"var": r
}).up();
}
xml.up();
_ref2 = this.items;
for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) {
i = _ref2[_k];
xml.cnode(i.toXML()).up();
}
}
return xml.tree();
};
Form.prototype.toJSON = function() {
var f, i, json, _i, _j, _len, _len1, _ref, _ref1;
json = {
type: this.type
};
if (this.title) {
json.title = this.title;
}
if (this.instructions) {
json.instructions = this.instructions;
}
if (this.fields.length > 0) {
json.fields = [];
_ref = this.fields;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
f = _ref[_i];
json.fields.push(f.toJSON());
}
} else if (this.items.length > 0) {
json.items = [];
json.reported = this.reported;
_ref1 = this.items;
for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
i = _ref1[_j];
json.items.push(i.toJSON());
}
}
return json;
};
Form.prototype.toHTML = function() {
var f, form, i, _i, _j, _len, _len1, _ref, _ref1;
form = $("