FANDOM


local PortableInfoboxBuilderView = {}
 
-- Define dependencies
local HF = mw.PortableInfoboxBuilderHF
 
-- Define a table to store global variables
PortableInfoboxBuilderView.vars = {}
 
 
--[[ Private methods ]]--
 
--[[
---- Function that renders a Line block
---- @param   table field  A table with all field's arguments
---- @return  object       Returns an mw.html object that can be merged with a container
]]--
local addRowLine = function( field )
	local data = mw.html.create( 'data' )
 
    -- Not supported by Portable Infoboxes
    -- if not HF.isempty( field.CssClass ) then
    --     row:addClass( field.CssClass )
    -- end
 
    if not HF.isempty( field.Label ) then
        data
            :tag('label')
            :wikitext( field.Label )
			:done()
    end
 
	data
		:tag( 'default' )
			:wikitext( field.Value )
			:done()
 
	return data
end
 
--[[
---- Function that renders a Header block
---- @param   table field  A table with all field's arguments
---- @return  object       Returns an mw.html object that can be merged with a container
]]--
local addRowHeader = function( field )
	local header = mw.html.create('header')
	    :wikitext( field.Value )
 
    -- Not supported by Portable Infoboxes
	-- if not HF.isempty( field.CssClass ) then
	--     header:addClass( field.CssClass )
	-- end
 
	return header
end
 
--[[
---- Function that renders a Title block
---- @param   table field  A table with all field's arguments
---- @return  object       Returns an mw.html object that can be merged with a container
]]--
local addRowTitle = function( field )
	local row = mw.html.create('tr')
	row:addClass( PortableInfoboxBuilderView.vars.Theme .. '-infobox-field-' .. field.Index )
 
	local cell = row:tag('th')
	cell
		:addClass( PortableInfoboxBuilderView.vars.Theme .. '-infobox-title' )
		:attr( 'colspan', '2' )
		:wikitext( field.Value )
 
	return row
end
 
--[[
---- Function that renders a MainImage block
---- @param   table field  A table with all field's arguments
---- @return  object       Returns an mw.html object that can be merged with a container
]]--
local addRowMainImage = function( field )
	local node = mw.html.create( '' )
 
	local row1 = node:tag( 'tr' )
	row1
		:addClass( PortableInfoboxBuilderView.vars.Theme .. '-infobox-field-' .. field.Index )
		:tag( 'td' )
			:addClass( PortableInfoboxBuilderView.vars.Theme .. '-infobox-main-image' )
			:attr( 'colspan', '2' )
			:wikitext( field.Value )
 
	if PortableInfoboxBuilderView.vars.MainImageCaption == "On" then
		local row2 = node:tag( 'tr' )
		row2
			:addClass( PortableInfoboxBuilderView.vars.Theme .. '-infobox-field-' .. field.Index )
			:tag( 'td' )
				:addClass( PortableInfoboxBuilderView.vars.Theme .. '-infobox-main-image-caption' )
				:attr( 'colspan', '2' )
				:wikitext( field.Label )
	end
 
	return node
end
 
--[[
---- Function that renders an Image block
---- @param   table field  A table with all field's arguments
---- @return  object       Returns an mw.html object that can be merged with a container
]]--
local addRowImage = function( field )
	local node = mw.html.create( '' )
 
	local row1 = node:tag( 'tr' )
	row1
		:addClass( PortableInfoboxBuilderView.vars.Theme .. '-infobox-field-' .. field.Index )
		:tag('td')
			:addClass( PortableInfoboxBuilderView.vars.Theme .. '-infobox-main-image' )
			:attr( 'colspan', '2' )
			:wikitext( field.Value )
 
	local row2 = node:tag( 'tr' )
	row2
		:addClass( PortableInfoboxBuilderView.vars.Theme .. '-infobox-field-' .. field.Index )
		:tag( 'td' )
			:addClass( PortableInfoboxBuilderView.vars.Theme .. '-infobox-image-caption' )
			:attr( 'colspan', '2' )
			:wikitext( field.Label )
 
	return node
end
 
--[[
---- Function that renders a Footer block
---- @param   table field  A table with all field's arguments
---- @return  object       Returns an mw.html object that can be merged with a container
]]--
local addRowFooter = function( field )
	local row = mw.html.create( 'tr' )
 
	row
		:addClass( PortableInfoboxBuilderView.vars.Theme .. '-infobox-field-' .. field.Index )
		:tag( 'td' )
			:addClass( PortableInfoboxBuilderView.vars.Theme .. '-infobox-footer' )
			:attr( 'colspan', '2' )
			:wikitext( field.Value )
 
	return row
end
 
