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@53
|
8 attr = attr, -- for views: table of HTML attributes
|
jbe@53
|
9 a_attr = a_attr, -- for actions: table of HTML attributes for the "a" tag
|
jbe@53
|
10 form_attr = form_attr, -- for actions: table of HTML attributes for the "form" tag
|
jbe/bsw@0
|
11 id = id, -- optional id to be passed to the view or action to select a particular data record
|
jbe/bsw@0
|
12 params = params, -- optional parameters to be passed to the view or action
|
jbe/bsw@0
|
13 routing = routing, -- optional routing information for action links, as described for ui.form{...}
|
jbe/bsw@0
|
14 text = text, -- link text
|
jbe/bsw@11
|
15 content = content, -- link content (overrides link text, except for submit buttons for action calls without JavaScript)
|
jbe@12
|
16 partial = { -- parameters for partial loading, see below
|
jbe/bsw@11
|
17 module = module,
|
jbe/bsw@11
|
18 view = view,
|
jbe/bsw@11
|
19 id = id,
|
jbe@12
|
20 params = params,
|
jbe@12
|
21 target = target
|
jbe/bsw@11
|
22 }
|
jbe/bsw@0
|
23 }
|
jbe/bsw@0
|
24
|
jbe/bsw@0
|
25 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
|
26
|
jbe@12
|
27 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
|
jbe@12
|
28 used to create an onclick event (onsubmit event for action links). The
|
jbe@12
|
29 "partial" setting table is passed to ui._partial_load_js as first argument.
|
jbe@12
|
30 See ui._partial_load_js(...) for further documentation.
|
jbe@12
|
31
|
jbe/bsw@0
|
32 --]]--
|
jbe/bsw@0
|
33
|
jbe/bsw@0
|
34 function ui.link(args)
|
jbe/bsw@0
|
35 local args = args or {}
|
jbe/bsw@4
|
36 local content = args.content or args.text
|
jbe/bsw@0
|
37 assert(content, "ui.link{...} needs a text.")
|
jbe/bsw@0
|
38 local function wrapped_content()
|
jbe/bsw@11
|
39 if args.image then
|
jbe/bsw@11
|
40 ui.image(args.image)
|
jbe/bsw@11
|
41 end
|
jbe/bsw@0
|
42 if type(content) == "function" then
|
jbe/bsw@0
|
43 content()
|
jbe/bsw@0
|
44 else
|
jbe/bsw@0
|
45 slot.put(encode.html(content))
|
jbe/bsw@0
|
46 end
|
jbe/bsw@0
|
47 end
|
jbe/bsw@0
|
48 if args.action then
|
jbe/bsw@0
|
49 local form_attr = table.new(args.form_attr)
|
jbe/bsw@0
|
50 local form_id
|
jbe/bsw@0
|
51 if form_attr.id then
|
jbe/bsw@0
|
52 form_id = form_attr.id
|
jbe/bsw@0
|
53 else
|
jbe/bsw@0
|
54 form_id = ui.create_unique_id()
|
jbe/bsw@0
|
55 end
|
jbe/bsw@0
|
56 local quoted_form_id = encode.json(form_id)
|
jbe/bsw@0
|
57 form_attr.id = form_id
|
jbe/bsw@0
|
58 local a_attr = table.new(args.attr)
|
jbe/bsw@0
|
59 a_attr.href = "#"
|
jbe/bsw@0
|
60 a_attr.onclick =
|
jbe@52
|
61 "var f = document.getElementById(" .. quoted_form_id .. "); if (! f.onsubmit || f.onsubmit() != false) { f.submit() }; return false;"
|
jbe/bsw@0
|
62 ui.form{
|
jbe/bsw@0
|
63 external = args.external,
|
jbe/bsw@0
|
64 module = args.module or request.get_module(),
|
jbe/bsw@0
|
65 action = args.action,
|
jbe/bsw@0
|
66 id = args.id,
|
jbe/bsw@0
|
67 params = args.params,
|
jbe/bsw@0
|
68 routing = args.routing,
|
jbe/bsw@11
|
69 partial = args.partial,
|
jbe/bsw@0
|
70 attr = form_attr,
|
jbe/bsw@0
|
71 content = function()
|
jbe/bsw@0
|
72 ui.submit{ text = args.text, attr = args.submit_attr }
|
jbe/bsw@0
|
73 end
|
jbe/bsw@0
|
74 }
|
jbe/bsw@0
|
75 ui.script{
|
jbe/bsw@0
|
76 type = "text/javascript",
|
jbe/bsw@0
|
77 script = (
|
jbe/bsw@0
|
78 "document.getElementById(" ..
|
jbe/bsw@0
|
79 quoted_form_id ..
|
jbe/bsw@0
|
80 ").style.display = 'none'; document.write(" ..
|
jbe/bsw@0
|
81 encode.json(
|
jbe/bsw@0
|
82 slot.use_temporary(
|
jbe/bsw@0
|
83 function()
|
jbe/bsw@0
|
84 ui.tag{
|
jbe/bsw@0
|
85 tag = "a",
|
jbe/bsw@0
|
86 attr = a_attr,
|
jbe/bsw@0
|
87 content = wrapped_content
|
jbe/bsw@0
|
88 }
|
jbe/bsw@0
|
89 end
|
jbe/bsw@0
|
90 )
|
jbe/bsw@0
|
91 ) ..
|
jbe/bsw@0
|
92 ");"
|
jbe/bsw@0
|
93 )
|
jbe/bsw@0
|
94 }
|
jbe/bsw@0
|
95 else
|
jbe/bsw@0
|
96 -- TODO: support content function
|
jbe/bsw@0
|
97 local a_attr = table.new(args.attr)
|
jbe/bsw@0
|
98 a_attr.href = encode.url{
|
jbe/bsw@0
|
99 external = args.external,
|
jbe/bsw@0
|
100 static = args.static,
|
jbe/bsw@0
|
101 module = args.module or request.get_module(),
|
jbe/bsw@0
|
102 view = args.view,
|
jbe/bsw@0
|
103 id = args.id,
|
jbe/bsw@0
|
104 params = args.params,
|
jbe/bsw@0
|
105 }
|
jbe/bsw@11
|
106 if ui.is_partial_loading_enabled() and args.partial then
|
jbe/bsw@11
|
107 a_attr.onclick = ui._partial_load_js(args.partial)
|
jbe/bsw@11
|
108 end
|
jbe/bsw@0
|
109 return ui.tag{ tag = "a", attr = a_attr, content = wrapped_content }
|
jbe/bsw@0
|
110 end
|
jbe/bsw@0
|
111 end
|