模块:收纳表
跳到导航
跳到搜索
此模块的文档可以在模块:收纳表/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(内容在选择栏左) 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 = {}, stab = {}, sitm = {}, ctab = {}, citm = {}, } --处理旧参数名与别名 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 fillWithDefaultCSS(contents, horizontal) local defaults = { div = { class = '', width = '95%', }, stab = { class='', display = 'grid', margin = '0', padding = '1%', gap = '6px', width = 'inherit', }, sitm = { class='', margin = '0', padding = '0', ['font-weight'] = '700', }, ctab = { class='', margin = '0', padding = '1%', ['margin-left'] = '0', width = 'inherit', }, citm = {class='',}, } if horizontal then contents['splitCount'] = contents['splitCount'] or '1' defaults['div']['display'] = defaults['div']['display'] or 'flex' defaults['stab']['display'] = 'inline-grid' defaults['stab']['height'] = '400px' defaults['stab']['overflow-y'] = 'auto' defaults['stab']['width'] = '20%' defaults['ctab']['display'] = 'inline' defaults['ctab']['height'] = '400px' defaults['ctab']['overflow-y'] = 'auto' defaults['ctab']['width'] = '60%' end contents['splitCount'] = contents['splitCount'] or '5' local elements = {'div', 'stab', 'sitm', 'ctab', 'citm'} for _, s in ipairs(elements) do local resizeFlowDir = {height='y', width='x'} for size, dir in pairs(resizeFlowDir) do if CSSvalue[s][size] then defaults[s]['overflow-'..dir] = 'auto' end end for CSS, defaultValue in pairs(defaults[s]) do CSSvalue[s][CSS] = CSSvalue[s][CSS] or defaultValue end end end --处理选择栏 local function renderSelect(contents) local tabs_pages = mw.html.create('div') :addClass('tabs_pages ' .. CSSvalue['stab']['class']) :css('grid-template-columns', string.rep('1fr ', tonumber(contents['splitCount']))) 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) local tabs_contents = mw.html.create('div') :addClass('tabs-contents ' .. CSSvalue['ctab']['class']) 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 --处理CSSvalue 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 else CSSvalue['div'][k] = v end end end local horizontal = args['textDisplay']=='left' or args['textDisplay']=='right' fillWithDefaultCSS(args, horizontal) --处理ordering local elements = {'div', 'stab', 'sitm', 'ctab', 'citm'} for _, s in ipairs(elements) do for k, _ in pairs(CSSvalue[s]) do if k ~= 'class' then table.insert(ordering[s], k) end end table.sort(ordering[s]) end table.sort(ordering['tabIndices']) local mainDiv = mw.html.create('div') 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)) mainDiv:node(renderSelect(args)) else mainDiv:node(renderSelect(args)) mainDiv:node(renderContent(args)) end return tostring(mainDiv) end return p