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