Module:Infobox Conflict: Difference between revisions

From Pactoria Wiki
Jump to navigation Jump to search
m Replaced 'conflict' label with 'title' and removed 'width' label
mNo edit summary
 
(One intermediate revision by the same user not shown)
Line 90: Line 90:
:tag('td')
:tag('td')
:wikitext(self.args.date)
:wikitext(self.args.date)
    :css('text-align', 'left')
    :css('vertical-align', 'top')
end
end


Line 100: Line 102:
:tag('div')
:tag('div')
:addClass('location')
:addClass('location')
:wikitext(self.args.place or '{{{place}}}') -- hack so that people who don't know Lua know that this parameter is required
:wikitext(self.args.location or '{{{location}}}') -- hack so that people who don't know Lua know that this parameter is required
    :css('text-align', 'left')
    :css('vertical-align', 'top')
:done()
:done()
builder = builder:done():done()
builder = builder:done():done()
Line 124: Line 128:
:newline()
:newline()
:wikitext(self.args.status or self.args.result)
:wikitext(self.args.status or self.args.result)
:css('text-align', 'left')
:css('vertical-align', 'top')
end
end


Line 136: Line 142:
:newline()
:newline()
:wikitext(self.args.territory)
:wikitext(self.args.territory)
:css('text-align', 'left')
    :css('vertical-align', 'top')
end
end
end
end
Line 185: Line 193:
self:renderPerCombatant(builder, 'Commanders and leaders', 'commander')
self:renderPerCombatant(builder, 'Commanders and leaders', 'commander')
self:renderPerCombatant(builder, 'Units involved', 'units')
self:renderPerCombatant(builder, 'Strength', 'strength')
self:renderPerCombatant(builder, 'Strength', 'strength')
self:renderPerCombatant(builder, 'Political support', 'polstrength')
self:renderPerCombatant(builder, 'Political support', 'polstrength')

Latest revision as of 22:23, 21 March 2026

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

require('Module:No globals')

local infoboxStyle = mw.loadData('Module:WPMILHIST Infobox style')
local templatestyles = 'Module:Infobox Conflict/styles.css'

local IMC = {}
IMC.__index = IMC

function IMC:renderPerCombatant(builder, headerText, prefix, suffix)
	prefix = prefix or ''
	suffix = suffix or ''
	local colspans = {}
	
	-- This may result in colspans[1] getting set twice, but
	-- this is no big deal. The second set will be correct.
	local lastCombatant = 1
	
	for i = 1,self.combatants do
		if self.args[prefix .. i .. suffix] then
			colspans[lastCombatant] = i - lastCombatant
			lastCombatant = i
		end
	end

	local jointText = self.args[prefix .. (self.combatants + 1) .. suffix]
	
	if headerText and (colspans[1] or jointText) then
		builder:tag('tr')
			:tag('th')
				:addClass('infobox-header') -- uses infobox-header from MediaWiki:Common.css
				:attr('colspan', self.combatants)
				:cssText(infoboxStyle.header_raw)
				:wikitext(headerText)
	end

	-- The only time colspans[1] wouldn't be set is if no
	-- combatant has a field with the given prefix and suffix.
	if colspans[1] then
		-- Since each found argument set the colspan for the previous
		-- one, the final one wasn't set above, so set it now.
		colspans[lastCombatant] = self.combatants - lastCombatant + 1
		builder = builder:tag('tr')
		for i = 1,self.combatants do
			-- At this point, colspans[i] will be set for i=1 unconditionally, and for
			-- any other value of i where self.args[prefix .. i .. suffix] is set.
			if colspans[i] then
				builder:tag('td')
					-- don't bother emitting colspan="1"
					:attr('colspan', colspans[i] ~= 1 and colspans[i] or nil)
					:css('width', math.floor(100 / self.combatants * colspans[i] + 0.5) .. '%')
					-- no border on the right of the rightmost column
					:css('border-right', i ~= lastCombatant and infoboxStyle.internal_border or nil)
					-- no padding on the left of the leftmost column
					:css('padding-left', i ~= 1 and '0.25em' or nil)
					-- don't show the border if we're directly under a header
					:css('border-top', not headerText and infoboxStyle.internal_border or nil)
					:newline()
					:wikitext(self.args[prefix .. i .. suffix])
			end
		end
	end

	if jointText then
		builder:tag('tr')
			:tag('td')
				:attr('colspan', self.combatants)
				:css('text-align', 'center')
				-- don't show the border if we're directly under a header
				:css('border-top', (not headerText or colspans[1]) and infoboxStyle.internal_border or nil)
				:newline()
				:wikitext(jointText)
	end
end

