Module:Listbox: Difference between revisions
Jump to navigation
Jump to search
Want an adless experience? Log in or Create an account.
(use args everywhere instead of extra) |
m (change label class to zdw-label to distinguish from bootstrap) |
||
(28 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
local | local Args = require( 'Module:Args' ) | ||
local | local Listing = require( 'Module:Listing' ) | ||
function | function buildHList( parent, sections ) | ||
local | local list = parent:addClass( 'hlist' ) | ||
local | :tag( 'ul' ) | ||
for _, section in ipairs( sections ) do | |||
-- !!!! This may break if Template:Main is modified !!!! | |||
local | local expandedMainTemplate = section.summary:match( 'Main article: %[%[(.-)]]' ) | ||
local link = expandedMainTemplate and mw.text.split( expandedMainTemplate, '|' )[1] | |||
list:tag( 'li' ) | |||
:wikitext( '[[' .. (link or section.name) .. '|' .. section.name .. ']]' ) | |||
end | end | ||
end | end | ||
local Navbox = Listing.Navbox | |||
local Listbox = setmetatable( {}, Navbox ) | |||
Listbox.__index = Listbox | |||
function Listbox.new( args ) | |||
local obj = Navbox.new( args ) | |||
return setmetatable( obj, Listbox ) | |||
end | end | ||
function | -- override | ||
function Listbox:renderContent() | |||
local listing = Listing._parseListing( self.subject ) | |||
local | |||
-- If categories weren't set already (because caller used single arg), then use the ones parsed from the page. | |||
-- CODE SMELL: setting unrelated state. this only works because renderFooter is called after renderContent. | |||
if #self.categories == 0 then self.categories = listing.categories end | |||
local content = mw.html.create( 'table' ) | local content = mw.html.create( 'table' ) | ||
if # | if #listing.topLevelLeaves > 0 then | ||
local defaultCell = content:tag( 'tr' ) | local defaultCell = content:tag( 'tr' ) | ||
:tag( 'td' ) | :tag( 'td' ) | ||
Line 121: | Line 39: | ||
:attr( 'colspan', '2' ) | :attr( 'colspan', '2' ) | ||
:css( 'text-align', 'center' ) | :css( 'text-align', 'center' ) | ||
buildHList( defaultCell, listing.topLevelLeaves ) | |||
end | end | ||
if | if #listing.groups > 0 then | ||
for index, group in ipairs( listing.groups ) do | |||
local row = content:tag( 'tr' ) | |||
for index, group in ipairs( groups ) do | row:tag( 'th' ) | ||
:addClass( 'zdw-label' ) | |||
:wikitext( group.name ) | |||
local cell = row:tag( 'td' ) | |||
:addClass( (index + (#listing.topLevelLeaves > 0 and 1 or 0)) % 2 == 0 and 'even' or 'odd' ) | |||
buildHList( cell, group.sections ) | |||
end | end | ||
end | end | ||
Line 142: | Line 56: | ||
end | end | ||
local p = {} | |||
local p | |||
function p. | function p.main( frame ) | ||
local | local listbox = Listbox.new( Args.fromFrame( frame ) ) | ||
return listbox:render() | |||
return | |||
end | end | ||
-- for use in the debug console: | -- for use in the debug console: | ||
-- =p. | -- =p.main(p.debugframe) | ||
p.debugframe = { | p.debugframe = { | ||
args = { | args = {}, | ||
[1] = "The Legend of Zelda" | getParent = function() return { | ||
args = { | |||
} | [1] = "The Legend of Zelda Locations" | ||
} | |||
} end | |||
} | } | ||
return | return p |
Latest revision as of 02:17, November 16, 2020
Documentation for this module may be created at Module:Listbox/doc
local Args = require( 'Module:Args' ) local Listing = require( 'Module:Listing' ) function buildHList( parent, sections ) local list = parent:addClass( 'hlist' ) :tag( 'ul' ) for _, section in ipairs( sections ) do -- !!!! This may break if Template:Main is modified !!!! local expandedMainTemplate = section.summary:match( 'Main article: %[%[(.-)]]' ) local link = expandedMainTemplate and mw.text.split( expandedMainTemplate, '|' )[1] list:tag( 'li' ) :wikitext( '[[' .. (link or section.name) .. '|' .. section.name .. ']]' ) end end local Navbox = Listing.Navbox local Listbox = setmetatable( {}, Navbox ) Listbox.__index = Listbox function Listbox.new( args ) local obj = Navbox.new( args ) return setmetatable( obj, Listbox ) end -- override function Listbox:renderContent() local listing = Listing._parseListing( self.subject ) -- If categories weren't set already (because caller used single arg), then use the ones parsed from the page. -- CODE SMELL: setting unrelated state. this only works because renderFooter is called after renderContent. if #self.categories == 0 then self.categories = listing.categories end local content = mw.html.create( 'table' ) if #listing.topLevelLeaves > 0 then local defaultCell = content:tag( 'tr' ) :tag( 'td' ) :addClass( 'odd' ) :attr( 'colspan', '2' ) :css( 'text-align', 'center' ) buildHList( defaultCell, listing.topLevelLeaves ) end if #listing.groups > 0 then for index, group in ipairs( listing.groups ) do local row = content:tag( 'tr' ) row:tag( 'th' ) :addClass( 'zdw-label' ) :wikitext( group.name ) local cell = row:tag( 'td' ) :addClass( (index + (#listing.topLevelLeaves > 0 and 1 or 0)) % 2 == 0 and 'even' or 'odd' ) buildHList( cell, group.sections ) end end return content end local p = {} function p.main( frame ) local listbox = Listbox.new( Args.fromFrame( frame ) ) return listbox:render() end -- for use in the debug console: -- =p.main(p.debugframe) p.debugframe = { args = {}, getParent = function() return { args = { [1] = "The Legend of Zelda Locations" } } end } return p