Открыть меню
Переключить меню настроек
Открыть персональное меню
Вы не представились системе
Ваш IP-адрес будет виден всем, если вы внесёте какие-либо изменения.

Модуль:Tabs

Материал из YastreWiki

Для документации этого модуля может быть создана страница Модуль:Tabs/doc

local p = {}

local function clean(val)
    if val == '' or val == nil then return nil end
    return mw.text.trim(val)
end

function p.render(frame)
    local styles = frame:extensionTag('templatestyles', '', { src = 'Шаблон:Tabs/styles.css' })
    
    local args = frame:getParent().args
    local root = mw.html.create('div')
    root:addClass('tab-container')

    local tabs = {}
    local contentBlocks = {}
    
    local uniqueId = tostring(math.random(1000, 9999))

    local i = 1
    while args['tab' .. i .. '_title'] do
        local title = clean(args['tab' .. i .. '_title'])
        local content = clean(args['tab' .. i .. '_content'])
        local id = 'yastre-tab-' .. uniqueId .. '-' .. i
        
        if title and content then
            table.insert(tabs, { id = id, title = title })
            table.insert(contentBlocks, { id = id, content = content })
        end
        i = i + 1
    end

    if #tabs == 0 then return "Ошибка: Вкладки не заполнены!" end

    local nav = root:tag('div'):addClass('tabs-nav')
    for index, tab in ipairs(tabs) do
        nav:tag('div')
            :addClass('tab-link')
            :addClass(index == 1 and 'active' or '')
            :attr('data-target', tab.id)
            :wikitext(tab.title)
    end

    local contentWrapper = root:tag('div'):addClass('tab-content')
    for index, block in ipairs(contentBlocks) do
        contentWrapper:tag('div')
            :addClass('tab-pane')
            :attr('id', block.id)
            :addClass(index == 1 and 'active' or '')
            :wikitext(block.content)
    end

    return styles .. tostring(root)
end

return p