Module:HonorChallPart

From Pathfinder Wiki
Revision as of 21:28, 31 July 2021 by DesignerThan (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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' }
	landingPage = string.gsub(landingPage, "[\n\r]", '')
	mw.log('HonorChallPart.getChallengingPart (landingPage): "'..landingPage..'"')
	local langSuffix = frame:preprocess( '{{PAGELANGUAGE}}' )
	local retString = nil
	
	if ( reqVariants ~= nil and reqVariants['requirements'] ~= nil ) then
		if ( #reqVariants['requirements'] > 1 ) then
			-- create tab's
			local tabString = ''
			local tabContentString = ''
			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']
				tabString = tabString..createNavTab( authAbbrev, tabText, isActive )
				
				local reqPageLink = landingPage..'/Requirements'
				if ( key > 1 ) then
					reqPageLink = reqPageLink..' '..key
				end
				reqPageLink = reqPageLink..'/'..langSuffix
				mw.log('HonorChallPart.getChallengingPart (reqPageLink): '..reqPageLink)
				local content = frame:callParserFunction( '#lst', reqPageLink, 'challenge' )
				tabContentString = tabContentString..createContentTab( authAbbrev, content, isActive )
			end
				
			local HTMLTabContainer = mw.html.create( 'ul' )
			HTMLTabContainer
				:addClass( 'nav nav-tabs' )
				:attr( 'id', 'challengeTab' )
				:attr( 'role', 'tablist' )
				:wikitext( tabString )
				
			local HTMLTabsContent = mw.html.create( 'div' )
			HTMLTabsContent
				:addClass( 'tab-content' )
				:attr( 'challengeTabContent' )
				:wikitext( tabContentString )
			
			retString = tostring( HTMLTabContainer )..tostring( HTMLTabsContent )
		else
			-- no or only 1 reqVariant's just use the default
			local reqPageLink = landingPage..'/Requirements/'..langSuffix
			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' )
	
	local isActiveCSSClass = ''
	if ( isActive == true ) then
		mw.log('HonorChallPart.createNavTab: Tab is active')
		isActiveCSSClass = ' active'
	end
	
	HTMLLink
		:attr( 'tagname', 'a')
		:addClass( 'nav-link'..isActiveCSSClass )
		: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' )
	
	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