Module:HonorTabs

From Pathfinder Wiki
Revision as of 22:29, 1 August 2021 by Jomegat (talk | contribs)

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

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

function p.generateHonorTabs( frame )
	local parentFrame = frame:getParent()
	mw.log('HonorTabs.generateHonorTabs (frame:getTitle): '..frame:getTitle())
	mw.log('HonorTabs.generateHonorTabs (parentFrame:getTitle): '..parentFrame:getTitle())
	local landingPage = parentFrame.args['landingPage']
	if landingPage == nil or landingPage == '' then
		landingPage = frame:expandTemplate{ title='GetHonorLandingPage' }
		landingPage = mw.ustring.gsub(landingPage,"[\n\r]", "")
	end
	
	local langSuffix = frame:expandTemplate{ title='GetLangSuffix' }
	tVariants = honorUtils.getVariantsData( frame, landingPage )
	
	local tActVariantInfo = honorUtils.getActVariantInfo( frame )
	local isRequirementsPage = false
	local isAnswerKeyPage = false
	if (tActVariantInfo ~= nil) then
		if (tActVariantInfo['variantType'] == 'requirements') then
			isRequirementsPage = true
		elseif (tActVariantInfo['variantType'] == 'answerKey') then
			isAnswerKeyPage = true
			-- if the langsuffix is empty and the variant has an source language set
			-- then we should use that one if my thoughts are correct
			if ((langSuffix == nil or langSuffix == '') and 
				tVariants ~= nil and tVariants['answers'] ~= nil) then
				local variantNumber = tActVariantInfo['variantNumber']
				local srcLangSuffix = tVariants['answers'][variantNumber]['srcLangSuffix']
				mw.log('HonorTabs.generateHonorTabs: Set the langSuffix to the source language of the answer key page. srcLangSuffix: '..srcLangSuffix)
				langSuffix = srcLangSuffix
			end
		end
	end
	
	tAnsReqTabs = convertVariantsToTabs( frame, tVariants, landingPage, langSuffix)
	local HTMLCodeString = ""
	-- create the overview tab
	HTMLCodeString = HTMLCodeString .. createSimpleTab(generateTabURL( frame, landingPage, langSuffix, 'Overview'))
	-- create requirement tab
	HTMLCodeString = HTMLCodeString .. createDropdownTab( frame, tAnsReqTabs['requirements'], 'Requirements', 'Req', isRequirementsPage)
	-- create answer variants tab
	HTMLCodeString = HTMLCodeString .. createDropdownTab( frame, tAnsReqTabs['answers'], 'Answer Key', 'Ans', isAnswerKeyPage)
	if (utilities.isSysOp() == true) then
		-- create lesson plans tab
		HTMLCodeString = HTMLCodeString .. createSimpleTab(generateTabURL( frame, landingPage..'/Lesson_Plans', langSuffix, 'Lesson Plans'))
		-- create worksheets tab
		HTMLCodeString = HTMLCodeString .. createSimpleTab(generateTabURL( frame, landingPage..'/Worksheet', langSuffix, 'Worksheet'))
	end
	
	local tabContainer = mw.html.create( 'ul' )
	tabContainer
		:attr( 'id', 'honor-nav' )
		:attr( 'class', 'noprint')
		:wikitext( HTMLCodeString )
	return tostring( tabContainer )
end

