Модул:Инфокутија Војни сукоб

С Википедије, слободне енциклопедије
Документација модула[прикажи] [уреди] [историја] [освежи]

Ово је модул којим се покреће Шаблон:Инфокутија Војни сукоб. За детаље погледајте документацију шаблона...

require('strict')

local infoboxStyle = mw.loadData('Модул:WPMILHIST Infobox style')
local templatestyles = 'Модул:Инфокутија Војни сукоб/styles.css'

local IMC = {}
IMC.__index = IMC

function IMC:renderPerStrana(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 lastStrana = 1
	
	for i = 1,self['стране'] do
		if self.args[prefix .. i .. suffix] then
			colspans[lastStrana] = i - lastStrana
			lastStrana = i
		end
	end

	local jointText = self.args[prefix .. (self['стране'] + 1) .. suffix]
	
	if headerText and (colspans[1] or jointText) then
		builder:tag('tr')
			:tag('th')
				:attr('colspan', self['стране'])
				: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[lastStrana] = self['стране'] - lastStrana + 1
		builder = builder:tag('tr')
		for i = 1,self['стране'] 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['стране'] * colspans[i] + 0.5) .. '%')
					-- no border on the right of the rightmost column
					:css('border-right', i ~= lastStrana 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['стране'])
				: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['датум'] or self.args['време'] then
		builder:tag('tr')
			:tag('th')
				:css('padding-right', '1em')
				:wikitext('Време')
			:done()
			:tag('td')
				:wikitext(self.args['датум'] or self.args['време'])
	end

	builder = builder:tag('tr')
		:tag('th')
			:css('padding-right', '1em')
			:wikitext('Место')
		:done()
		:tag('td')
			:tag('div')
				:addClass('location')
				:wikitext(self.args['локација'] or self.args['место'] or '{{{локација}}}') -- hack so that people who don't know Lua know that this parameter is required
			:done()
	if self.args['координате'] then
		builder:wikitext('<br />' .. self.args['координате'])
	end
	builder = builder:done():done()

	-- only for "Putsch"
    if self.args['акција'] then
		builder:tag('tr')
			:tag('th')
				:css('padding-right', '1em')
				:wikitext(self.args['акција'] and 'Акција')
			:done()
			:tag('td')
				:wikitext(self.args['акција'])
	end

	if self.args['повод'] then
		builder:tag('tr')
			:tag('th')
				:css('padding-right', '1em')
				:wikitext('Повод')
			:done()
			:tag('td')
				:wikitext(self.args['повод'])
	end

	if self.args['узрок'] then
		builder:tag('tr')
			:tag('th')
				:css('padding-right', '1em')
				:wikitext('Узрок')
			:done()
			:tag('td')
				:wikitext(self.args['узрок'])
	end

	if self.args['статус'] or self.args['резултат'] then
		builder:tag('tr')
			:tag('th')
				:css('padding-right', '1em')
				:wikitext(self.args['статус'] and 'Статус' or 'Исход')
			:done()
			:tag('td')
				:addClass('status')
				:newline()
				:wikitext(self.args['статус'] or self.args['резултат'])
	end

	if self.args['територија'] then
		builder:tag('tr')
			:tag('th')
				:css('padding-right', '1em')
				:wikitext('Територијалне<br />промене')
			:done()
			:tag('td')
				:newline()
				:wikitext(self.args['територија'])
	end
end

