Jump to content

Module:Road data/browse/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Chinissai (talk | contribs) at 23:47, 30 April 2016 (Created page with 'local p = {} -- Detect if this is a sandbox module. local sandbox = mw.title.getCurrentTitle().subpageText == "sandbox" and "/sandbox" or "" local shieldModule...'). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
local p = {}

-- Detect if this is a sandbox module.
local sandbox = mw.title.getCurrentTitle().subpageText == "sandbox" and "/sandbox" or ""

local shieldModuleName = "Module:Road data/shield"

local format = mw.ustring.format

local function shield(route)
	local shieldModule = require(shieldModuleName)
	return shieldModule.shield(route)
end

local function link(route)
	if route.type == '' then return route.route end

	local parserModule = require("Module:Road data/parser" .. sandbox)
	local parser = parserModule.parser
	local abbr = parser(route, 'abbr')
	if not abbr then
		route.typeerror = true
		return format("<span class=\"error\">Invalid type: %s</span>", route.type)
	end
	if route.nolink then return abbr end

	local link = parser(route, 'link') or ''
	if link == '' then return abbr end

	return format("[[%s|%s]]", link, abbr)
end

local function previousRoute(route)
	local shieldText = shield(route)
	local linkText = link(route)
	local cell = mw.html.create('td'):css({
		width = "45%",
		["vertical-align"] = "middle",
		padding = "0",
		["text-align"] = "left"
	})
	local routeText = mw.html.create("span"):css({display = "inline-block"})
	routeText:wikiText(shieldText .. ' ' .. linkText)
	cell:wikitext("← " .. tostring(routeText))
	return cell
end

local function nextRoute(route)
	local shieldText = shield(route)
	local linkText = link(route)
	local cell = mw.html.create('td'):css({
		width = "45%",
		["vertical-align"] = "middle",
		padding = "0",
		["text-align"] = "right"
	})
	local routeText = mw.html.create("span"):css({display = "inline-block"})
	routeText:wikiText(linkText .. ' ' .. shieldText)
	cell:wikitext(tostring(routeText) .. " →")
	return cell
end

function p._browse(route)
	local country = route.country
	local state = route.state or route.province
	local county = route.county
	
	local previousData = {
		country = country, state = state, county = county,
		type = route.previous_type, route = route.previous_route,
		dab = route.previous_dab
	}
	local nextData = {
		country = country, state = state, county = county,
		type = route.next_type, route = route.next_route,
		dab = route.next_dab
	}
	
	local previousRoute = previousRoute(previousData)
	local nextRoute = nextRoute(nextData)
	if previousData.typeerror or nextData.typeerror then
		route.typeerror = true
	end
	
	local centerRoute = mw.html.create('td'):css({
		["text-align"] = "center",
		["white-space"] = "nowrap",
		width = "10%",
		["vertical-align"] = "middle",
		padding = "0"
	})
	local route = route.browse_route
	if route then
		centerRoute:wikitext("'''" .. route .. "'''")
	end
	
	local browseRow = mw.html.create('tr')
	browseRow:node(previousRoute):node(centerRoute):node(nextRoute)
	return tostring(browseRow)
end

function p.browse(frame)
	-- Import module function to work with passed arguments
	local getArgs = require('Module:Arguments').getArgs
	local args = getArgs(frame)
	args.browse_route = args.route
	return p._browse(args)
end

return p