// 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 = $("
"); if (this.title) { form.append("

" + this.title + "

"); } if (this.instructions) { form.append("

" + this.instructions + "

"); } if (this.fields.length > 0) { _ref = this.fields; for (_i = 0, _len = _ref.length; _i < _len; _i++) { f = _ref[_i]; (helper.createHtmlFieldCouple(f)).appendTo(form); } } else if (this.items.length > 0) { _ref1 = this.items; for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { i = _ref1[_j]; ($(i.toHTML())).appendTo(form); } } return form[0]; }; Form.fromXML = function(xml) { var f, fields, i, instr, items, j, r, reported, title; xml = $(xml); f = new Form({ type: xml.attr("type") }); title = xml.find("title"); if (title.length === 1) { f.title = title.text(); } instr = xml.find("instructions"); if (instr.length === 1) { f.instructions = instr.text(); } fields = xml.find("field"); items = xml.find("item"); if (items.length > 0) { f.items = (function() { var _i, _len, _results; _results = []; for (_i = 0, _len = items.length; _i < _len; _i++) { i = items[_i]; _results.push(Item.fromXML(i)); } return _results; })(); } else if (fields.length > 0) { f.fields = (function() { var _i, _len, _results; _results = []; for (_i = 0, _len = fields.length; _i < _len; _i++) { j = fields[_i]; _results.push(Field.fromXML(j)); } return _results; })(); } reported = xml.find("reported"); if (reported.length === 1) { fields = reported.find("field"); f.reported = (function() { var _i, _len, _results; _results = []; for (_i = 0, _len = fields.length; _i < _len; _i++) { r = fields[_i]; _results.push(($(r)).attr("var")); } return _results; })(); } return f; }; Form.fromHTML = function(html) { var f, field, fields, i, instructions, item, items, j, title, _i, _j, _len, _len1, _ref, _ref1, _ref2; html = $(html); f = new Form({ type: html.attr("data-type") }); title = html.find("h1").text(); if (title) { f.title = title; } instructions = html.find("p").text(); if (instructions) { f.instructions = instructions; } items = html.find("fieldset"); fields = helper.getHtmlFields(html); if (items.length > 0) { f.items = (function() { var _i, _len, _results; _results = []; for (_i = 0, _len = items.length; _i < _len; _i++) { i = items[_i]; _results.push(Item.fromHTML(i)); } return _results; })(); _ref = f.items; for (_i = 0, _len = _ref.length; _i < _len; _i++) { item = _ref[_i]; _ref1 = item.fields; for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { field = _ref1[_j]; if (!(_ref2 = field["var"], __indexOf.call(f.reported, _ref2) >= 0)) { f.reported.push(field["var"]); } } } } else if (fields.length > 0) { f.fields = (function() { var _k, _len2, _results; _results = []; for (_k = 0, _len2 = fields.length; _k < _len2; _k++) { j = fields[_k]; _results.push(Field.fromHTML(j)); } return _results; })(); } return f; }; return Form; })(); Field = (function() { Field._types = ["boolean", "fixed", "hidden", "jid-multi", "jid-single", "list-multi", "list-single", "text-multi", "text-private", "text-single"]; Field._multiTypes = ["list-multi", "jid-multi", "text-multi", "hidden"]; function Field(opt) { this.toHTML = __bind(this.toHTML, this); this.toXML = __bind(this.toXML, this); this.toJSON = __bind(this.toJSON, this); this.addOptions = __bind(this.addOptions, this); this.addOption = __bind(this.addOption, this); this.addValues = __bind(this.addValues, this); this.addValue = __bind(this.addValue, this); var _ref, _ref1; this.options = []; this.values = []; if (opt) { if (_ref = opt.type, __indexOf.call(Field._types, _ref) >= 0) { this.type = opt.type.toString(); } if (opt.desc) { this.desc = opt.desc.toString(); } if (opt.label) { this.label = opt.label.toString(); } this["var"] = ((_ref1 = opt["var"]) != null ? _ref1.toString() : void 0) || "_no_var_was_defined_"; this.required = opt.required === true || opt.required === "true"; if (opt.options) { this.addOptions(opt.options); } if (opt.value) { opt.values = [opt.value]; } if (opt.values) { this.addValues(opt.values); } } } Field.prototype.type = "text-single"; Field.prototype.desc = null; Field.prototype.label = null; Field.prototype["var"] = "_no_var_was_defined_"; Field.prototype.required = false; Field.prototype.addValue = function(val) { return this.addValues([val]); }; Field.prototype.addValues = function(vals) { var multi, v, _ref; multi = (_ref = this.type, __indexOf.call(Field._multiTypes, _ref) >= 0); if (multi || (!multi && vals.length === 1)) { this.values = __slice.call(this.values).concat(__slice.call((function() { var _i, _len, _results; _results = []; for (_i = 0, _len = vals.length; _i < _len; _i++) { v = vals[_i]; _results.push(v.toString()); } return _results; })())); } return this; }; Field.prototype.addOption = function(opt) { return this.addOptions([opt]); }; Field.prototype.addOptions = function(opts) { var o; if (this.type === "list-single" || this.type === "list-multi") { if (typeof opts[0] !== "object") { opts = (function() { var _i, _len, _results; _results = []; for (_i = 0, _len = opts.length; _i < _len; _i++) { o = opts[_i]; _results.push(new Option({ value: o.toString() })); } return _results; })(); } helper.fill(opts, this.options, Option); } return this; }; Field.prototype.toJSON = function() { var json, o, _i, _len, _ref; json = { type: this.type, "var": this["var"], required: this.required }; if (this.desc) { json.desc = this.desc; } if (this.label) { json.label = this.label; } if (this.values) { json.values = this.values; } if (this.options) { json.options = []; _ref = this.options; for (_i = 0, _len = _ref.length; _i < _len; _i++) { o = _ref[_i]; json.options.push(o.toJSON()); } } return json; }; Field.prototype.toXML = function() { var attrs, o, v, xml, _i, _j, _len, _len1, _ref, _ref1; attrs = { type: this.type, "var": this["var"] }; if (this.label) { attrs.label = this.label; } xml = $build("field", attrs); if (this.desc) { xml.c("desc").t(this.desc).up(); } if (this.required) { xml.c("required").up(); } if (this.values) { _ref = this.values; for (_i = 0, _len = _ref.length; _i < _len; _i++) { v = _ref[_i]; xml.c("value").t(v.toString()).up(); } } if (this.options) { _ref1 = this.options; for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { o = _ref1[_j]; xml.cnode(o.toXML()).up(); } } return xml.tree(); }; Field.prototype.toHTML = function() { var el, k, line, o, opt, txt, val, _i, _j, _len, _len1, _ref, _ref1, _ref2; switch (this.type.toLowerCase()) { case 'list-single': case 'list-multi': el = $("