jbe/bsw@0: --[[-- jbe/bsw@0: ui.link{ jbe/bsw@0: external = external, -- external URL jbe/bsw@0: static = static, -- URL relative to the static file directory jbe/bsw@0: module = module, -- module name jbe/bsw@0: view = view, -- view name jbe/bsw@0: action = action, -- action name jbe@53: attr = attr, -- for views: table of HTML attributes jbe@53: a_attr = a_attr, -- for actions: table of HTML attributes for the "a" tag jbe@53: form_attr = form_attr, -- for actions: table of HTML attributes for the "form" tag jbe/bsw@0: id = id, -- optional id to be passed to the view or action to select a particular data record jbe/bsw@0: params = params, -- optional parameters to be passed to the view or action jbe/bsw@0: routing = routing, -- optional routing information for action links, as described for ui.form{...} jbe@79: anchor = anchor, -- for views: anchor in destination URL jbe/bsw@0: text = text, -- link text jbe@223: content = content -- link content (overrides link text, except for submit buttons for action calls without JavaScript) jbe/bsw@0: } jbe/bsw@0: jbe/bsw@0: 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: jbe/bsw@0: --]]-- jbe/bsw@0: jbe/bsw@0: function ui.link(args) jbe/bsw@0: local args = args or {} jbe/bsw@4: local content = args.content or args.text jbe/bsw@0: assert(content, "ui.link{...} needs a text.") jbe/bsw@0: local function wrapped_content() jbe/bsw@11: if args.image then jbe/bsw@11: ui.image(args.image) jbe/bsw@11: end jbe/bsw@0: if type(content) == "function" then jbe/bsw@0: content() jbe/bsw@0: else jbe/bsw@0: slot.put(encode.html(content)) jbe/bsw@0: end jbe/bsw@0: end jbe/bsw@0: if args.action then jbe/bsw@0: local form_attr = table.new(args.form_attr) jbe/bsw@0: local form_id jbe/bsw@0: if form_attr.id then jbe/bsw@0: form_id = form_attr.id jbe/bsw@0: else jbe/bsw@0: form_id = ui.create_unique_id() jbe/bsw@0: end jbe/bsw@0: local quoted_form_id = encode.json(form_id) jbe/bsw@0: form_attr.id = form_id jbe/bsw@0: local a_attr = table.new(args.attr) jbe/bsw@0: a_attr.href = "#" jbe/bsw@0: a_attr.onclick = jbe@52: "var f = document.getElementById(" .. quoted_form_id .. "); if (! f.onsubmit || f.onsubmit() != false) { f.submit() }; return false;" jbe/bsw@0: ui.form{ jbe/bsw@0: external = args.external, jbe/bsw@0: module = args.module or request.get_module(), jbe/bsw@0: action = args.action, jbe/bsw@0: id = args.id, jbe/bsw@0: params = args.params, jbe/bsw@0: routing = args.routing, jbe/bsw@0: attr = form_attr, jbe/bsw@0: content = function() jbe/bsw@0: ui.submit{ text = args.text, attr = args.submit_attr } jbe/bsw@0: end jbe/bsw@0: } jbe/bsw@0: ui.script{ jbe/bsw@0: type = "text/javascript", jbe/bsw@0: script = ( jbe/bsw@0: "document.getElementById(" .. jbe/bsw@0: quoted_form_id .. jbe/bsw@0: ").style.display = 'none'; document.write(" .. jbe/bsw@0: encode.json( jbe/bsw@0: slot.use_temporary( jbe/bsw@0: function() jbe/bsw@0: ui.tag{ jbe/bsw@0: tag = "a", jbe/bsw@0: attr = a_attr, jbe/bsw@0: content = wrapped_content jbe/bsw@0: } jbe/bsw@0: end jbe/bsw@0: ) jbe/bsw@0: ) .. jbe/bsw@0: ");" jbe/bsw@0: ) jbe/bsw@0: } jbe/bsw@0: else jbe/bsw@0: -- TODO: support content function jbe/bsw@0: local a_attr = table.new(args.attr) jbe/bsw@0: a_attr.href = encode.url{ jbe/bsw@0: external = args.external, jbe/bsw@0: static = args.static, jbe/bsw@0: module = args.module or request.get_module(), jbe/bsw@0: view = args.view, jbe/bsw@0: id = args.id, jbe/bsw@0: params = args.params, jbe@79: anchor = args.anchor jbe/bsw@0: } jbe/bsw@0: return ui.tag{ tag = "a", attr = a_attr, content = wrapped_content } jbe/bsw@0: end jbe/bsw@0: end