webmcp
view framework/env/ui/form_element.lua @ 409:ebe9416db4e0
Added missing mondelefant_cleanup(conn) call for previous commit in __gc metamethod
author | jbe |
---|---|
date | Thu Jan 07 01:26:11 2016 +0100 (2016-01-07) |
parents | 9fdfb27f8e67 |
children | 993fbee179ae |
line source
1 --[[--
2 ui.form_element(
3 args, -- external arguments
4 { -- options for this function call
5 fetch_value = fetch_value_flag, -- true causes automatic determination of args.value, if nil
6 fetch_record = fetch_record_flag, -- true causes automatic determination of args.record, if nil
7 disable_label_for_id = disable_label_for_id_flag, -- true suppresses automatic setting of args.attr.id for a HTML label_for reference
8 },
9 function(args)
10 ... -- program code
11 end
12 )
14 This function helps other form helpers by preprocessing arguments passed to the helper, e.g. fetching a value from a record stored in a state-table of the currently active slot.
16 --]]--
18 -- TODO: better documentation
20 function ui.form_element(args, extra_args, func)
21 local args = table.new(args)
22 if extra_args then
23 for key, value in pairs(extra_args) do
24 args[key] = value
25 end
26 end
27 local slot_state = slot.get_state_table()
28 args.html_name = args.html_name or args.name
29 if args.fetch_value then
30 if args.value == nil then
31 if not args.record and slot_state then
32 args.record = slot_state.form_record
33 end
34 if args.record then
35 args.value = args.record[args.name]
36 end
37 else
38 args.value = nihil.lower(args.value)
39 end
40 elseif args.fetch_record then
41 if not args.record and slot_state then
42 args.record = slot_state.form_record
43 end
44 end
45 if
46 args.html_name and
47 not args.readonly and
48 slot_state.form_readonly == false
49 then
50 args.readonly = false
51 local prefix
52 if args.html_name_prefix == nil then
53 prefix = slot_state.html_name_prefix
54 else
55 prefix = args.html_name_prefix
56 end
57 if prefix then
58 args.html_name = prefix .. args.html_name
59 end
60 else
61 args.readonly = true
62 end
63 if args.label then
64 if not args.disable_label_for_id then
65 if not args.attr then
66 args.attr = { id = ui.create_unique_id() }
67 elseif not args.attr.id then
68 args.attr.id = ui.create_unique_id()
69 end
70 end
71 if not args.label_attr then
72 args.label_attr = { class = "ui_field_label" }
73 elseif not args.label_attr.class then
74 args.label_attr.class = "ui_field_label"
75 end
76 end
77 ui.container{
78 auto_args = args,
79 content = function() return func(args) end
80 }
81 end