function IMC:render()
	local builder = mw.html.create()
	if self.args['војна_кампања'] 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['ширина'] or nil)

	builder:tag('tr')
		:tag('th')
			:addClass('summary')
			:attr('colspan', self['стране'])
			:cssText(infoboxStyle.header_raw)
			:wikitext(self.args['сукоб'] or self.args['рат'] or self.args['битка'] or self.args['побуна'] or self.args['револуција'] or self.args['државни_удар'] or self.args['пуч'] or self.args['државни_пуч'] or self.args['војни_пуч'] or self.args['народни_пуч'] or self.args['кампања'] or mw.title.getCurrentTitle().text)
	if self.args['део'] then
		builder:tag('tr')
			:tag('td')
				:attr('colspan', self['стране'])
				:cssText(infoboxStyle.sub_header_raw)
				:wikitext('Део ' .. self.args['део'])
	end
	if self.args['слика'] then
		builder:tag('tr')
			:tag('td')
				:attr('colspan', self['стране'])
				:cssText(infoboxStyle.image_box_raw)
				:wikitext(string.format('%s%s%s',
					require('Module:InfoboxImage').InfoboxImage{args = {
						image = self.args['слика'],
						size = self.args['величина_слике'] or self.args['ширина_слике'],
						sizedefault = 'frameless',
						upright = 1,
						alt = self.args['алтернативни_текст']
					}},
					self.args['опис_слике'] and '<br />' or '',
					self.args['опис_слике'] or ''
				))
	end

	self:renderHeaderTable(builder:tag('tr'):tag('td'):attr('colspan', self['стране']))
	self['renderPerStrana'](self, builder:css('font-size', '85%'), self.args['вођство_сукобљених_страна'] or 'Сукобљене стране', 'страна')
	-- can be un-hardcoded once gerrit:165108 is merged
        
	for _,v in ipairs{'а','б','в','г'} do
       
		self['renderPerStrana'](self, builder, nil, 'страна', v)
	end
	
	self['renderPerStrana'](self, builder, 'Команданти и вође', 'командант')
	self['renderPerStrana'](self, builder, 'Команданти и вође', 'заповедник')
	self['renderPerStrana'](self, builder, 'Укључене јединице', 'јединице')
	self['renderPerStrana'](self, builder, 'Јачина', 'јачина')
	self['renderPerStrana'](self, builder, 'Политичка подршка', 'политичка_јачина')
	self['renderPerStrana'](self, builder, 'Војна подршка', 'војна_јачина')
	self['renderPerStrana'](self, builder, 'Жртве и губици', 'губици')

	if self.args['напомене'] then
		builder:tag('tr')
			:tag('td')
				:attr('colspan', self['стране'])
				:css('border-top', infoboxStyle.section_border)
				:newline()
				:wikitext(self.args['напомене'])
	end
	if self.args['тип_мапе'] then
		builder:tag('tr')
			:tag('td')
				:attr('colspan', self['стране'])
				:css('border-top', infoboxStyle.internal_border)
				:node(require('Module:Location map').main(self.frame, {
						self.args['тип_мапе'],
						relief = self.args['рељеф_мапе'],
						lat = self.args['географска_ширина'],
						long = self.args['географска_дужина'],
						width = self.args['величина_мапе'] or 220,
						float = 'center',
						border = 'none',
						mark = self.args['ознака_мапе'],
						marksize = self.args['величина_ознаке'] or 8,
						label = self.args['текст_ознаке'],
						alt = self.args['алтернативни_текст_мапе'],
						caption = self.args['опис_мапе'] or ('Локација сукоба на мапи ' .. self.args['локација_генитив'])
					}))
	end
	builder = builder:done()
	if self.args['војна_кампања'] then
		builder = builder:done():done():tag('tr')
			:tag('td')
				:wikitext(self.args['војна_кампања'])
			:done()
		:done()
	end
	return builder
end

function IMC.new(frame, args)
	if not args then
		args = require('Module:Arguments').getArgs(frame, {wrappers = 'Template:Инфокутија Војни сукоб'})
	end
	local obj = {
		frame = frame,
		args = args
	}

	-- док gerrit:165108 не буде укључен, постојаће cap-ови на странама, али чим се укључи, може се апдејтовати ово мало кода да би се стране uncap-овале
	-- такође, не покушавајте ово учинити ефикаснијим или би у противном референце могле да буду поређане како не треба
	obj['стране'] = 2
	for _,v in ipairs{'', 'a', 'b', 'c', 'd'} do
		for i = 1,5 do
			if args['страна' .. i .. v] then
				obj['стране'] = math.max(obj['стране'], 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