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.
mNo edit summary
m (change label class to zdw-label to distinguish from bootstrap)
 
(43 intermediate revisions by the same user not shown)
Line 1: Line 1:
local Box = require( 'Module:Box' ).Box
local Args = require( 'Module:Args' )
local Listing = require( 'Module:Listing' )


local Navbox = Box.new()
function buildHList( parent, sections )
Navbox.__index = Navbox
  local list = parent:addClass( 'hlist' )
setmetatable( Navbox, Box )
    :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


function Navbox.new( format, args )
local Navbox = Listing.Navbox
  local subject = args[1] .. ' ' .. args[2]
 
  args.class = 'navbox'
local Listbox = setmetatable( {}, Navbox )
  args.title = subject
Listbox.__index = Listbox
  args.edit = subject
 
  args.hide = 'show' -- TODO count number of navboxes, hide after the second or third (could relegate to the calling template)
function Listbox.new( args )
   local obj = Box.new( 'light', args )
   local obj = Navbox.new( args )
  obj.subject = subject
   return setmetatable( obj, Listbox )
  obj.categories = { args[1], subject, args[2] }
   return setmetatable( obj, Navbox )
end
end


function Navbox:renderContent()
-- override
  -- get list of pages in the category
function Listbox:renderContent()
   local pages = mw.ext.dpl.getPagenames{ category = self.subject, ordermethod = 'sortkey', order = 'ascending' }
   local listing = Listing._parseListing( self.subject )
  -- 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
   -- If categories weren't set already (because caller used single arg), then use the ones parsed from the page.
   local content = mw.html.create( 'div' )
  -- CODE SMELL: setting unrelated state. this only works because renderFooter is called after renderContent.
     :addClass( 'hlist' )
  if #self.categories == 0 then self.categories = listing.categories end
  local hlist = content:tag( 'ul' )
 
   for i, v in ipairs(pages) do
   local content = mw.html.create( 'table' )
    -- TODO support variants
  if #listing.topLevelLeaves > 0 then
    hlist:tag( 'li' )
     local defaultCell = content:tag( 'tr' )
       :wikitext( '[[' .. v .. ']]' )
      :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
   end


   return tostring( content )
   return content
end
end


local p, mt = {}, {}
local p = {}
 
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 p.main( frame )
function mt.__index( table, key )
  local listbox = Listbox.new( Args.fromFrame( frame ) )
   return function ( frame )
   return listbox:render()
    return table._main( key, frame.args )
  end
end
end


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


return setmetatable( p, mt )
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