Module:Infobox military conflict: Difference between revisions
Appearance
Content deleted Content added
Jackmcbarn (talk | contribs) rework border handling |
Jackmcbarn (talk | contribs) allow changing the width, and other fixes from sandbox |
||
Line 1: | Line 1: | ||
require('Module:No globals') |
require('Module:No globals') |
||
local infoboxStyle = mw.loadData('Module:WPMILHIST Infobox style') |
|||
local IMC = {} |
local IMC = {} |
||
Line 24: | Line 26: | ||
if headerText and (colspans[1] or jointText) then |
if headerText and (colspans[1] or jointText) then |
||
builder:tag('tr') |
builder:tag('tr') |
||
:tag('th') |
|||
⚫ | |||
self.combatants |
:attr('colspan', self.combatants) |
||
:cssText(infoboxStyle.header_raw) |
|||
self.header_bar, |
|||
headerText |
:wikitext(headerText) |
||
)) |
|||
end |
end |
||
Line 47: | Line 48: | ||
:css('width', math.floor(100 / self.combatants * colspans[i] + 0.5) .. '%') |
:css('width', math.floor(100 / self.combatants * colspans[i] + 0.5) .. '%') |
||
-- no border on the right of the rightmost column |
-- no border on the right of the rightmost column |
||
:css('border-right', i ~= lastCombatant and |
:css('border-right', i ~= lastCombatant and infoboxStyle.internal_border or nil) |
||
-- no padding on the left of the leftmost column |
-- no padding on the left of the leftmost column |
||
:css('padding-left', i ~= 1 and '0.25em' or nil) |
:css('padding-left', i ~= 1 and '0.25em' or nil) |
||
-- don't show the border if we're directly under a header |
-- don't show the border if we're directly under a header |
||
:css('border-top', not headerText and |
:css('border-top', not headerText and infoboxStyle.internal_border or nil) |
||
:wikitext(self.args[prefix .. i .. suffix]) |
:wikitext(self.args[prefix .. i .. suffix]) |
||
end |
end |
||
Line 63: | Line 64: | ||
:css('text-align', 'center') |
:css('text-align', 'center') |
||
-- don't show the border if we're directly under a header |
-- don't show the border if we're directly under a header |
||
:css('border-top', (not headerText or colspans[1]) and |
:css('border-top', (not headerText or colspans[1]) and infoboxStyle.internal_border or nil) |
||
:wikitext(jointText) |
:wikitext(jointText) |
||
end |
end |
||
Line 132: | Line 133: | ||
:tag('tr'):tag('td') |
:tag('tr'):tag('td') |
||
end |
end |
||
builder = builder:tag('table') |
|||
builder:wikitext('<table ' .. self.frame:expandTemplate{title = 'WPMILHIST Infobox style', args = {'main_box', 'vevent'}} .. '>') |
|||
:addClass('infobox vevent') |
|||
:cssText(infoboxStyle.main_box_raw_auto_width) |
|||
:css('width', self.args.width or '315px') |
|||
builder:tag('tr') |
builder:tag('tr') |
||
:tag('th') |
|||
:wikitext(string.format('<th class="summary" colspan="%d" %s>%s</th>', |
|||
:addClass('summary') |
|||
self.combatants, |
|||
self. |
:attr('colspan', self.combatants) |
||
:cssText(infoboxStyle.header_raw) |
|||
self.args.conflict or mw.title.getCurrentTitle().text |
:wikitext(self.args.conflict or mw.title.getCurrentTitle().text) |
||
)) |
|||
if self.args.partof then |
if self.args.partof then |
||
builder:tag('tr') |
builder:tag('tr') |
||
:tag('td') |
|||
:wikitext(string.format('<td colspan="%d" %s>Part of %s</td>', |
|||
self.combatants |
:attr('colspan', self.combatants) |
||
:cssText(infoboxStyle.sub_header_raw) |
|||
self.frame:expandTemplate{title = 'WPMILHIST Infobox style', args = {'sub_header_bar'}}, |
|||
self.args.partof |
:wikitext('Part of ' .. self.args.partof) |
||
)) |
|||
end |
end |
||
if self.args.image then |
if self.args.image then |
||
builder:tag('tr') |
builder:tag('tr') |
||
:tag('td') |
|||
:wikitext(string.format('<td colspan="%d" %s>%s%s%s</td>', |
|||
self.combatants |
:attr('colspan', self.combatants) |
||
:cssText(infoboxStyle.image_box_raw) |
|||
self.frame:expandTemplate{title = 'WPMILHIST Infobox style', args = {'image_box'}}, |
|||
⚫ | |||
require('Module:InfoboxImage').InfoboxImage{args = { |
require('Module:InfoboxImage').InfoboxImage{args = { |
||
⚫ | |||
image = self.args.image, |
|||
size = self.args.image_size, |
|||
sizedefault = 'frameless', |
|||
upright = 1, |
|||
⚫ | |||
}}, |
|||
self.args.caption and '<br />' or '', |
}}, |
||
self.args.caption and '<br />' or '', |
|||
self.args.caption or '' |
self.args.caption or '' |
||
)) |
)) |
||
end |
end |
||
self:renderHeaderTable(builder:tag('tr'):tag('td'):attr('colspan', self.combatants)) |
self:renderHeaderTable(builder:tag('tr'):tag('td'):attr('colspan', self.combatants)) |
||
Line 179: | Line 184: | ||
:attr('colspan', self.combatants) |
:attr('colspan', self.combatants) |
||
:css('font-size', '90%') |
:css('font-size', '90%') |
||
:css('border-top', |
:css('border-top', infoboxStyle.section_border) |
||
:wikitext(self.args.notes) |
:wikitext(self.args.notes) |
||
end |
end |
||
Line 186: | Line 191: | ||
:tag('td') |
:tag('td') |
||
:attr('colspan', self.combatants) |
:attr('colspan', self.combatants) |
||
:css('border-top', |
:css('border-top', infoboxStyle.internal_border) |
||
:tag('center') |
:tag('center') |
||
:node(require('Module:Location map').main(self.frame, { |
:node(require('Module:Location map').main(self.frame, { |
||
Line 202: | Line 207: | ||
})) |
})) |
||
end |
end |
||
builder: |
builder = builder:done() |
||
if self.args.campaignbox then |
if self.args.campaignbox then |
||
builder = builder:done():done():tag('tr') |
builder = builder:done():done():tag('tr') |
||
Line 219: | Line 224: | ||
local obj = { |
local obj = { |
||
frame = frame, |
frame = frame, |
||
args = args |
args = args |
||
header_bar = frame:expandTemplate{title = 'WPMILHIST Infobox style', args = {'header_bar'}}, |
|||
internal_border = frame:expandTemplate{title = 'WPMILHIST Infobox style', args = {'internal_border'}} |
|||
} |
} |
||
Revision as of 15:41, 7 October 2014
![]() | This module depends on the following other modules: |
![]() | This module uses TemplateStyles: |
Implements {{Infobox military conflict}}
require('Module:No globals')
local infoboxStyle = mw.loadData('Module:WPMILHIST Infobox style')
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')
: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)
: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)
: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')
: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('span')
:addClass('location')
:wikitext(self.args.place or '{{{place}}}') -- hack so that people who don't know Lua know that this parameter is required
:done()
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')
:tag('th')
:css('padding-right', '1em')
:wikitext(self.args.status and 'Status' or 'Result')
:done()
:tag('td')
:wikitext(self.args.status or self.args.result)
end
if self.args.territory then
builder:tag('tr')
:tag('th')
:css('padding-right', '1em')
:wikitext('Territorial<br />changes')
: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('background', 'transparent')
:css('margin', 0)
:css('padding', 0)
:tag('tr'):tag('td')
end
builder = builder:tag('table')
:addClass('infobox vevent')
:cssText(infoboxStyle.main_box_raw_auto_width)
:css('width', self.args.width or '315px')
builder:tag('tr')
:tag('th')
:addClass('summary')
:attr('colspan', self.combatants)
:cssText(infoboxStyle.header_raw)
:wikitext(self.args.conflict or mw.title.getCurrentTitle().text)
if self.args.partof then
builder:tag('tr')
:tag('td')
:attr('colspan', self.combatants)
:cssText(infoboxStyle.sub_header_raw)
:wikitext('Part of ' .. self.args.partof)
end
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,
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'):tag('td'):attr('colspan', self.combatants))
self:renderPerCombatant(builder, self.args.combatants_header or '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, 'Units involved', 'units')
self:renderPerCombatant(builder, 'Strength', 'strength')
self:renderPerCombatant(builder, 'Casualties and losses', 'casualties')
if self.args.notes then
builder:tag('tr')
:tag('td')
:attr('colspan', self.combatants)
:css('font-size', '90%')
:css('border-top', infoboxStyle.section_border)
:wikitext(self.args.notes)
end
if self.args.map_type then
builder:tag('tr')
:tag('td')
:attr('colspan', self.combatants)
:css('border-top', infoboxStyle.internal_border)
:tag('center')
:node(require('Module:Location map').main(self.frame, {
self.args.map_type,
relief = self.args.map_relief,
lat = self.args.latitude,
long = self.args.longitude,
width = self.args.map_size or 220,
float = 'center',
border = 'none',
marksize = self.args.map_marksize or 8,
label = self.args.map_label,
alt = self.args.map_alt,
caption = self.args.map_caption or ('Location within ' .. self.args.map_type)
}))
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('Module: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,4 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