Module:Beach Org

From zunagi

Revision as of 08:03, 14 September 2026 by Lee G (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:Beach Org/doc

local p = {}

-- Single source of truth for each organisation code -- previously duplicated across two
-- separate #switch templates (Template:Beach Org URL and Template:Beach Org Name) that had
-- to be kept in sync by hand. One table removes that risk entirely.
local orgs = {
	va   = { name = "VA",   url = "https://abvt.volleyball.org.au/" },
	nsw  = { name = "VNSW", url = "https://www.volleyballnsw.com.au/competitions/beach-volleyball-tour-summer" },
	qld  = { name = "VQ",   url = "https://www.vq.org.au/competitions/tournaments/queensland-beach-volleyball-tour/" },
	act  = { name = "VACT", url = "https://www.volleyballact.com.au/my-competition/act-beach-series" },
	vic  = { name = "VV",   url = "https://vicbeach.com.au/ranking-series/" },
	nbva = { name = "NBVA", url = "https://nbva.org.au/" },
	sa   = { name = "VSA",  url = "https://www.volleyballsa.com.au/SABVS/overview" },
	wa   = { name = "VWA",  url = "https://volleyballwa.com.au/" },
	tas  = { name = "VTAS", url = "https://www.volleyballtasmania.com.au/play/beach" },
	bbva = { name = "BBVA", url = "https://www.bbva.org.au/" },
}

local function trim(s)
	if not s then return nil end
	s = mw.text.trim(s)
	if s == "" then return nil end
	return s
end

-- Prefers org_code, falls back to the deprecated region_code -- same behaviour
-- Template:Beach Org Code provided, evaluated once instead of three times per page.
local function resolveCode(args)
	return trim(args.org_code) or trim(args.region_code) or ""
end

-- {{#invoke:Beach Org|code|org_code={{{org_code|}}}|region_code={{{region_code|}}}}}
-- Exposed on its own in case the bare resolved code is ever needed somewhere other than
-- the Organisation cell below.
function p.code(frame)
	return resolveCode(frame.args)
end

-- {{#invoke:Beach Org|cell|org_code=...|region_code=...|org=...|org_url=...}}
-- Renders the entire Organisation row value in one call: the state-icon span, then either
-- a link (default url/name for the resolved code, each overridable via org_url=/org=), or
-- plain org text, or "Not available yet" -- exactly what the old nested-template version
-- built from 6 separate transclusions.
function p.cell(frame)
	local args = frame.args
	local code = resolveCode(args)
	local org = orgs[string.lower(code)] or {}

	local url = trim(args.org_url) or org.url
	local name = trim(args.org) or org.name

	local out = mw.html.create("")
	out:tag("span")
		:attr("class", "state-icon " .. code)
	out:wikitext(" ")

	if url then
		out:wikitext("[" .. url .. " " .. (name or "") .. "]")
	elseif name then
		out:wikitext(name)
	else
		out:tag("i"):wikitext("Not available yet")
	end

	return tostring(out)
end

-- {{#invoke:Beach Org|url|org_code=...|region_code=...|org_url=...}}
-- Returns just the resolved organisation URL (the org_url override if given, else the
-- default URL for the resolved org_code/region_code), or an empty string if neither is
-- available.
function p.url(frame)
	local args = frame.args
	local code = resolveCode(args)
	local org = orgs[string.lower(code)] or {}
	return trim(args.org_url) or org.url or ""
end

return p