Jump to content

Module:Wikipedia ads/navbox

Permanently protected module
From Wikipedia, the free encyclopedia

-- This module displays a navbox of all available Wikipedia ads. It is used in
-- [[Template:Wikipedia ads/doc]].

local DATA_MODULE = 'Module:Wikipedia ads/data'

local p = {}

function p.main()
	local data = mw.loadData(DATA_MODULE)

	-- Table root.
	local root = mw.html.create('table')
	root
		:addClass('navbox collapsible collapsed')
		:css('width', '100%')
		:css('background', '#f7f8ff')
		:css('text-align', 'left')
		:css('border', 'solid 1px #aaaaaa')
		:css('margin', 0)
		:tag('tr')
			:tag('th')
				:css('background', 'transparent')
				:css('font-size', '105%;')
				:css('text-align', 'center;')
				:css('font-weight', 'bold')
				:wikitext('[[Template:Wikipedia ads|Wikipedia ads]]: [[Template:Wikipedia ads/navbox|Overview]]')
	
	-- Ad table.
	local adTable = root
		:tag('tr')
			:tag('td')
				:tag('table')
					:addClass('plainlinks sortable wikitable')
					:css('width', '100%')
					:css('text-align', 'left')
					:css('margin', '0')
					:css('font-size', '90%')

	-- Ad table headers.
	adTable
		:tag('tr')
			:tag('th')
				:css('width', '3em')
				:wikitext('#')
				:done()
			:tag('th')
				:addClass('unsortable')
				:wikitext('Image')
				:done()
			:tag('th')
				:wikitext('Page')
				:done()
			:tag('th')
				:wikitext('Author')
				:done()

	-- Ads.
	for i, imageData in ipairs(data.list) do
		-- Sanity checks. The ID is already checked by the data module.
		for j, field in ipairs{'image', 'link', 'author'} do
			assert(imageData[field], string.format(
				"no '%s' field found in image data with ID '%s'",
				field, tostring(imageData.id)
			))
		end
		adTable
			:tag('tr')
				:tag('th')
					:wikitext(imageData.id)
					:done()
				:tag('td')
					:wikitext(string.format(
						'[[File:%s|140px]]',
						imageData.image
					))
					:done()
				:tag('td')
					:wikitext(string.format('[[%s]]', imageData.link))
					:done()
				:tag('td')
					:wikitext(imageData.author)
	end

	return tostring(root)
end

return p