Comparison

util/dataforms.lua @ 845:fc3dced9801e

util.dataforms: First commit, incomplete
author Matthew Wild <mwild1@gmail.com>
date Fri, 27 Feb 2009 17:56:07 +0000
child 851:b48c7ed3f7f8
comparison
equal deleted inserted replaced
844:503ca8da1000 845:fc3dced9801e
1
2 module "dataforms"
3
4 local xmlns_forms = 'jabber:x:data';
5
6 local form_t = {};
7 local form_mt = { __index = form_t };
8
9 function new(layout)
10 return setmetatable(layout, form_mt);
11 end
12
13 local form_x_attr = { xmlns = xmlns_forms };
14
15 function form_t.form(layout, data)
16 local form = st.tag("x", form_x_attr);
17 for n, field in ipairs(layout) do
18 local field_type = field.type;
19 -- Add field tag
20 form:tag("field", { type = field_type, var = field.name });
21
22 local value = data[field.name];
23
24 -- Add value, depending on type
25 if field_type == "hidden" then
26 if type(value) == "table" then
27 -- Assume an XML snippet
28 form:add_child(value);
29 elseif value then
30 form:text(tostring(value));
31 end
32 elseif field_type == "boolean" then
33 form:text((value and "1") or "0");
34 elseif field_type == "fixed" then
35
36 elseif field_type == "jid-multi" then
37 for _, jid in ipairs(value) do
38 form:tag("value"):text(jid):up();
39 end
40 elseif field_type == "jid-single" then
41 form:tag("value"):text(value):up();
42
43 end
44
45 -- Jump back up to list of fields
46 form:up();
47 end
48 end
49
50 function form_t.data(layout, stanza)
51
52 end
53
54
55
56 --[[
57
58 Layout:
59 {
60
61 title = "MUC Configuration",
62 instructions = [[Use this form to configure options for this MUC room.]],
63
64 { name = "FORM_TYPE", type = "hidden", required = true };
65 { name = "field-name", type = "field-type", required = false };
66 }
67
68
69 --]]