webmcp

annotate 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
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/bsw@0 8 id = id, -- optional id to be passed to the view or action to select a particular data record
jbe/bsw@0 9 params = params, -- optional parameters to be passed to the view or action
jbe/bsw@0 10 routing = routing, -- optional routing information for action links, as described for ui.form{...}
jbe/bsw@0 11 text = text, -- link text
jbe/bsw@0 12 content = content -- alternative name for 'text' option, preferred for functions
jbe/bsw@0 13 }
jbe/bsw@0 14
jbe/bsw@0 15 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 16
jbe/bsw@0 17 --]]--
jbe/bsw@0 18
jbe/bsw@0 19 function ui.link(args)
jbe/bsw@0 20 local args = args or {}
jbe/bsw@0 21 local content = args.text or args.content -- TODO: decide which argument name to use
jbe/bsw@0 22 assert(content, "ui.link{...} needs a text.")
jbe/bsw@0 23 local function wrapped_content()
jbe/bsw@0 24 -- TODO: icon/image
jbe/bsw@0 25 if type(content) == "function" then
jbe/bsw@0 26 content()
jbe/bsw@0 27 else
jbe/bsw@0 28 slot.put(encode.html(content))
jbe/bsw@0 29 end
jbe/bsw@0 30 end
jbe/bsw@0 31 if args.action then
jbe/bsw@0 32 local form_attr = table.new(args.form_attr)
jbe/bsw@0 33 local form_id
jbe/bsw@0 34 if form_attr.id then
jbe/bsw@0 35 form_id = form_attr.id
jbe/bsw@0 36 else
jbe/bsw@0 37 form_id = ui.create_unique_id()
jbe/bsw@0 38 end
jbe/bsw@0 39 local quoted_form_id = encode.json(form_id)
jbe/bsw@0 40 form_attr.id = form_id
jbe/bsw@0 41 local a_attr = table.new(args.attr)
jbe/bsw@0 42 a_attr.href = "#"
jbe/bsw@0 43 a_attr.onclick =
jbe/bsw@0 44 "var f = document.getElementById(" .. quoted_form_id .. "); if (! f.onsubmit || f.onsubmit() != false) { f.submit() };"
jbe/bsw@0 45 ui.form{
jbe/bsw@0 46 external = args.external,
jbe/bsw@0 47 module = args.module or request.get_module(),
jbe/bsw@0 48 action = args.action,
jbe/bsw@0 49 id = args.id,
jbe/bsw@0 50 params = args.params,
jbe/bsw@0 51 routing = args.routing,
jbe/bsw@0 52 attr = form_attr,
jbe/bsw@0 53 content = function()
jbe/bsw@0 54 ui.submit{ text = args.text, attr = args.submit_attr }
jbe/bsw@0 55 end
jbe/bsw@0 56 }
jbe/bsw@0 57 ui.script{
jbe/bsw@0 58 type = "text/javascript",
jbe/bsw@0 59 script = (
jbe/bsw@0 60 "document.getElementById(" ..
jbe/bsw@0 61 quoted_form_id ..
jbe/bsw@0 62 ").style.display = 'none'; document.write(" ..
jbe/bsw@0 63 encode.json(
jbe/bsw@0 64 slot.use_temporary(
jbe/bsw@0 65 function()
jbe/bsw@0 66 ui.tag{
jbe/bsw@0 67 tag = "a",
jbe/bsw@0 68 attr = a_attr,
jbe/bsw@0 69 content = wrapped_content
jbe/bsw@0 70 }
jbe/bsw@0 71 end
jbe/bsw@0 72 )
jbe/bsw@0 73 ) ..
jbe/bsw@0 74 ");"
jbe/bsw@0 75 )
jbe/bsw@0 76 }
jbe/bsw@0 77 else
jbe/bsw@0 78 -- TODO: support content function
jbe/bsw@0 79 local a_attr = table.new(args.attr)
jbe/bsw@0 80 a_attr.href = encode.url{
jbe/bsw@0 81 external = args.external,
jbe/bsw@0 82 static = args.static,
jbe/bsw@0 83 module = args.module or request.get_module(),
jbe/bsw@0 84 view = args.view,
jbe/bsw@0 85 id = args.id,
jbe/bsw@0 86 params = args.params,
jbe/bsw@0 87 }
jbe/bsw@0 88 return ui.tag{ tag = "a", attr = a_attr, content = wrapped_content }
jbe/bsw@0 89 end
jbe/bsw@0 90 end

Impressum / About Us