webmcp
annotate framework/env/ui/link.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 | 5e32ef998acf | 
| children | f3d3203cd2e4 | 
| rev | line source | 
|---|---|
| jbe/bsw@0 | 1 --[[-- | 
| jbe/bsw@0 | 2 ui.link{ | 
| jbe/bsw@0 | 3 external = external, -- external URL | 
| jbe/bsw@0 | 4 static = static, -- URL relative to the static file directory | 
| jbe/bsw@0 | 5 module = module, -- module name | 
| jbe/bsw@0 | 6 view = view, -- view name | 
| jbe/bsw@0 | 7 action = action, -- action name | 
| jbe/bsw@0 | 8 id = id, -- optional id to be passed to the view or action to select a particular data record | 
| jbe/bsw@0 | 9 params = params, -- optional parameters to be passed to the view or action | 
| jbe/bsw@0 | 10 routing = routing, -- optional routing information for action links, as described for ui.form{...} | 
| jbe/bsw@0 | 11 text = text, -- link text | 
| jbe/bsw@11 | 12 content = content, -- link content (overrides link text, except for submit buttons for action calls without JavaScript) | 
| jbe/bsw@11 | 13 partial = { -- TODO: documentation | 
| jbe/bsw@11 | 14 module = module, | 
| jbe/bsw@11 | 15 view = view, | 
| jbe/bsw@11 | 16 id = id, | 
| jbe/bsw@11 | 17 params = params | 
| jbe/bsw@11 | 18 } | 
| jbe/bsw@0 | 19 } | 
| jbe/bsw@0 | 20 | 
| jbe/bsw@0 | 21 This function inserts a link into the active slot. It may be either an internal application link ('module' given and 'view' or 'action' given), or a link to an external web page ('external' given), or a link to a file in the static file directory of the application ('static' given). | 
| jbe/bsw@0 | 22 | 
| jbe/bsw@0 | 23 --]]-- | 
| jbe/bsw@0 | 24 | 
| jbe/bsw@0 | 25 function ui.link(args) | 
| jbe/bsw@0 | 26 local args = args or {} | 
| jbe/bsw@4 | 27 local content = args.content or args.text | 
| jbe/bsw@0 | 28 assert(content, "ui.link{...} needs a text.") | 
| jbe/bsw@0 | 29 local function wrapped_content() | 
| jbe/bsw@11 | 30 if args.image then | 
| jbe/bsw@11 | 31 ui.image(args.image) | 
| jbe/bsw@11 | 32 end | 
| jbe/bsw@0 | 33 if type(content) == "function" then | 
| jbe/bsw@0 | 34 content() | 
| jbe/bsw@0 | 35 else | 
| jbe/bsw@0 | 36 slot.put(encode.html(content)) | 
| jbe/bsw@0 | 37 end | 
| jbe/bsw@0 | 38 end | 
| jbe/bsw@0 | 39 if args.action then | 
| jbe/bsw@0 | 40 local form_attr = table.new(args.form_attr) | 
| jbe/bsw@0 | 41 local form_id | 
| jbe/bsw@0 | 42 if form_attr.id then | 
| jbe/bsw@0 | 43 form_id = form_attr.id | 
| jbe/bsw@0 | 44 else | 
| jbe/bsw@0 | 45 form_id = ui.create_unique_id() | 
| jbe/bsw@0 | 46 end | 
| jbe/bsw@0 | 47 local quoted_form_id = encode.json(form_id) | 
| jbe/bsw@0 | 48 form_attr.id = form_id | 
| jbe/bsw@0 | 49 local a_attr = table.new(args.attr) | 
| jbe/bsw@0 | 50 a_attr.href = "#" | 
| jbe/bsw@0 | 51 a_attr.onclick = | 
| jbe/bsw@0 | 52 "var f = document.getElementById(" .. quoted_form_id .. "); if (! f.onsubmit || f.onsubmit() != false) { f.submit() };" | 
| jbe/bsw@0 | 53 ui.form{ | 
| jbe/bsw@0 | 54 external = args.external, | 
| jbe/bsw@0 | 55 module = args.module or request.get_module(), | 
| jbe/bsw@0 | 56 action = args.action, | 
| jbe/bsw@0 | 57 id = args.id, | 
| jbe/bsw@0 | 58 params = args.params, | 
| jbe/bsw@0 | 59 routing = args.routing, | 
| jbe/bsw@11 | 60 partial = args.partial, | 
| jbe/bsw@0 | 61 attr = form_attr, | 
| jbe/bsw@0 | 62 content = function() | 
| jbe/bsw@0 | 63 ui.submit{ text = args.text, attr = args.submit_attr } | 
| jbe/bsw@0 | 64 end | 
| jbe/bsw@0 | 65 } | 
| jbe/bsw@0 | 66 ui.script{ | 
| jbe/bsw@0 | 67 type = "text/javascript", | 
| jbe/bsw@0 | 68 script = ( | 
| jbe/bsw@0 | 69 "document.getElementById(" .. | 
| jbe/bsw@0 | 70 quoted_form_id .. | 
| jbe/bsw@0 | 71 ").style.display = 'none'; document.write(" .. | 
| jbe/bsw@0 | 72 encode.json( | 
| jbe/bsw@0 | 73 slot.use_temporary( | 
| jbe/bsw@0 | 74 function() | 
| jbe/bsw@0 | 75 ui.tag{ | 
| jbe/bsw@0 | 76 tag = "a", | 
| jbe/bsw@0 | 77 attr = a_attr, | 
| jbe/bsw@0 | 78 content = wrapped_content | 
| jbe/bsw@0 | 79 } | 
| jbe/bsw@0 | 80 end | 
| jbe/bsw@0 | 81 ) | 
| jbe/bsw@0 | 82 ) .. | 
| jbe/bsw@0 | 83 ");" | 
| jbe/bsw@0 | 84 ) | 
| jbe/bsw@0 | 85 } | 
| jbe/bsw@0 | 86 else | 
| jbe/bsw@0 | 87 -- TODO: support content function | 
| jbe/bsw@0 | 88 local a_attr = table.new(args.attr) | 
| jbe/bsw@0 | 89 a_attr.href = encode.url{ | 
| jbe/bsw@0 | 90 external = args.external, | 
| jbe/bsw@0 | 91 static = args.static, | 
| jbe/bsw@0 | 92 module = args.module or request.get_module(), | 
| jbe/bsw@0 | 93 view = args.view, | 
| jbe/bsw@0 | 94 id = args.id, | 
| jbe/bsw@0 | 95 params = args.params, | 
| jbe/bsw@0 | 96 } | 
| jbe/bsw@11 | 97 if ui.is_partial_loading_enabled() and args.partial then | 
| jbe/bsw@11 | 98 a_attr.onclick = ui._partial_load_js(args.partial) | 
| jbe/bsw@11 | 99 end | 
| jbe/bsw@0 | 100 return ui.tag{ tag = "a", attr = a_attr, content = wrapped_content } | 
| jbe/bsw@0 | 101 end | 
| jbe/bsw@0 | 102 end |