webmcp
annotate framework/env/ui/form.lua @ 11:d76a8857ba62
Added ui.partial and other functions, which allow partial content replacement using XMLHttpRequests; Image support for ui.link
Also includes following changes:
- Fix for rocketcgi library to accept POST data content-types, which contain additional charset information.
- Support arrays passed as params to encode.url (only for keys ending with "[]")
- Version information changed to "1.0.7"
Documentation for added functions is not yet complete.
Also includes following changes:
- Fix for rocketcgi library to accept POST data content-types, which contain additional charset information.
- Support arrays passed as params to encode.url (only for keys ending with "[]")
- Version information changed to "1.0.7"
Documentation for added functions is not yet complete.
author | jbe/bsw |
---|---|
date | Fri Feb 12 18:40:22 2010 +0100 (2010-02-12) |
parents | 9fdfb27f8e67 |
children | f3d3203cd2e4 |
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@11 | 30 local function prepare_routing_params(params, routing, default_module) |
jbe/bsw@11 | 31 local routing_default_given = false |
jbe/bsw@11 | 32 if routing then |
jbe/bsw@11 | 33 for status, settings in pairs(routing) do |
jbe/bsw@11 | 34 if status == "default" then |
jbe/bsw@11 | 35 routing_default_given = true |
jbe/bsw@11 | 36 end |
jbe/bsw@11 | 37 local module = settings.module or default_module or request.get_module() |
jbe/bsw@11 | 38 assert(settings.mode, "No mode specified in routing entry.") |
jbe/bsw@11 | 39 assert(settings.view, "No view specified in routing entry.") |
jbe/bsw@11 | 40 params["_webmcp_routing." .. status .. ".mode"] = settings.mode |
jbe/bsw@11 | 41 params["_webmcp_routing." .. status .. ".module"] = module |
jbe/bsw@11 | 42 params["_webmcp_routing." .. status .. ".view"] = settings.view |
jbe/bsw@11 | 43 params["_webmcp_routing." .. status .. ".id"] = settings.id |
jbe/bsw@11 | 44 if settings.params then |
jbe/bsw@11 | 45 for key, value in pairs(settings.params) do |
jbe/bsw@11 | 46 params["_webmcp_routing." .. status .. ".params." .. key] = value |
jbe/bsw@11 | 47 end |
jbe/bsw@11 | 48 end |
jbe/bsw@11 | 49 end |
jbe/bsw@11 | 50 end |
jbe/bsw@11 | 51 if not routing_default_given then |
jbe/bsw@11 | 52 params["_webmcp_routing.default.mode"] = "forward" |
jbe/bsw@11 | 53 params["_webmcp_routing.default.module"] = request.get_module() |
jbe/bsw@11 | 54 params["_webmcp_routing.default.view"] = request.get_view() |
jbe/bsw@11 | 55 end |
jbe/bsw@11 | 56 return params |
jbe/bsw@11 | 57 end |
jbe/bsw@11 | 58 |
jbe/bsw@0 | 59 function ui.form(args) |
jbe/bsw@0 | 60 local args = args or {} |
jbe/bsw@0 | 61 local slot_state = slot.get_state_table() |
jbe/bsw@0 | 62 local old_record = slot_state.form_record |
jbe/bsw@0 | 63 local old_readonly = slot_state.form_readonly |
jbe/bsw@0 | 64 slot_state.form_record = args.record |
jbe/bsw@0 | 65 if args.readonly then |
jbe/bsw@0 | 66 slot_state.form_readonly = true |
jbe/bsw@0 | 67 ui.container{ attr = args.attr, content = args.content } |
jbe/bsw@0 | 68 else |
jbe/bsw@0 | 69 slot_state.form_readonly = false |
jbe/bsw@0 | 70 local params = table.new(args.params) |
jbe/bsw@11 | 71 prepare_routing_params(params, args.routing, args.module) |
jbe/bsw@0 | 72 params._webmcp_csrf_secret = request.get_csrf_secret() |
jbe/bsw@0 | 73 local attr = table.new(args.attr) |
jbe/bsw@0 | 74 attr.action = encode.url{ |
jbe/bsw@0 | 75 external = args.external, |
jbe/bsw@0 | 76 module = args.module or request.get_module(), |
jbe/bsw@0 | 77 view = args.view, |
jbe/bsw@0 | 78 action = args.action, |
jbe/bsw@0 | 79 } |
jbe/bsw@0 | 80 attr.method = args.method and string.upper(args.method) or "POST" |
jbe/bsw@11 | 81 if ui.is_partial_loading_enabled() and args.partial then |
jbe/bsw@11 | 82 attr.onsubmit = slot.use_temporary(function() |
jbe/bsw@11 | 83 local partial_mode = "form_normal" |
jbe/bsw@11 | 84 if args.action then |
jbe/bsw@11 | 85 partial_mode = "form_action" |
jbe/bsw@11 | 86 slot.put( |
jbe/bsw@11 | 87 'var element; ', |
jbe/bsw@11 | 88 'var formElements = []; ', |
jbe/bsw@11 | 89 'for (var i=0; i<this.elements.length; i++) { ', |
jbe/bsw@11 | 90 'formElements[formElements.length] = this.elements[i]; ', |
jbe/bsw@11 | 91 '} ', |
jbe/bsw@11 | 92 'for (i=0; i<formElements.length; i++) { ', |
jbe/bsw@11 | 93 'element = formElements[i]; ', |
jbe/bsw@11 | 94 'if (element.name.search(/^_webmcp_routing\\./) >= 0) { ', |
jbe/bsw@11 | 95 'element.parentNode.removeChild(element); ', |
jbe/bsw@11 | 96 '} ', |
jbe/bsw@11 | 97 '}' |
jbe/bsw@11 | 98 ) |
jbe/bsw@11 | 99 local routing_params = {} |
jbe/bsw@11 | 100 prepare_routing_params( |
jbe/bsw@11 | 101 routing_params, |
jbe/bsw@11 | 102 args.partial.routing, |
jbe/bsw@11 | 103 args.partial.module |
jbe/bsw@11 | 104 ) |
jbe/bsw@11 | 105 for key, value in pairs(routing_params) do |
jbe/bsw@11 | 106 slot.put( |
jbe/bsw@11 | 107 ' ', |
jbe/bsw@11 | 108 'element = document.createElement("input"); ', |
jbe/bsw@11 | 109 'element.setAttribute("type", "hidden"); ', |
jbe/bsw@11 | 110 'element.setAttribute("name", ', encode.json(key), '); ', |
jbe/bsw@11 | 111 'element.setAttribute("value", ', encode.json(value), '); ', |
jbe/bsw@11 | 112 'this.appendChild(element);' |
jbe/bsw@11 | 113 ) |
jbe/bsw@11 | 114 end |
jbe/bsw@11 | 115 slot.put(' ') |
jbe/bsw@11 | 116 end |
jbe/bsw@11 | 117 slot.put(ui._partial_load_js(args.partial, partial_mode)) |
jbe/bsw@11 | 118 end) |
jbe/bsw@11 | 119 end |
jbe/bsw@0 | 120 if slot_state.form_opened then |
jbe/bsw@0 | 121 error("Cannot open a non-readonly form inside a non-readonly form.") |
jbe/bsw@0 | 122 end |
jbe/bsw@0 | 123 slot_state.form_opened = true |
jbe/bsw@0 | 124 ui.tag { |
jbe/bsw@0 | 125 tag = "form", |
jbe/bsw@0 | 126 attr = attr, |
jbe/bsw@0 | 127 content = function() |
jbe/bsw@0 | 128 if args.id then |
jbe/bsw@0 | 129 ui.hidden_field{ name = "_webmcp_id", value = args.id } |
jbe/bsw@0 | 130 end |
jbe/bsw@0 | 131 for key, value in pairs(params) do |
jbe/bsw@0 | 132 ui.hidden_field{ name = key, value = value } |
jbe/bsw@0 | 133 end |
jbe/bsw@0 | 134 if args.content then |
jbe/bsw@0 | 135 args.content() |
jbe/bsw@0 | 136 end |
jbe/bsw@0 | 137 end |
jbe/bsw@0 | 138 } |
jbe/bsw@0 | 139 slot_state.form_opened = false |
jbe/bsw@0 | 140 end |
jbe/bsw@0 | 141 slot_state.form_readonly = old_readonly |
jbe/bsw@0 | 142 slot_state.form_record = old_record |
jbe/bsw@0 | 143 end |