模块:沙盒/Kurgenera/tabs

维基百科,自由的百科全书
跳转到导航 跳转到搜索
local p = {}

function p.main(frame)
    local args = frame:getParent().args
    local id = args.id or '0'
    local color = args.color or 'Lila'
    local sel = tonumber(args.sel) or 0
    local pageName = args.PageName or frame:preprocess('{{PAGENAMEE}}')
    local urlPrev = args.urlPrev or ''
    local urlPost = args.urlPost or ''

    local root = mw.html.create('div')
        :attr('id', 'mc' .. id)
        :addClass('mc' .. color)
    
    root:node(frame:extensionTag('templatestyles', '', { src = 'User:Kurgenera/tabs.css' }))

    local buttonsDiv = root:tag('div'):addClass('skin-invert')
    local contentDiv = root:tag('div'):addClass('mcContingut')

    -- 循环体开始,i 的生命周期从此开始
    for i = 1, 20 do
        local bt = args['bt' .. i]
        
        -- 只有当 btN 参数存在时才执行生成逻辑
        if bt and bt ~= '' then
            -- 1. 生成按钮 (mcBoto)
            local btnClass = (sel == i) and 'mcBotoSel' or 'mcBoto'
            local bticon = args['bticon' .. i] or ''
            local link = args['link' .. i]
            
            if not link or link == '' then
                link = string.format('%s/%s%s%s', pageName, urlPrev, bt, urlPost)
            end

            local btn = buttonsDiv:tag('div')
                :attr('id', 'mc' .. id .. 'bt' .. i)
                :addClass(btnClass)
            
            if bticon ~= '' then
                btn:wikitext(bticon .. ' ')
            end
            btn:wikitext(string.format('[[%s|%s]]', link, bt))

            -- 2. 生成内容面板 (mcPestanya)
            local tabContent = args['tab' .. i] or '沒有內容。'
            local panelStyle = (sel == i) and 'display:block;visibility:visible;' or 'display:none;visibility:hidden;'
            
            local panel = contentDiv:tag('div')
                :attr('id', 'mc' .. id .. 'ps' .. i)
                :addClass('mcPestanya')
                :cssText(panelStyle)
                :wikitext(tabContent)
            
            panel:tag('div'):cssText('clear: both;')
        end
    end -- 循环体结束,i 的生命周期在此终结

    return tostring(root)
end

return p