Jump to content

Module:GrantmakingNavbar

From Meta, a Wikimedia project coordination wiki
Module documentation

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(frame)
	-- formats the wrapper div and the navbar icon
	local root = mw.html.create('ul')
	root
		:addClass('grantmaking-navbar')
		-- add TemplateStyles
		:wikitext(frame:extensionTag('templatestyles', '', {src = 'Module:GrantmakingNavbar/styles.css'}))
		-- add first icon section
		:tag('li')
			:addClass('grantmaking-navbar-icon')
			:wikitext('[[File:Plant and coins.svg|18px|link=]]')
			:done()
	return root			
end

function addPages(root, frame, 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	
		local linktext = mw.text.trim(frame:expandTemplate{title = lang_path, args = {pages.titleKey[k]}})
		root
			:tag('li')
				:css('background', bgcolor)
				:wikitext('[[' .. page_path .. '|' .. linktext .. ']]')
				:done()		
	end
	return root
end

function p.navbar(frame)
	local args = frame.args
	local uselang = args['uselang'] or 'en'
	-- format the bar and return
	local root = formatBar(frame)
	addPages(root, frame, uselang)
	return tostring(root)
end	

return p