Module:Message box

From annadreambrush.com/wiki
Revision as of 13:37, 19 September 2013 by imported>Mr. Stradivarius (start work on a module for displaying Template:Mbox-family message boxes)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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

-- This is a meta-module for producing message box templates, including {{mbox}}, {{ambox}}, {{imbox}}, {{tmbox}}, {{ombox}}, {{cmbox}} and {{fmbox}}.

local htmlBuilder = require('Module:HtmlBuilder')

local p = {}

function p.build(data, args)
    -- Process config data using the args passed to the template.
    local isSmall = args.small == 'yes' or args.small == true
    local typeData = data.types[args.type]
    local invalidType = args.type and not typeData and true or false
    typeData = typeData or data.types[data.default]

    -- Build the box.
    local root = htmlBuilder.create() -- The template root. Includes error messages and categories added after the box.
    local box = root.tag('table') -- The box.
    box
        .attr('id', args.id)
    for i, class in ipairs(data.classes) do
        box
            .addClass(class)
    end
    box
        .addClass(typeData.class)
        .addClass(args.class)
        .cssText(args.style)
        .attr('role', 'presentation')

    -- Add the left-hand image.
    local row = box.tag('tr')
    if args.image ~= 'none' then
        row.tag('td')
            .addClass('mbox-image')
            .wikitext(args.image or mw.ustring.format(
                '[[File:%s|%s|link=|alt=]]',
                typeData.image,
                (args.small == 'yes' or args.small == true) and data.imageSizeSmall or data.imageSizeLarge or data.imageSize
            ))
    elseif data.imageEmptyCell then
        row.tag('td')
            .addClass('mbox-empty-cell')
    end

    -- Add the text.
    row.tag('td')
        .addClass('mbox-text')
        .cssText(args.textstyle)
        .wikitext(args.text)

    -- Add the right-hand image.
    if args.imageright then
        row.tag('td')
            .addClass('mbox-imageright')
            .wikitext(args.imageright)
    end

    -- Add error messages and categories.
    if invalidType then
        local title = mw.title.getCurrentTitle()
        local catsort = (title.namespace == 0 and 'Main:' or '') .. title.prefixedText
        root.tag('div')
            .css('text-align', 'center')
            .wikitext(mw.ustring.format('This message box is using an invalid "type=%s" parameter and needs fixing.', args.type or ''))
            .done()
        .wikitext(mw.ustring.format('[[Category:Wikipedia message box parameter needs fixing|%s]]', catsort))
    end
    return tostring(root)
end

function p._fmbox(args)
    local data = {}
    data.types = {
        warning = {
            class = 'fmbox-warning',
            image = 'Cmbox deletion.png'
        },
        editnotice = {
            class = 'fmbox-editnotice',
            image = 'Imbox notice.png'
        },
        system = {
            class = 'fmbox-system',
            image = 'Imbox notice.png'
        }
    }
    data.default = 'system'
    data.classes = { 'plainlinks', 'fmbox' }
    data.imageSize = '40x40px'
    data.imageEmptyCell = false
    return p.build(data, args)
end

function p._ombox(args)
    local data = {}
    data.types = {
        speedy = {
            class = 'ombox-speedy',
            image = 'Imbox speedy deletion.png'
        },
        delete = {
            class = 'ombox-delete',
            image = 'Imbox deletion.png'
        },
        content = {
            class = 'ombox-content',
            image = 'Imbox content.png'
        },
        style = {
            class = 'ombox-style',
            image = 'Edit-clear.svg'
        },
        move = {
            class = 'ombox-move',
            image = 'Imbox move.png'
        },
        protection = {
            class = 'ombox-protection',
            image = 'Imbox protection.png'
        },
        notice = {
            class = 'ombox-notice',
            image = 'Imbox notice.png'
        }
    }
    data.default = 'notice'
    data.classes = {'plainlinks', 'ombox'}
    data.imageSizeLarge = '40x40px'
    data.imageSizeSmall = '30x30px'
    data.imageEmptyCell = true
    return p.build(data, args)
end

local function makeWrapper(func)
    return function (frame)
        -- If called via #invoke, use the args passed into the invoking
        -- template, or the args passed to #invoke if any exist. Otherwise
        -- assume args are being passed directly in from the debug console
        -- or from another Lua module.
        local origArgs
        if frame == mw.getCurrentFrame() then
            origArgs = frame:getParent().args
            for k, v in pairs(frame.args) do
                origArgs = frame.args
                break
            end
        else
            origArgs = frame
        end
        -- Trim whitespace and remove blank arguments.
        local args = {}
        for k, v in pairs(origArgs) do
            if type(v) == 'string' then
                v = mw.text.trim(v)
            end
            if v ~= '' then
                args[k] = v
            end
        end
        return func(args)
    end
end

p.fmbox = makeWrapper(p._fmbox)
p.ombox = makeWrapper(p._ombox)

return p