webmcp

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

Impressum / About Us