Module:Listbox

From Zelda Dungeon Wiki
Revision as of 03:59, June 21, 2020 by Locke (talk | contribs)
Jump to navigation Jump to search
Want an adless experience? Log in or Create an account.

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( 'light', 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 )