模块:收纳表
外观
此模块的文档可以在模块:收纳表/doc创建
--理论上能调整所有的style,并可新增class
--|stab(css) 选择栏的css。曾经为select(css)
--|sitem(css) 选择栏中每个物件的css
--|ctab(css) 内容栏的css。曾经为text(css)
--|citem(css) 内容栏中每个物件的css
--[[ 非css的参数设定
splitCount = 5: 决定每行有几个物件
textDisplay = down: 内容相对于选择栏的位置,可输入
top(内容在选择栏上)
down(内容在选择栏下)
left(内容在选择栏左,此时若用户无指定,则selectWidth会被预设成20%、textWidth被设成60%、textHeight, selectHeight皆被设成400px)
right(内容在选择栏右,同上)
--]]
local p = {}
local notCSS = { --非CSS的参数
textDisplay = true,
splitCount = true
}
--用于处理编号非连续的情况与css优先度的问题
--优先度问题如:直接塞:css(table),margin-left被margin盖掉
--前者范围较小,因此(应该是)希望前者优先
local ordering = {
tabIndices = {},
div = {},
stab = {},
sitm = {},
ctab = {},
citm = {},
}
local CSSvalue = {
div = {class=''},
stab = {
class = '',
display = 'grid',
margin = '2.5%',
gap = '6px',
},
sitm = {
class = '',
margin = '0',
padding = '0',
['font-weight'] = '700',
},
ctab = {
class = '',
display = 'inline',
margin = '2.5%',
['margin-left'] = '0',
},
citm = {class=''},
}
--处理旧参数名与别名
local function replaceArgsName(str)
str = str:gsub('^select', 'stab')
str = str:gsub('^text', 'ctab')
str = str:gsub('^sitem', '^sitm')
str = str:gsub('^citem', '^citm')
--[[str = str:gsub('^selecttab', 'stab')
str = str:gsub('^selectitem', 'sitem')
str = str:gsub('^contenttab', 'ctab')
str = str:gsub('^contentitem', 'citem')]]--
return str:gsub("^%u", string.lower) --第一个字小写
end
--处理选择栏
local function renderSelect(contents, horizontal)
local tabs_pages = mw.html.create('div')
:addClass('tabs_pages ' .. CSSvalue['stab']['class'])
:css('grid-template-columns',
string.rep('1fr ', tonumber(contents['splitCount'])))
if horizontal then
CSSvalue['stab']['display'] = CSSvalue['stab']['display'] or 'inline-grid'
CSSvalue['stab']['height'] = CSSvalue['stab']['height'] or '400px'
CSSvalue['stab']['overflow-y'] = CSSvalue['stab']['overflow-y'] or 'auto'
CSSvalue['stab']['width'] = CSSvalue['stab']['width'] or '20%'
end
if CSSvalue['stab']['height'] ~= nil then
CSSvalue['stab']['overflow-y'] = CSSvalue['stab']['overflow-y'] or 'auto'
end
if CSSvalue['stab']['width'] ~= nil then
CSSvalue['stab']['overflow-x'] = CSSvalue['stab']['overflow-x'] or 'auto'
end
for _, CSS in ipairs(ordering['stab']) do
tabs_pages:css(CSS, CSSvalue['stab'][CSS])
end
for _, tabIndex in ipairs(ordering['tabIndices']) do
local sitm = tabs_pages:tag('div')
:addClass('tab ' .. CSSvalue['sitm']['class'])
:wikitext(contents['tab' .. tabIndex])
for _, CSS in ipairs(ordering['sitm']) do
sitm:css(CSS, CSSvalue['sitm'][CSS])
end
end
return tostring(tabs_pages)
end
--处理内容栏
local function renderContent(contents, horizontal)
local tabs_contents = mw.html.create('div')
:addClass('tabs-contents ' .. CSSvalue['ctab']['class'])
if horizontal then
CSSvalue['ctab']['height'] = CSSvalue['ctab']['height'] or '400px'
CSSvalue['ctab']['overflow-y'] = CSSvalue['ctab']['overflow-y'] or 'auto'
CSSvalue['ctab']['width'] = CSSvalue['ctab']['width'] or '60%'
end
if CSSvalue['ctab']['height'] ~= nil then
CSSvalue['ctab']['overflow-y'] = CSSvalue['ctab']['overflow-y'] or 'auto'
end
if CSSvalue['ctab']['width'] ~= nil then
CSSvalue['ctab']['overflow-x'] = CSSvalue['ctab']['overflow-x'] or 'auto'
end
for _, CSS in ipairs(ordering['ctab']) do
tabs_contents:css(CSS, CSSvalue['ctab'][CSS])
end
for _, tabIndex in ipairs(ordering['tabIndices']) do
local citm = tabs_contents:tag('div')
:addClass('tab-c ' .. CSSvalue['citm']['class'])
:wikitext(contents['content' .. tabIndex])
for _, CSS in ipairs(ordering['citm']) do
citm:css(CSS, CSSvalue['citm'][CSS])
end
end
return tostring(tabs_contents)
end
function p.selectTable(frame)
local args = {}
--获取页面传来的参数,而非模板#invoke時的参数
local page_args = frame:getParent().args;
--将获取的参数传给args
for k, v in pairs(page_args) do
if v ~= 'nil' then
if notCSS[k] then args[k] = v
else args[replaceArgsName(k)] = v end
end
end
table.sort(args)
local validArgs = {stab=true, sitm=true, ctab=true, citm=true}
for k, v in pairs(args) do
k = '' .. k; v = '' .. v
if not (notCSS[k] or k:match('^content%d+')) then
if k:match('^tab%d+$') then
table.insert(ordering['tabIndices'], tonumber(k:sub(4)))
elseif validArgs[k:sub(1, 4)] then
local prefix = k:sub(1, 4)
local CSS = k:sub(5)
CSSvalue[prefix][CSS] = v
if not (CSS == 'class') then table.insert(ordering[prefix], CSS) end
else
table.insert(ordering['div'], k)
CSSvalue['div'][k] = v
end
end
end --由于args被排序过了,因此ordering内的阵列也是排序好的
local mainDiv = mw.html.create('div')
local horizontal = args['textDisplay'] == 'left' or args['textDisplay'] == 'right'
if horizontal then
CSSvalue['div']['display'] = CSSvalue['div']['display'] or 'flex'
args['splitCount'] = args['splitCount'] or 1
end
args['splitCount'] = args['splitCount'] or 5
mainDiv:addClass('tabs-container ' .. CSSvalue['div']['class'])
for _, divCSS in ipairs(ordering['div']) do
mainDiv:css(divCSS, CSSvalue['div'][divCSS])
end
if args['textDisplay'] == 'top' or args['textDisplay'] == 'left' then
mainDiv:node(renderContent(args, horizontal))
mainDiv:node(renderSelect(args, horizontal))
else
mainDiv:node(renderSelect(args, horizontal))
mainDiv:node(renderContent(args, horizontal))
end
return tostring(mainDiv)
end
return p