Module:Listbox

From Zelda Dungeon Wiki
Revision as of 00:28, July 26, 2020 by Locke (talk | contribs) (Locke moved page Module:Navbox to Module:Listbox without leaving a redirect: swapping names so that Galbox and Listbox extend Navbox)
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 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
    list:tag( 'li' )
      :wikitext( '[[' .. section.name .. ']]' )
  end
end

local Listbox = Listing.Listbox

local Navbox = setmetatable( {}, Listbox )
Navbox.__index = Navbox

function Navbox.new( args )
  local obj = Listbox.new( args )
  return setmetatable( obj, Navbox )
end

-- override
function Navbox:renderContent()
  local listing = Listing._parseListing( 'User:Locke/Sandbox/Listing' ) -- TODO self.subject )

  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( '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 navbox = Navbox.new( Args.fromFrame( frame ) )
  return navbox:render()
end

-- for use in the debug console:
-- =p.main(p.debugframe)
p.debugframe = {
  args = {},
  getParent = function() return {
    args = {
      [1] = "The Legend of Zelda",
      [2] = "Enemies"
    }
  } end
}

return p