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

Module:Sandbox/Sirogohan

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

local stylelib = {
	tabs = {
		top = 'border-bottom:2px solid TITLECOLOR; display:flex; align-items:center;',
		topmargin = 'margin-left:calc(ICONSIZE/2); margin-bottom:calc(ICONSIZE/2 - 0.8em);',
		iconmap = 'display:inline-block; height:10px; width:calc(ICONSIZE + 0.15em); position:relative; top:calc(-ICONSIZE/2 + 0.8em); vertical-align:top; margin-left:calc(-ICONSIZE/2 - 0.3em);',
		icon = 'border-radius:calc(ICONSIZE/2); border:2px solid #fff; box-shadow:0 0 3px TITLECOLOR; overflow:hidden; background:#fff; text-align:center; height:calc(ICONSIZE - 4px); width:calc(ICONSIZE - 4px);',
		title = 'background:TITLECOLOR; color:#fff; border-top-left-radius:6px; border-top-right-radius:6px; display:inline-block; padding:0 0.3em; font-weight:bold; font-size:1.2em; margin-bottom:-2px; white-space:nowrap;',
		alt = 'color:#666; font-size:80%; padding-top:0.2em; padding-left:1em; white-space:nowrap; overflow-x:auto;',
		defaulttitlemargin = '0.3em',
		defaultcolor = '#9ecdd9',
		defaultsize = '48px',
		defaultmargin = '1em'
	},
}

local function itemValue(parameter, itemnum)
	local key
	if itemnum then
		key = format('item%d-', itemnum) .. parameter
	else
		key = parameter
	end
	return args[key]
end

local function findTheme(theme, itemnum)
    local style
    if(stylelib[theme]) then
        style = stylelib[theme]
    else
        style = stylelib['tabs']
    end
	
	if itemnum then	
		style.top = string.gsub(style.top, 'TITLECOLOR', itemValue('color', itemnum) or args['color'] or style.defaultcolor)
		style.icon = string.gsub(style.icon, 'TITLECOLOR', itemValue('color', itemnum) or args['color'] or style.defaultcolor)
		style.title = string.gsub(style.title, 'TITLECOLOR', itemValue('color', itemnum) or args['color'] or style.defaultcolor)
		style.topmargin = string.gsub(style.topmargin, 'ICONSIZE', itemValue('iconsize', itemnum) or args['iconsize'] or style.defaultsize)
		style.iconmap = string.gsub(style.iconmap, 'ICONSIZE', itemValue('iconsize', itemnum) or args['iconsize'] or style.defaultsize)
		style.icon = string.gsub(style.icon, 'ICONSIZE', itemValue('iconsize', itemnum) or args['iconsize'] or style.defaultsize)
	
		style.top = style.top .. (args['style-top'] or '')
		style.title = style.title .. (args['style-title'] or '')
		style.alt = style.alt .. (args['style-alt'] or '')
		style.topmargin = style.topmargin .. (args['style-topmargin'] or '')
		style.iconmap = style.iconmap .. (args['style-iconmap'] or '')
		style.icon = style.icon .. (args['style-icon'] or '')
		
		style.top = style.top .. (itemValue('style-top', itemnum) or '')
		style.title = style.title .. (itemValue('style-title', itemnum) or '')
		style.alt = style.alt .. (itemValue('style-alt', itemnum) or '')
		style.topmargin = style.topmargin .. (itemValue('style-topmargin', itemnum) or '')
		style.iconmap = style.iconmap .. (itemValue('style-iconmap', itemnum) or '')
		style.icon = style.icon .. (itemValue('style-icon', itemnum) or '')
	end
	
    return style
end

local function renderDummyItem(ctr, dummynum)
	for i = 1, dummynum do
		ctr:tag('div')
			:addClass('flexinfo-itembox')
			:css('flex-basis', args['dummywidth'] or args['width'] or '400px')
			:css('flex-grow', args['grow'] or '1')
			:css('flex-shrink', args['shrink'] or args['grow'] or '1')
	end
end

