Module:ShellTraits

Documentation for this module may be created at Module:ShellTraits/doc

require( 'strict' )

local ShellTraits = {}

local metatable = {}
local methodtable = {}

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

metatable.__index = methodtable

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

--- Wrap the ShellTraits HTML
---
--- @param innerHtml string inner html of the ShellTraits
--- @return string html ShellTraits html with templatestyles
function methodtable.renderShellTraits( self, innerHtml )
    checkType( 'Module:ShellTraits.renderShellTraits', 1, self, 'table' )
    checkType( 'Module:ShellTraits.renderShellTraits', 2, self, 'table' )

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

    local function renderContent()
        local html = mw.html.create( 'div' )
                       :addClass( 'percentage-bar__container' )
                       :wikitext( innerHtml )
        return tostring( html )
    end

    local output = renderContent()

    local frame = mw.getCurrentFrame()

    return frame:extensionTag {
        name = 'templatestyles', args = { src = "Module:PercentageBar/styles.css" }
    } .. output
end

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

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

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

    setmetatable( instance, metatable )

    return instance
end

--- Create an PageSwitcher from args
---
--- @param frame table
--- @return string
function ShellTraits.fromArgs( frame )
    local config = mw.loadJsonData( 'Module:ShellTraits/config.json' )

    local instance = ShellTraits:new(nil, config.core)
    local args = require( 'Module:Arguments' ).getArgs( frame, {
        wrappers = 'Template:Shell traits'
    } )

    local percentageBar = require('Module:PercentageBar'):new(config.core)

    local traits = {}

    local bars = {}

    for k, v in pairs(config.traitMapping) do
        if args[k] then
            table.insert(traits, v.order, { label = v.label, value = args[k] })
        end
    end

    for _, trait in pairs( traits ) do
        table.insert(bars, percentageBar.renderPercentageBar(percentageBar, trait, false))
    end

    return instance:renderShellTraits( bars )
end

return ShellTraits