Modul:Infokutija vojni sukob

Dokumentaciju za ovaj modul možete napraviti na stranici Modul:Infokutija vojni sukob/dok

require('Modul:No globals')

local infoboxStyle = mw.loadData('Modul:WPMILHIST Infobox style')
local templatestyles = 'Modul:Infokutija vojni sukob/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')
				: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)
		:css('display', 'inline-table')

	if self.args.datum then
		builder:tag('tr')
			:tag('th')
				:css('padding-right', '1em')
				:wikitext('Datum')
			:done()
			:tag('td')
				:wikitext(self.args.datum)
	end

	builder = builder:tag('tr')
		:tag('th')
			:css('padding-right', '1em')
			:wikitext('Lokacija')
		:done()
		:tag('td')
			:tag('div')
				:addClass('location')
				:wikitext(self.args.mjesto or '{{{mjesto}}}') -- hack so that people who don't know Lua know that this parameter is required
			:done()
	if self.args.koordinate then
		builder:wikitext(self.args.koordinate)
	end
	builder = builder:done():done()

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

	if self.args.status or self.args.rezultat then
		builder:tag('tr')
			:tag('th')
				:css('padding-right', '1em')
				:wikitext(self.args.status and 'Status' or 'Rezultat')
			:done()
			:tag('td')
				:addClass('status')
				:newline()
				:wikitext(self.args.status or self.args.rezultat)
	end

	if self.args.teritorija or self.args.posjed then
		builder:tag('tr')
			:tag('th')
				:css('padding-right', '1em')
				:wikitext('Teritorijalne<br />promjene')
			:done()
			:tag('td')
				:newline()
				:wikitext(self.args.teritorija or self.args.posjed)
	end
end

function IMC:render()
	local builder = mw.html.create()
	if self.args.campaignbox then
		-- 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.sirina or nil)

	builder:tag('tr')
		:tag('th')
			:addClass('summary')
			:attr('colspan', self.combatants)
			:cssText(infoboxStyle.header_raw)
			:wikitext(self.args.sukob or mw.title.getCurrentTitle().text)
	if self.args.dio then
		builder:tag('tr')
			:tag('td')
				:attr('colspan', self.combatants)
				:cssText(infoboxStyle.sub_header_raw)
				:wikitext('Dio ' .. self.args.dio)
	end
	if self.args.slika then
		builder:tag('tr')
			:tag('td')
				:attr('colspan', self.combatants)
				:cssText(infoboxStyle.image_box_raw)
				:wikitext(string.format('%s%s%s',
					require('Modul:InfoboxImage').InfoboxImage{args = {
						image = self.args.slika,
						size = self.args.velicina_slike,
						sizedefault = 'frameless',
						upright = 1,
						alt = self.args.alt
					}},
					self.args.opis_slike and '<br />' or '',
					self.args.opis_slike or ''
				))
	end
	self:renderHeaderTable(builder:tag('tr'):tag('td'):attr('colspan', self.combatants))
	self:renderPerCombatant(builder, self.args.strane_zaglavlje or 'Sukobljene strane', 'strana')
	-- can be un-hardcoded once gerrit:165108 is merged
	for _,v in ipairs{'a','b','c','d'} do
		self:renderPerCombatant(builder, nil, 'strana', v)
	end
	
	self:renderPerCombatant(builder, self.args.komandanti_zaglavlje or 'Komandanti i lideri', 'komandant')
	for _,v in ipairs{'a','b','c','d'} do
		self:renderPerCombatant(builder, nil, 'komandant', v)
	end
	
	self:renderPerCombatant(builder, self.args.units_header or 'Uključene jedinice', 'jedinice')
	self:renderPerCombatant(builder, self.args.strengths_header or 'Snage', 'snage')
	self:renderPerCombatant(builder, self.args.polstrengths_header or 'Politička podrška', 'polsnaga')
	self:renderPerCombatant(builder, self.args.milstrengths_header or 'Vojna podrška', 'vojsnaga')
	self:renderPerCombatant(builder, self.args.casualties_header or 'Žrtve i gubici', 'žrtve')

	if self.args.napomena then
		builder:tag('tr')
			:tag('td')
				:attr('colspan', self.combatants)
				:css('border-top', infoboxStyle.section_border)
				:newline()
				:wikitext(self.args.napomena)
	end
	if self.args.karta_vrsta then
		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.karta_vrsta,
					relief = self.args.karta_reljef,
					coordinates = self.args.koordinate,
					width = self.args.karta_velicina or 220,
					float = 'center',
					border = 'none',
					mark = self.args.karta_znak,
					marksize = self.args.karta_znakvel or 8,
					label = self.args.karta_oznaka,
					alt = self.args.karta_alt,
					caption = self.args.karta_opis or ('Lokacija u ' 
						.. (require('Modul:Location map').data(self.frame, {self.args.karta_vrsta, 'name'})))
				}))
	end
	builder = builder:done()
	if self.args.vojna_kampanja then
		builder = builder:done()
		builder:wikitext(self.args.vojna_kampanja .. self.frame:expandTemplate{ title = 'stack end'})
	end
	return builder
end

function IMC.new(frame, args)
	if not args then
		args = require('Modul:Arguments').getArgs(frame, {wrappers = 'Šablon:Infokutija vojni sukob'})
	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['strana' .. 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