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@79
|
14 anchor = anchor, -- for views: anchor in destination URL
|
jbe/bsw@0
|
15 text = text, -- link text
|
jbe@223
|
16 content = content -- link content (overrides link text, except for submit buttons for action calls without JavaScript)
|
jbe/bsw@0
|
17 }
|
jbe/bsw@0
|
18
|
jbe/bsw@0
|
19 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
|
20
|
jbe/bsw@0
|
21 --]]--
|
jbe/bsw@0
|
22
|
jbe/bsw@0
|
23 function ui.link(args)
|
jbe/bsw@0
|
24 local args = args or {}
|
jbe/bsw@4
|
25 local content = args.content or args.text
|
jbe/bsw@0
|
26 assert(content, "ui.link{...} needs a text.")
|
jbe/bsw@0
|
27 local function wrapped_content()
|
jbe/bsw@11
|
28 if args.image then
|
jbe/bsw@11
|
29 ui.image(args.image)
|
jbe/bsw@11
|
30 end
|
jbe/bsw@0
|
31 if type(content) == "function" then
|
jbe/bsw@0
|
32 content()
|
jbe/bsw@0
|
33 else
|
jbe/bsw@0
|
34 slot.put(encode.html(content))
|
jbe/bsw@0
|
35 end
|
jbe/bsw@0
|
36 end
|
jbe/bsw@0
|
37 if args.action then
|
jbe/bsw@0
|
38 local form_attr = table.new(args.form_attr)
|
jbe/bsw@0
|
39 local form_id
|
jbe/bsw@0
|
40 if form_attr.id then
|
jbe/bsw@0
|
41 form_id = form_attr.id
|
jbe/bsw@0
|
42 else
|
jbe/bsw@0
|
43 form_id = ui.create_unique_id()
|
jbe/bsw@0
|
44 end
|
jbe/bsw@0
|
45 local quoted_form_id = encode.json(form_id)
|
jbe/bsw@0
|
46 form_attr.id = form_id
|
jbe/bsw@0
|
47 local a_attr = table.new(args.attr)
|
jbe/bsw@0
|
48 a_attr.href = "#"
|
jbe/bsw@0
|
49 a_attr.onclick =
|
jbe@52
|
50 "var f = document.getElementById(" .. quoted_form_id .. "); if (! f.onsubmit || f.onsubmit() != false) { f.submit() }; return false;"
|
jbe/bsw@0
|
51 ui.form{
|
jbe/bsw@0
|
52 external = args.external,
|
jbe/bsw@0
|
53 module = args.module or request.get_module(),
|
jbe/bsw@0
|
54 action = args.action,
|
jbe/bsw@0
|
55 id = args.id,
|
jbe/bsw@0
|
56 params = args.params,
|
jbe/bsw@0
|
57 routing = args.routing,
|
jbe/bsw@0
|
58 attr = form_attr,
|
jbe/bsw@0
|
59 content = function()
|
jbe/bsw@0
|
60 ui.submit{ text = args.text, attr = args.submit_attr }
|
jbe/bsw@0
|
61 end
|
jbe/bsw@0
|
62 }
|
jbe/bsw@0
|
63 ui.script{
|
jbe/bsw@0
|
64 type = "text/javascript",
|
jbe/bsw@0
|
65 script = (
|
jbe/bsw@0
|
66 "document.getElementById(" ..
|
jbe/bsw@0
|
67 quoted_form_id ..
|
jbe/bsw@0
|
68 ").style.display = 'none'; document.write(" ..
|
jbe/bsw@0
|
69 encode.json(
|
jbe/bsw@0
|
70 slot.use_temporary(
|
jbe/bsw@0
|
71 function()
|
jbe/bsw@0
|
72 ui.tag{
|
jbe/bsw@0
|
73 tag = "a",
|
jbe/bsw@0
|
74 attr = a_attr,
|
jbe/bsw@0
|
75 content = wrapped_content
|
jbe/bsw@0
|
76 }
|
jbe/bsw@0
|
77 end
|
jbe/bsw@0
|
78 )
|
jbe/bsw@0
|
79 ) ..
|
jbe/bsw@0
|
80 ");"
|
jbe/bsw@0
|
81 )
|
jbe/bsw@0
|
82 }
|
jbe/bsw@0
|
83 else
|
jbe/bsw@0
|
84 -- TODO: support content function
|
jbe/bsw@0
|
85 local a_attr = table.new(args.attr)
|
jbe/bsw@0
|
86 a_attr.href = encode.url{
|
jbe/bsw@0
|
87 external = args.external,
|
jbe/bsw@0
|
88 static = args.static,
|
jbe/bsw@0
|
89 module = args.module or request.get_module(),
|
jbe/bsw@0
|
90 view = args.view,
|
jbe/bsw@0
|
91 id = args.id,
|
jbe/bsw@0
|
92 params = args.params,
|
jbe@79
|
93 anchor = args.anchor
|
jbe/bsw@0
|
94 }
|
jbe/bsw@0
|
95 return ui.tag{ tag = "a", attr = a_attr, content = wrapped_content }
|
jbe/bsw@0
|
96 end
|
jbe/bsw@0
|
97 end
|