Module:Infobox Area

Revision as of 21:22, 4 November 2021 by Admin (talk | contribs) (1 revision imported)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:Infobox Area/doc

--------------------------
-- Module for [[Template:Infobox Area]]
------------------------
local p = {}

local onmain = require('Module:Mainonly').on_main
local yesno = require('Module:Yesno')
local paramtest = require('Module:Paramtest')
local infobox = require('Module:Infobox')
local cleanimg = require('Module:Clean image').clean

-- Main function called with invokes
function p.main(frame)
	local args = frame:getParent().args
	local ret = infobox.new(args)

	ret:defineParams{
		{ name = 'name', func = 'name' },
		{ name = 'aka', func = 'has_content' },
		{ name = 'image', func = imgarg },
		{ name = 'release', func = 'release' },
		{ name = 'removal', func = 'removal' },
		{ name = 'kingdom', func = 'has_content' },
		{ name = 'sea', func = 'has_content' },
		{ name = 'members', func = 'has_content' },
		{ name = 'floors', func = 'has_content' },
		{ name = 'music', func = 'has_content' },
		{ name = 'leader', func = 'has_content' },
		{ name = 'race', func = 'has_content' },
		{ name = 'tele', func = 'has_content' },
		{ name = 'bank', func = featurearg },
		{ name = 'altar', func = featurearg },
		{ name = 'obelisk', func = featurearg },
		{ name = 'map', func = { name = maparg, params = { 'map' }, flag = { 'd' } } },
		{ name = 'intmap', func = { name = intmaparg, params = { 'intmap' }, flag = { 'd' } } },
		{ name = 'mapdisp', func = { name = mapdisp, params = { 'map', 'intmap' }, flag = { 'd', 'd' } }, dupes = true },
	}
	
	ret:useSMWSubobject({
		members = 'Is members only'
    })
    
    ret:create()
	ret:cleanParams()
	
	ret:defineLinks({ links = {{ 'Template:%s/doc', 'doc' },
		{ 'Template_talk:%s', 'Talk page' }}, colspan = '2' })

	ret:defineName('Infobox Area')
	ret:addButtonsCaption()

	ret:addRow{
		{ tag = 'argd', content = 'image', class='infobox-image', colspan = '2' }
	}
	ret:addRow{
		{ tag = 'argh', content = 'name', class='infobox-header',  colspan = '2' }
	}
	
	-- PARAMETER: aka
	if ret:paramDefined('aka') then
		ret:addRow{
			{ tag = 'th', content = 'Also called' },
			{ tag = 'argd', content = 'aka' }
		}
	end
	
	-- PARAMETER: release
	ret:addRow{
		{ tag = 'th', content = 'Release' },
		{ tag = 'argd', content = 'release' }
	}
	-- PARAMETER: removal
	if ret:paramDefined('removal') then
		ret:addRow{ { tag = 'th', content = 'Removal' },
				{ tag = 'argd', content = 'removal' } }
	end
	
	-- PARAMETER: leader
	if ret:paramDefined('leader') then
		ret:addRow{
			{ tag = 'th', content = 'Leader' },
			{ tag = 'argd', content = 'leader' }
		}
	end
	
	-- PARAMETER: kingdom
	ret:addRow{
		{ tag = 'th', content = 'Kingdom' },
		{ tag = 'argd', content = 'kingdom' }
	}
	
	-- PARAMETER: sea
	if ret:paramDefined('sea') then
		ret:addRow{
			{ tag = 'th', content = 'Sea' },
			{ tag = 'argd', content = 'sea' }
		}
	end
	
	-- PARAMETER: members
	ret:addRow{
		{ tag = 'th', content = '[[Members]]' },
		{ tag = 'argd', content = 'members' }
	}
	
	-- PARAMETER: floors
	if ret:paramDefined('floors') then
		ret:addRow{
			{ tag = 'th', content = 'Floors' },
			{ tag = 'argd', content = 'floors' }
		}
	end
	
	-- PARAMETER: music
	ret:addRow{
		{ tag = 'th', content = 'Music' },
		{ tag = 'argd', content = 'music' }
	}
	
	-- PARAMETER: race
	if ret:paramDefined('race') then
		ret:addRow{
			{ tag = 'th', content = 'Inhabitants' },
			{ tag = 'argd', content = 'race' }
		}
	end
	
	-- PARAMETER: tele
	ret:addRow{
		{ tag = 'th', content = 'Teleports' },
		{ tag = 'argd', content = 'tele' }
	}
	
	-- Features
	ret:addRow{
		{ tag = 'th', content = 'Features', class='infobox-subheader',  colspan = '2' }
	}
	
	-- PARAMETER: bank
	ret:addRow{
		{ tag = 'th', content = '[[Bank]]s [[File:Bank map icon.png|15px|link=Bank]]' },
		{ tag = 'argd', content = 'bank' }
	}
	
	-- PARAMETER: altar
	ret:addRow{
		{ tag = 'th', content = '[[Altar]]s [[File:Altar map icon.png|15px|link=Altar]]' },
		{ tag = 'argd', content = 'altar' }
	}
	
	-- PARAMETER: obelisk
	ret:addRow{
		{ tag = 'th', content = '[[Obelisk]]s [[File:Small Summoning obelisk map icon.png|15px|link=Small obelisk]]' },
		{ tag = 'argd', content = 'obelisk' }
	}

	-- PARAMETER: map
	if ret:paramGrep('mapdisp', function(x) return x == true end) then
		local mapcontent
		if ret:paramDefined('map') then
			mapcontent = 'map'
		elseif ret:paramDefined('intmap') then
			mapcontent = 'intmap'
		end
		ret:addRow{
			{ tag = 'argd', content = mapcontent, class = 'infobox-map', colspan = '2' } }
	end

	ret:finish()
	
	if onmain() then
		local a1 = ret:param('all')
		local a2 = ret:categoryData()
		ret:wikitext(addcategories(a1, a2))
	end
	return ret:tostring()
