Module:Box: Difference between revisions
Jump to navigation
Jump to search
Want an adless experience? Log in or Create an account.
(add class parameter) |
(refactor with O-O) |
||
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 | local Box = {} | ||
Box.__index = Box | |||
function | function Box.new( boxType, args ) | ||
return | local obj = { | ||
boxType = boxType, | |||
args = args, | |||
categories = {} | |||
} | |||
return setmetatable( obj, Box ) | |||
end | end | ||
function | function Box:render() | ||
local root = mw.html.create( 'div' ) | |||
:addClass( self.boxType == 'dark' and 'darkbox' or '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 | ||
-- render title, with edit and toggle links if supplied | |||
function Box:renderHeader() | |||
local header = mw.html.create( 'div' ) | |||
:addClass( 'title' ) | |||
:css( 'text-align', 'center' ) | |||
:wikitext( self.args.title ) | |||
if self.args.edit then | |||
header:tag( 'span' ) | |||
:addClass( 'edit plainlinks' ) | |||
:tag( 'a' ) | |||
:attr( 'href', tostring( mw.uri.fullUrl( self.args.edit, { action = 'edit' } ) ) ) | |||
:wikitext( '[edit]' ) | |||
end | |||
if self.args.hide then | |||
header:wikitext( mw.getCurrentFrame():expandTemplate{ title = 'Toggler', args = { default = self.args.hide } } ) | |||
end | |||
return tostring( header ) | |||
end | |||
-- render content | |||
function Box:renderContent() | |||
local content = mw.html.create( 'div' ) | |||
:wikitext( self.args[1] ) | |||
return tostring( content ) | |||
end | |||
-- render footer with categories if provided | |||
-- so far Module:Navigation is the only thing that uses a footer, | |||
-- so this implementation is specific to that scenario. | |||
-- If other scenarios come up, this should be moved to Module:Navigation | |||
function Box:renderFooter() | |||
local footer = mw.html.create( 'div' ) | |||
:addClass( 'title hlist' ) | |||
:css( 'text-align', 'center' ) | |||
local catlist = footer:tag( 'ul' ) | |||
for i, v in ipairs( self.categories ) do | |||
catlist:tag( 'li' ) | |||
:wikitext( mw.getCurrentFrame():expandTemplate{ title = 'Catlink', args = { [1] = v } } ) | |||
end | |||
return tostring( footer ) | |||
end | |||
local p, mt = Box, {} | |||
function p._main( boxType, args ) | |||
local box = Box.new( boxType, args ) | |||
return box:render() | |||
end | |||
-- TODO deprecated, use p._main instead | |||
function p._light( args ) | function p._light( args ) | ||
return p._box( false, args ) | return p._box( false, args ) | ||
end | end | ||
-- TODO deprecated, use p._main instead | |||
function p._dark( args ) | function p._dark( args ) | ||
return p._box( true, args ) | |||
end | end | ||
-- | -- TODO deprecated | ||
function p._box( dark, args ) | function p._box( dark, args ) | ||
local box = mw.html.create( 'div' ) | local box = mw.html.create( 'div' ) | ||
Line 64: | Line 152: | ||
end | end | ||
return p | -- 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 | |||
return setmetatable( p, mt ) |
Revision as of 18:00, June 20, 2020
Documentation for this module may be created at Module:Box/doc
-- This is the base module for creating boxes with common styling. -- invoker args: -- class -- align ('left', 'right', 'center') -- width (e.g. '250px') -- title -- titlealign ('left', 'right', 'center') -- edit (e.g. 'Template:SomeNavbox') - adds an edit link to this page to the title bar -- hide ('hide' or 'show') - adds a toggle button to the title bar which toggles visibility of the content, with the specified default state local Box = {} Box.__index = Box function Box.new( boxType, args ) local obj = { boxType = boxType, args = args, categories = {} } return setmetatable( obj, Box ) end function Box:render() local root = mw.html.create( 'div' ) :addClass( self.boxType == 'dark' and 'darkbox' or '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 -- render title, with edit and toggle links if supplied function Box:renderHeader() local header = mw.html.create( 'div' ) :addClass( 'title' ) :css( 'text-align', 'center' ) :wikitext( self.args.title ) if self.args.edit then header:tag( 'span' ) :addClass( 'edit plainlinks' ) :tag( 'a' ) :attr( 'href', tostring( mw.uri.fullUrl( self.args.edit, { action = 'edit' } ) ) ) :wikitext( '[edit]' ) end if self.args.hide then header:wikitext( mw.getCurrentFrame():expandTemplate{ title = 'Toggler', args = { default = self.args.hide } } ) end return tostring( header ) end -- render content function Box:renderContent() local content = mw.html.create( 'div' ) :wikitext( self.args[1] ) return tostring( content ) end -- render footer with categories if provided -- so far Module:Navigation is the only thing that uses a footer, -- so this implementation is specific to that scenario. -- If other scenarios come up, this should be moved to Module:Navigation function Box:renderFooter() local footer = mw.html.create( 'div' ) :addClass( 'title hlist' ) :css( 'text-align', 'center' ) local catlist = footer:tag( 'ul' ) for i, v in ipairs( self.categories ) do catlist:tag( 'li' ) :wikitext( mw.getCurrentFrame():expandTemplate{ title = 'Catlink', args = { [1] = v } } ) end return tostring( footer ) end local p, mt = Box, {} function p._main( boxType, args ) local box = Box.new( boxType, args ) return box:render() end -- TODO deprecated, use p._main instead function p._light( args ) return p._box( false, args ) end -- TODO deprecated, use p._main instead function p._dark( args ) return p._box( true, args ) end -- TODO deprecated function p._box( dark, args ) local box = mw.html.create( 'div' ) :addClass( dark and 'darkbox' or 'box' ) :addClass( args.class ) -- box styles if args.align == 'right' or args.align == 'left' or args.align == 'center' then box:addClass( args.align ) end if args.width then box:css( 'width', args.width ) end -- title, with edit and toggle links if supplied if args.title then local title = box:tag( 'div' ) :addClass( 'title' ) :css( 'text-align', args.titlealign or 'center' ) :wikitext( args.title ) if args.edit then 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 title:wikitext( mw.getCurrentFrame():expandTemplate{ title = 'Toggler', args = { default = args.hide } } ) end end -- content, add toggle classes if necessary local content = box:tag( 'div' ) :addClass( args.hide and '_toggle_init' .. args.hide .. ' _toggle' ) return box, content end -- 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 return setmetatable( p, mt )