Module:Navigation with arrows

From International Robin Hood Bibliography
Revision as of 17:27, 25 May 2022 by Henryfunk (talk | contribs)

The module expects template calls like {{NavigationArrows|Row1Cat1=|Row1Cat2=|Row1Lp=|Row1LpSect=|Row1LpAlias=}}, where only the first parameter is required to exist and hold a non-empty string. The parameter set |Row1Cat1=|Row1Cat2=|Row1Lp=|Row1LpSect=|Row1LpAlias= may be repeated up to five times with Row1Cat1 incremented to Row2Cat1, Row1Cat2 to Row2Cat2 etc., the highest meaningful set of parameters being Row6Cat1 etc. The landing page link and text are Row1Cat1 (etc.) unless a valid value is supplied for Row1LpSect (etc.), in which case a valid value for Row1LpAlias (etc.) takes precedence over any for Row1Cat1 (etc.) as text. For category names that are to be uppercased (and have any relevant entity reference normalized to a corresponding apostrophe), the template parameters Row1Cat1Uc and Row1Cat2Uc must be used instead of Row1Cat1 and Row1Cat2.


local p = {}
local u = require 'Module:Utilities'
local pageName = nil
local pFrame
local testing = false


function p.createNavigation(frame)
	pFrame = frame:getParent()
	local smwToExecute = ''
	pageName = u.getFrameParam('PageName', pFrame)
	if u.isValidParam(pageName) then
		testing = u.setTestState(pFrame)
		pageName = u.ucSanitize(pageName)
		local startIdx, maxNavLines, step = 1, 6, 1
		for catIdx = startIdx, maxNavLines, step do
			local cat1, cat1Val  = 'Row' .. catIdx .. 'Cat1', nil
			cat1Val = u.getFrameCategoryParam(cat1, pFrame)
			if u.isValidParam(cat1Val) then
				if catIdx > 1 then smwToExecute = smwToExecute .. '<br/>' end
				local cat2, cat2Val  = 'Row' .. catIdx .. 'Cat2', nil
				cat2Val = u.getFrameCategoryParam(cat2, pFrame)
				smwToExecute = smwToExecute .. buildQuery( cat1Val, cat2Val, u.getFrameParam('Row' .. catIdx .. 'Lp', pFrame), u.getFrameParam('Row' .. catIdx .. 'LpSect', pFrame), u.getFrameParam('Row' .. catIdx .. 'LpAlias', pFrame) )
				else break
			end
		end
	end
	if not testing then return frame:preprocess(smwToExecute)
	else return '<nowiki>' .. smwToExecute .. '</nowiki>' end
end


function buildQuery(cat1Val, cat2Val, lp, lpSect, lpAlias)
	local crit = '[[Category:' .. cat1Val .. ']]'
	if u.isValidParam(cat2Val) then crit = crit .. '[[Category:' .. cat2Val .. ']]' end
	return buildQueryLine(crit, '<<', 'descending', 'ItemPrevious') .. buildLandingPage(lp, cat1Val, lpSect, lpAlias) .. buildQueryLine(crit, '>>', 'ascending', 'ItemNext')
end


function buildQueryLine(criteria, directionOperator, direction, outroTemplate)
	return '{{#ask:' .. criteria .. '[[Utitle::' .. directionOperator .. pageName .. ']]|order=' .. direction .. '|searchlabel=|format=template|introtemplate=' .. outroTemplate .. 'Prefix|template=ItemPrint|outrotemplate=' .. outroTemplate .. '|link=none|limit=1|sort=Utitle}}'
end


function buildLandingPage(lp, cat, lpSect, lpAlias)
	local landingPage
	if u.isValidParam(lp) then landingPage = lp else landingPage = cat end
	
	if u.isValidParam(lpSect) then landingPage = landingPage .. '#' .. lpSect
	end
	
	if u.isValidParam(lpSect) or u.isValidParam(lpAlias) then
		landingPage = landingPage .. '|'
		if u.isValidParam(lpAlias) then landingPage = landingPage .. lpAlias
			else landingPage = landingPage .. lp
		end
	end
 	return '[[' .. landingPage .. ']]'
end

return p