rev |
line source |
jbe/bsw@0
|
1 --[[--
|
jbe/bsw@0
|
2 ui.autofield{
|
jbe/bsw@0
|
3 name = name, -- field name (also used by default as HTML name)
|
jbe/bsw@0
|
4 html_name = html_name, -- explicit HTML name to be used instead of 'name'
|
jbe/bsw@0
|
5 value = nihil.lift(value), -- initial value, nil causes automatic lookup of value, use nihil.lift(nil) for nil
|
jbe/bsw@0
|
6 container_attr = container_attr, -- extra HTML attributes for the container (div) enclosing field and label
|
jbe/bsw@0
|
7 attr = attr, -- extra HTML attributes for the field
|
jbe/bsw@0
|
8 label = label, -- text to be used as label for the input field
|
jbe/bsw@0
|
9 label_attr = label_attr, -- extra HTML attributes for the label
|
jbe/bsw@0
|
10 readonly = readonly_flag -- set to true, to force read-only mode
|
jbe/bsw@0
|
11 record = record, -- record to be used, defaults to record given to ui.form{...}
|
jbe/bsw@0
|
12 ... -- extra arguments for applicable ui.field.* helpers
|
jbe/bsw@0
|
13 }
|
jbe/bsw@0
|
14
|
jbe/bsw@0
|
15 This function automatically selects a ui.field.* helper to be used for a field of a record.
|
jbe/bsw@0
|
16
|
jbe/bsw@0
|
17 --]]--
|
jbe/bsw@0
|
18
|
jbe/bsw@0
|
19 function ui.autofield(args)
|
jbe/bsw@0
|
20 local args = table.new(args)
|
jbe/bsw@0
|
21 assert(args.name, "ui.autofield{...} needs a field 'name'.")
|
jbe/bsw@0
|
22 if not args.record then
|
jbe/bsw@0
|
23 local slot_state = slot.get_state_table()
|
jbe/bsw@0
|
24 if not slot_state then
|
jbe/bsw@0
|
25 error("ui.autofield{...} was called without an explicit record to be used, and is also not called inside a form.")
|
jbe/bsw@0
|
26 elseif not slot_state.form_record then
|
jbe/bsw@0
|
27 error("ui.autofield{...} was called without an explicit record to be used, and the form does not have a record assigned either.")
|
jbe/bsw@0
|
28 else
|
jbe/bsw@0
|
29 args.record = slot_state.form_record
|
jbe/bsw@0
|
30 end
|
jbe/bsw@0
|
31 end
|
jbe/bsw@0
|
32 local class = args.record._class
|
jbe/bsw@0
|
33 assert(class, "Used ui.autofield{...} on a record with no class information stored in the '_class' attribute.")
|
jbe/bsw@0
|
34 local fields, field_info, ui_field_type, ui_field_options
|
jbe/bsw@0
|
35 fields = class.fields
|
jbe/bsw@0
|
36 if fields then
|
jbe/bsw@0
|
37 field_info = fields[args.name]
|
jbe/bsw@0
|
38 end
|
jbe/bsw@0
|
39 if field_info then
|
jbe/bsw@0
|
40 ui_field_type = field_info.ui_field_type
|
jbe/bsw@0
|
41 ui_field_options = table.new(field_info.ui_field_options)
|
jbe/bsw@0
|
42 end
|
jbe/bsw@0
|
43 if not ui_field_type then
|
jbe/bsw@0
|
44 ui_field_type = "text"
|
jbe/bsw@0
|
45 end
|
jbe/bsw@0
|
46 if not ui_field_options then
|
jbe/bsw@0
|
47 ui_field_options = {}
|
jbe/bsw@0
|
48 end
|
jbe/bsw@0
|
49 local ui_field_func = ui.field[ui_field_type]
|
jbe/bsw@0
|
50 if not ui_field_func then
|
jbe/bsw@0
|
51 error(string.format("Did not find ui.field helper of type %q.", ui_field_type))
|
jbe/bsw@0
|
52 end
|
jbe/bsw@0
|
53 for key, value in pairs(ui_field_options) do
|
jbe/bsw@0
|
54 if args[key] == nil then
|
jbe/bsw@0
|
55 args[key] = value
|
jbe/bsw@0
|
56 end
|
jbe/bsw@0
|
57 end
|
jbe/bsw@0
|
58 return ui_field_func(args)
|
jbe/bsw@0
|
59 end
|