Module:GrantmakingNavbar

From Meta, a Wikimedia project coordination wiki
Jump to navigation Jump to search
Module documentation[view] [edit] [history] [purge]

This module will implement {{GrantmakingNavbar}}. Data about each portal (which affects how the name is displayed) is stored in Module:GrantmakingNavbar/pages.

--
-- this module implements {{GrantmakingNavbar}}
--

local p = {}

function formatBar(align)
	-- formats the wrapper div and the navbar icon
	local root = mw.html.create('div')
	root
		:css('clear', 'both')
		:css('font-size', '1.1em')
		:css('line-height', '1.5em')
		:css('background', '#FFF')
		:css('color', '#222')
		-- add first icon section
		:tag('div')
			:css('float', align)
			:css('margin', '1px')
			:css('background', '#000')
			:css('padding', '.5em')
			:css('white-space', 'nowrap')
			:tag('span')
				:css('color', '#FFF')
				:css('opacity', '.6')
				:wikitext('[[File:Plant and coins.svg|18px|link=]]')
				:done()
			:done()
	return root			
end

function addPages(root, frame, align, uselang)
	-- get the name of the page where the template is called
	local title = mw.title.getCurrentTitle(frame)
	-- formats the nav section each Grantmaking portal page
	local pages = mw.loadData("Module:GrantmakingNavbar/pages")
	local bgcolor
	local lang_path
	local page_path
	-- check if bar appears on a non-en subpage for which the template has a translation
	if (uselang ~= '' and mw.title.makeTitle( 'Template', 'GrantmakingNavbar/Content/' .. uselang ).exists) then
		lang_path = 'GrantmakingNavbar/Content/' .. uselang
	else -- defaults to en version
		lang_path = 'GrantmakingNavbar/Content/en'
	end	
	-- build the portal nav sections
	for k, v in pairs(pages.pagePath) do
		-- color the nav section for the current page 
		if 'Grants:' .. pages.pagePath[k] == title.prefixedText
		or ('Grants:' .. pages.pagePath[k] .. '/' .. uselang) == title.prefixedText then
			bgcolor = pages.bgActive[k]
		else
			bgcolor = pages.bgInactive[k]
		end	
		if (uselang ~= '' and uselang ~= 'en' and mw.title.makeTitle('Grants', pages.pagePath[k] .. '/' .. uselang ).exists) then
			page_path = 'Grants:' .. pages.pagePath[k] .. '/' .. uselang 
		else -- defaults to en version
			page_path = 'Grants:' .. pages.pagePath[k]
		end	
		root
			:tag('div')
				:css('float', align)
				:css('margin', '1px')
				:css('background', bgcolor)
				:css('padding', '.5em')
				:css('white-space','nowrap')
				:wikitext('[[' .. page_path .. '|')				
				:tag('span')
					:css('color', '#FFF')
					:wikitext(mw.text.trim(frame:expandTemplate{title = lang_path, args = {pages.titleKey[k]}}))		
					:done()	
				:wikitext("]]")
				:done()		
	end
	return root
end	
	
function endBar(root)
	root
		:tag('div')
			:css('clear', 'both')
			:done()
	return tostring(root)
end

function p.navbar(frame)
	local args = frame.args
	local uselang = args['uselang'] or 'en'
	local dir = args['dir'] or 'ltr'
	local align
	if dir == 'ltr' then
		align = 'left'
	else
		align = 'right'
	end
	-- format the bar and return
	local root = formatBar(align)
	addPages(root, frame, align, uselang)
	return endBar(root)
end	

return p