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