Module:Listbox: Difference between revisions
Jump to navigation
Jump to search
Want an adless experience? Log in or Create an account.
m (Locke moved page Module:Navigation to Module:Navbox without leaving a redirect) |
(refactor to inherit from Box in a more O-O way) |
||
Line 1: | Line 1: | ||
local | local Box = require( 'Module:Box' ).Box | ||
local Navbox = Box.new() | |||
Navbox.__index = Navbox | |||
setmetatable( Navbox, Box ) | |||
function | function Navbox.new( format, args ) | ||
local | local subject = args[1] .. ' ' .. args[2] | ||
args.class = 'navbox' | |||
args.title = subject | |||
args.edit = subject | |||
args.hide = 'show' -- TODO count number of navboxes, hide after the second or third (could relegate to the calling template) | |||
local obj = Box.new( 'dark', args ) | |||
obj.subject = subject | |||
obj.categories = { args[1], subject, args[2] } | |||
return setmetatable( obj, Navbox ) | |||
end | |||
} | |||
function Navbox:renderContent() | |||
-- get list of pages in the category | -- get list of pages in the category | ||
local pages = mw.ext.dpl.getPagenames{ category = | local pages = mw.ext.dpl.getPagenames{ category = self.subject, ordermethod = 'sortkey', order = 'ascending' } | ||
-- TODO get the page contents, look for grouping and variant directives, and restructure the table accordingly | -- TODO get the page contents, look for grouping and variant directives, and restructure the table accordingly | ||
-- TODO support groupings using table; for now just one big hlist | -- TODO support groupings using table; for now just one big hlist | ||
local | local content = mw.html.create( 'div' ) | ||
:addClass( 'hlist' ) | :addClass( 'hlist' ) | ||
local hlist = content:tag( 'ul' ) | |||
for i, v in ipairs(pages) do | for i, v in ipairs(pages) do | ||
-- TODO support variants | -- TODO support variants | ||
Line 37: | Line 32: | ||
end | end | ||
return tostring( content ) | |||
end | |||
return | local p, mt = {}, {} | ||
function p._main( format, args ) | |||
local navbox = Navbox.new( format, args ) | |||
return navbox:render() | |||
end | end | ||
return p | -- translates p.function( frame ) to p._main( function, args ) | ||
function mt.__index( table, key ) | |||
return function ( frame ) | |||
return table._main( key, frame.args ) | |||
end | |||
end | |||
-- for use in the debug console: | |||
-- =p.list(p.debugframe) | |||
p.debugframe = { | |||
args = { | |||
[1] = "A Link to the Past", | |||
[2] = "Enemies", | |||
} | |||
} | |||
return setmetatable( p, mt ) |
Revision as of 03:58, June 21, 2020
Documentation for this module may be created at Module:Listbox/doc
local Box = require( 'Module:Box' ).Box local Navbox = Box.new() Navbox.__index = Navbox setmetatable( Navbox, Box ) function Navbox.new( format, args ) local subject = args[1] .. ' ' .. args[2] args.class = 'navbox' args.title = subject args.edit = subject args.hide = 'show' -- TODO count number of navboxes, hide after the second or third (could relegate to the calling template) local obj = Box.new( 'dark', args ) obj.subject = subject obj.categories = { args[1], subject, args[2] } return setmetatable( obj, Navbox ) end function Navbox:renderContent() -- get list of pages in the category local pages = mw.ext.dpl.getPagenames{ category = self.subject, ordermethod = 'sortkey', order = 'ascending' } -- TODO get the page contents, look for grouping and variant directives, and restructure the table accordingly -- TODO support groupings using table; for now just one big hlist local content = mw.html.create( 'div' ) :addClass( 'hlist' ) local hlist = content:tag( 'ul' ) for i, v in ipairs(pages) do -- TODO support variants hlist:tag( 'li' ) :wikitext( '[[' .. v .. ']]' ) end return tostring( content ) end local p, mt = {}, {} function p._main( format, args ) local navbox = Navbox.new( format, args ) return navbox:render() end -- translates p.function( frame ) to p._main( function, args ) function mt.__index( table, key ) return function ( frame ) return table._main( key, frame.args ) end end -- for use in the debug console: -- =p.list(p.debugframe) p.debugframe = { args = { [1] = "A Link to the Past", [2] = "Enemies", } } return setmetatable( p, mt )