Difference between revisions of "Module:HonorTabs"

From Pathfinder Wiki
Line 9: Line 9:
 
end
 
end
 
local langSuffix = frame:expandTemplate{ title='GetLangSuffix' }
 
local langSuffix = frame:expandTemplate{ title='GetLangSuffix' }
 +
tVariants = honorUtils.getExpandedHonorVariantsV2( frame, landingPage )
 +
tAnsReqTabs = convertVariantsToTabs( frame, tVariants, landingPage, langSuffix)
 
local HTMLCodeString = ""
 
local HTMLCodeString = ""
 
-- create the overview tab
 
-- create the overview tab
 
HTMLCodeString = HTMLCodeString .. createSimpleTab(generateTabURL( frame, landingPage, langSuffix, 'Overview'))
 
HTMLCodeString = HTMLCodeString .. createSimpleTab(generateTabURL( frame, landingPage, langSuffix, 'Overview'))
 
-- create requirement tab
 
-- create requirement tab
 +
HTMLCodeString = HTMLCodeString .. createDropdownTab( frame, tAnsReqTabs['requirements'], 'Requirements', 'Req', false)
 
-- create answer variants tab
 
-- create answer variants tab
 +
HTMLCodeString = HTMLCodeString .. createDropdownTab( frame, tAnsReqTabs['answers'], 'Answer_Key', 'Ans', false)
 
-- create lesson plans tab
 
-- create lesson plans tab
 
HTMLCodeString = HTMLCodeString .. createSimpleTab(generateTabURL( frame, landingPage..'/Lesson_Plans', langSuffix, 'Lesson Plans'))
 
HTMLCodeString = HTMLCodeString .. createSimpleTab(generateTabURL( frame, landingPage..'/Lesson_Plans', langSuffix, 'Lesson Plans'))
 
-- create worksheets tab
 
-- create worksheets tab
 
HTMLCodeString = HTMLCodeString .. createSimpleTab(generateTabURL( frame, landingPage..'/Worksheet', langSuffix, 'Worksheet'))
 
HTMLCodeString = HTMLCodeString .. createSimpleTab(generateTabURL( frame, landingPage..'/Worksheet', langSuffix, 'Worksheet'))
tVariants = honorUtils.getExpandedHonorVariantsV2( frame, landingPage )
 
tSpecialTabs = convertVariantsToTabs( frame, tVariants, landingPage, langSuffix)
 
 
return HTMLCodeString
 
return HTMLCodeString
 
end
 
end
Line 168: Line 170:
 
end
 
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
 +
HTMLCodeString = createSimpleTab( tConfig[1].url )
 +
else
 +
-- now there should be more then 1 variants
 +
local HTMLTabParent = mw.html.create( 'li' )
 +
local buttonArgs = {}
 +
buttonArgs['tagname'] = 'button'
 +
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 = frame:expandTemplate{ title='Localize', args = { text } }
 +
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', 'dropdownAnswerKeys')
 +
for key, tab in pairs(tConfig) do
 +
HTMLVariantLink = mw.html.create( 'span' )
 +
HTMLVariantLink
 +
:addClass( 'dropdown-item' )
 +
:wikitext( tab.url )
 +
:done()
 +
HTMLVariantsContainer:node( HTMLVariantLink )
 +
end
 +
HTMLVariantsContainer:done()
 +
HTMLTabParent:node( HTMLVariantsContainer )
 +
HTMLCodeString = tostring( HTMLTabParent )
 +
end
 +
return HTMLCodeString
 +
end
 +
 
function generateTabURL( frame, page, langSuffix, text )
 
function generateTabURL( frame, page, langSuffix, text )
 
-- check inputs
 
-- check inputs

Revision as of 22:49, 10 July 2021

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

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