--[[
---- Function that renders a Split block
---- @param   table field  A table with all field's arguments
---- @return  object       Returns an mw.html object that can be merged with a container
]]--
local addRowSplit = function( field )
	local node = mw.html.create( '' )
 
	local labelLeft = mw.html.create( 'td' )
	labelLeft:addClass( PortableInfoboxBuilderView.vars.Theme .. '-infobox-split-label-left' )
	local valueLeft = mw.html.create( 'td' )
	valueLeft:addClass( PortableInfoboxBuilderView.vars.Theme .. '-infobox-split-value-left' )
 
	local labelRight = mw.html.create( 'td' )
	labelRight:addClass( PortableInfoboxBuilderView.vars.Theme .. '-infobox-split-label-right' )
	local valueRight = mw.html.create( 'td' )
	valueRight:addClass( PortableInfoboxBuilderView.vars.Theme .. '-infobox-split-value-right' )
 
	if type( field.Label ) == "string" then
		labelLeft:wikitext( field.Label )
	end
 
	if type( field.Value ) == "string" then
		valueLeft:wikitext( field.Value )
	elseif type( field.Value ) == "table" then
		if not HF.isempty( field.Value[1] ) then
			labelLeft:wikitext( field.Label[1] )
			valueLeft:wikitext( field.Value[1] )
		end
		if not HF.isempty( field.Value[2] ) then
			labelRight:wikitext( field.Label[2] )
			valueRight:wikitext( field.Value[2] )
		end
	end
 
	local row1 = node:tag( 'tr' )
	row1:addClass( PortableInfoboxBuilderView.vars.Theme .. '-infobox-field-' .. field.Index )
 
	row1:node( labelLeft )
	row1:node( labelRight )
 
	local row2 = node:tag( 'tr' )
	row2:addClass( PortableInfoboxBuilderView.vars.Theme .. '-infobox-field-' .. field.Index )
 
	row2:node( valueLeft )
	row2:node( valueRight )
 
	return node
end
 
--[[
---- Function that renders a Custom block
---- @param   table field  A table with all field's arguments
---- @return  object       Returns an mw.html object that can be merged with a container
]]--
local addRowCustom = function( field )
	local node = mw.html.create( field.Label )
	node
		:addClass( PortableInfoboxBuilderView.vars.Theme .. '-infobox-custom' )
		:addClass( PortableInfoboxBuilderView.vars.Theme .. '-infobox-field-' .. field.Index )
		:wikitext( field.Value )
 
	if not HF.isempty( field.CssClass ) then
		node:addClass( field.CssClass )
	end
 
	return node
end
 
 
--[[ Public methods ]]--
 
--[[
---- Main function that controls the rendering of prepared fields
---- @param   table input  A sorted table with modified arguments - check PortableInfoboxBuilder.lua@execute()
---- @param   table vars   A table containing global variables
---- @return  string       Returns a wikitext-compliant string with a rendered infobox
]]--
function PortableInfoboxBuilderView.render( input, vars )
	local fields   = input.fields
	local sections = input.sections
 
 
	PortableInfoboxBuilderView.vars = vars
 
	-- Let's go! Create a container
	local Infobox = mw.html.create( 'infobox' )
	--Infobox:attr( 'theme', PortableInfoboxBuilderView.vars.Theme)
 
	-- Iterate over each field and render a row if a value is not empty
	for index, field in ipairs( fields ) do
		if not HF.isempty( field.Value ) then
			if field.Type == "Line" then
				Infobox:node( addRowLine( field ) )
			elseif field.Type == "Header" and input.sections[index] == "On" then
				Infobox:node( addRowHeader( field ) )
			elseif field.Type == "Title" then
				Infobox:node( addRowTitle( field ) )
			elseif field.Type == "MainImage" then
				Infobox:node( addRowMainImage( field ) )
			elseif field.Type == "Image" then
				Infobox:node( addRowImage( field ) )
			elseif field.Type == "Footer" then
				Infobox:node( addRowFooter( field ) )
			elseif field.Type == "Custom" then
				Infobox:node( addRowCustom( field ) )
			elseif field.Type == "Split" then
				Infobox:node( addRowSplit( field ) )
			end
		end
    end
 
    local output = mw.getCurrentFrame():preprocess( tostring( Infobox ) )
	return output
end
 
php = mw_interface
mw_interface = nil
 
-- Register module as "mw.PortableInfoboxBuilder.View" global
mw = mw or {}
mw.PortableInfoboxBuilderView = PortableInfoboxBuilderView
 
package.loaded['mw.PortableInfoboxBuilderView'] = PortableInfoboxBuilderView
 
return PortableInfoboxBuilderView

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.