Module:HonorChallPart

From Pathfinder Wiki
Revision as of 22:14, 31 July 2021 by DesignerThan (talk | contribs)

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

local p = {}
local honorUtils = require ( "Module:HonorUtils" )
local utilities = require ( "Module:Utilities" )

function p.getChallengingPart( frame )
	local reqVariants = honorUtils.getVariantsData( frame, nil, 'requirements', nil )
	local landingPage = frame:expandTemplate{ title='GetHonorLandingPage' }
	local langSuffix = frame:preprocess( '{{PAGELANGUAGE}}' )
	local retString = nil
	
	if ( reqVariants ~= nil and reqVariants['requirements'] ~= nil ) then
		if ( #reqVariants['requirements'] > 1 ) then
			-- create tab's
			for key, variant in pairs(reqVariants['requirements']) do
				tabText = ''
				for authorityKey, authority in pairs(variant['authorities']) do
					if ( #tabText > 0 ) then
						tabText = tabText..' & '..authority['translated']
					else
						tabText = authority['translated']
					end
				end
				local isActive = false
				if ( key == 1 ) then
					isActive = true
				end
				authAbbrev = variant['authorities'][1]['orig']
				
				local HTMLTabContainer = mw.html.create( 'ul' )
				HTMLTabContainer
					:addClass( 'nav nav-tabs' )
					:attr( 'id', 'challengeTab' )
					:attr( 'role', 'tablist' )
					:wikitext( createNavTab( authAbbrev, tabText, isActive) )
				
				local reqPageLink = landingPage..'/Requirements'
				if ( key > 1 ) then
					reqPageLink = reqPageLink..' '..key
				end
				reqPageLink = reqPageLink..'/'..langSuffix
				reqPageLink = mw.ustring.gsub(reqPageLink, "[\n\r]", '', 0)
				mw.log('HonorChallPart.createContentTab (reqPageLink): '..reqPageLink)
				local content = frame:callParserFunction( '#lst', reqPageLink, 'challenge' )
				local HTMLTabsContent = mw.html.create( 'div' )
				HTMLTabsContent
					:addClass( 'tab-content' )
					:attr( 'challengeTabContent' )
					:wikitext( createContentTab( authAbbrev, content, isActive ))
				
				retString = tostring( HTMLTabContainer )..tostring( HTMLTabsContent )
			end
		else
			-- no or only 1 reqVariant's just use the default
			local reqPageLink = landingPage..'/Requirements/'..langSuffix
			reqPageLink = mw.ustring.gsub(reqPageLink, "[\n\r]", '', 0)
			retString = frame:callParserFunction( '#lst', reqPageLink, 'challenge' )
		end
	end
	return frame:preprocess(retString)
end

function createNavTab( authAbbrev, tabText, isActive)
	-- create a single tab
	local HTMLLink = mw.html.create( 'htmltag' )
	-- if first add css class 'active'
	HTMLLink
		:attr( 'tagname', 'a')
		:addClass( 'nav-link' )
		:attr( 'id', authAbbrev..'-tab' )
		:attr( 'data-toggle', 'tab' )
		:attr( 'href', '#'..authAbbrev )
		:attr( 'role', 'tab' )
		:attr( 'aria-controls', authAbbrev )
		:attr( 'aria-selected', tostring(isActive) )
		:wikitext( tabText )
	
	local HTMLLi = mw.html.create( 'li' )
	HTMLLi
		:addClass( 'nav-item' )
		:attr( 'role', 'presentation' )
		:wikitext( tostring( HTMLLink ) )
	
	return tostring( HTMLLi )
end

function createContentTab( authAbbrev, content, isActive )
	mw.log('HonorChallPart.createContentTab (authAbbrev, content, isActive): '..authAbbrev..', '..content..', '..tostring(isActive))
	--create a content tab
	local HTMLContentTab = mw.html.create( 'div' )
	-- if first add css class 'active & show'
	local isActiveCSSClass = ''
	if ( isActive == true ) then
		mw.log('HonorChallPart.createContentTab: Tab is active')
		isActiveCSSClass = ' active show'
	end
	
	HTMLContentTab
		:addClass( 'tab-pane fade'..isActiveCSSClass )
		:attr( 'id', authAbbrev )
		:attr( 'role', 'tabpanel' )
		:attr( 'aria-labelledby', authAbbrev..'-tab' )
		:wikitext( content )
	
	return tostring( HTMLContentTab )
end

return p