webmcp

view framework/env/ui/link.lua @ 12:f3d3203cd2e4

Documentation for partial loading added

NOTE: Previous changeset d76a8857ba62 also modified behaviour of ui.script: Scripts containing "]]>" are now rejected to avoid ambiguities
author jbe
date Fri Feb 19 16:43:29 2010 +0100 (2010-02-19)
parents d76a8857ba62
children 2a3257f29c02
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 = { -- parameters for partial loading, see below
14 module = module,
15 view = view,
16 id = id,
17 params = params,
18 target = target
19 }
20 }
22 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).
24 When passing a table as "partial" argument, AND if partial loading has been enabled by calling ui.enable_partial_loading(), then ui._partial_load_js is
25 used to create an onclick event (onsubmit event for action links). The
26 "partial" setting table is passed to ui._partial_load_js as first argument.
27 See ui._partial_load_js(...) for further documentation.
29 --]]--
31 function ui.link(args)
32 local args = args or {}
33 local content = args.content or args.text
34 assert(content, "ui.link{...} needs a text.")
35 local function wrapped_content()
36 if args.image then
37 ui.image(args.image)
38 end
39 if type(content) == "function" then
40 content()
41 else
42 slot.put(encode.html(content))
43 end
44 end
45 if args.action then
46 local form_attr = table.new(args.form_attr)
47 local form_id
48 if form_attr.id then
49 form_id = form_attr.id
50 else
51 form_id = ui.create_unique_id()
52 end
53 local quoted_form_id = encode.json(form_id)
54 form_attr.id = form_id
55 local a_attr = table.new(args.attr)
56 a_attr.href = "#"
57 a_attr.onclick =
58 "var f = document.getElementById(" .. quoted_form_id .. "); if (! f.onsubmit || f.onsubmit() != false) { f.submit() };"
59 ui.form{
60 external = args.external,
61 module = args.module or request.get_module(),
62 action = args.action,
63 id = args.id,
64 params = args.params,
65 routing = args.routing,
66 partial = args.partial,
67 attr = form_attr,
68 content = function()
69 ui.submit{ text = args.text, attr = args.submit_attr }
70 end
71 }
72 ui.script{
73 type = "text/javascript",
74 script = (
75 "document.getElementById(" ..
76 quoted_form_id ..
77 ").style.display = 'none'; document.write(" ..
78 encode.json(
79 slot.use_temporary(
80 function()
81 ui.tag{
82 tag = "a",
83 attr = a_attr,
84 content = wrapped_content
85 }
86 end
87 )
88 ) ..
89 ");"
90 )
91 }
92 else
93 -- TODO: support content function
94 local a_attr = table.new(args.attr)
95 a_attr.href = encode.url{
96 external = args.external,
97 static = args.static,
98 module = args.module or request.get_module(),
99 view = args.view,
100 id = args.id,
101 params = args.params,
102 }
103 if ui.is_partial_loading_enabled() and args.partial then
104 a_attr.onclick = ui._partial_load_js(args.partial)
105 end
106 return ui.tag{ tag = "a", attr = a_attr, content = wrapped_content }
107 end
108 end

Impressum / About Us