Mobius Final Fantasy Wiki
Advertisement

local Infobox = {}

-- Main function
function Infobox.build( html, css, cols )
	style, COLS = css, cols
	local t = mw.html.create( 'table' )
	t	:css( style['table'] )
		:attr( { cellspacing = "0", cellpadding = "0" } )

	for i, v in ipairs( html ) do
		-- Add a border-bottom exept for the last row
		v.bbottom = v.bbottom or true
		if i == #html then
			bbottom = false
			-- Add border radius
			if v[1].css then table.insert( v[1].css, style.blradius )
			else v[1].css = style.blradius
			end
			if #v == 1 then table.insert( v[1].css, style.brradius )
			elseif v[#v].css then table.insert( v[#v].css, style.brradius )
			else v[#v].css = style.brradius
			end
		end
		-- Create table row for each entry of html
		if v['type'] == "group" then
			t:node( newGroup(v) )
		elseif v['type'] == "section" then
			t:node( newTr(v, false) )
		else
			t:node( newTr(v, bbottom) )
		end
	end
	return tostring(t)
end

--[[ Creates a <tr> element
td parameter is a table (see function td below)
bbottom draws a border-bottom if true
]]
function newTr( td, bbottom )
	local tr = mw.html.create( 'tr' )
	for k, v in ipairs( td ) do
		tr:node( newTd(v, bbottom) )
	end
	return tr
end

--[[ Creates a <td> element
data parameter is a table :
	css key defines css
	attr key defines attr
	unnamed key is the text content
	Example : { "text", css = { rules }, attr = { rules } }
bbottom is inherited from the tr call
]]
function newTd( data, bbottom )
	local td = mw.html.create( 'td' ):wikitext( data[1] )
	if data.css then addCss( td, data.css ) end
	if data.attr then addAttr( td, data.attr ) end
	if bbottom then td:css( style.borderbottom ) end
	return td
end

--[[ Creates a group of div of same length.
data parameter is a table :
	n is the number of tiles, used to calc the length
	bbottom defines if the tiles have a border-bottom or not
	Other params represent the tiles as a table :
		{ text, css = {}, attr = {} }
]]
function newGroup( data )
	local n, bbottom = data['n'], data['bbottom'] or false
	local hgroup = mw.html.create( 'div' )
	hgroup:css( 'display', 'flex' )
	for i=1, n do
		if i == n then
			hgroup:node( newTile( data[i], bbottom, false , n) )
		else
			hgroup:node( newTile( data[i], bbottom, true , n) )
		end
	end
	local row = mw.html.create( 'td' ):attr( 'colspan', COLS ):node( hgroup )
	return mw.html.create( 'tr' ):node( row )
end

function newTile( cell, borderbottom, borderright, n )
	local width = "calc(100% / ".. n .. ")"
	local css = { style.group }
	if borderbottom then table.insert( css, style.borderbottom ) end
	if borderright then table.insert( css, style.borderright ) end
	local tile = mw.html.create( 'div' )
	tile:css( 'width', width ):wikitext( cell[1] )
	tile = addCss( tile, css )
	if cell.css then tile = addCss( tile, cell.css ) end
	return tile
end

-- Returns a section title
function section( text, css, attr )
	local title = mw.html.create( 'td' ):wikitext( text )
	title:attr( 'colspan', COLS )
	return title
end

--[[ Adds CSS to elmt
css parameter must be a table of css rules ]]
function addCss( elmt, css )
	if css == nil then return elmt end
	for i, v in ipairs( css ) do
		elmt:css( v )
		css[i] = nil
	end
	elmt:css( css )
	return elmt
end

-- Adds attributes to elmt (id, class, etc)
function addAttr( elmt, attr )
	if attr == nil then return elmt end
	for k, v in pairs( attr ) do
		if tonumber(k) == nil then
			elmt:attr( attr )
			break
		else
			elmt:attr( v )
		end
	end
	return elmt
end

return Infobox
Advertisement