Module:Indicator

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

Usage instructions

Namespace names and valid overrides are defined in Module:Indicator/config.json.

Custom configuration can be passed to the constructor.

Indicator.new( {} )

Example

To create an indicator for the page the calling template is transcluded on use the following code.

local indicator = require('Module:Indicator').new()
    indicator:getIndicatorData(mw.title.getCurrentTitle().namespace)
toString( indicator )

To override the namespace provide a valid string as the second aregument.

local indicator = require('Module:Indicator').new()
    indicator:getIndicatorData(mw.title.getCurrentTitle().namespace, self.namespace)
toString( indicator )

require('strict')

local Indicator = {}
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:renderIndicator())
end

function Indicator.main(frame)
    local args = require('Module:Arguments').getArgs(frame, { parentOnly = true })
    local namespace = mw.title.getCurrentTitle().namespace

    local instance = Indicator.new()
    instance:getIndicatorData(namespace, args.featured == args[1] and args[2] or args[1], args.featured == 'true' and true or false)

    return instance:renderIndicator()
end

--- Gets image, text, and link from the indicator that matches indicatorName or namespace
--- @param namespace string namespaceID
--- @param indicatorName string name of the indicator
--- @param featured boolean if this page is featured
--- @return table {image, text, link}
function methodtable:getIndicatorData(namespace, indicatorName, featured)
    checkType('Indicator.getIndicatorData', 1, self, 'table')
    checkTypeMulti('Indicator.getIndicatorData', 2, namespace, { 'string', 'number' })
    checkTypeMulti('Indicator.getIndicatorData', 3, indicatorName, { 'string', 'nil' })
    checkTypeMulti('Indicator.getIndicatorData', 2, featured, { 'boolean', 'nil' })

    local indicator, text

    if indicatorName and self.config.indicators[indicatorName:lower()] then
        indicator = self.config.indicators[indicatorName:lower()]
    elseif self.config.indicators[self.config.namespaces[namespace]] then
        indicator = self.config.indicators[self.config.namespaces[namespace]]
    else
        return
    end

    self.image = indicator.image
    self.text = indicator.text
    self.link = indicator.link or text

    self.featured = featured or false

    return indicator.image, indicator.text, indicator.link or text
end

--- Create indicator based on specified data or data from self
--- @param includeStyle boolean|nil include templatestyles in output
--- @param image string|nil
--- @param text string|nil
--- @param link string|nil
--- @return string html
function methodtable:renderIndicator(includeStyle, image, text, link)
    checkType('Indicator.renderIndicator', 1, self, 'table')
    checkTypeMulti('Indicator.renderIndicator', 2, includeStyle, { 'boolean', 'nil' })
    checkTypeMulti('Indicator.renderIndicator', 3, image, { 'string', 'nil' })
    checkTypeMulti('Indicator.renderIndicator', 4, text, { 'string', 'nil' })
    checkTypeMulti('Indicator.renderIndicator', 5, link, { 'string', 'nil' })

    if includeStyle == nil then
        includeStyle = true
    end

    image = image or self.image
    text = text or self.text
    link = link or self.link or text

    if not image or not text then
        return ''
    end

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

    if self.featured then
        html:tag('div')
            :addClass('indicator__container')
            :tag('div')
            :addClass('indicator__image')
            :wikitext(string.format("[[File:%s|%sx%spx|link=%s]]", self.config.featuredIcon, self.config.imageWidth or '', self.config.imageHeight or '', self.config.featuredLink or ''))
            :done()
            :done()
    end

    html:tag('div')
        :addClass('indicator__container')
        :tag('div')
        :addClass('indicator__image')
        :wikitext(string.format("[[File:%s|%sx%spx|link=%s]]", image, self.config.imageWidth or '', self.config.imageHeight or '', link))
        :done()
        :tag('div')
        :addClass('indicator__text')
        :wikitext(string.format("[[%s|%s]]", link, text))
        :done()
        :done()

    if includeStyle then
        local frame = mw.getCurrentFrame()

        return frame:extensionTag {
            name = 'templatestyles', args = { src = "Module:Indicator/styles.css" }
        } .. tostring(html)
    else
        return tostring(html)
    end
end

--- New Instance
--- @param config table {imageWidth, imageHeight, namespaces, indicators}
--- @return table Indicator
function Indicator.new(config)
    config = config or mw.loadJsonData('Module:Indicator/config.json')

    local baseConfig = {
        imageWidth = nil,
        imageHeight = 28
    }

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

    local instance = {
        config = baseConfig,
        modules = {}
    }

    setmetatable(instance, metatable)

    return instance
end

return Indicator