Difference between revisions of "Module:HonorTabs"

From Pathfinder Wiki
Line 1: Line 1:
 
local p = {}
 
local p = {}
local strUtils = require( "Module:StringUtils" )
+
strUtils = require( "Module:StringUtils" )
  
 
function p.createAnswerTab( frame )
 
function p.createAnswerTab( frame )

Revision as of 10:06, 5 May 2021

Documentation for this module may be created at Module:HonorTabs/doc

local p = {}
strUtils = require( "Module:StringUtils" )

function p.createAnswerTab( frame )
	local ansVariants = ""
	if frame.args.answerVariants then ansVariants = frame.args.answerVariants end
	result_string = outputVariantsTab(frame, prepareAnswerVariants( frame, frame.args.landingPage, ansVariants))
	return result_string
end

function prepareAnswerVariants(frame, landingPage, variants)
	-- check the input variables
	if (frame == nil or landingPage == nil or landingPage == "") then
		return "fatal error"
	end
	if (variants == nil or variants == "") then
		local linkTitle = frame:expandTemplate{ title = 'Localize', args = { 'Answer Key' } }
		return {
			{
				url = "[[" .. landingPage .. "/Answer_Key" .. frame:expandTemplate{title='GetLangSuffix'} .. "|" .. linkTitle .. "]]"
			}
		}
	end
	
	-- get the variants
	local t_variants = strUtils.explode(";;", variants)
	
	-- get the subvariants & translate it
	for key, variant in pairs(t_variants) do
		t_variants[key] = {
			orig = variant,
			split = strUtils.explode(";", variant)
		}
		for subkey, subVariant in pairs(t_variants[key]['split']) do
			local trans = frame:expandTemplate{ title = 'Localize', args = { subVariant, 'Answer Variants' } }
			local t_splitTrans = {
				orig = subVariant,
				translation = trans
			}
			t_variants[key]['split'][subkey] = t_splitTrans
		end
	end
	
	-- assamble link
	for key, variant in pairs(t_variants) do
		linkTitle = ""
		local len = #linkTitle
		for subkey, subvariant in pairs(t_variants[key]['split']) do
			if (#linkTitle > 0) then
				linkTitle = linkTitle .. " & " .. subvariant['translation']
			else
				linkTitle = subvariant['translation']
			end
		end
		t_variants[key]['url'] = "[[" .. landingPage .. "_(" .. variant['orig'] .. ")/Answer_Key" .. frame:expandTemplate{title='GetLangSuffix'} .. "|" .. linkTitle .. "]]"
	end
	
	return t_variants
end

function outputVariantsTab(frame, variants)
	local HTMLCode = ""
	local HTMLCodeString = ""
	if (variants == nil or #variants < 1) then 
		-- should not happen!
		HTMLCodeString = "no variants?!"
	elseif (#variants == 1) then
		HTMLCode = mw.html.create( 'li' )
		HTMLCode
			:addClass( 'honor-nav-item' )
			:wikitext( variants[1].url )
		HTMLCodeString = tostring( HTMLCode )
	else
		-- now there should be more then 1 variants
		local HTMLTabParent = mw.html.create( 'li' )
		local buttonArgs = {}
		buttonArgs['tagname'] = 'button'
		local explodedPageName = strUtils.explode('/', frame:expandTemplate{title = 'PAGENAMEwithoutLang'})
		buttonArgs['class'] = 'dropdown-toggle' .. ' ' .. explodedPageName[#explodedPageName]
		if (explodedPageName[#explodedPageName] == 'Answer Key') then
			buttonArgs['class'] = buttonArgs['class'] .. ' selflink'
		end
		buttonArgs['type'] = 'button'
		buttonArgs['id'] = 'dropdownAnswerKeys'
		buttonArgs['data-toggle'] = 'dropdown'
		buttonArgs['aria-haspopup'] = 'true'
		buttonArgs['aria-expanded'] = 'false'
		buttonText = frame:expandTemplate{title = 'Localize', args = {'Answer Key'} }
		local dropdownButton = frame:extensionTag('htmltag', buttonText, buttonArgs)
		HTMLTabParent
			:addClass( 'honor-nav-item' )
			:addClass( 'dropdown' )
			-- :wikitext( frame:expandTemplate{title='User:DesignerThan/Button'} )
			:wikitext( dropdownButton )
		
		HTMLVariantsContainer = mw.html.create( 'div' )
		HTMLVariantsContainer
			:addClass( 'dropdown-menu' )
			:attr( 'aria-labelledby', 'dropdownAnswerKeys')
		for key, variant in pairs(variants) do
			HTMLVariantLink = mw.html.create( 'span' )
			HTMLVariantLink
				:addClass( 'dropdown-item' )
				:wikitext( variant.url )
				:done()
			HTMLVariantsContainer:node( HTMLVariantLink )
		end
		HTMLVariantsContainer:done()
		HTMLTabParent:node( HTMLVariantsContainer )
		HTMLCodeString = tostring( HTMLTabParent )
	end
	return HTMLCodeString
end

return p