function convertVariantsToTabs( frame, tVariants, landingPage, langSuffix )
	mw.log('HonorTabs.convertVariantsToTabs: Start converting!')
	tTabs = {
		answers = {},
		requirements = {}
	}
	if ( tVariants == nil ) then
		mw.log('HonorTabs.convertVariantsToTabs: invalid Variants table (nil)!')
		return nil;
	end
	if ( tVariants['requirements'] == nil ) then
		mw.log('HonorTabs.convertVariantsToTabs: No Requirement Variants! Create default Tab.')
		local pageTitle = landingPage..'/Requirements'..langSuffix
		local linkTitle = utilities.localize( 'Requirements' )
		-- check if Localization returned something, if not set the original string
		if ( linkTitle == '' ) then
			linkTitle = 'Requirements'
		end
		tTabs['requirements'][1] = {
			pageTitle = pageTitle,
			linkTitle = linkTitle,
			wikilink = '[['..pageTitle..'|'..linkTitle..']]'
		}
	else
		mw.log('HonorTabs.convertVariantsToTabs: '..#tVariants['requirements']..' Requriement Variants to convert!')
		for varKey, variant in pairs(tVariants['requirements']) do
			local linkTitle = ""
			if ( variant['authorities'] == nil ) then
				mw.log('HonorTabs.convertVariantsToTabs: Invalid Requirement Variant (Authorities)!')
			else
				for authorityKey, authority in pairs(variant['authorities']) do
					if ( #linkTitle > 0 ) then
						linkTitle = linkTitle..' & '..authority['translated']
					else
						linkTitle = authority['translated']
					end
				end
			end
			local strVariantNumber = ''
			-- add number only for variants bigger than 1
			if varKey > 1 then
				strVariantNumber = '_'..varKey
			end
			local pageTitle = landingPage..'/Requirements'..strVariantNumber..langSuffix
			tTabs['requirements'][varKey] = {
				pageTitle = pageTitle,
				linkTitle = linkTitle,
				wikilink = '[['..pageTitle..'|'..linkTitle..']]'
			}
			mw.log('HonorTabs.convertVariantsToTabs (Tab URL '..varKey..': '..tTabs['requirements'][varKey]['wikilink'])
		end
		mw.log('HonorTabs.convertVariantsToTabs: '..#tTabs['requirements']..' Requirement Variants were converted to Tabs!')
	end
	if ( tVariants['answers'] == nil ) then
		mw.log('HonorTabs.convertVariantsToTabs: No Answer Variants! Create default Tab.')
		local linkTitle = utilities.localize( 'Answer Key' )
		-- check if Localization returned something, if not set the original string
		if ( linkTitle == '' ) then
			linkTitle = 'Answer Key'
		end
		local pageTitle = landingPage..'/Answer_Key'..langSuffix
		tTabs['answers'][1] = {
			pageTitle = pageTitle,
			linkTitle = linkTitle,
			wikilink = '[['..pageTitle..'|'..linkTitle..']]'
		}
	else
		mw.log('HonorTabs.convertVariantsToTabs: '..#tVariants['answers']..' Answer Variants to convert!')
		for varKey, variant in pairs(tVariants['answers']) do
			local linkTitle = ""
			if ( variant['countries'] == nil or variant['countries']['split'] == nil) then
				mw.log('HonorTabs.convertVariantsToTabs: There are probably no Countries defined or something went horribly wrong!')
			else
				for countryKey, country in pairs(variant['countries']['split']) do
					if ( #linkTitle > 0 ) then
						linkTitle = linkTitle..' & '..country['translated']
					else
						linkTitle = country['translated']
					end
				end
			end
			if ( variant['authority'] == nil ) then
				mw.log('HonorTabs.convertVariantsToTabs: No Authority for this variant!')
			else
				mw.log('HonorTabs.convertVariantsToTabs (variant["authority"]): '..variant['authority'])
				-- get the authority
				local authority = tVariants['requirements'][tonumber(variant['authority'])]['authorities'][1]['translated']
				-- check if there are countries if not, parentheses are not needed
				if ( linkTitle == '' ) then
					linkTitle = authority
				else
					-- if there is only one requirement variant, don't show the authority (it's the only one available)
					if ( #tTabs['requirements'] > 1 ) then
						linkTitle = linkTitle..' ('..authority..')'
					else
						mw.log("HonorTabs.convertVariantsToTabs: Authority wasn't added, because there is only one!")
					end
				end
			end
			local strVariantNumber = ''
			-- add number only for variants bigger than 1
			if varKey > 1 then
				strVariantNumber = '_'..varKey
			end
			local locLangSuffix = langSuffix
			if ( variant['srcLangSuffix'] == locLangSuffix ) then
				-- Source Language of the answer key is the same than the content language in use
				-- that the link doesn't lead to the copy of the translation extension
				-- the langSuffix needs to be removed
				locLangSuffix = ''
			elseif ( variant['srcLangSuffix'] ~= '' and locLangSuffix == '' ) then
				-- Source Language is set and locLangSuffix is an empty string
				-- as the locLangSuffix is empty the current content language is english
				-- so we need to add /en to the link of this variant because it's source language
				-- is NOT english (source language pages have NO language suffix)
				locLangSuffix = '/en'
			end
			local pageTitle = landingPage..'/Answer_Key'..strVariantNumber..locLangSuffix
			tTabs['answers'][varKey] = {
				pageTitle = pageTitle,
				linkTitle = linkTitle,
				wikilink = '[['..pageTitle..'|'..linkTitle..']]'
			}
			mw.log('HonorTabs.convertVariantsToTabs (Tab URL '..varKey..': '..tTabs['answers'][varKey]['wikilink'])
		end
		mw.log('HonorTabs.convertVariantsToTabs: '..#tTabs['answers']..' Answer Variants were converted to Tabs!')
	end
	return tTabs
end

function createSimpleTab( wikiLink )
	local HTMLCode = mw.html.create( 'li' )
	HTMLCode
		:addClass( 'honor-nav-item' )
		:wikitext( wikiLink )
	return tostring( HTMLCode )
end

function createDropdownTab ( frame, tConfig, text, id, isSelflink )
	local HTMLCode = ""
	local HTMLCodeString = ""
	if ( tConfig == nil ) then
		mw.log('HonorTabs.createDropdownTab: Invalid Tab Config (nil)!')
	elseif ( #tConfig < 1 ) then
		mw.log('HonorTabs.createDropdownTab: Invalid Tab Config (< 1)!')
	elseif ( #tConfig == 1 ) then
		mw.log('HonorTabs.createDropdownTab (num, text, id, isSelflink): 1 Option, '..text..', '..id..', '..tostring(isSelflink))
		local transText = utilities.localize( text )
		-- check if Localization returned something, if not set the original string
		if ( transText == '' ) then
			transText = text
		end
		HTMLCodeString = createSimpleTab( '[['..tConfig[1].pageTitle..'|'..transText..']]' )
	else
		mw.log('HonorTabs.createDropdownTab (num, text, id, isSelflink): '..#tConfig..' Options, '..text..', '..id..', '..tostring(isSelflink))
		-- now there should be more then 1 variants
		local HTMLTabParent = mw.html.create( 'li' )
		local buttonArgs = {}
		buttonArgs['tagname'] = 'button'
		buttonArgs['class'] = 'dropdown-toggle'
		if (isSelflink == true) then
			buttonArgs['class'] = buttonArgs['class'] .. ' selflink'
		end
		buttonArgs['type'] = 'button'
		buttonArgs['id'] = 'dropdown'..id..'Keys'
		buttonArgs['data-toggle'] = 'dropdown'
		buttonArgs['aria-haspopup'] = 'true'
		buttonArgs['aria-expanded'] = 'false'
		buttonText = utilities.localize( text )
		-- check if Localization returned something, if not set the original string
		if ( buttonText == '' ) then
			buttonText = text
		end
		local dropdownButton = frame:extensionTag('htmltag', buttonText, buttonArgs)
		HTMLTabParent
			:addClass( 'honor-nav-item' )
			:addClass( 'dropdown' )
			:wikitext( dropdownButton )
		
		HTMLVariantsContainer = mw.html.create( 'div' )
		HTMLVariantsContainer
			:addClass( 'dropdown-menu' )
			:attr( 'aria-labelledby', 'dropdown'..id..'Keys')
		for key, tab in pairs(tConfig) do
			HTMLVariantLink = mw.html.create( 'span' )
			HTMLVariantLink
				:addClass( 'dropdown-item' )
				:wikitext( tab.wikilink )
				:done()
			HTMLVariantsContainer:node( HTMLVariantLink )
		end
		HTMLVariantsContainer:done()
		HTMLTabParent:node( HTMLVariantsContainer )
		HTMLCodeString = tostring( HTMLTabParent )
	end
	return HTMLCodeString
end
		
function generateTabURL( frame, page, langSuffix, text )
	-- check inputs
	if ( page == nil or page == "" ) then return 'fatal error' end
	
	local transText = utilities.localize( text )
	-- check if Localization returned something, if not set the original string
	if ( transText == '' ) then
		transText = text
	end
	
	return '[[' .. page .. langSuffix .. '|' .. transText .. ']]'
end

return p