webmcp

diff framework/env/ui/autofield.lua @ 0:9fdfb27f8e67

Version 1.0.0
author jbe/bsw
date Sun Oct 25 12:00:00 2009 +0100 (2009-10-25)
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/framework/env/ui/autofield.lua	Sun Oct 25 12:00:00 2009 +0100
     1.3 @@ -0,0 +1,59 @@
     1.4 +--[[--
     1.5 +ui.autofield{
     1.6 +  name           = name,               -- field name (also used by default as HTML name)
     1.7 +  html_name      = html_name,          -- explicit HTML name to be used instead of 'name'
     1.8 +  value          = nihil.lift(value),  -- initial value, nil causes automatic lookup of value, use nihil.lift(nil) for nil
     1.9 +  container_attr = container_attr,     -- extra HTML attributes for the container (div) enclosing field and label
    1.10 +  attr           = attr,               -- extra HTML attributes for the field
    1.11 +  label          = label,              -- text to be used as label for the input field
    1.12 +  label_attr     = label_attr,         -- extra HTML attributes for the label
    1.13 +  readonly       = readonly_flag       -- set to true, to force read-only mode
    1.14 +  record         = record,             -- record to be used, defaults to record given to ui.form{...}
    1.15 +  ...                                  -- extra arguments for applicable ui.field.* helpers
    1.16 +}
    1.17 +
    1.18 +This function automatically selects a ui.field.* helper to be used for a field of a record.
    1.19 +
    1.20 +--]]--
    1.21 +
    1.22 +function ui.autofield(args)
    1.23 +  local args = table.new(args)
    1.24 +  assert(args.name, "ui.autofield{...} needs a field 'name'.")
    1.25 +  if not args.record then
    1.26 +    local slot_state = slot.get_state_table()
    1.27 +    if not slot_state then
    1.28 +      error("ui.autofield{...} was called without an explicit record to be used, and is also not called inside a form.")
    1.29 +    elseif not slot_state.form_record then
    1.30 +      error("ui.autofield{...} was called without an explicit record to be used, and the form does not have a record assigned either.")
    1.31 +    else
    1.32 +      args.record = slot_state.form_record
    1.33 +    end
    1.34 +  end
    1.35 +  local class = args.record._class
    1.36 +  assert(class, "Used ui.autofield{...} on a record with no class information stored in the '_class' attribute.")
    1.37 +  local fields, field_info, ui_field_type, ui_field_options
    1.38 +  fields = class.fields
    1.39 +  if fields then
    1.40 +    field_info = fields[args.name]
    1.41 +  end
    1.42 +  if field_info then
    1.43 +    ui_field_type    = field_info.ui_field_type
    1.44 +    ui_field_options = table.new(field_info.ui_field_options)
    1.45 +  end
    1.46 +  if not ui_field_type then
    1.47 +    ui_field_type = "text"
    1.48 +  end
    1.49 +  if not ui_field_options then
    1.50 +    ui_field_options = {}
    1.51 +  end
    1.52 +  local ui_field_func = ui.field[ui_field_type]
    1.53 +  if not ui_field_func then
    1.54 +    error(string.format("Did not find ui.field helper of type %q.", ui_field_type))
    1.55 +  end
    1.56 +  for key, value in pairs(ui_field_options) do
    1.57 +    if args[key] == nil then
    1.58 +      args[key] = value
    1.59 +    end
    1.60 +  end
    1.61 +  return ui_field_func(args)
    1.62 +end

Impressum / About Us