Module:Pagination: Difference between revisions

From Growtopia
Jump to navigationJump to search
>HashJona
No edit summary
>HashJona
No edit summary
Line 3: Line 3:
function p.pagination(frame)
function p.pagination(frame)
     local args = frame.args
     local args = frame.args
     local paramName = args.param or "offset"
     local paramName = mw.text.trim(args.param) or "offset"
     local offsetVal = tonumber(args.offset or "0") or 0
     local offsetVal = tonumber(mw.text.trim(args.offset or "0")) or 0
     local countVal  = tonumber(args.count  or "0") or 0
     local countVal  = tonumber(mw.text.trim(args.count  or "0")) or 0
     local totalVal  = tonumber(args.total  or "0") or 0
     local totalVal  = tonumber(mw.text.trim(args.total  or "0")) or 0
     local debug    = args.debug or "0"
     local debug    = args.debug or "0"



Revision as of 19:47, 8 March 2025

Documentation for this module may be created at Module:Pagination/doc

local p = {}

function p.pagination(frame)
    local args = frame.args
    local paramName = mw.text.trim(args.param) or "offset"
    local offsetVal = tonumber(mw.text.trim(args.offset  or "0")) or 0
    local countVal  = tonumber(mw.text.trim(args.count  or "0")) or 0
    local totalVal  = tonumber(mw.text.trim(args.total  or "0")) or 0
    local debug     = args.debug or "0"

    local prevOffset = math.max(0, offsetVal - countVal)
    local nextOffset = offsetVal + countVal
    local lastOffset = 0
    
    if countVal > 0 then
        lastOffset = math.floor((totalVal - 1) / countVal) * countVal
        if lastOffset < 0 then
            lastOffset = 0
        end
    end

    local out = {}
    local function w(line) table.insert(out, line) end

    if debug == "1" then
        w('{| class="wikitable" style="margin:1em auto;"')
        w('|-')
        w('! colspan="2" | Debug Information')
        w('|-')
        w('| param')
        w('| ' .. paramName .. ' <small>(default: offset)</small>')
        w('|-')
        w('| offset')
        w('| ' .. offsetVal .. ' <small>(default: 0)</small>')
        w('|-')
        w('| count')
        w('| ' .. countVal .. ' <small>(default: 0)</small>')
        w('|-')
        w('| total')
        w('| ' .. totalVal .. ' <small>(default: 0)</small>')
        w('|}')
    end

    w('<div class="category-page__pagination" style="display:flex; justify-content: space-between; width:100%">')
    w('<div>')

    if offsetVal > 0 then
        local firstLink = frame:callParserFunction('fullurl', {
            mw.title.getCurrentTitle().prefixedText,
            paramName .. "=0"
        })
        w(string.format('[%s <span class="wds-button wds-is-text">First</span>]', firstLink))
        local prevLink = frame:callParserFunction('fullurl', {
            mw.title.getCurrentTitle().prefixedText,
            paramName .. "=" .. prevOffset
        })
        w(string.format('[%s <span class="category-page__pagination-prev wds-button wds-is-secondary">&lt; Previous</span>]', prevLink))
    end

    w('</div><div>')

    if (offsetVal + countVal) < totalVal then
        local nextLink = frame:callParserFunction('fullurl', {
            mw.title.getCurrentTitle().prefixedText,
            paramName .. "=" .. nextOffset
        })
        w(string.format('[%s <span class="category-page__pagination-prev wds-button wds-is-secondary">Next &gt;</span>]', nextLink))

        local lastLink = frame:callParserFunction('fullurl', {
            mw.title.getCurrentTitle().prefixedText,
            paramName .. "=" .. lastOffset
        })
        w(string.format('[%s <span class="wds-button wds-is-text">Last</span>]', lastLink))
    end

    w('</div></div>')

    return table.concat(out, '\n')
end

function p.test(frame)
local rawValue = frame.args.total or ""
-- rawValue = mw.ustring.gsub(rawValue, "^%s*(.-)%s*$", "%1")
-- rawValue = mw.ustring.gsub(rawValue, "%z\194\160", "")  -- or other patterns


local numeric = tonumber(mw.text.trim(rawValue))
if not numeric then
    return "Debug: Could not parse to a number: '" .. rawValue .. "'"
end
end

return p