webmcp

view framework/env/ui/form_element.lua @ 476:993fbee179ae

Added json sub attribute support for form element
author bsw
date Tue Jun 06 11:47:33 2017 +0200 (2017-06-06)
parents 9fdfb27f8e67
children be5db15e2d9a
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 local path = {}
36 for match in string.gmatch(args.name, "[^%.]+") do
37 path[#path+1] = match
38 end
39 local value = args.record
40 for i, path_element in ipairs(path) do
41 if type(value) == "table" then
42 value = value[path_element]
43 else
44 value = nil
45 end
46 end
47 args.value = value
48 end
49 else
50 args.value = nihil.lower(args.value)
51 end
52 elseif args.fetch_record then
53 if not args.record and slot_state then
54 args.record = slot_state.form_record
55 end
56 end
57 if
58 args.html_name and
59 not args.readonly and
60 slot_state.form_readonly == false
61 then
62 args.readonly = false
63 local prefix
64 if args.html_name_prefix == nil then
65 prefix = slot_state.html_name_prefix
66 else
67 prefix = args.html_name_prefix
68 end
69 if prefix then
70 args.html_name = prefix .. args.html_name
71 end
72 else
73 args.readonly = true
74 end
75 if args.label then
76 if not args.disable_label_for_id then
77 if not args.attr then
78 args.attr = { id = ui.create_unique_id() }
79 elseif not args.attr.id then
80 args.attr.id = ui.create_unique_id()
81 end
82 end
83 if not args.label_attr then
84 args.label_attr = { class = "ui_field_label" }
85 elseif not args.label_attr.class then
86 args.label_attr.class = "ui_field_label"
87 end
88 end
89 ui.container{
90 auto_args = args,
91 content = function() return func(args) end
92 }
93 end

Impressum / About Us