function p.generateHonorTabs( frame )
	local parentFrame = frame:getParent()
	local landingPage = parentFrame.args['landingPage']
	if landingPage == nil or landingPage == '' then
		landingPage = frame:expandTemplate{ title='GetHonorLandingPage' }
	end
	local langSuffix = frame:expandTemplate{ title='GetLangSuffix' }
	tVariants = honorUtils.getExpandedHonorVariantsV2( frame, landingPage )
	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', false)
	-- create answer variants tab
	HTMLCodeString = HTMLCodeString .. createDropdownTab( frame, tAnsReqTabs['answers'], 'Answer_Key', 'Ans', false)
	-- 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'))
	return HTMLCodeString
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['answers'] == nil or
		 tVariants['answers']['split'] == nil ) then
		mw.log('HonorTabs.convertVariantsToTabs: No Answer Variants! Create default Tab.')
		tTabs['answers'][1] = {
			url = '[['..landingPage..'/Answer_Key'..langSuffix..'|'..frame:expandTemplate{ title = 'Localize', args = {'Answer Key'} }..']]'
		}
	else
		mw.log('HonorTabs.convertVariantsToTabs: '..#tVariants['answers']['split']..' Answer Variants to convert!')
		for varKey, variant in pairs(tVariants['answers']['split']) do
			local linkTitle = ""
			if ( variant['countries'] == nil or variant['countries']['split'] == nil) then
				mw.log('HonorTabs.convertVariantsToTabs: Invalid Answer Variant (Countries)!')
			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 or
				 variant['authority']['translated'] == nil or
				 variant['authority']['translated'] == '') then
				mw.log('HonorTabs.convertVariantsToTabs: No Authority for this variant!')
			else
				linkTitle = linkTitle..' ('..variant['authority']['translated']..')'
			end
			tTabs['answers'][varKey] = {
				url = '[['..landingPage..'/Answer_Key/'..varKey..langSuffix..'|'..linkTitle..']]'
			}
			mw.log('HonorTabs.convertVariantsToTabs (Tab URL '..varKey..': '..tTabs['answers'][varKey]['url'])
		end
		mw.log('HonorTabs.convertVariantsToTabs: '..#tTabs['answers']..' Answer Variants were converted to Tabs!')
	end
	if ( tVariants['requirements'] == nil or
		 tVariants['requirements']['split'] == nil ) then
		mw.log('HonorTabs.convertVariantsToTabs: No Requirement Variants! Create default Tab.')
		tTabs['answers'][1] = {
			url = '[['..landingPage..'/Requirements'..langSuffix..'|'..frame:expandTemplate{ title = 'Localize', args = {'Requirements'} }..']]'
		}
	else
		mw.log('HonorTabs.convertVariantsToTabs: '..#tVariants['requirements']['split']..' Requriement Variants to convert!')
		for varKey, variant in pairs(tVariants['requirements']['split']) 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
			tTabs['requirements'][varKey] = {
				url = '[['..landingPage..'/Requirement/'..varKey..langSuffix..'|'..linkTitle..']]'
			}
			mw.log('HonorTabs.convertVariantsToTabs (Tab URL '..varKey..': '..tTabs['requirements'][varKey]['url'])
		end
		mw.log('HonorTabs.convertVariantsToTabs: '..#tTabs['requirements']..' Requirement Variants were converted to Tabs!')
	end
	return tTabs
end

function p.createAnswerTab( frame )
	local ansVariants = ""
	if frame.args.answerVariants then ansVariants = frame.args.answerVariants end
	local landingPage = frame.args.landingPage
	local t_answerVariants = honorUtils.getExpandedHonorVariants(frame, ansVariants, landingPage, "Answer Key")
	result_string = outputVariantsTab(frame, t_answerVariants)
	return result_string
end

function outputVariantsTab( frame, variants )
	local HTMLCode = ""
	local HTMLCodeString = ""
	if (variants == nil) then
		-- should not happen!
		HTMLCodeString = "no variants?! = nil"
	elseif (#variants < 1) then
		-- should not happen!
		HTMLCodeString = "no variants?! < 1"
	elseif (#variants == 1) then
		HTMLCodeString = createSimpleTab( variants[1].url )
	else
		-- now there should be more then 1 variants
		local HTMLTabParent = mw.html.create( 'li' )
		local buttonArgs = {}
		buttonArgs['tagname'] = 'button'
		local explodedPageName = mw.text.split( frame:expandTemplate{title='PAGENAMEwithoutLang'}, '/', true)
		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( 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

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
		HTMLCodeString = createSimpleTab( tConfig[1].url )
	else
		-- now there should be more then 1 variants
		local HTMLTabParent = mw.html.create( 'li' )
		local buttonArgs = {}
		buttonArgs['tagname'] = 'button'
		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 = frame:expandTemplate{ title='Localize', args = { text } }
		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', 'dropdownAnswerKeys')
		for key, tab in pairs(tConfig) do
			HTMLVariantLink = mw.html.create( 'span' )
			HTMLVariantLink
				:addClass( 'dropdown-item' )
				:wikitext( tab.url )
				: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
	
	return '[[' .. page .. langSuffix .. '|' .. frame:expandTemplate{ title='Localize', args = { text } } .. ']]'
end

return p