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