Module:DPL scroll

--[=[

--]=] -- local p = {} local dt = require('Module:Paramtest').default_to

-- defaults for number local function get_num(v1, v2) return tonumber(dt(v1, v2 or 0)) or 0 end

-- actually make the pages -- args: total, count, offset, query local function make_pages(a) local total = get_num(a.total) -- total results local count = get_num(a.count) -- results per page local offset = get_num(a.offset) -- starting point local pages = math.ceil(total / count) -- total number of pages local currpage = math.ceil(offset / count) -- current page, starting at 0 local css = {['padding-right'] = '24px'} local query = a.query or {} query.DPL_offset = 0 query.action = 'purge' -- one (or no) page, don't do anything else if pages < 2 then return false, '' end local title = mw.title.getCurrentTitle local span = mw.html.create('span') span:addClass('dpl-pagination') if currpage == 0 then -- first page, have first interval bold span:tag('span'):css(css):wikitext(string.format("1..%s ", count)):done elseif currpage == 1 then -- second page, first interval linked, second bolded span:tag('span'):css(css) :wikitext(string.format("[%s 1..%s]", title:fullUrl(query), count)) -- offset=0 :done span:tag('span'):css(css) :wikitext(string.format("%s..%s ", count + 1, math.min(count * 2, total))) :done else -- third and onwards, start link, prev link, bolded current span:tag('span'):css(css) :wikitext(string.format("[%s Start] ", title:fullUrl(query))) -- offset=0 :done query.DPL_offset = (currpage - 1) * count span:tag('span'):css(css) :wikitext(string.format("[%s %s..%s] ", title:fullUrl(query), query.DPL_offset + 1, query.DPL_offset + count)) -- offset=prev :done span:tag('span'):css(css) :wikitext(string.format("%s..%s ", query.DPL_offset + count + 1, math.min(query.DPL_offset + count * 2, total))) :done end if currpage < pages - 1 then -- at least another page left, so add next page link query.DPL_offset = (currpage + 1) * count span:tag('span'):css(css) :wikitext(string.format("[%s %s..%s]", title:fullUrl(query), query.DPL_offset + 1, math.min(query.DPL_offset + count, total))) -- offset=next :done if currpage < pages - 2 then -- at least 2 pages left, so add 'end' link query.DPL_offset = (pages - 1) * count span:tag('span'):css(css) :wikitext(string.format("[%s End]", title:fullUrl(query))) -- offset=end :done end end return true, span end

--[=[ module access point args: total: total number of results; default 0 count: number of results per page; default 0 offset: beginning of this page of results; default 0 query: optional additional query, which can be either: a string of form param1=value1&value2=param2...etc - can (but doesn't need to) start with & a table of param = value primitive pairs (both strings or numbers; don't nest tables) - the better option for when using this in another module returns: boolean, span boolean: true if there is more than one page, false if there is only one page span: mw.html object of span containing page navigation (or empty string) --]=] function p._main(args) local a = {} a.total = args.total or 0 a.count = args.count or 0 a.offset = args.offset or 0 a.query = {} if type(args.query) == 'table' then a.query = args.query elseif type(args.query) == 'string' then a.query = mw.uri.parseQueryString(args.query) end return make_pages(a) end

--[=[ template access point args: as above obviously a table for query cannot be passed via template --]=] function p.main(frame) return p._main(frame:getParent.args) end

return p