Module:Message box

From NeuroWiki
Jump to navigation Jump to search

此模块用于实现{{Message box}}。使用方法见模板文档

依赖项




local p = {}

function commentSprite( pos, link )
	if not pos or mw.text.trim( pos ) == '' then
		return ''
	end
	return require( 'Module:Sprite' ).base{ name = 'Comment', pos = pos, image = 'CommentCSS.png', sheetsize = 48, stylesheet = 1, link = link }
end

function p.main( f )
	local args = f
	local frame = mw.getCurrentFrame()
	if f == frame then
		args = require( 'Module:ProcessArgs' ).merge( true )
	end

	local rawTitle = frame:preprocess( frame:getParent().args.title )
	local rawText = frame:preprocess( frame:getParent().args.text )

	-- Create main frame
	local result = mw.html.create( 'div' ):addClass( 'msgbox searchaux' ):addClass( 'msgbox-' .. ( args.type or 'notice' ) ):addClass( args.class ):cssText( args.css ):css{
		[ 'background-color' ] = args.bgcol,
		[ 'border-left-color' ] = args.linecol,
		[ 'font-size' ] = args[ 'font-size' ],
		[ 'width' ] = args.width or ( args.compat and 'fit-content' or nil ),
	}
	if args.mini then
		result:addClass( 'msgbox-mini' )
	end

	-- Create optional icon / image frame
	if args.icon or args.image then
		result:addClass( 'has-image' )
		local resultOpt = mw.html.create( 'div' ):addClass( 'msgbox-icon-image' ):cssText( args.imagecss )
		local imagefield = ''
		if args.icon then
			imagefield = imagefield .. commentSprite( args.icon, args.iconlink )
		end
		if args.image then
			imagefield = table.concat{
				imagefield, '[[',
				'File:', mw.text.split( args.image, '|', true )[ 1 ],
				'|link=', mw.text.split( args.imagelink or '', '|', true )[ 1 ],
				'|', args.imagesize or ( args.mini and '16px' or '32px' ),
				args.imageclass and ( '|class=' .. args.imageclass ) or '',
				'|', 'text-top',
				']]'
			}
		end
		resultOpt:wikitext( frame:preprocess( args.imagetextbefore or '' ) .. imagefield .. frame:preprocess ( args.imagetextafter or '' ) )
		result:node( resultOpt )
	end

	-- Create displaytext frame
	local resultNext = mw.html.create( 'div' ):addClass( 'msgbox-body' ):addClass( 'align-' .. ( args[ 'text-align' ] or 'left' ) ):cssText( args.textcss )
	-- Create optional title frame inside displaytext frame
	if args.title then
		local resultNextTitle = mw.html.create( 'div' ):addClass( 'msgbox-title' )
		local resultNextTitleText = "<b>" .. rawTitle .. "</b>"
		if args.customaction then
			resultNextTitleText = resultNextTitleText .. '<sup>' .. frame:preprocess( args.customaction ) .. '</sup>'
		end
		if args.discuss or args.discussPage or args.discussAnchor then
			local directLinkTarget = args.discussPage or mw.title.getCurrentTitle().talkPageTitle.fullText
			if args.discussAnchor then
				directLinkTarget = directLinkTarget .. '#' .. args.discussAnchor
			end
			local discussText = '&nbsp;<sup>' .. require( 'Module:Direct link' ).main{ directLinkTarget, '讨论' } .. '</sup>'
			if args.linkshere then
				discussText = discussText .. '&nbsp;<sup>[[Special:WhatLinksHere/' .. mw.title.getCurrentTitle().fullText .. '|' .. require( 'Module:STConversion' ).call{ [ 'zh-cn'] = '链入', [ 'zh-tw' ] = '連入' } .. ']]</sup>'
			end
			resultNextTitleText = resultNextTitleText .. discussText
		end
		resultNextTitle:wikitext( resultNextTitleText )
		resultNext:node( resultNextTitle )
	end
	-- Create optional text frame inside displaytext frame, after optional title frame
	if args.text then
		resultNext:tag( 'div' ):addClass( 'msgbox-text' ):wikitext( '<p>\n' .. rawText .. '\n</p>' )
	end
	-- Add displaytext frame to main frame
	result:node( resultNext )

	-- Add templatestyles to final result
	result = require( 'Module:TSLoader' ).call( 'Template:Message box/styles.css' ) .. tostring( result )

	-- Append tracking categories
	if args.bgcol then
		result = result .. '[[Category:使用Message box的bgcol参数的页面]]'
	end
	if args.linecol then
		result = result .. '[[Category:使用Message box的linecol参数的页面]]'
	end

	return result
end

return p