Module:Box: Difference between revisions

Jump to navigation Jump to search
Want an adless experience? Log in or Create an account.
1,117 bytes added ,  June 22, 2020
switch to zdw selectors
(output content so subclasses can do what they want with it. I'll need to add content support to the base Box functions later.)
(switch to zdw selectors)
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
-- This is the base module for creating boxes with common styling.
-- This is the base module for creating boxes with common styling.
-- invoker args:
-- invoker args:
--  content (or [1]) (required)
--  class
--  align ('left', 'right', 'center')
--  align ('left', 'right', 'center')
--  width (e.g. '250px')
--  width (e.g. '250px')
Line 9: Line 9:
--  hide ('hide' or 'show') - adds a toggle button to the title bar which toggles visibility of the content, with the specified default state
--  hide ('hide' or 'show') - adds a toggle button to the title bar which toggles visibility of the content, with the specified default state


local p = {}
local Box = {}
Box.__index = Box


function p.light( frame )
function Box.new( boxType, args )
     return tostring( p._light( frame.args ) )
  local obj = {
    boxType = boxType,
    args = args,
     categories = {}
  }
 
  return setmetatable( obj, Box )
end
end


function p.dark( frame )
function Box:render()
     return tostring( p._dark( frame.args ) )
  local root = mw.html.create( 'div' )
    :addClass( self.boxType == 'dark' and 'zdw-box--dark' or 'zdw-box' )
    :addClass( self.args.class )
 
    -- box styles
     if self.args.align == 'right'
      or self.args.align == 'left'
      or self.args.align == 'center'
      then root:addClass( self.args.align )
    end
    if self.args.width then root:css( 'width', self.args.width ) end
 
    if self.args.title then
      root:node( self:renderHeader() )
    end
 
    root:tag( 'div' )
      :addClass( self.args.hide and '_toggle_init' .. self.args.hide .. ' _toggle' )
      :node( self:renderContent() )
 
    if #self.categories > 0 then
      root:node( self:renderFooter() )
    end
 
  return tostring( root )
end
end


function p._light( args )
-- render title, with edit and toggle links if supplied
     return p._box( false, args )
function Box:renderHeader()
  local header = mw.html.create( 'div' )
    :addClass( 'zdw-box__title' )
    :css( 'text-align', 'center' )
    :wikitext( self.args.title )
 
  if self.args.edit then
    header:tag( 'span' )
      :addClass( 'edit plainlinks' )
      :wikitext( '[' .. tostring( mw.uri.fullUrl( self.args.edit, { action = 'edit' } ) ) .. ' [edit]]' )
  end
 
  if self.args.hide then
     header:wikitext( mw.getCurrentFrame():expandTemplate{ title = 'Toggler', args = { default = self.args.hide } } )
  end
 
  return header
end
end


function p._dark( args )
-- render content
    return p._box( true, args )
function Box:renderContent()
  local content = mw.html.create( 'div' )
    :wikitext( self.args[1] )
 
  return tostring( content )
end
end


-- Constructs the box
-- render footer with categories if provided
function p._box( dark, args )
-- so far Module:Navigation is the only thing that uses a footer,
    local box = mw.html.create( 'div' )
--  so this implementation is specific to that scenario.
        :addClass( dark and 'darkbox' or 'box' )
--  If other scenarios come up, this should be moved to Module:Navigation
function Box:renderFooter()
  local footer = mw.html.create( 'div' )
    :addClass( 'zdw-box__title zdw-hlist' )
    :css( 'text-align', 'center' )


    -- box styles
  local catlist = footer:tag( 'ul' )
    if args.align == 'right' or args.align == 'left' or args.align == 'center' then box:addClass( args.align ) end
  for i, v in ipairs( self.categories ) do
    if args.width then box:css( 'width', args.width ) end
    catlist:tag( 'li' )
      :wikitext( mw.getCurrentFrame():expandTemplate{ title = 'Catlink', args = { [1] = v } } )
  end


    -- title, with edit and toggle links if supplied
  return tostring( footer )
    if args.title then
end
        local title = box:tag( 'div' )
            :addClass( 'title' )
            :css( 'text-align', args.titlealign or 'center' )
            :wikitext( args.title )


        if args.edit then
local p, mt = {}, {}
            title:tag( 'span' )
                :addClass( 'edit plainlinks' )
                :tag( 'a' )
                    :attr( 'href', tostring( mw.uri.fullUrl( args.edit, { action = 'edit' } ) ) )
                    :wikitext( '[edit]' )
        end


        if args.hide then
p.Box = Box
            title:wikitext( mw.getCurrentFrame():expandTemplate{ title = 'Toggler', args = { default = args.hide } } )
        end
    end


    -- content, add toggle classes if necessary
function p._main( boxType, args )
    local content = box:tag( 'div' )
  local box = Box.new( boxType, args )
        :addClass( args.hide and '_toggle_init' .. args.hide .. ' _toggle' )
  return box:render()
end


     return box, content
-- translates p.function( frame ) to p._main( function, args )
function mt.__index( table, key )
  return function ( frame )
     return table._main( key, frame.args )
  end
end
end


return p
return setmetatable( p, mt )

Navigation menu