Jump to content

Module:Location/Components/Infobox

From Creopedia

This documentation is transcluded from Module:Location/Components/Infobox/doc.

Output an infobox for Module:Location using Module:InfoboxLua.


require( 'strict' )

local DIRECTIONS = { 'north', 'east', 'south', 'west' }

local infoboxLua = require( 'Module:InfoboxLua' )
local buttonLua = require( 'Module:ButtonLua' )

local p = {}

--- @param dir string
--- @param location string
--- @return mw.html|nil
local function getConnectionHtml( dir, location )
	if not location then
		return nil
	end

	local html = mw.html.create( 'div' )

	html
		:addClass( 't-location-infobox-connections-item' )
		:addClass( 't-location-infobox-connections-item--' .. dir )
		:wikitext( buttonLua.render( {
			label = location,
			link = location
		} ) )

	return html
end

--- @param args table
--- @return string
local function getConnections( args )
	local html = mw.html.create( 'div' ):addClass( 't-location-infobox-connections' )

	html:node( getConnectionHtml( 'current', args.name ) )

	for _, dir in ipairs( DIRECTIONS ) do
		local connectionHtml = getConnectionHtml( dir, args[dir] )

		if connectionHtml then
			html:node( connectionHtml )
		end
	end

	return tostring( html )
end

--- @param args table
--- @return string|nil
local function getMap( args )
	if not args.map then
		return nil
	end

	local html = mw.html.create( 'div' ):addClass( 't-location-infobox-map' )

	html:wikitext( string.format( '[[File:%s|320px]]', args.map ) )

	return tostring( html )
end

--- Build the infobox data needed from the raw data
---
--- @param context table
--- @return table
local function getInfoboxData( context )
	local args = context.args or {}

	return {
		title = args.name,
		subtitle = args.region,
		image = args.image,
		sections = {
			{
				content = getConnections( args )
			},
			{
				content = getMap( args )
			}
		}
	}
end

--- @param context ComponentContext
--- @return string
function p.render( context )
	return infoboxLua.render( getInfoboxData( context ) )
end

return p