Module:Box

From Zelda Dungeon Wiki
Revision as of 06:17, February 8, 2017 by Locke (talk | contribs) (add class parameter)
Jump to navigation Jump to search
Want an adless experience? Log in or Create an account.

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 p = {}

function p.light( frame )
    return tostring( p._light( frame.args ) )
end

function p.dark( frame )
    return tostring( p._dark( frame.args ) )
end

function p._light( args )
    return p._box( false, args )
end

function p._dark( args )
    return p._box( true, args )
end

-- Constructs the box
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

return p