end

function maparg(arg)
	if infobox.isDefined(arg) then
		local low = string.lower(arg)
		if yesno(low) == false then
			return nil
		elseif low == 'name' then
			return string.format('[[File:%s location.png]]', mw.title.getCurrentTitle().text)
		elseif string.find(low, '.png') then
			return cleanimg{ file = arg, width = 250 }
		else
			return arg
		end
	else
		return nil
	end
end

function intmaparg(arg)
	if infobox.isDefined(arg) then
		if yesno(arg) == false then
			return nil
		else
			return mw.getCurrentFrame():preprocess( arg )
		end
	else
		return nil
	end
end

function mapdisp(map, intmap)
	if infobox.isDefined(intmap) or infobox.isDefined(map) then
		if ( intmap == 'None' ) and ( map == 'None' ) then
			return false
		end
		return true
	end
	return nil
end

function featurearg(arg)
	if not infobox.isDefined(arg) then
		return '-'
	end

	if  infobox.isDefined(arg) then
		return arg
	end

	return 'N/A'
end

function imgarg(arg)
	if infobox.isDefined(arg) then
		return cleanimg{ file = arg, width = 300, height = 300 }
	end
	return nil
end

function addcategories(args, catargs)
	local ret = { 'Locations' }

	-- Add the associated category if the parameter doesn't have content
	local notdefined_args = {
		image = 'Needs image',
		release = 'Needs release date',
		mapdisp = 'Needs map',
	}
	for n, v in pairs(notdefined_args) do
		if catargs[n] and catargs[n].all_defined == false then
			table.insert(ret, v)
		end
	end

	-- combine table and format category wikicode
	for i, v in ipairs(ret) do
		if (v ~= '') then
			ret[i] = string.format('[[Category:%s]]', v)
		end
	end

	return table.concat(ret, '')
end

return p