function IMC:renderHeaderTable(builder)
	builder = builder:tag('table')
		:css('width', '100%')
		:css('margin', 0)
		:css('padding', 0)
		:css('border', 0)

	if self.args.date then
		builder:tag('tr')
			:tag('th')
				:css('padding-right', '1em')
				:css('text-align', 'left')
				:css('vertical-align', 'top')
				:wikitext('Date')
			:done()
			:tag('td')
				:wikitext(self.args.date)
    			:css('text-align', 'left')
    			:css('vertical-align', 'top')
	end

	builder = builder:tag('tr')
		:tag('th')
			:css('padding-right', '1em')
			:wikitext('Location')
		:done()
		:tag('td')
			:tag('div')
				:addClass('location')
				:wikitext(self.args.location or '{{{location}}}') -- hack so that people who don't know Lua know that this parameter is required
    			:css('text-align', 'left')
    			:css('vertical-align', 'top')
			:done()
	builder = builder:done():done()

	-- only for "Putsch"
    if self.args.action then
		builder:tag('tr')
			:tag('th')
				:css('padding-right', '1em')
				:wikitext(self.args.action and 'Action')
			:done()
			:tag('td')
				:wikitext(self.args.action)
	end

	if self.args.status or self.args.result then
		builder:tag('tr')
			:tag('th')
				:css('padding-right', '1em')
				:wikitext(self.args.status and 'Status' or 'Result')
			:done()
			:tag('td')
				:newline()
				:wikitext(self.args.status or self.args.result)
				:css('text-align', 'left')
				:css('vertical-align', 'top')
	end

	if self.args.territory then
		builder:tag('tr')
			:tag('th')
				:css('padding-right', '1em')
				:css('text-align', 'left')
				:wikitext('Territorial<br />changes')
			:done()
			:tag('td')
				:newline()
				:wikitext(self.args.territory)
				:css('text-align', 'left')
    			:css('vertical-align', 'top')
	end
end

function IMC:render()
	local builder = mw.html.create()
-- if self.args.campaignbox then
--     builder = builder:tag('div')
--         :addClass('mw-stack desktop-float-right')
--         :tag('div')
--             :css('overflow', 'hidden')
--             :css('margin', '1px')
-- end
	builder = builder:tag('table')
    	:addClass('infobox')
    	:css('width', '22em')   -- set a default width
    	:css('table-layout', 'fixed')             -- ensures columns respect width
		:css('text-align', 'left')

	builder:tag('tr')
		:tag('th')
    	:addClass('infobox-header')  -- uses MediaWiki:Common.css CSS
    	:cssText(infoboxStyle.header_raw .. '; text-align: center;')
    	:attr('colspan', self.combatants)
    	:wikitext(self.args.title or mw.title.getCurrentTitle().text)
	if self.args.image then
		builder:tag('tr')
			:tag('td')
				:attr('colspan', self.combatants)
				:cssText(infoboxStyle.image_box_raw)
				:wikitext(string.format('%s%s%s',
					require('Module:InfoboxImage').InfoboxImage{args = {
        				image = self.args.image,
        				sizedefault = 'frameless',
        				alt = self.args.alt,
        				style = 'max-height:200px; width:auto;'   -- constrain vertical size
    }
},
					self.args.caption and '<br />' or '',
					self.args.caption and "''" .. self.args.caption .. "''" or ''
				))
	end
	self:renderHeaderTable(builder:tag('tr'):tag('td'):attr('colspan', self.combatants))
	self:renderPerCombatant(builder, 'Belligerents', 'combatant')
	-- can be un-hardcoded once gerrit:165108 is merged
	for _,v in ipairs{'a','b','c','d'} do
		self:renderPerCombatant(builder, nil, 'combatant', v)
	end
	
	self:renderPerCombatant(builder, 'Commanders and leaders', 'commander')
	self:renderPerCombatant(builder, 'Strength', 'strength')
	self:renderPerCombatant(builder, 'Political support', 'polstrength')
	self:renderPerCombatant(builder, 'Military support', 'milstrength')
	self:renderPerCombatant(builder, 'Casualties and losses', 'casualties')

	builder = builder:done()
-- if self.args.campaignbox then
--     builder = builder:done()
--         :tag('div')
--             :css('overflow', 'hidden')
--             :css('margin', '1px')
--             :wikitext(self.args.campaignbox)
--         :done()
--     :done()
-- end
	return builder
end

function IMC.new(frame, args)
	if not args then
		args = require('Module:Arguments').getArgs(frame, {wrappers = 'Template:Infobox Conflict'})
	end
	local obj = {
		frame = frame,
		args = args
	}

	-- until gerrit:165108 is merged, there's still a cap on combatants, but as soon as it merges, we can update this little bit of code to uncap it
	-- also, don't try to make this more efficient, or references could be in the wrong order
	obj.combatants = 2
	for _,v in ipairs{'', 'a', 'b', 'c', 'd'} do
		for i = 1,5 do
			if args['combatant' .. i .. v] then
				obj.combatants = math.max(obj.combatants, i)
			end
		end
	end

	return setmetatable(obj, IMC)
end

local p = {}

function p.main(frame)
	return frame:extensionTag{ name = 'templatestyles', args = { src = templatestyles} } .. tostring(IMC.new(frame):render())
end

return p