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