Module:Mbox: Difference between revisions

From Ato Wiki
m (1 revision imported)
m (1 revision imported)
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
-- This Module is used for making templates based in the Lua language.
local libraryUtil = require( 'libraryUtil' )
-- See more details about Lua in [[w:Help:Lua]].
local checkType = libraryUtil.checkType
-- The Fandom Developer's Wiki hosts Global Lua Modules that can be imported and locally overridden.
local mArguments -- lazily initialise [[Module:Arguments]]
-- The next line imports the Mbox module from the [[w:c:dev:Global Lua Modules]].
local mError    -- lazily initialise [[Module:Error]]
local Mbox = require('Dev:Mbox')
-- See more details about this module at [[w:c:dev:Global_Lua_Modules/Mbox]]


-- The imported Module is overwritten locally to include default styling.
local p = {}
-- For a more flexible Mbox experience, delete the function below and 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


--  
--- Helper function to throw error
-- BEGIN DELETION HERE
--
--
-- @param msg string - Error message
--
-- @return string - Formatted error message in wikitext
local function makeWikitextError( msg )
mError = require( 'Module:Error' )
return mError.error {
message = 'Error: ' .. msg .. '.'
}
end


local getArgs = require('Dev:Arguments').getArgs
function p.mbox( frame )
local localCSS = mw.loadData('Module:Mbox/data').localStyle
mArguments = require( 'Module:Arguments' )
 
local args = mArguments.getArgs( frame )
function Mbox.main(frame)
local title = args[ 1 ] or args[ 'title' ]
    local args = getArgs(frame)
local text = args[ 2 ] or args[ 'text' ]
 
if not title or not text then
    -- styles
return makeWikitextError(
    local styles = {}
'no title or text specified'
    if args.bordercolor then
)
        styles['border-left-color'] = args.bordercolor
end
    elseif args.type then
return p._mbox( title, text, {
        styles['border-left-color'] = 'var(--type-' .. args.type .. ')'
extraclasses = args.extraclasses,
    end
icon = args.icon
 
} )
    if args.bgcolor then
end
        styles['background-color'] = args.bgcolor
    end
 
    -- images
    local image = args.image or ''
    local imagewidth = args.imagewidth or '80px'
    local imagelink = ''
    if args.imagelink then
        imagelink = '|link=' .. args.imagelink
    end
 
    local imagewikitext = ('%sFile:%s|%s%s' .. ']]'):format('[[', image, imagewidth, imagelink)
 
    -- id for closure
    local id = args.id or 'mbox'
 
    local container = mw.html.create('div')
        :addClass('mbox')
        :addClass(args.class)
        :css(styles)
        :css(localCSS['mbox'])
        :cssText(args.style)


    local content = container:tag('div')
function p._mbox( title, text, options )
        :addClass('mbox__content')
checkType( '_mbox', 1, title, 'string' )
        :css(localCSS['mbox__content'])
checkType( '_mbox', 2, text, 'string' )
checkType( '_mbox', 3, options, 'table', true )


    if args.image then
options = options or {}
        local image = content:tag('div')
local mbox = mw.html.create( 'div' )
            :addClass('mbox__content__image')
local extraclasses
            :addClass('mw-collapsible')
if type( options.extraclasses ) == 'string' then
            :attr('id', 'mw-customcollapsible-' .. id)
extraclasses = options.extraclasses
        :css(localCSS['mbox__content__image'])
end
            :wikitext(imagewikitext)
            if args.collapsed then
                image:addClass('mw-collapsed')
            end
    end


    local contentwrapper = content:tag('div')
mbox
        :addClass('mbox__content__wrapper')
:attr( 'role', 'presentation' )
        :css(localCSS['mbox__content__wrapper'])
:addClass( 'mbox' )
:addClass( extraclasses )


    if args.header then
local mboxTitle = mbox:tag( 'div' ):addClass( 'mbox-title' )
        contentwrapper:tag('div')
            :addClass('mbox__content__header')
            :css(localCSS['mbox__content__header'])
            :wikitext(args.header)
    end


    if args.text then
if options.icon and type( options.icon ) == 'string' then
        local text = contentwrapper:tag('div')
