2023年政策修订增补工作正在进行中,欢迎参与!
  • Moegirl.ICU:萌娘百科流亡社群 581077156(QQ),欢迎对萌娘百科运营感到失望的编辑者加入
  • Moegirl.ICU:账号认领正在试运行,有意者请参照账号认领流程

Module:Sandbox/公的驱逐舰/Tabs

萌娘百科,万物皆可萌的百科全书!转载请标注来源页面的网页链接,并声明引自萌娘百科。内容不可商用。
跳转到导航 跳转到搜索
Template-info.svg 模块文档  [创建] [刷新]
local p = {}

local getArgs = require ( 'Module:Arguments' ).getArgs
local wrapperArray = { 'Template:Tabs', 'Template:Tabs/core', 'Template:沙盒' }

local function isEmpty( s )
	return (s == nil or s == '')
end
-- F: getArgFromAlias
local function gafa ( args, argsAliasArray, defaultReturn )
	for i = 1, #argsAliasArray do
		if ( args[argsAliasArray[i]] ~= nil ) then
			return args[argsAliasArray[i]]
		end
	end
	return defaultReturn
end
-- F: getArgsByPrefix (will convert every key to string, including numeral keys)
local function gabp ( args, prefix )
	local t = {}
	for k,v in pairs ( args ) do
		local p = '^' .. prefix
		if tostring( k ):match( p ) then
			t [ tostring( k ):gsub( p, '' ) ] = ( v or '' )
		end
	end
	return t
end

local function processArgs ( frame, isCore )
	local args = getArgs ( frame, { wrappers = wrapperArray	} )
	local pargs = {}
	pargs.color = string.lower ( gafa ( args, {"color"}, 'black' ) )
	pargs.LabelSide = string.lower ( gafa ( args, {"LabelSide"}, '' ) )
	pargs.LabelSide = ( ( pargs.LabelSide == 'top' or pargs.LabelSide == 'bottom' or pargs.LabelSide == 'left' or pargs.LabelSide == 'right' ) and pargs.LabelSide or 'top' )
	
	pargs.LabelColorSideReverse = gafa ( args, {"LabelColorSideReverse"}, '' )
	pargs.DividerSize = gafa ( args, {"DividerSize"}, '' )
	pargs.style = gafa ( args, {"style"} )
	pargs.DefaultTab = gafa ( args, {"DefaultTab"} )
	pargs.LabelPadding = gafa ( args, {"LabelPadding"} )
	pargs.LabelBorderColor = gafa ( args, {"LabelBorderColor"} )
	pargs.LabelColor = gafa ( args, {"LabelColor"} )
	pargs.TextBorderColor = gafa ( args, {"TextBorderColor"} )
	pargs.TextPadding = gafa ( args, {"TextPadding"} )
	
	pargs.AutoWidth = string.lower ( gafa ( args, {"AutoWidth"}, '' ) )
	pargs.AutoWidth = ( pargs.AutoWidth == 'yes' or pargs.AutoWidth == 'true' or pargs.AutoWidth == '是' )
	
	pargs.LabelBackgroundColor = gafa ( args, {"LabelBackgroundColor"} )
	pargs.TextBackgroundColor = gafa ( args, {"TextBackgroundColor"} )
	
	pargs.Float = string.lower ( gafa ( args, {"Float"}, '' ) )
	pargs.Float = ( ( pargs.Float == 'left' or pargs.Float == 'right' ) and pargs.Float or nil )
	
	if isCore then
		pargs.labels = gabp ( args, 'label' )
		pargs.texts = gabp ( args, 'text' )
	else
		pargs.tabs = gabp ( args, 'tab' )
		pargs.bts = gabp ( args, 'bt' )
		pargs.bticons = gabp ( args, 'bticon' )
	end
	return pargs
end

local function head ( args )
	return mw.html.create('div')
		:addClass( "Tabs" ):addClass( args.color )
		:attr( 'data-label-side', args.LabelSide )
		:attr( 'data-label-color-side-reverse', args.LabelColorSideReverse )
		:attr( 'data-divider-size', args.DividerSize )
		:cssText( args.style )
		:attr( 'data-default-tab', args.DefaultTab )
		:attr( 'data-label-padding', args.LabelPadding )
		:attr( 'data-label-border-color', args.LabelBorderColor )
		:attr( 'data-label-color', args.LabelColor )
		:attr( 'data-text-border-color', args.TextBorderColor )
		:attr( 'data-text-padding', args.TextPadding )
		:attr( 'data-auto-width', ( args.AutoWidth and 'yes' or nil ) )
		:attr( 'data-label-background-color', args.LabelBackgroundColor )
		:attr( 'data-text-background-color', args.TextBackgroundColor )
		:attr( 'data-float', args.Float )
end

function p.main ( frame )
	return p._main ( processArgs ( frame, false ) )
end
function p._main ( args )
	local tabsDiv = head ( args )
	for k, v in pairs( args.tabs ) do
		tabsDiv
		:tag('div'):addClass( "Tab" )
			:tag('div')
				:addClass( "TabLabelText" ):wikitext( ( args.bticons[k] or '' ) .. ( args.bts[k] or '' ) )
			:done()
			:tag('div')
				:addClass( "TabContentText" ):wikitext( v )
			:done()
		:done()
	end
	return tostring( tabsDiv )
end
function p.core ( frame )
	local pargs = processArgs ( frame, true )
	return p._core ( pargs )
end
function p._core ( args )
	local tabsDiv = head ( args )
	for k, v in pairs( args.texts ) do
		tabsDiv
		:tag('div')
			:addClass( "Tabs" )
			:tag('div')
				:addClass( "TabLabelText" ):wikitext( ( args.labels[k] or '' ) )
			:done()
			:tag('div')
				:addClass( "TabContentText" ):wikitext( v )
			:done()
		:done()
	end
	return tostring( tabsDiv )
end

return p