Modul:Community-Portal

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen

Die Dokumentation für dieses Modul kann unter Modul:Community-Portal/Doku erstellt werden

local p = {}

local globalFrame
local collapsibleTemplate = 'Wikipedia:Wikimedia Deutschland/Community-Portal/Vorlagen/CollapseNavigation'

function appendChildren(children)
    if not children then
        return ''
    end

    local childHtml = ''

    for childIndex, childNode in ipairs(children) do
        if childNode.name then
            local classes = ''

            if childNode.classes then
                classes = childNode.classes
            end

            if childNode.children then
                classes = classes .. ' cp-has-submenu'
            end

            childHtml = childHtml .. '<div class="community-portal cp-header ' .. classes .. '">'

            if (childNode.page) then
                childHtml = childHtml .. '[[' .. childNode.page .. '|' .. childNode.name .. ']]'
            else
                childHtml = childHtml .. '<span>' .. childNode.name .. '</span>'
            end

            if childNode.children then
                childHtml = childHtml .. globalFrame:expandTemplate { title = collapsibleTemplate, args = { content = appendChildren(childNode.children), id = string.lower(childNode.name):gsub(" ", "") } }
            end

            childHtml = childHtml .. '</div>'
        end
    end

    return childHtml
end

function p.navi(frame)
    if not frame then
        globalFrame = mw.getCurrentFrame()
    else
        globalFrame = frame
    end

    local configPage, configData, rootNodes, naviHtml, currentPage

    currentPage = mw.title.getCurrentTitle()

    if not configPage then
        configPage = 'Wikipedia:Wikimedia_Deutschland/Community-Portal/Vorlagen/config.json'
    end
    configData = mw.text.jsonDecode(mw.title.new(configPage):getContent())

    rootNodes = configData.structure

    naviHtml = ''

    if not rootNodes then
        return naviHtml
    end

    for rootIndex, rootNode in ipairs(rootNodes) do
        if rootNode.name and rootNode.page then
            local classes = ''

            if rootNode.classes then
                classes = rootNode.classes
            end

            if rootNode.children then
                classes = classes .. ' cp-has-submenu'
            end

            naviHtml = naviHtml .. '<div class="community-portal cp-header ' .. classes .. '">'
            naviHtml = naviHtml .. '[[' .. rootNode.page .. '|' .. rootNode.name .. ']]'

            if rootNode.children then
                local isCurrent = false
                
                if ( mw.title.compare( currentPage, mw.title.new( rootNode.page ) ) > -1 ) then
                	isCurrent = true
                end
               
                
                naviHtml = naviHtml .. globalFrame:expandTemplate { title = collapsibleTemplate, args = {
                    content = appendChildren(rootNode.children),
                    id = string.lower(rootNode.name):gsub(" ", ""),
                    active = isCurrent
                } }
            end

            naviHtml = naviHtml .. '</div>'
        end
    end

    return naviHtml
end

return p