Modul:Infobox military conflict
Utseende
Denne modulen håndterer infobokser om militære konflikter og slag. Se malen {{Infoboks slag}} for informasjon om argumenter.
require('strict')
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')
:addClass('overskrift')
:tag('td')
:addClass('center')
:attr('colspan', self.combatants)
: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')
builder:addClass('rad')
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)
-- border on the right for all but the first column
:addClass(i ~= 1 and 'dotleft' or nil)
:css('width', math.floor(100 / self.combatants * colspans[i] + 0.5) .. '%')
:attr('width', math.floor(100 / self.combatants * colspans[i] + 0.5) .. '%')
: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')
: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')
:addClass('rad')
:tag('td')
:addClass('th')
:css('width', '25%')
:wikitext('Dato')
:done()
:tag('td')
:wikitext(self.args.date)
end
if self.args.start then
builder:tag('tr')
:addClass('rad')
:tag('td')
:addClass('th')
:css('width', '25%')
:wikitext('Start')
:done()
:tag('td')
:wikitext(self.args.date)
end
builder = builder:tag('tr')
:addClass('rad')
:tag('td')
:addClass('th')
:css('width', '25%')
:wikitext('Sted')
:done()
:tag('td')
:wikitext(self.args.place or '{{{sted}}}') -- hack so that people who don't know Lua know that this parameter is required
if self.args.coordinates then
builder:wikitext('<br />' .. self.args.coordinates)
end
builder = builder:done():done()
if self.args.status or self.args.result then
builder:tag('tr')
:addClass('rad')
:tag('td')
:addClass('th')
:css('width', '25%')
:wikitext(self.args.status and 'Status' or 'Resultat')
:done()
:tag('td')
:wikitext(self.args.status or self.args.result)
end
if self.args.casus then
builder:tag('tr')
:addClass('rad')
:tag('td')
:addClass('th')
:css('width', '25%')
:wikitext('[[Casus belli]]')
:done()
:tag('td')
:wikitext(self.args.casus)
end
if self.args.territory then
builder:tag('tr')
:addClass('rad')
:tag('td')
:addClass('th')
:css('width', '25%')
:wikitext('Territoriale<br />endringer')
:done()
:tag('td')
:wikitext(self.args.territory)
end
end
function IMC:render()
local builder = mw.html.create()
if self.args.campaignbox then
builder = builder:tag('table')
:css('float', 'right')
:css('clear', 'right')
:css('margin', 0)
:css('padding', 0)
:tag('tr'):tag('td')
end
builder = builder:tag('table')
:addClass('infobox infoboks bred')
:css('width', self.args.width or '315px')
builder:tag('tr')
:addClass('hode overskrift')
:tag('th')
:attr('colspan', self.combatants)
:wikitext(self.args.conflict or mw.title.getCurrentTitle().text)
if self.args.partof then
builder:tag('tr')
:addClass('hode')
:tag('th')
:attr('colspan', self.combatants)
:wikitext('Konflikt: ' .. self.args.partof)
end
if self.args.image then
builder:tag('tr')
:addClass('bilde')
:tag('td')
:addClass('sentrert')
:attr('colspan', self.combatants)
:wikitext(string.format('%s%s%s',
require('Modul:InfoboxImage').InfoboxImage{args = {
image = self.args.image,
size = self.args.image_size,
sizedefault = 'frameless',
upright = 1,
alt = self.args.alt
}},
self.args.caption and '<br />' or '',
self.args.caption or ''
))
end
self:renderHeaderTable(builder:tag('tr'):addClass('rad'):tag('td'):attr('colspan', self.combatants))
self:renderPerCombatant(builder, self.args.combatants_header or 'Stridende parter', '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, 'Kommandanter og ledere', 'commander')
self:renderPerCombatant(builder, 'Enheter involvert', 'units')
self:renderPerCombatant(builder, 'Styrker', 'strength')
self:renderPerCombatant(builder, 'Tap', 'casualties')
if self.args.notes then
builder:tag('tr')
:addClass('rad')
:tag('td')
:attr('colspan', self.combatants)
:css('font-size', '90%')
:wikitext(self.args.notes)
end
if self.args.map_type then
builder:tag('tr')
:addClass('bilde')
:tag('td')
:addClass('sentrert')
:attr('colspan', self.combatants)
:node(require('Modul:Location map').main(self.frame, {
self.args.map_type,
relieff = self.args.map_relief,
lat = self.args.latitude,
long = self.args.longitude,
bredde = self.args.map_size or 220,
float = 'center',
ramme = 'none',
merkestr = self.args.map_marksize or 8,
stedsnavn = self.args.map_label,
alt = self.args.map_alt,
undertekst = self.args.map_caption or nil
}))
elseif self.args.map then
builder:tag('tr')
:addClass('bilde')
:tag('td')
:addClass('sentrert')
:attr('colspan', self.combatants)
:wikitext(string.format('%s%s%s',
require('Modul:InfoboxImage').InfoboxImage{args = {
image = self.args.map,
size = self.args.map_size or 220,
sizedefault = 'frameless',
upright = 1,
alt = self.args.map_alt
}},
self.args.map_caption and '<br />' or '',
self.args.map_caption or ''
))
end
builder = builder:done()
if self.args.campaignbox then
builder = builder:done():done():tag('tr')
:tag('td')
:wikitext(self.args.campaignbox)
:done()
:done()
end
return builder
end
function IMC.new(frame, args)
if not args then
args = require('Modul:Arguments').getArgs(frame, {wrappers = 'Template:Infobox military 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 IMC.new(frame):render()
end
return p