Module:Dimensions

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

Usage instructions

Config

Dimensions.new( config )
Config options
{
  "color": "hex color of the border and + in the center. Defaults to #5c5c5c",
  "backgroundColor": "hex color of the background. Defaults to #313238",
  "elementSize": "size of the individual size grids. Defaults to 40px"
}

Set dimensions

instance:setDimensions( width, height)

Render

instance:renderDimensions()

Example

local instance = Dimensions.new()
instance:setDimensions(4, 2)
instance:renderDimensions()

or

Dimensions.main(nil, 4, 2)

require( 'strict' )

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


--- Sets the width and height of the Dimensions object
--- @param width number
--- @param height number
function methodtable:setDimensions(width, height)
    checkType('Dimensions.renderDimensions', 1, self, 'table')
    checkTypeMulti('Dimensions.renderDimensions', 2, width, { 'number'})
    checkTypeMulti('Dimensions.renderDimensions', 3, height, { 'number'})

    self.width = width
    self.height = height
end

--- Create indicator based on specified data or data from self
--- @param includeStyle boolean|nil include templatestyles in output
--- @return string html
function methodtable:renderDimensions(includeStyle)
    checkType('Dimensions.renderDimensions', 1, self, 'table')
    checkTypeMulti('Dimensions.renderDimensions', 2, includeStyle, { 'boolean', 'nil'})

    if not self.width or not self.height then return '' end

    if includeStyle == nil then
        includeStyle = true
    end

    local amount = self.width * self.height

    local grid_template_columns = '';

    for i = 1, self.width do
        grid_template_columns = grid_template_columns .. "auto "
    end

    local html = mw.html.create( nil )
            :tag('div')
            :addClass('dimensions__container')
            :css( 'grid-template-columns', grid_template_columns)
            :css( '--dimensions-color', self.config.color or '#5c5c5c')
            :css( '--dimensions-background-color', self.config.backgroundColor or '#313238')
            :css( '--dimensions-size', self.config.elementSize or '40px')

    for i = 1, amount do
        html:tag('div')
                :addClass('dimensions__grid-element')
                :tag('div')
                :addClass('dimensions__grid-decorator')
                :done()
                :done()
    end

    html:allDone()

    if includeStyle then
        local frame = mw.getCurrentFrame()

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

--- New Instance
--- @param config table {imageWidth, imageHeight, namespaces, indicators}
--- @return table Dimensions
function Dimensions.new( config )

    local baseConfig = {
        elementSize = "40px",
    }

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

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

    setmetatable( instance, metatable )

    return instance
end

function Dimensions._main(config, width, height, includeStyle)
    local instance = Dimensions.new(config)
    instance:setDimensions(width, height)
    return instance:renderDimensions(includeStyle)
end

function Dimensions.example()
    return Dimensions._main({}, 4, 2)
end

return Dimensions