webmcp
diff 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 |
line diff
1.1 --- a/framework/env/ui/form.lua Wed Feb 03 00:57:18 2010 +0100 1.2 +++ b/framework/env/ui/form.lua Fri Feb 12 18:40:22 2010 +0100 1.3 @@ -27,6 +27,35 @@ 1.4 1.5 --]]-- 1.6 1.7 +local function prepare_routing_params(params, routing, default_module) 1.8 + local routing_default_given = false 1.9 + if routing then 1.10 + for status, settings in pairs(routing) do 1.11 + if status == "default" then 1.12 + routing_default_given = true 1.13 + end 1.14 + local module = settings.module or default_module or request.get_module() 1.15 + assert(settings.mode, "No mode specified in routing entry.") 1.16 + assert(settings.view, "No view specified in routing entry.") 1.17 + params["_webmcp_routing." .. status .. ".mode"] = settings.mode 1.18 + params["_webmcp_routing." .. status .. ".module"] = module 1.19 + params["_webmcp_routing." .. status .. ".view"] = settings.view 1.20 + params["_webmcp_routing." .. status .. ".id"] = settings.id 1.21 + if settings.params then 1.22 + for key, value in pairs(settings.params) do 1.23 + params["_webmcp_routing." .. status .. ".params." .. key] = value 1.24 + end 1.25 + end 1.26 + end 1.27 + end 1.28 + if not routing_default_given then 1.29 + params["_webmcp_routing.default.mode"] = "forward" 1.30 + params["_webmcp_routing.default.module"] = request.get_module() 1.31 + params["_webmcp_routing.default.view"] = request.get_view() 1.32 + end 1.33 + return params 1.34 +end 1.35 + 1.36 function ui.form(args) 1.37 local args = args or {} 1.38 local slot_state = slot.get_state_table() 1.39 @@ -39,31 +68,7 @@ 1.40 else 1.41 slot_state.form_readonly = false 1.42 local params = table.new(args.params) 1.43 - local routing_default_given = false 1.44 - if args.routing then 1.45 - for status, settings in pairs(args.routing) do 1.46 - if status == "default" then 1.47 - routing_default_given = true 1.48 - end 1.49 - local module = settings.module or args.module or request.get_module() 1.50 - assert(settings.mode, "No mode specified in routing entry.") 1.51 - assert(settings.view, "No view specified in routing entry.") 1.52 - params["_webmcp_routing." .. status .. ".mode"] = settings.mode 1.53 - params["_webmcp_routing." .. status .. ".module"] = module 1.54 - params["_webmcp_routing." .. status .. ".view"] = settings.view 1.55 - params["_webmcp_routing." .. status .. ".id"] = settings.id 1.56 - if settings.params then 1.57 - for key, value in pairs(settings.params) do 1.58 - params["_webmcp_routing." .. status .. ".params." .. key] = value 1.59 - end 1.60 - end 1.61 - end 1.62 - end 1.63 - if not routing_default_given then 1.64 - params["_webmcp_routing.default.mode"] = "forward" 1.65 - params["_webmcp_routing.default.module"] = request.get_module() 1.66 - params["_webmcp_routing.default.view"] = request.get_view() 1.67 - end 1.68 + prepare_routing_params(params, args.routing, args.module) 1.69 params._webmcp_csrf_secret = request.get_csrf_secret() 1.70 local attr = table.new(args.attr) 1.71 attr.action = encode.url{ 1.72 @@ -73,6 +78,45 @@ 1.73 action = args.action, 1.74 } 1.75 attr.method = args.method and string.upper(args.method) or "POST" 1.76 + if ui.is_partial_loading_enabled() and args.partial then 1.77 + attr.onsubmit = slot.use_temporary(function() 1.78 + local partial_mode = "form_normal" 1.79 + if args.action then 1.80 + partial_mode = "form_action" 1.81 + slot.put( 1.82 + 'var element; ', 1.83 + 'var formElements = []; ', 1.84 + 'for (var i=0; i<this.elements.length; i++) { ', 1.85 + 'formElements[formElements.length] = this.elements[i]; ', 1.86 + '} ', 1.87 + 'for (i=0; i<formElements.length; i++) { ', 1.88 + 'element = formElements[i]; ', 1.89 + 'if (element.name.search(/^_webmcp_routing\\./) >= 0) { ', 1.90 + 'element.parentNode.removeChild(element); ', 1.91 + '} ', 1.92 + '}' 1.93 + ) 1.94 + local routing_params = {} 1.95 + prepare_routing_params( 1.96 + routing_params, 1.97 + args.partial.routing, 1.98 + args.partial.module 1.99 + ) 1.100 + for key, value in pairs(routing_params) do 1.101 + slot.put( 1.102 + ' ', 1.103 + 'element = document.createElement("input"); ', 1.104 + 'element.setAttribute("type", "hidden"); ', 1.105 + 'element.setAttribute("name", ', encode.json(key), '); ', 1.106 + 'element.setAttribute("value", ', encode.json(value), '); ', 1.107 + 'this.appendChild(element);' 1.108 + ) 1.109 + end 1.110 + slot.put(' ') 1.111 + end 1.112 + slot.put(ui._partial_load_js(args.partial, partial_mode)) 1.113 + end) 1.114 + end 1.115 if slot_state.form_opened then 1.116 error("Cannot open a non-readonly form inside a non-readonly form.") 1.117 end