webmcp
view framework/env/ui/link.lua @ 375:fb98b17056e5
Proper support for mutable data types (JSON objects/arrays) in table columns
| author | jbe | 
|---|---|
| date | Sat Nov 14 15:44:53 2015 +0100 (2015-11-14) | 
| parents | 32ec28229bb5 | 
| children | 
 line source
     1 --[[--
     2 ui.link{
     3   external  = external,   -- external URL
     4   static    = static,     -- URL relative to the static file directory
     5   module    = module,     -- module name
     6   view      = view,       -- view name
     7   action    = action,     -- action name
     8   attr      = attr,       -- for views: table of HTML attributes
     9   a_attr    = a_attr,     -- for actions: table of HTML attributes for the "a" tag
    10   form_attr = form_attr,  -- for actions: table of HTML attributes for the "form" tag
    11   id        = id,         -- optional id to be passed to the view or action to select a particular data record
    12   params    = params,     -- optional parameters to be passed to the view or action
    13   routing   = routing,    -- optional routing information for action links, as described for ui.form{...}
    14   anchor    = anchor,     -- for views: anchor in destination URL
    15   text      = text,       -- link text
    16   content   = content     -- link content (overrides link text, except for submit buttons for action calls without JavaScript)
    17 }
    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).
    21 --]]--
    23 function ui.link(args)
    24   local args = args or {}
    25   local content = args.content or args.text
    26   assert(content, "ui.link{...} needs a text.")
    27   local function wrapped_content()
    28     if args.image then
    29       ui.image(args.image)
    30     end
    31     if type(content) == "function" then
    32       content()
    33     else
    34       slot.put(encode.html(content))
    35     end
    36   end
    37   if args.action then
    38     local form_attr   = table.new(args.form_attr)
    39     local form_id
    40     if form_attr.id then
    41       form_id = form_attr.id
    42     else
    43       form_id = ui.create_unique_id()
    44     end
    45     local quoted_form_id = encode.json(form_id)
    46     form_attr.id      = form_id
    47     local a_attr      = table.new(args.attr)
    48     a_attr.href       = "#"
    49     a_attr.onclick    =
    50       "var f = document.getElementById(" .. quoted_form_id .. "); if (! f.onsubmit || f.onsubmit() != false) { f.submit() }; return false;"
    51     ui.form{
    52       external = args.external,
    53       module   = args.module or request.get_module(),
    54       action   = args.action,
    55       id       = args.id,
    56       params   = args.params,
    57       routing  = args.routing,
    58       attr     = form_attr,
    59       content  = function()
    60         ui.submit{ text = args.text, attr = args.submit_attr }
    61       end
    62     }
    63     ui.script{
    64       type = "text/javascript",
    65       script = (
    66         "document.getElementById(" ..
    67         quoted_form_id ..
    68         ").style.display = 'none'; document.write(" ..
    69         encode.json(
    70           slot.use_temporary(
    71             function()
    72               ui.tag{
    73                 tag     = "a",
    74                 attr    = a_attr,
    75                 content = wrapped_content
    76               }
    77             end
    78           )
    79         ) ..
    80         ");"
    81       )
    82     }
    83   else
    84     -- TODO: support content function
    85     local a_attr = table.new(args.attr)
    86     a_attr.href = encode.url{
    87       external  = args.external,
    88       static    = args.static,
    89       module    = args.module or request.get_module(),
    90       view      = args.view,
    91       id        = args.id,
    92       params    = args.params,
    93       anchor    = args.anchor
    94     }
    95     return ui.tag{ tag  = "a", attr = a_attr, content = wrapped_content }
    96   end
    97 end
