Module:Pagination

Summary
Pagination feature



local p = {}

--[[
Bookstrap pagination

{{#invoke:Pagination|...
|total={{{Total|0}}}
|totalvisible=
|target={{{Target|{{FULLPAGENAME}}}}}
|currentoffset={{#cr-urlget:par={{#var:@name offset}}|default=0}}
|nameoffset={{{Name offset|offset}}}
|withurlparameters=
}}
]]

p.getPagination = function(frame) 
	local total = tonumber( frame.args.total or 0 )
	local totalVisible = tonumber( frame.args.totalvisible or 0 )
	local initialOffset = tonumber( frame.args.initialoffset or 0 )
	local nameOffset = frame.args.nameoffset or "offset"
	local target = frame.args.target or ""
	local withUrlParameters = frame.args.withurlparameters or nil

	-- calculated variables
	-- whether to show pagination at all {{#ifexpr: {{#var:@total|0}} < {{#var:@total visible|0}}|false|true}} 
	if ( total < totalVisible ) then return "" end

	-- loop
	local numberOfLoops = math.ceil( total / totalVisible )
	
		--currentOffset
	local str = ""

	for i=1, numberOfLoops do
		--str = str .. " " .. i
		local currentOffset = ( i * totalVisible ) - totalVisible
		--local offset = ( offset * totalVisible ) + totalVisible
		local class = "page-item"
		if currentOffset == initialOffset then class = class .. " active" end
		local href = tostring( mw.uri.fullUrl( target, { [nameOffset] = currentOffset } ) )
		if withUrlParameters ~= nil then
			href = href .. "&" .. withUrlParameters
		end
		local widget = '[' .. i .. "=" .. currentOffset .. '] '
		local widget = frame:callParserFunction( "#widget", { "Link",
			["type"] = "a",
			["class"] = "page-link",
			["href"] = href,
			["text"] = i
		} )
		local li = '<li class="' .. class .. '">' .. widget .. '</li>'
		str = str .. li
	end

	return '<ul class="pagination pagination-lg pl-0 flex-wrap">' .. str .. '</ul>'

	
end

return p;