Module:Navigation with arrows: Difference between revisions
mNo edit summary |
mNo edit summary |
||
(10 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
local | local u = require 'Module:Utilities' | ||
local pageName = nil | local pageName = nil | ||
local pFrame | local pFrame | ||
local | local testing = false | ||
Line 17: | Line 9: | ||
pFrame = frame:getParent() | pFrame = frame:getParent() | ||
local smwToExecute = '' | local smwToExecute = '' | ||
pageName = getFrameParam('PageName') | pageName = u.getFrameParam('PageName', pFrame) | ||
if isValidParam(pageName) then | if u.isValidParam(pageName) then | ||
testing = setTestState() | testing = u.setTestState(pFrame) | ||
pageName = | pageName = u.ucSanitize(pageName) | ||
local startIdx, maxNavLines, step = 1, 6, 1 | local startIdx, maxNavLines, step = 1, 6, 1 | ||
for catIdx = startIdx, maxNavLines, step do | for catIdx = startIdx, maxNavLines, step do | ||
local cat1, cat1Val = 'Row' .. catIdx .. 'Cat1', nil | local cat1, cat1Val = 'Row' .. catIdx .. 'Cat1', nil | ||
cat1Val = getFrameCategoryParam(cat1) | cat1Val = u.getFrameCategoryParam(cat1, pFrame) | ||
if isValidParam(cat1Val) then | if u.isValidParam(cat1Val) then | ||
if catIdx > 1 then smwToExecute = smwToExecute .. '<br/>' end | if catIdx > 1 then smwToExecute = smwToExecute .. '<br/>' end | ||
local cat2, cat2Val = 'Row' .. catIdx .. 'Cat2', nil | local cat2, cat2Val = 'Row' .. catIdx .. 'Cat2', nil | ||
cat2Val = getFrameCategoryParam(cat2) | cat2Val = u.getFrameCategoryParam(cat2, pFrame) | ||
smwToExecute = smwToExecute .. buildQuery( cat1Val, cat2Val, getFrameParam('Row' .. catIdx .. 'Lp'), getFrameParam('Row' .. catIdx .. 'LpSect'), getFrameParam('Row' .. catIdx .. 'LpAlias') ) | 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 | else break | ||
end | end | ||
Line 35: | Line 28: | ||
end | end | ||
if not testing then return frame:preprocess(smwToExecute) | if not testing then return frame:preprocess(smwToExecute) | ||
else return smwToExecute end | else return '<nowiki>' .. smwToExecute .. '</nowiki>' end | ||
end | end | ||
function buildQuery(cat1Val, cat2Val, lp, lpSect, lpAlias) | function buildQuery(cat1Val, cat2Val, lp, lpSect, lpAlias) | ||
local crit = | local crit = '[[Category:' .. cat1Val .. ']]' | ||
if isValidParam(cat2Val) then crit = crit | if u.isValidParam(cat2Val) then crit = crit .. '[[Category:' .. cat2Val .. ']]' end | ||
return buildQueryLine(crit, '<<', 'descending', 'ItemPrevious') .. buildLandingPage(lp, cat1Val, lpSect, lpAlias) .. buildQueryLine(crit, '>>', 'ascending', 'ItemNext') | return buildQueryLine(crit, '<<', 'descending', 'ItemPrevious') .. buildLandingPage(lp, cat1Val, lpSect, lpAlias) .. buildQueryLine(crit, '>>', 'ascending', 'ItemNext') | ||
end | end | ||
Line 47: | Line 40: | ||
function buildQueryLine(criteria, directionOperator, direction, outroTemplate) | function buildQueryLine(criteria, directionOperator, direction, outroTemplate) | ||
return '{{#ask:' .. criteria | 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 | end | ||
Line 53: | Line 46: | ||
function buildLandingPage(lp, cat, lpSect, lpAlias) | function buildLandingPage(lp, cat, lpSect, lpAlias) | ||
local landingPage | local landingPage | ||
if isValidParam(lp) then landingPage = lp else landingPage = cat end | if u.isValidParam(lp) then landingPage = lp else landingPage = cat end | ||
if isValidParam(lpSect) then landingPage = landingPage .. '#' .. lpSect | if u.isValidParam(lpSect) then landingPage = landingPage .. '#' .. lpSect | ||
end | end | ||
if isValidParam(lpSect) or isValidParam(lpAlias) then | if u.isValidParam(lpSect) or u.isValidParam(lpAlias) then | ||
landingPage = landingPage .. '|' | landingPage = landingPage .. '|' | ||
if isValidParam(lpAlias) then landingPage = landingPage .. lpAlias | if u.isValidParam(lpAlias) then landingPage = landingPage .. lpAlias | ||
else landingPage = landingPage .. lp | else landingPage = landingPage .. lp | ||
end | end | ||
end | end | ||
return | return '[[' .. landingPage .. ']]' | ||
end | end | ||
return p | return p |
Latest revision as of 01:49, 1 June 2022
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