Module:Listbox: Difference between revisions

From Zelda Dungeon Wiki
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)
m (change label class to zdw-label to distinguish from bootstrap)
 
(45 intermediate revisions by the same user not shown)
Line 1: Line 1:
local p = require ( 'Module:Box' )
local Args = require( 'Module:Args' )
local Listing = require( 'Module:Listing' )


-- for use in the debug console:
function buildHList( parent, sections )
-- =p.list(p.debugframe)
  local list = parent:addClass( 'hlist' )
p.debugframe = {
    :tag( 'ul' )
  args = {
  for _, section in ipairs( sections ) do
    [1] = "A Link to the Past",
    -- !!!! This may break if Template:Main is modified !!!!
     [2] = "Enemies",
    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 p.list( frame )
function Listbox.new( args )
   local game = frame.args[1]
   local obj = Navbox.new( args )
   local category = frame.args[2]
   return setmetatable( obj, Listbox )
  local intersection = game .. ' ' .. category
end


  -- construct the box
-- override
   local box, content = p._light{
function Listbox:renderContent()
    class = 'navbox',
   local listing = Listing._parseListing( self.subject )
    title = intersection,
    hide = 'show', -- TODO count number of navboxes, hide after the second or third (could relegate to the calling template)
    edit = intersection
  }


   -- get list of pages in the category
   -- If categories weren't set already (because caller used single arg), then use the ones parsed from the page.
   local pages = mw.ext.dpl.getPagenames{ category = intersection, ordermethod = 'sortkey', order = 'ascending' }
   -- CODE SMELL: setting unrelated state. this only works because renderFooter is called after renderContent.
  -- TODO get the page contents, look for grouping and variant directives, and restructure the table accordingly
  if #self.categories == 0 then self.categories = listing.categories end


   -- TODO support groupings using table; for now just one big hlist
   local content = mw.html.create( 'table' )
   local hlist = content:tag( 'div' )
   if #listing.topLevelLeaves > 0 then
    :addClass( 'hlist' )
    local defaultCell = content:tag( 'tr' )
    :tag( 'ul' )
      :tag( 'td' )
   for i, v in ipairs(pages) do
      :addClass( 'odd' )
    -- TODO support variants
      :attr( 'colspan', '2' )
    hlist:tag( 'li' )
      :css( 'text-align', 'center' )
       :wikitext( '[[' .. v .. ']]' )
    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
   end


   -- TODO footer with catlinks (add it to the box module instead of hacking it on the end of the content's table)
   return content
end
 
local p = {}


   return tostring( box )
function p.main( frame )
  local listbox = Listbox.new( Args.fromFrame( frame ) )
   return listbox:render()
end
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
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