webmcp
diff framework/env/ui/link.lua @ 0:9fdfb27f8e67
Version 1.0.0
author | jbe/bsw |
---|---|
date | Sun Oct 25 12:00:00 2009 +0100 (2009-10-25) |
parents | |
children | 5e32ef998acf |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/framework/env/ui/link.lua Sun Oct 25 12:00:00 2009 +0100 1.3 @@ -0,0 +1,90 @@ 1.4 +--[[-- 1.5 +ui.link{ 1.6 + external = external, -- external URL 1.7 + static = static, -- URL relative to the static file directory 1.8 + module = module, -- module name 1.9 + view = view, -- view name 1.10 + action = action, -- action name 1.11 + id = id, -- optional id to be passed to the view or action to select a particular data record 1.12 + params = params, -- optional parameters to be passed to the view or action 1.13 + routing = routing, -- optional routing information for action links, as described for ui.form{...} 1.14 + text = text, -- link text 1.15 + content = content -- alternative name for 'text' option, preferred for functions 1.16 +} 1.17 + 1.18 +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). 1.19 + 1.20 +--]]-- 1.21 + 1.22 +function ui.link(args) 1.23 + local args = args or {} 1.24 + local content = args.text or args.content -- TODO: decide which argument name to use 1.25 + assert(content, "ui.link{...} needs a text.") 1.26 + local function wrapped_content() 1.27 + -- TODO: icon/image 1.28 + if type(content) == "function" then 1.29 + content() 1.30 + else 1.31 + slot.put(encode.html(content)) 1.32 + end 1.33 + end 1.34 + if args.action then 1.35 + local form_attr = table.new(args.form_attr) 1.36 + local form_id 1.37 + if form_attr.id then 1.38 + form_id = form_attr.id 1.39 + else 1.40 + form_id = ui.create_unique_id() 1.41 + end 1.42 + local quoted_form_id = encode.json(form_id) 1.43 + form_attr.id = form_id 1.44 + local a_attr = table.new(args.attr) 1.45 + a_attr.href = "#" 1.46 + a_attr.onclick = 1.47 + "var f = document.getElementById(" .. quoted_form_id .. "); if (! f.onsubmit || f.onsubmit() != false) { f.submit() };" 1.48 + ui.form{ 1.49 + external = args.external, 1.50 + module = args.module or request.get_module(), 1.51 + action = args.action, 1.52 + id = args.id, 1.53 + params = args.params, 1.54 + routing = args.routing, 1.55 + attr = form_attr, 1.56 + content = function() 1.57 + ui.submit{ text = args.text, attr = args.submit_attr } 1.58 + end 1.59 + } 1.60 + ui.script{ 1.61 + type = "text/javascript", 1.62 + script = ( 1.63 + "document.getElementById(" .. 1.64 + quoted_form_id .. 1.65 + ").style.display = 'none'; document.write(" .. 1.66 + encode.json( 1.67 + slot.use_temporary( 1.68 + function() 1.69 + ui.tag{ 1.70 + tag = "a", 1.71 + attr = a_attr, 1.72 + content = wrapped_content 1.73 + } 1.74 + end 1.75 + ) 1.76 + ) .. 1.77 + ");" 1.78 + ) 1.79 + } 1.80 + else 1.81 + -- TODO: support content function 1.82 + local a_attr = table.new(args.attr) 1.83 + a_attr.href = encode.url{ 1.84 + external = args.external, 1.85 + static = args.static, 1.86 + module = args.module or request.get_module(), 1.87 + view = args.view, 1.88 + id = args.id, 1.89 + params = args.params, 1.90 + } 1.91 + return ui.tag{ tag = "a", attr = a_attr, content = wrapped_content } 1.92 + end 1.93 +end