Jump to content

Module:Side box/sandbox: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
redundant
nothing could go possibly wrong with this design
Line 5: Line 5:
local p = {}
local p = {}


function p.main(frame)
local function makeData(args)
local origArgs = frame:getParent().args
local args = {}
for k, v in pairs(origArgs) do
v = v:match('%s*(.-)%s*$')
if v ~= '' then
args[k] = v
end
end
return p._main(args)
end

function p._main(args)
local data = p.makeData(args)
return p.renderSidebox(data)
end

function p.makeData(args)
local data = {}
local data = {}


Line 31: Line 14:
end
end
if args.position and args.position:lower() == 'left' then
if args.position and args.position:lower() == 'left' then
table.insert(data.classes, 'mbox-small-left')
table.insert(data.classes, 'side-bar-left')
else
else
table.insert(data.classes, 'mbox-small')
table.insert(data.classes, 'side-bar-right')
end
end
table.insert(data.classes, args.class)
table.insert(data.classes, args.class)
Line 69: Line 52:
end
end


function p.renderSidebox(data)
local function renderSidebox(data)
-- Renders the sidebox HTML.
-- Renders the sidebox HTML.


-- Table root
-- Table root
local root = mw.html.create('table')
local root = mw.html.create('div')
root:attr('role', 'presentation')
root:attr('role', 'presentation')
:addClass('side-box')
for i, class in ipairs(data.classes or {}) do
for i, class in ipairs(data.classes or {}) do
root:addClass(class)
root:addClass(class)
end
end
root:css{border = '1px solid #aaa', ['background-color'] = '#f9f9f9', color = '#000'}
if data.style then
if data.style then
root:cssText(data.style)
root:cssText(data.style)
Line 85: Line 68:
-- The "above" row
-- The "above" row
if data.above then
if data.above then
local aboveCell = root:newline():tag('tr'):tag('td')
local above = root:newline():tag('div')
above:addClass('side-box-abovebelow')
aboveCell
:newline()
:attr('colspan', data.imageright and 3 or 2)
:addClass('mbox-text')
:wikitext(data.above)
if data.textstyle then
if data.textstyle then
aboveCell:cssText(data.textstyle)
above:cssText(data.textstyle)
end
end
if data.abovestyle then
if data.abovestyle then
aboveCell:cssText(data.abovestyle)
above:cssText(data.abovestyle)
end
end
aboveCell
:newline()
:wikitext(data.above)
end
end


-- The body row
-- The body row
local bodyRow = root:newline():tag('tr'):newline()
local body = root:newline():tag('div'):addClass('side-box-flex'):newline()
if data.image then
if data.image then
bodyRow:tag('td')
body:tag('div')
:addClass('mbox-image')
:addClass('side-box-image')
:wikitext(data.image)
:wikitext(data.image)
else
bodyRow:tag('td'):css('width', '1px')
end
end
local textCell = bodyRow:newline():tag('td')
local text = body:newline():tag('div')
textCell:addClass('mbox-text')
text:addClass('side-box-text')
textCell:addClass(data.textclass or 'plainlist')
:addClass(data.textclass or 'plainlist')
if data.textstyle then
if data.textstyle then
textCell:cssText(data.textstyle)
text:cssText(data.textstyle)
end
end
textCell:wikitext(data.text)
text:wikitext(data.text)
if data.imageright then
if data.imageright then
bodyRow:newline():tag('td')
body:newline():tag('div')
:addClass('mbox-imageright')
:addClass('side-box-imageright')
:wikitext(data.imageright)
:wikitext(data.imageright)
end
end
Line 124: Line 102:
-- The below row
-- The below row
if data.below then
if data.below then
local belowCell = root:newline():tag('tr'):tag('td')
local below = root:newline():tag('div')
below
belowCell
:addClass('side-box-abovebelow')
:attr('colspan', data.imageright and 3 or 2)
:addClass('mbox-text')
:wikitext(data.below)
if data.textstyle then
if data.textstyle then
belowCell:cssText(data.textstyle)
below:cssText(data.textstyle)
end
end
belowCell:wikitext(data.below)
end
end


root:newline()
root:newline()
return tostring(root)
return mw.getCurrentFrame():extensionTag{
'templatestyles', args = { src = 'Module:Side box/styles.css' }
} .. tostring(root)
end

function p._main(args)
local data = makeData(args)
return renderSidebox(data)
end

function p.main(frame)
local origArgs = frame:getParent().args
local args = {}
for k, v in pairs(origArgs) do
v = v:match('%s*(.-)%s*$')
if v ~= '' then
args[k] = v
end
end
return p._main(args)
end
end



Revision as of 21:20, 27 May 2022

-- This module implements {{side box}}.

local yesno = require('Module:Yesno')

local p = {}

local function makeData(args)
	local data = {}

	-- Main table classes
	data.classes = {}
	if yesno(args.metadata) ~= false then
		table.insert(data.classes, 'metadata')
	end
	if args.position and args.position:lower() == 'left' then
		table.insert(data.classes, 'side-bar-left')
	else
		table.insert(data.classes, 'side-bar-right')
	end
	table.insert(data.classes, args.class)
	
	-- Image
	if args.image and args.image ~= 'none' then
		data.image = args.image
	end

	-- Copy over data that does not need adjusting
	local argsToCopy = {
		-- Classes
		'textclass',

		-- Styles
		'style',
		'textstyle',

		-- Above row
		'above',
		'abovestyle',

		-- Body row
		'text',
		'imageright',

		-- Below row
		'below',
	}
	for i, key in ipairs(argsToCopy) do
		data[key] = args[key]
	end

	return data
end

local function renderSidebox(data)
	-- Renders the sidebox HTML.

	-- Table root
	local root = mw.html.create('div')
	root:attr('role', 'presentation')
		:addClass('side-box')
	for i, class in ipairs(data.classes or {}) do
		root:addClass(class)
	end
	if data.style then
		root:cssText(data.style)
	end

	-- The "above" row
	if data.above then
		local above = root:newline():tag('div')
		above:addClass('side-box-abovebelow')
			:newline()
			:wikitext(data.above)
		if data.textstyle then
			above:cssText(data.textstyle)
		end
		if data.abovestyle then
			above:cssText(data.abovestyle)
		end
	end

	-- The body row
	local body = root:newline():tag('div'):addClass('side-box-flex'):newline()
	if data.image then
		body:tag('div')
			:addClass('side-box-image')
			:wikitext(data.image)
	end
	local text = body:newline():tag('div')
	text:addClass('side-box-text')
		:addClass(data.textclass or 'plainlist')
	if data.textstyle then
		text:cssText(data.textstyle)
	end
	text:wikitext(data.text)
	if data.imageright then
		body:newline():tag('div')
			:addClass('side-box-imageright')
			:wikitext(data.imageright)
	end

	-- The below row
	if data.below then
		local below = root:newline():tag('div')
		below
			:addClass('side-box-abovebelow')
			:wikitext(data.below)
		if data.textstyle then
			below:cssText(data.textstyle)
		end
	end

	root:newline()
	return mw.getCurrentFrame():extensionTag{
		'templatestyles', args = { src = 'Module:Side box/styles.css' }
	} .. tostring(root)
end

function p._main(args)
	local data = makeData(args)
	return renderSidebox(data)
end

function p.main(frame)
	local origArgs = frame:getParent().args
	local args = {}
	for k, v in pairs(origArgs) do
		v = v:match('%s*(.-)%s*$')
		if v ~= '' then
			args[k] = v
		end
	end
	return p._main(args)
end

return p