webmcp

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

Impressum / About Us