Module:PageSwitcher

Module documentation[view][edit][history][purge]
This documentation is transcluded from Module:PageSwitcher/doc. Changes can be proposed in the talk page.
An icon from the Wikimedia Codex library.
Module:PageSwitcher loads styles from Module:PageSwitcher/styles.css.

This module is invoked by Template:PageSwitcher.


require('strict')

local PageSwitcher = {}
local metatable = {}
local methodtable = {}

local libraryUtil = require('libraryUtil')
local checkType = libraryUtil.checkType
local checkTypeMulti = libraryUtil.checkTypeMulti

metatable.__index = methodtable

metatable.__tostring = function(self)
    return tostring(self:renderSwitcher())
end

--- Wrap the HTML into an pageSwitcher button
---
--- @param data table {label, path, first, last, current}
--- @return string html
function methodtable:renderPageButton(data)
    checkType('Module:PageSwitcher.renderPageButton', 1, self, 'table')
    checkType('Module:PageSwitcher.renderPageButton', 2, data, 'table')

    local button = mw.html.create('div')
                     :addClass('pageswitcher__button')

    if data.first then
        button:addClass('pageswitcher__button--first')
    end
    if data.last then
        button:addClass('pageswitcher__button--last')
    end
    if data.current then
        button:addClass('pageswitcher__button--current')
    end

    button:wikitext(string.format("'''[[%s|%s]]'''", data.path, data.label))

    table.insert(self.entries, tostring(button))
    return(tostring(button))
end

--- Wrap the pageSwitcher HTML
---
--- @param innerHtml string inner html of the pageSwitcher
--- @return string html pageSwitcher html with templatestyles
function methodtable:renderSwitcher(innerHtml)
    checkType('Module:PageSwitcher.renderSwitcher', 1, self, 'table')
    checkTypeMulti('Module:PageSwitcher.renderSwitcher', 2, innerHtml, { 'string', 'table', 'nil' })

    innerHtml = innerHtml or self.entries
    if type(innerHtml) == 'table' then
        innerHtml = table.concat(self.entries)
    end

    local indicator = require('Module:Indicator').new()
    indicator:getIndicatorData(mw.title.getCurrentTitle().namespace, self.namespace, self.featured == 'true' and true or false)

    local html = mw.html.create(nil)
                   :tag('div')
                   :addClass('pageswitcher__parent')
                   :tag('div')
                   :addClass('pageswitcher__container')
                   :tag('div')
                   :addClass('pageswitcher__row')
                   :wikitext(innerHtml)
                   :done()
                   :done()

    if self.namespace ~= "blank" then
        html:wikitext( tostring(indicator) )
    end
    html:done()

    local frame = mw.getCurrentFrame()

    return frame:extensionTag {
        name = 'templatestyles',
        args = { src = 'Module:PageSwitcher/styles.css' }
    }
            .. tostring(html)
            .. table.concat(self.categories)
end

--- New Instance
---
--- @return table PageSwitcher
function PageSwitcher.new(config)
    local baseConfig = {}

    for k, v in pairs(config or {}) do
        baseConfig[k] = v
    end

    local instance = {
        categories = {},
        config = baseConfig,
        entries = {}
    }

    setmetatable(instance, metatable)

    return instance
end

--- Create an PageSwitcher from args
---
--- @param frame table
--- @return string
function PageSwitcher.fromArgs(frame)
    local args = require('Module:Arguments').getArgs(frame, { parentOnly = true })
    local instance = PageSwitcher.new()

    local currentFullPageName = mw.title.getCurrentTitle().fullText
    local pages = {}

    local i = 1
    while args['page' .. i] do
        local rawPath = args['page' .. i]
        local path = rawPath

        if rawPath:sub(-1) == ':' then
            local _, page = currentFullPageName:match("^(.-):(.*)$")
            path = rawPath .. (page or currentFullPageName)
        elseif rawPath:sub(1, 1) == '/' then
            path = currentFullPageName .. rawPath
        end

        local label = args['label' .. i]
                or rawPath:gsub("^/", ""):gsub(":$", "")

        table.insert(pages, {
            label = label,
            path = path,
            current = (path == currentFullPageName)
        })

        i = i + 1
    end

    instance.namespace = args['indicator']
    instance.featured = args['featured']

    for index, page in ipairs(pages) do
        instance:renderPageButton {
            label = page.label,
            path = page.path,
            first = (index == 1),
            last = (index == #pages),
            current = page.current
        }
    end

    return instance:renderSwitcher(nil)
end

return PageSwitcher