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.
(start implementation with buttons only)
 
(add page content)
Line 1: Line 1:
local Args = require( 'Module:Args' )
local Args = require( 'Module:Args' )
local Box = require( 'Module:Box' ).Box
local Box = require( 'Module:Box' ).Box
local PAGE_NUMBER_FORMAT = "\{(#+)}"


local PagedGallery = Box.new()
local PagedGallery = Box.new()
Line 8: Line 10:
function PagedGallery.new( args )
function PagedGallery.new( args )
   args.class = 'zdw-paged-gallery'
   args.class = 'zdw-paged-gallery'
  local width = args.width or 300
  args.width = width * 2 .. 'px'
   local obj = Box.new( 'light', args )
   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 )
   return setmetatable( obj, PagedGallery )
end
end
Line 29: Line 35:
   addButton( nav, 'last', 'Last >>' )
   addButton( nav, 'last', 'Last >>' )


-- TODO pages
  -- 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
   return content
end
function PagedGallery:renderImage( pageNum )
  return '[[File:' .. self.args.format:gsub( PAGE_NUMBER_FORMAT, string.format( self.pageNumReplacementFormat, pageNum ) ) .. '|' .. self.imageWidth .. ']]'
end
end


Line 47: Line 74:
   getParent = function() return {
   getParent = function() return {
     args = {
     args = {
      format = 'The-Legend-of-Zelda-North-American-Instruction-Manual-Page-{##}.jpg',
      numPages = 47
     }
     }
   } end
   } end

Revision as of 01:04, 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 )
  args.class = 'zdw-paged-gallery'
  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' );
  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