local function renderNest(ctr, itemnum)
	local style = findTheme(args['theme'], itemnum)

	-- item box
	local itembox = ctr:tag('div')
		:addClass('flexinfo-itembox')
		:css('flex-basis', itemValue('width', itemnum) or args['width'] or '400px')
		:css('flex-grow', itemValue('grow', itemnum) or args['grow'] or '1')
		:css('flex-shrink', itemValue('shrink', itemnum) or args['shrink'] or itemValue('grow', itemnum) or args['grow'] or '1')
		:cssText(args['style-itembox'])
		:cssText(itemValue('style-itembox', itemnum))
		:wikitext(itemValue('nest', itemnum))
end

local function renderItem(ctr, itemnum)
	local style = findTheme(args['theme'], itemnum)

	-- item box
	local itembox = ctr:tag('div')
		:addClass('flexinfo-itembox')
		:css('flex-basis', itemValue('width', itemnum) or args['width'] or '400px')
		:css('flex-grow', itemValue('grow', itemnum) or args['grow'] or '1')
		:css('flex-shrink', itemValue('shrink', itemnum) or args['shrink'] or itemValue('grow', itemnum) or args['grow'] or '1')
		:css('margin-top', '1em')
		:cssText(args['style-itembox'])
		:cssText(itemValue('style-itembox', itemnum))
	
	-- item main div
	local item = itembox:tag('div')
		:addClass('flexinfo-item')
		:css('margin-left', args['margin'] or style.defaultmargin or '1em')
		:css('margin-right', args['margin'] or style.defaultmargin or '1em')
		
	-- item top bar
	local itemtop = item:tag('div')
		:addClass('flexinfo-itemtop')
		:cssText(style.top)
		
	-- item title / name
	local itemtitle = itemtop:tag('div')
		:addClass('flexinfo-title')
		:cssText(style.title)
		
	-- icon config
	if itemValue('icon', itemnum) then
		itemtop:cssText(style.topmargin)
		local iconmap = itemtitle:tag('div')
			:addClass('flexinfo-iconmap')
			:cssText(style.iconmap)
	
		local icon = iconmap:tag('div')
			:addClass('flexinfo-icon')
			:cssText(style.icon)
			:wikitext(itemValue('icon', itemnum))
	end
	
	itemtitle:wikitext(args[format("item%d", itemnum)])
	
	-- item subhead / alt / cv
	if itemValue('alt', itemnum) then
		local itemcv = itemtop:tag('div')
			:addClass('flexinfo-alt')
			:cssText(style.alt)
			:wikitext(itemValue('alt', itemnum))
	elseif itemValue('cv', itemnum) then
		local itemcv = itemtop:tag('div')
			:addClass('flexinfo-alt')
			:cssText(style.alt)
			:wikitext('CV:' .. itemValue('cv', itemnum))
	end
	
	-- item info / text
	local itemtext = item:tag('p')
		:wikitext(itemValue('text', itemnum))
end

function p._flexinfo(infoArgs)
	args = infoArgs
	local style = findTheme(args['theme'])
	local itemnums = {}

	for k, _ in pairs(args) do
		if type(k) == 'string' then
			local itemnum = k:match('^item(%d+)$')
			if itemnum then table.insert(itemnums, tonumber(itemnum)) end
		end
	end
	table.sort(itemnums)

	local ctr = mw.html.create('div')
		:addClass('infoflex')
		:css('display', 'flex')
		:css('flex-wrap', 'wrap')
		
	local margin = '1em'	
	if(stylelib[theme]) then
        margin = stylelib[theme]['defaultmargin']
    end

	if not args['nest'] then
		ctr
			:css('margin-top', '-0.5em')
			:css('margin-bottom', '1em')
			:css('margin-left', ('-' .. margin))
			:css('margin-right', ('-' .. margin))
	end
	
	ctr:cssText(args['containerstyle'])

	local itemnums_size = #itemnums
	for i, itemnum in ipairs(itemnums) do
		if(args[format("item%d-nest", i)]) then
			renderNest(ctr, itemnum)
		else
			renderItem(ctr, itemnum)
		end
	end
	
	if(args['dummy']) then
		renderDummyItem(ctr, args['dummy']);
	end

	return ctr
end

function p.flexinfo(frame)
    if not getArgs then
    	getArgs = require('Module:Arguments').getArgs
    end
    args = getArgs(frame)
	
	return p._flexinfo(args)
end

return p