webmcp
view framework/env/encode/url.lua @ 79:3a6962b9121c
Anchor support for encode.url{...}, ui.link{...} and ui.paginate{...}
| author | jbe | 
|---|---|
| date | Wed Jun 27 18:57:53 2012 +0200 (2012-06-27) | 
| parents | d76a8857ba62 | 
| children | ca3f7b001014 | 
 line source
     1 --[[--
     2 url_string =              -- a string containing an URL
     3 encode.url{
     4   external  = external,   -- external URL (instead of specifying base, module, etc. below)
     5   base      = base,       -- optional string containing a base URL of a WebMCP application
     6   static    = static,     -- an URL relative to the static file directory
     7   module    = module,     -- a module name of the WebMCP application
     8   view      = view,       -- a view name of the WebMCP application
     9   action    = action,     -- an action name of the WebMCP application
    10   id        = id,         -- optional id to be passed to the view or action to select a particular data record
    11   params    = params,     -- optional parameters to be passed to the view or action
    12   anchor    = anchor      -- anchor in URL
    13 }
    15 This function creates URLs to external locations, to static files within the WebMCP application or to a certain view or action inside a module.
    17 --]]--
    19 function encode.url(args)
    20   local external  = args.external
    21   local base      = args.base or request.get_relative_baseurl()
    22   local static    = args.static
    23   local module    = args.module
    24   local view      = args.view
    25   local action    = args.action
    26   local id        = args.id
    27   local params    = args.params or {}
    28   local result    = {}
    29   local id_as_param = false
    30   local function add(...)
    31     for i = 1, math.huge do
    32       local v = select(i, ...)
    33       if v == nil then break end
    34       result[#result+1] = v
    35     end
    36   end
    37   if external then
    38     add(external)
    39   else
    40     add(base)
    41     if not string.find(base, "/$") then
    42       add("/")
    43     end
    44     if static then
    45       add("static/")
    46       add(static)
    47     elseif module or view or action or id then
    48       assert(module, "Module not specified.")
    49       add(encode.url_part(module), "/")
    50       if view and not action then
    51         local view_base, view_suffix = string.match(
    52           view,
    53           "^([^.]*)(.*)$"
    54         )
    55         add(encode.url_part(view_base))
    56         if args.id then
    57           add("/", encode.url_part(id))
    58         end
    59         if view_suffix == "" then
    60           add(".html")
    61         else
    62           add(view_suffix)  -- view_suffix includes dot as first character
    63         end
    64       elseif action and not view then
    65         add(encode.url_part(action))
    66         id_as_param = true
    67       elseif view and action then
    68         error("Both a view and an action was specified.")
    69       end
    70     end
    71     do
    72       local new_params = request.get_perm_params()
    73       for key, value in pairs(params) do
    74         new_params[key] = value
    75       end
    76       params = new_params
    77     end
    78   end
    79   if next(params) ~= nil or (id and id_as_param) then
    80     add("?")
    81     if id and id_as_param then
    82       add("_webmcp_id=", encode.url_part(id), "&")
    83     end
    84     for key, value in pairs(params) do
    85       -- TODO: better way to detect arrays?
    86       if string.match(key, "%[%]$") then
    87         for idx, entry in ipairs(value) do
    88           add(encode.url_part(key), "=", encode.url_part(entry), "&")
    89         end
    90       else
    91         add(encode.url_part(key), "=", encode.url_part(value), "&")
    92       end
    93     end
    94     result[#result] = nil  -- remove last '&' or '?'
    95   end
    96   local string_result = table.concat(result)
    97   if anchor ~= nil then
    98     string_result = string.match(string_result, "^[^#]*")
    99     if anchor then
   100       string_result = string_result .. anchor
   101     end
   102   end
   103   return string_result
   104 end
