webmcp
annotate framework/env/ui/form.lua @ 1:985024b16520
Version 1.0.1
New feature: JSON requests
Changes in ui.paginate: Current page setting is directly fetched from CGI params, instead of view params
Changed behavior of load methods of atom library to accept nil as input
Bugfixes in mondelefant_atom_connector timestamp(tz) loaders
Added global constant _WEBMCP_VERSION containing a version string
New feature: JSON requests
Changes in ui.paginate: Current page setting is directly fetched from CGI params, instead of view params
Changed behavior of load methods of atom library to accept nil as input
Bugfixes in mondelefant_atom_connector timestamp(tz) loaders
Added global constant _WEBMCP_VERSION containing a version string
author | jbe |
---|---|
date | Tue Nov 17 12:00:00 2009 +0100 (2009-11-17) |
parents | 9fdfb27f8e67 |
children | d76a8857ba62 |
rev | line source |
---|---|
jbe/bsw@0 | 1 --[[-- |
jbe/bsw@0 | 2 ui.form{ |
jbe/bsw@0 | 3 record = record, -- optional record to be used |
jbe/bsw@0 | 4 read_only = read_only, -- set to true, if form should be read-only (no submit button) |
jbe/bsw@0 | 5 external = external, -- external URL to be used as HTML form action |
jbe/bsw@0 | 6 module = module, -- module name to be used for HTML form action |
jbe/bsw@0 | 7 view = view, -- view name to be used for HTML form action |
jbe/bsw@0 | 8 action = action, -- action name to be used for HTML form action |
jbe/bsw@0 | 9 routing = { |
jbe/bsw@0 | 10 default = { -- default routing for called action |
jbe/bsw@0 | 11 mode = mode, -- "forward" or "redirect" |
jbe/bsw@0 | 12 module = module, -- optional module name, defaults to current module |
jbe/bsw@0 | 13 view = view, -- view name |
jbe/bsw@0 | 14 id = id, -- optional id to be passed to the view |
jbe/bsw@0 | 15 params = params -- optional params to be passed to the view |
jbe/bsw@0 | 16 }, |
jbe/bsw@0 | 17 ok = { ... }, -- routing when "ok" is returned by the called action |
jbe/bsw@0 | 18 error = { ... }, -- routing when "error" is returned by the called action |
jbe/bsw@0 | 19 ... = { ... } -- routing when "..." is returned by the called action |
jbe/bsw@0 | 20 } |
jbe/bsw@0 | 21 content = function() |
jbe/bsw@0 | 22 ... -- code creating the contents of the form |
jbe/bsw@0 | 23 end |
jbe/bsw@0 | 24 } |
jbe/bsw@0 | 25 |
jbe/bsw@0 | 26 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. |
jbe/bsw@0 | 27 |
jbe/bsw@0 | 28 --]]-- |
jbe/bsw@0 | 29 |
jbe/bsw@0 | 30 function ui.form(args) |
jbe/bsw@0 | 31 local args = args or {} |
jbe/bsw@0 | 32 local slot_state = slot.get_state_table() |
jbe/bsw@0 | 33 local old_record = slot_state.form_record |
jbe/bsw@0 | 34 local old_readonly = slot_state.form_readonly |
jbe/bsw@0 | 35 slot_state.form_record = args.record |
jbe/bsw@0 | 36 if args.readonly then |
jbe/bsw@0 | 37 slot_state.form_readonly = true |
jbe/bsw@0 | 38 ui.container{ attr = args.attr, content = args.content } |
jbe/bsw@0 | 39 else |
jbe/bsw@0 | 40 slot_state.form_readonly = false |
jbe/bsw@0 | 41 local params = table.new(args.params) |
jbe/bsw@0 | 42 local routing_default_given = false |
jbe/bsw@0 | 43 if args.routing then |
jbe/bsw@0 | 44 for status, settings in pairs(args.routing) do |
jbe/bsw@0 | 45 if status == "default" then |
jbe/bsw@0 | 46 routing_default_given = true |
jbe/bsw@0 | 47 end |
jbe/bsw@0 | 48 local module = settings.module or args.module or request.get_module() |
jbe/bsw@0 | 49 assert(settings.mode, "No mode specified in routing entry.") |
jbe/bsw@0 | 50 assert(settings.view, "No view specified in routing entry.") |
jbe/bsw@0 | 51 params["_webmcp_routing." .. status .. ".mode"] = settings.mode |
jbe/bsw@0 | 52 params["_webmcp_routing." .. status .. ".module"] = module |
jbe/bsw@0 | 53 params["_webmcp_routing." .. status .. ".view"] = settings.view |
jbe/bsw@0 | 54 params["_webmcp_routing." .. status .. ".id"] = settings.id |
jbe/bsw@0 | 55 if settings.params then |
jbe/bsw@0 | 56 for key, value in pairs(settings.params) do |
jbe/bsw@0 | 57 params["_webmcp_routing." .. status .. ".params." .. key] = value |
jbe/bsw@0 | 58 end |
jbe/bsw@0 | 59 end |
jbe/bsw@0 | 60 end |
jbe/bsw@0 | 61 end |
jbe/bsw@0 | 62 if not routing_default_given then |
jbe/bsw@0 | 63 params["_webmcp_routing.default.mode"] = "forward" |
jbe/bsw@0 | 64 params["_webmcp_routing.default.module"] = request.get_module() |
jbe/bsw@0 | 65 params["_webmcp_routing.default.view"] = request.get_view() |
jbe/bsw@0 | 66 end |
jbe/bsw@0 | 67 params._webmcp_csrf_secret = request.get_csrf_secret() |
jbe/bsw@0 | 68 local attr = table.new(args.attr) |
jbe/bsw@0 | 69 attr.action = encode.url{ |
jbe/bsw@0 | 70 external = args.external, |
jbe/bsw@0 | 71 module = args.module or request.get_module(), |
jbe/bsw@0 | 72 view = args.view, |
jbe/bsw@0 | 73 action = args.action, |
jbe/bsw@0 | 74 } |
jbe/bsw@0 | 75 attr.method = args.method and string.upper(args.method) or "POST" |
jbe/bsw@0 | 76 if slot_state.form_opened then |
jbe/bsw@0 | 77 error("Cannot open a non-readonly form inside a non-readonly form.") |
jbe/bsw@0 | 78 end |
jbe/bsw@0 | 79 slot_state.form_opened = true |
jbe/bsw@0 | 80 ui.tag { |
jbe/bsw@0 | 81 tag = "form", |
jbe/bsw@0 | 82 attr = attr, |
jbe/bsw@0 | 83 content = function() |
jbe/bsw@0 | 84 if args.id then |
jbe/bsw@0 | 85 ui.hidden_field{ name = "_webmcp_id", value = args.id } |
jbe/bsw@0 | 86 end |
jbe/bsw@0 | 87 for key, value in pairs(params) do |
jbe/bsw@0 | 88 ui.hidden_field{ name = key, value = value } |
jbe/bsw@0 | 89 end |
jbe/bsw@0 | 90 if args.content then |
jbe/bsw@0 | 91 args.content() |
jbe/bsw@0 | 92 end |
jbe/bsw@0 | 93 end |
jbe/bsw@0 | 94 } |
jbe/bsw@0 | 95 slot_state.form_opened = false |
jbe/bsw@0 | 96 end |
jbe/bsw@0 | 97 slot_state.form_readonly = old_readonly |
jbe/bsw@0 | 98 slot_state.form_record = old_record |
jbe/bsw@0 | 99 end |