Module:PagedGallery: Difference between revisions

From Zelda Dungeon Wiki
Jump to navigation Jump to search
Want an adless experience? Log in or Create an account.
m (fix regex escapes)
m (move 'zdw-paged-gallery' class to content div so the js works)
 
Line 9: Line 9:


function PagedGallery.new( args )
function PagedGallery.new( args )
  args.class = 'zdw-paged-gallery'
   local width = args.width or 300
   local width = args.width or 300
   args.width = width * 2 .. 'px'
   args.width = width * 2 .. 'px'
Line 27: Line 26:
-- override Box:renderContent
-- override Box:renderContent
function PagedGallery:renderContent()
function PagedGallery:renderContent()
   local content = mw.html.create( 'div' );
   local content = mw.html.create( 'div' )
    :addClass( 'zdw-paged-gallery' )
   local nav = content:tag( 'div' )
   local nav = content:tag( 'div' )
     :addClass( 'zdw-paged-gallery__navigation' )
     :addClass( 'zdw-paged-gallery__navigation' )

Latest revision as of 01:31, April 3, 2021

Documentation for this module may be created at Module:PagedGallery/doc

local Args = require( 'Module:Args' )
local Box = require( 'Module:Box' ).Box

local PAGE_NUMBER_FORMAT = "%((#+)%)"

local PagedGallery = Box.new()
PagedGallery.__index = PagedGallery
setmetatable( PagedGallery, Box )

function PagedGallery.new( args )
  local width = args.width or 300
  args.width = width * 2 .. 'px'
  local obj = Box.new( 'light', args )
  obj.imageWidth = width .. 'px'
  obj.pageNumReplacementFormat = "%0" .. #string.match( args.format, PAGE_NUMBER_FORMAT ) .. "d"
  return setmetatable( obj, PagedGallery )
end

function addButton( nav, kind, text )
  nav:tag( 'div' )
    :addClass( 'zdw-button zdw-button--paging' )
    :attr( 'data-nav', kind )
    :wikitext( text )
end

-- override Box:renderContent
function PagedGallery:renderContent()
  local content = mw.html.create( 'div' )
    :addClass( 'zdw-paged-gallery' )
  local nav = content:tag( 'div' )
    :addClass( 'zdw-paged-gallery__navigation' )
  addButton( nav, 'first', '<< First' )
  addButton( nav, 'prev', '< Previous' )
  addButton( nav, 'next', 'Next >' )
  addButton( nav, 'last', 'Last >>' )

  -- cover
  content:tag( 'div' )
    :addClass( 'zdw-paged-gallery__page active' )
    :wikitext( self:renderImage( 0 ) )

  -- pages
  for i = 1, self.args.numPages - 1, 2 do
    content:tag( 'div' )
      :addClass( 'zdw-paged-gallery__page' )
      :wikitext( self:renderImage( i ) .. self:renderImage( i + 1 ) )
  end

  -- back
  if self.args.numPages % 2 == 1 then
    content:tag( 'div' )
      :addClass( 'zdw-paged-gallery__page' )
      :wikitext( self:renderImage( self.args.numPages ) )
  end

  return content
end

function PagedGallery:renderImage( pageNum )
  return '[[File:' .. self.args.format:gsub( PAGE_NUMBER_FORMAT, string.format( self.pageNumReplacementFormat, pageNum ) ) .. '|' .. self.imageWidth .. ']]'
end

local p = {}

function p.main( frame )
  local gallery = PagedGallery.new( Args.fromFrame( frame ) )
  return gallery:render()
end

-- for use in the debug console:
-- =p.main(p.debugframe)
p.debugframe = {
  args = {},
  getParent = function() return {
    args = {
      format = 'The-Legend-of-Zelda-North-American-Instruction-Manual-Page-(##).jpg',
      numPages = 47
    }
  } end
}

return p