mboxTitle:tag( 'div' )
            :addClass('mbox__content__text')
:addClass( 'mbox-icon metadata' )
            :addClass('mw-collapsible')
:wikitext( '[[File:' .. options.icon .. '|14px|link=]]' )
            :attr('id', 'mw-customcollapsible-' .. id)
:done()
            :css(localCSS['mbox__content__text'])
:tag( 'div' )
            :wikitext(args.text)
:wikitext( title )
            if args.collapsed then
else
                text:addClass('mw-collapsed')
mboxTitle:wikitext( title )
            end
end


        if args.comment then
mbox:tag( 'div' )
            text:tag('div')
:addClass( 'mbox-text' )
                :addClass('mbox__content__text__comment')
:wikitext( text )
                :css(localCSS['mbox__content__text__comment'])
                :wikitext(args.comment)
        end
    end


    contentwrapper:tag('span')
return mw.getCurrentFrame():extensionTag {
        :addClass('mbox__close')
name = 'templatestyles', args = { src = 'Module:Mbox/styles.css' }
        :addClass('mw-customtoggle-' .. id)
} .. tostring( mbox )
        :css(localCSS['mbox__close'])
        :attr('title', 'Dismiss')
 
    if args.aside then
        local aside = content:tag('div')
            :addClass('mbox__content__aside')
            :addClass('mw-collapsible')
            :attr('id', 'mw-customcollapsible-' .. id)
            :css(localCSS['mbox__content__aside'])
            :wikitext(args.aside)
            if args.collapsed then
                aside:addClass('mw-collapsed')
            end
    end
 
    return container
end
end


--
return p
-- END DELETION HERE
--
 
-- The last line produces the output for the template
return Mbox

Latest revision as of 09:39, 1 May 2024

Module documentation[view][edit][history][purge]
This documentation is transcluded from Module:Mbox/doc. Changes can be proposed in the talk page.
Function list
L 13 — makeWikitextError
L 20 — p.mbox
L 36 — p._mbox

local libraryUtil = require( 'libraryUtil' )
local checkType = libraryUtil.checkType
local mArguments -- lazily initialise [[Module:Arguments]]
local mError     -- lazily initialise [[Module:Error]]

local p = {}

--- Helper function to throw error
--
-- @param msg string - Error message
--
-- @return string - Formatted error message in wikitext
local function makeWikitextError( msg )
	mError = require( 'Module:Error' )
	return mError.error {
		message = 'Error: ' .. msg .. '.'
	}
end

function p.mbox( frame )
	mArguments = require( 'Module:Arguments' )
	local args = mArguments.getArgs( frame )
	local title = args[ 1 ] or args[ 'title' ]
	local text = args[ 2 ] or args[ 'text' ]
	if not title or not text then
		return makeWikitextError(
			'no title or text specified'
		)
	end
	return p._mbox( title, text, {
		extraclasses = args.extraclasses,
		icon = args.icon
	} )
end

function p._mbox( title, text, options )
	checkType( '_mbox', 1, title, 'string' )
	checkType( '_mbox', 2, text, 'string' )
	checkType( '_mbox', 3, options, 'table', true )

	options = options or {}
	local mbox = mw.html.create( 'div' )
	local extraclasses
	if type( options.extraclasses ) == 'string' then
		extraclasses = options.extraclasses
	end

	mbox
		:attr( 'role', 'presentation' )
		:addClass( 'mbox' )
		:addClass( extraclasses )

	local mboxTitle = mbox:tag( 'div' ):addClass( 'mbox-title' )

	if options.icon and type( options.icon ) == 'string' then
		mboxTitle:tag( 'div' )
			:addClass( 'mbox-icon metadata' )
			:wikitext( '[[File:' .. options.icon .. '|14px|link=]]' )
			:done()
			:tag( 'div' )
			:wikitext( title )
	else
		mboxTitle:wikitext( title )
	end

	mbox:tag( 'div' )
		:addClass( 'mbox-text' )
		:wikitext( text )

	return mw.getCurrentFrame():extensionTag {
		name = 'templatestyles', args = { src = 'Module:Mbox/styles.css' }
	} .. tostring( mbox )
end

return p