webmcp
view framework/env/ui_deprecated/input.lua @ 205:48ee8826efbe
Correct handling of nil's in return tuples in execute.chunk{...}
| author | jbe | 
|---|---|
| date | Fri Jan 09 21:32:23 2015 +0100 (2015-01-09) | 
| parents | 9fdfb27f8e67 | 
| children | 
 line source
     1 --
     2 -- Creates an input field in a form
     3 --
     4 -- label      (string) The label of the input field
     5 -- field      (string) The name of the record field
     6 -- field_type (string) The type of the record field
     7 --
     8 -- Example:
     9 --
    10 --  ui_deprecated.input({
    11 --    label = _'Comment',
    12 --    field = 'comment', 
    13 --    field_type = 'textarea'
    14 --  })
    15 --
    16 local field_type_to_atom_class_map = {
    17   text       = atom.string,
    18   textarea   = atom.string,
    19   number     = atom.number,
    20   percentage = atom.number,
    21 }
    23 function ui_deprecated.input(args)
    24   local record = assert(slot.get_state_table(), "ui_deprecated.input was not called within a form.").form_record
    26   local field_type = args.field_type or "text"
    28   local field_func = assert(ui_deprecated.input_field[field_type], "no field helper for given type '" .. field_type .. "'")
    30   local html_name = args.name or args.field
    31   local field_html
    33   if args.field then
    34     local param_type = field_type_to_atom_class_map[field_type] or error('Unkown field type')
    35     field_html = field_func{
    36       name  = html_name, 
    37       value = param.get(html_name, param_type) 
    38               or record[args.field],
    39       height = args.height,
    40     }
    42   elseif args.value then
    43     field_html = field_func{
    44       name  = html_name, 
    45       value = args.value,
    46       height = args.height,
    47     }
    49   else
    50     field_html = field_func{
    51       name  = html_name, 
    52       value = '',
    53       height = args.height,
    54     }
    56   end
    58   slot.put('<div class="ui_field ui_input_', field_type, '">\n')
    59   if args.label then
    60     slot.put('<div class="label">', encode.html(args.label), '</div>\n')
    61   end
    62   slot.put('<div class="value">',
    63         field_html,
    64       '</div>\n',
    65     '</div>\n'
    66   )
    67 end
