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