模块:Test2:修订间差异

来自SS唯基
跳到导航 跳到搜索
(建立内容为“local p = {} local notCSS = { splitCount = true, initialTab = true, textDisplay = true, } local ordering = { tabIndices = {}, stgt = {},…”的新页面)
 
(清空全部内容)
标签清空
 
第1行: 第1行:
local p = {}
local notCSS = {
    splitCount  = true,
    initialTab  = true,
    textDisplay = true,
}
local ordering = {
    tabIndices = {},
    stgt = {},
    stab = {},
    sitm = {},
    ctab = {},
    citm = {},
}
local CSSvalue = {
    stgt = {},
    stab = {},
    sitm = {},
    ctab = {},
    citm = {},
}
local initialTab = nil
local function replaceArgsName(str)
    str = str:gsub('^select', 'stab')
    str = str:gsub('^text', 'ctab')
    str = str:gsub('^sitem', 'sitm')
    str = str:gsub('^citem', 'citm')
    return str:gsub("^%u", string.lower)
end
local function fillWithDefaultCSS(contents, horizontal)
    local defaults = {
        stgt = {class = '',},
        stab = {class = '',},
        sitm = {class = '',},
        ctab = {class = '',},
        citm = {class = '',},
    }
    if horizontal then
        contents['splitCount'] = contents['splitCount'] or '1'
    end
    contents['splitCount'] = contents['splitCount'] or '5'


    local elements = {'stgt', 'stab', 'sitm', 'ctab', 'citm'}
    for _, elem in ipairs(elements) do
        local resizeFlowDir = {height='y', width='x'}
        for size, dir in pairs(resizeFlowDir) do
            if CSSvalue[elem][size] then
                defaults[elem]['overflow-'..dir] = 'auto'
            end
        end
        for CSS, defaultValue in pairs(defaults[elem]) do
            CSSvalue[elem][CSS] = CSSvalue[elem][CSS] or defaultValue
        end
    end
end
local function renderSelect(contents)
    local tabs_pages = mw.html.create('div')
        :addClass('tabs_pages stab ' .. 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 originalIndex, tabIndex in ipairs(ordering['tabIndices']) do
        local sitm = tabs_pages:tag('div')
            :addClass('tab sitm ' .. CSSvalue['sitm']['class'])
            :wikitext(contents['tab' .. tabIndex])
        if originalIndex == initialTab then sitm:addClass('active') end
        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 ctab ' .. CSSvalue['ctab']['class'])
    for _, CSS in ipairs(ordering['ctab']) do
        tabs_contents:css(CSS, CSSvalue['ctab'][CSS])
    end
    for originalIndex, tabIndex in ipairs(ordering['tabIndices']) do
        local citm = tabs_contents:tag('div')
            :addClass('tab-c citm ' .. CSSvalue['citm']['class'])
            :wikitext(contents['content' .. tabIndex])
        if originalIndex == initialTab then citm:addClass('active') end
        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 = {}
    local page_args = frame:getParent().args;
    for k, v in pairs(page_args) do
    k = '' .. k
        if v ~= 'nil' then
            if notCSS[k] then args[k] = v
            else args[replaceArgsName(k)] = v end
        end
    end
    initialTab = tonumber(args['initialTab'])
    local validArgs = {stgt=true, 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['stgt'][k] = v
            end
        end
    end
    local horizontal = args['textDisplay']=='left' or args['textDisplay']=='right'
    fillWithDefaultCSS(args, horizontal)
    if horizontal then
    CSSvalue['stgt']['class'] = CSSvalue['stgt']['class'] .. ' hori'
        CSSvalue['stab']['class'] = CSSvalue['stab']['class'] .. ' hori'
        CSSvalue['ctab']['class'] = CSSvalue['stab']['class'] .. ' hori'
    end
    local elements = {'stgt', '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 stgt ' .. CSSvalue['stgt']['class'])
    for _, divCSS in ipairs(ordering['stgt']) do
        mainDiv:css(divCSS, CSSvalue['stgt'][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

2022年10月2日 (日) 12:59的最新版本

此模块的文档可以在模块:Test2/doc创建