webmcp

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

Version 1.0.0
author jbe/bsw
date Sun Oct 25 12:00:00 2009 +0100 (2009-10-25)
parents
children d76a8857ba62
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/framework/env/ui/form.lua	Sun Oct 25 12:00:00 2009 +0100
     1.3 @@ -0,0 +1,99 @@
     1.4 +--[[--
     1.5 +ui.form{
     1.6 +  record    = record,     -- optional record to be used 
     1.7 +  read_only = read_only,  -- set to true, if form should be read-only (no submit button)
     1.8 +  external  = external,   -- external URL to be used as HTML form action
     1.9 +  module    = module,     -- module name to be used for HTML form action
    1.10 +  view      = view,       -- view name   to be used for HTML form action
    1.11 +  action    = action,     -- action name to be used for HTML form action
    1.12 +  routing = {
    1.13 +    default = {           -- default routing for called action
    1.14 +      mode   = mode,      -- "forward" or "redirect"
    1.15 +      module = module,    -- optional module name, defaults to current module
    1.16 +      view   = view,      -- view name
    1.17 +      id     = id,        -- optional id to be passed to the view
    1.18 +      params = params     -- optional params to be passed to the view
    1.19 +    },
    1.20 +    ok    = { ... },      -- routing when "ok"    is returned by the called action
    1.21 +    error = { ... },      -- routing when "error" is returned by the called action
    1.22 +    ...   = { ... }       -- routing when "..."   is returned by the called action
    1.23 +  }
    1.24 +  content = function()
    1.25 +    ...                   -- code creating the contents of the form
    1.26 +  end
    1.27 +}
    1.28 +
    1.29 +This functions creates a web form, which encloses the content created by the given 'content' function. When a 'record' is given, ui.field.* helper functions will be able to automatically determine field values by using the given record. If 'read_only' is set to true, then a call of ui.submit{...} will be ignored, and ui.field.* helper functions will behave differently.
    1.30 +
    1.31 +--]]--
    1.32 +
    1.33 +function ui.form(args)
    1.34 +  local args = args or {}
    1.35 +  local slot_state = slot.get_state_table()
    1.36 +  local old_record   = slot_state.form_record
    1.37 +  local old_readonly = slot_state.form_readonly
    1.38 +  slot_state.form_record = args.record
    1.39 +  if args.readonly then
    1.40 +    slot_state.form_readonly = true
    1.41 +    ui.container{ attr = args.attr, content = args.content }
    1.42 +  else
    1.43 +    slot_state.form_readonly = false
    1.44 +    local params = table.new(args.params)
    1.45 +    local routing_default_given = false
    1.46 +    if args.routing then
    1.47 +      for status, settings in pairs(args.routing) do
    1.48 +        if status == "default" then
    1.49 +          routing_default_given = true
    1.50 +        end
    1.51 +        local module = settings.module or args.module or request.get_module()
    1.52 +        assert(settings.mode, "No mode specified in routing entry.")
    1.53 +        assert(settings.view, "No view specified in routing entry.")
    1.54 +        params["_webmcp_routing." .. status .. ".mode"]   = settings.mode
    1.55 +        params["_webmcp_routing." .. status .. ".module"] = module
    1.56 +        params["_webmcp_routing." .. status .. ".view"]   = settings.view
    1.57 +        params["_webmcp_routing." .. status .. ".id"]     = settings.id
    1.58 +        if settings.params then
    1.59 +          for key, value in pairs(settings.params) do
    1.60 +            params["_webmcp_routing." .. status .. ".params." .. key] = value
    1.61 +          end
    1.62 +        end
    1.63 +      end
    1.64 +    end
    1.65 +    if not routing_default_given then
    1.66 +      params["_webmcp_routing.default.mode"]   = "forward"
    1.67 +      params["_webmcp_routing.default.module"] = request.get_module()
    1.68 +      params["_webmcp_routing.default.view"]   = request.get_view()
    1.69 +    end
    1.70 +    params._webmcp_csrf_secret = request.get_csrf_secret()
    1.71 +    local attr = table.new(args.attr)
    1.72 +    attr.action = encode.url{
    1.73 +      external  = args.external,
    1.74 +      module    = args.module or request.get_module(),
    1.75 +      view      = args.view,
    1.76 +      action    = args.action,
    1.77 +    }
    1.78 +    attr.method = args.method and string.upper(args.method) or "POST"
    1.79 +    if slot_state.form_opened then
    1.80 +      error("Cannot open a non-readonly form inside a non-readonly form.")
    1.81 +    end
    1.82 +    slot_state.form_opened = true
    1.83 +    ui.tag {
    1.84 +      tag     = "form",
    1.85 +      attr    = attr,
    1.86 +      content = function()
    1.87 +        if args.id then
    1.88 +          ui.hidden_field{ name = "_webmcp_id", value = args.id }
    1.89 +        end
    1.90 +        for key, value in pairs(params) do
    1.91 +          ui.hidden_field{ name = key, value = value }
    1.92 +        end
    1.93 +        if args.content then
    1.94 +          args.content()
    1.95 +        end
    1.96 +      end
    1.97 +    }
    1.98 +    slot_state.form_opened = false
    1.99 +  end
   1.100 +  slot_state.form_readonly = old_readonly
   1.101 +  slot_state.form_record   = old_record
   1.102 +end

Impressum / About Us