webmcp
diff framework/env/ui/form_element.lua @ 0:9fdfb27f8e67
Version 1.0.0
author | jbe/bsw |
---|---|
date | Sun Oct 25 12:00:00 2009 +0100 (2009-10-25) |
parents | |
children | 993fbee179ae |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/framework/env/ui/form_element.lua Sun Oct 25 12:00:00 2009 +0100 1.3 @@ -0,0 +1,81 @@ 1.4 +--[[-- 1.5 +ui.form_element( 1.6 + args, -- external arguments 1.7 + { -- options for this function call 1.8 + fetch_value = fetch_value_flag, -- true causes automatic determination of args.value, if nil 1.9 + fetch_record = fetch_record_flag, -- true causes automatic determination of args.record, if nil 1.10 + disable_label_for_id = disable_label_for_id_flag, -- true suppresses automatic setting of args.attr.id for a HTML label_for reference 1.11 + }, 1.12 + function(args) 1.13 + ... -- program code 1.14 + end 1.15 +) 1.16 + 1.17 +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. 1.18 + 1.19 +--]]-- 1.20 + 1.21 +-- TODO: better documentation 1.22 + 1.23 +function ui.form_element(args, extra_args, func) 1.24 + local args = table.new(args) 1.25 + if extra_args then 1.26 + for key, value in pairs(extra_args) do 1.27 + args[key] = value 1.28 + end 1.29 + end 1.30 + local slot_state = slot.get_state_table() 1.31 + args.html_name = args.html_name or args.name 1.32 + if args.fetch_value then 1.33 + if args.value == nil then 1.34 + if not args.record and slot_state then 1.35 + args.record = slot_state.form_record 1.36 + end 1.37 + if args.record then 1.38 + args.value = args.record[args.name] 1.39 + end 1.40 + else 1.41 + args.value = nihil.lower(args.value) 1.42 + end 1.43 + elseif args.fetch_record then 1.44 + if not args.record and slot_state then 1.45 + args.record = slot_state.form_record 1.46 + end 1.47 + end 1.48 + if 1.49 + args.html_name and 1.50 + not args.readonly and 1.51 + slot_state.form_readonly == false 1.52 + then 1.53 + args.readonly = false 1.54 + local prefix 1.55 + if args.html_name_prefix == nil then 1.56 + prefix = slot_state.html_name_prefix 1.57 + else 1.58 + prefix = args.html_name_prefix 1.59 + end 1.60 + if prefix then 1.61 + args.html_name = prefix .. args.html_name 1.62 + end 1.63 + else 1.64 + args.readonly = true 1.65 + end 1.66 + if args.label then 1.67 + if not args.disable_label_for_id then 1.68 + if not args.attr then 1.69 + args.attr = { id = ui.create_unique_id() } 1.70 + elseif not args.attr.id then 1.71 + args.attr.id = ui.create_unique_id() 1.72 + end 1.73 + end 1.74 + if not args.label_attr then 1.75 + args.label_attr = { class = "ui_field_label" } 1.76 + elseif not args.label_attr.class then 1.77 + args.label_attr.class = "ui_field_label" 1.78 + end 1.79 + end 1.80 + ui.container{ 1.81 + auto_args = args, 1.82 + content = function() return func(args) end 1.83 + } 1.84 +end