模块:Mbox

来自Timberborn Wiki
跳转到导航 跳转到搜索

本模块一般透过 {{通知}}使用,用来制作通知模板


-- 本模块基于Lua语言,用来制作模板。
-- 关于Lua语言,详见[[w:Help:Lua]].
-- The Fandom Developer's Wiki hosts Global Lua Modules that can be imported and locally overridden.
-- The next line imports the Mbox module from the [[w:c:dev:Global Lua Modules]].
local p = require('Dev:Mbox')
-- See more details about this module at [[w:c:dev:Global_Lua_Modules/Mbox]]

-- For a more flexible Mbox experience, import 
-- https://dev.fandom.com/wiki/MediaWiki:Global_Lua_Modules/Mbox.css
-- or paste (and modify as you like) its contents in your wiki's 
-- [[MediaWiki:Wikia.css]] (see [[w:Help:Including_additional_CSS_and_JS]])
-- or look at https://dev.fandom.com/wiki/Global_Lua_Modules/Mbox
-- for more customization inspiration

local getArgs = require('Dev:Arguments').getArgs
local i18n = require('Dev:I18n').loadMessages('Mbox')

function p.unitMul(s, n)
    return mw.ustring.gsub(s, '%d+',
                           tonumber(mw.ustring.match(s, "[%d.]+")) * n, 1)
end

-- test by: = p._main{bordercolor="red", image="复制人.png"}
-- test by: = p._main{type="important", image="复制人.png"}
-- test by: = p._main{type="important", icon="add"}
function p._main(args)
    -- styles
    local styles = {}
    local styles__leftborder = {}
    if args.bordercolor then
        styles__leftborder['background-color'] = args.bordercolor
    elseif args.type then
        -- styles__leftborder['background-color'] = 'var(--type-' .. args.type .. ')'
        -- will be ignored and sub with /* insecure input */
    end
    if args.bgcolor then styles['background-color'] = args.bgcolor end

    -- images
    local image = args.image or ''
    local imageadjust = ''
    if args.imageadjust then imageadjust = '|' .. args.imageadjust end
    local imagewidth = args.imagewidth or '80px'
    local imagelink = '|link='
    local imagelinkarg = args.imagelink
    if imagelinkarg then imagelink = imagelink .. imagelinkarg end
    local imagewikitext =
        '[[File:' .. image .. '|' .. imagewidth .. imageadjust .. imagelink ..
            ']]'
    if mw.ustring.sub(image, 1, 8) == "https://" then imagewikitext = image end

    -- id for closure
    local id = args.id or 'mbox'
    local typeclass = args.type

    local container = mw.html.create('div'):addClass('mbox')
                          :addClass(args.class):css(styles):cssText(args.style)

    local content = container:tag('div'):addClass('mbox__content')
    local collapsed = args.collapsed

    if args.bordercolor or args.type then
        content:tag('div'):addClass('mbox__content__leftborder'):addClass(
            typeclass and ('mbox-type-' .. typeclass)):css(styles__leftborder)
    end

    local sizeRatio = .7
    if args.icon or image ~= '' then
        local image = content:tag('div'):addClass('mbox__content__image')
                          :addClass('mw-collapsible')
                          :attr('id', 'mw-customcollapsible-' .. id)
        if args.icon then
            image:tag('span'):addClass('fandom-icons'):css({
                ["font-size"] = p.unitMul(imagewidth, sizeRatio),
                ["display"] = "inline-block",
                ["margin"] = p.unitMul(imagewidth, (1 - sizeRatio) / 2)
            }):wikitext(args.icon)
        else
            image:wikitext(imagewikitext)
        end
        if collapsed then image:addClass('mw-collapsed') end
    end

    local contentwrapper = content:tag('div'):addClass('mbox__content__wrapper')
    local header = args.header

    if header then
        contentwrapper:tag('div'):addClass('mbox__content__header'):wikitext(
            header)
    end

    local textarg = args.text
    if textarg then
        local text = contentwrapper:tag('div'):addClass('mbox__content__text')
                         :addClass('mw-collapsible')
                         :attr('id', 'mw-customcollapsible-' .. id)
                         :wikitext(textarg)
        if collapsed then text:addClass('mw-collapsed') end

        local comment = args.comment
        if comment then
            text:tag('div'):addClass('mbox__content__text__comment'):wikitext(
                comment)
        end
    end

    contentwrapper:tag('span'):addClass('mbox__close'):addClass(
        'mw-customtoggle-' .. id):attr('title', i18n:msg('dismiss'))

    local asidearg = args.aside
    if asidearg then
        local aside = content:tag('div'):addClass('mbox__content__aside')
                          :addClass('mw-collapsible')
                          :attr('id', 'mw-customcollapsible-' .. id)
                          :wikitext(asidearg)
        if collapsed then aside:addClass('mw-collapsed') end
    end

    return container
end

function p.main(frame) return p._main(getArgs(frame)) end

return p