Jump to content

Module:Infobox military conflict

Permanently protected module
From Wikipedia, the free encyclopedia

require('strict')

localinfoboxStyle=mw.loadData('Module:WPMILHIST Infobox style')
localtemplatestyles='Module:Infobox military conflict/styles.css'

localIMC={}
IMC.__index=IMC

functionIMC:renderPerCombatant(builder,headerText,prefix,suffix)
prefix=prefixor''
suffix=suffixor''
localcolspans={}

-- This may result in colspans[1] getting set twice, but
-- this is no big deal. The second set will be correct.
locallastCombatant=1

fori=1,self.combatantsdo
ifself.args[prefix..i..suffix]then
colspans[lastCombatant]=i-lastCombatant
lastCombatant=i
end
end

localjointText=self.args[prefix..(self.combatants+1)..suffix]

ifheaderTextand(colspans[1]orjointText)then
builder:tag('tr')
:tag('th')
: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.
ifcolspans[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')
fori=1,self.combatantsdo
-- 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.
ifcolspans[i]then
builder:tag('td')
-- don't bother emitting colspan= "1"
:attr('colspan',colspans[i]~=1andcolspans[i]ornil)
:css('width',math.floor(100/self.combatants*colspans[i]+0.5)..'%')
-- no border on the right of the rightmost column
:css('border-right',i~=lastCombatantandinfoboxStyle.internal_borderornil)
-- no padding on the left of the leftmost column
:css('padding-left',i~=1and'0.25em'ornil)
-- don't show the border if we're directly under a header
:css('border-top',notheaderTextandinfoboxStyle.internal_borderornil)
:newline()
:wikitext(self.args[prefix..i..suffix])
end
end
end

ifjointTextthen
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',(notheaderTextorcolspans[1])andinfoboxStyle.internal_borderornil)
:newline()
:wikitext(jointText)
end
end

functionIMC:renderHeaderTable(builder)
builder=builder:tag('table')
:css('width','100%')
:css('margin',0)
:css('padding',0)
:css('border',0)
:css('display','inline-table')

ifself.args.datethen
builder:tag('tr')
:tag('th')
:css('padding-right','1em')
:wikitext('Date')
:done()
:tag('td')
:wikitext(self.args.date)
end

builder=builder:tag('tr')
:tag('th')
:css('padding-right','1em')
:wikitext('Location')
:done()
:tag('td')
:tag('div')
:addClass('location')
:wikitext(self.args.placeor'{{{place}}}')-- hack so that people who don't know Lua know that this parameter is required
:done()
ifself.args.coordinatesthen
builder:wikitext(self.args.coordinates)
end
builder=builder:done():done()

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

ifself.args.statusorself.args.resultthen
builder:tag('tr')
:tag('th')
:css('padding-right','1em')
:wikitext(self.args.statusand'Status'or'Result')
:done()
:tag('td')
:addClass('status')
:newline()
:wikitext(self.args.statusorself.args.result)
end

ifself.args.territorythen
builder:tag('tr')
:tag('th')
:css('padding-right','1em')
:wikitext('Territorial<br />changes')
:done()
:tag('td')
:newline()
:wikitext(self.args.territory)
end
end

functionIMC:render()
localbuilder=mw.html.create()
ifself.args.campaignboxthen
-- this should be the same as using {{stack|clear=right|...}}
builder:wikitext(self.frame:expandTemplate{title='stack begin',args={clear='true'}})
end
builder=builder:tag('table')
:addClass('infobox vevent')
:cssText(infoboxStyle.main_box_raw)
:css('width',self.args.widthornil)

builder:tag('tr')
:tag('th')
:addClass('summary')
:attr('colspan',self.combatants)
:cssText(infoboxStyle.header_raw)
:wikitext(self.args.conflictormw.title.getCurrentTitle().text)
ifself.args.partofthen
builder:tag('tr')
:tag('td')
:attr('colspan',self.combatants)
:cssText(infoboxStyle.sub_header_raw)
:wikitext('Part of '..self.args.partof)
end
ifself.args.imagethen
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,
size=self.args.image_size,
sizedefault='frameless',
upright=1,
alt=self.args.alt
}},
self.args.captionand'<br />'or'',
self.args.captionor''
))
end
self:renderHeaderTable(builder:tag('tr'):tag('td'):attr('colspan',self.combatants))
self:renderPerCombatant(builder,self.args.combatants_headeror'Belligerents','combatant')
-- can be un-hardcoded once gerrit:165108 is merged
for_,vinipairs{'a','b','c','d'}do
self:renderPerCombatant(builder,nil,'combatant',v)
end

self:renderPerCombatant(builder,self.args.commanders_headeror'Commanders and leaders','commander')
for_,vinipairs{'a','b','c','d'}do
self:renderPerCombatant(builder,nil,'commander',v)
end

self:renderPerCombatant(builder,self.args.units_headeror'Units involved','units')
self:renderPerCombatant(builder,self.args.strengths_headeror'Strength','strength')
self:renderPerCombatant(builder,self.args.polstrengths_headeror'Political support','polstrength')
self:renderPerCombatant(builder,self.args.milstrengths_headeror'Military support','milstrength')
self:renderPerCombatant(builder,self.args.casualties_headeror'Casualties and losses','casualties')

ifself.args.notesthen
builder:tag('tr')
:tag('td')
:attr('colspan',self.combatants)
:css('border-top',infoboxStyle.section_border)
:newline()
:wikitext(self.args.notes)
end
ifself.args.map_typeandself.args.coordinatesthen
builder:tag('tr')
:tag('td')
:attr('colspan',self.combatants)
:css('border-top',infoboxStyle.internal_border)
:node(require('Module:Location map').main(self.frame,{
self.args.map_type,
relief=self.args.map_relief,
coordinates=self.args.coordinates,
width=self.args.map_sizeor220,
float='center',
border='none',
mark=self.args.map_mark,
marksize=self.args.map_marksizeor8,
label=self.args.map_label,
alt=self.args.map_alt,
caption=self.args.map_captionor('Location within '
..(require('Module:Location map').data(self.frame,{self.args.map_type,'name'})))
}))
end
builder=builder:done()
ifself.args.campaignboxthen
builder=builder:done()
builder:wikitext(self.args.campaignbox..self.frame:expandTemplate{title='stack end'})
end
returnbuilder
end

functionIMC.new(frame,args)
ifnotargsthen
args=require('Module:Arguments').getArgs(frame,{wrappers='Template:Infobox military conflict'})
end
localobj={
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_,vinipairs{'','a','b','c','d'}do
fori=1,5do
ifargs['combatant'..i..v]then
obj.combatants=math.max(obj.combatants,i)
end
end
end

returnsetmetatable(obj,IMC)
end

localp={}

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

returnp