Module:Icons

From Workers & Resources: Soviet Republic Official Wiki

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

local p = {}
local util = require( 'Module:Util' )
--[[
	Icon data is now stored on the sub page /data to seperate functional code
	for the dataset users will often need to update. This reduces the risk of
	breaking the code by entering data in the wrong place and makes it easier to
	manage the code itself by not having to pass through the large dataset at 
	the top.
	
	This module was taken from Nukapedia. All credits go to said wiki.
]]

require( 'Module:Icons/data' ) 

local iconSize = {
    --[[
        All sizes are controlled on the height to ensure a string of icons maintain 
        a consistent line height
    ]]
    ["small"]           = "x10px",
    ["medium"]          = "x14px",
    ["normal"]          = "x14px",
    ["big"]             = "x20px",
}

--[[ 28/Oct/2021 Added class control table to array as to handle light/dark 
	themes. This was originally put into the frontend template, when class
	handling should be back end. Adding the ['class'] = 'light' or 
	['class'] = 'dark' parameter to a icon's dataset on /data will enable it to
	access a class if needed.
	]]

--[[ 08/Nov/2021 Remove the class definition as to be able to just pass straight through
local class = {
	['light'] = 'lighticon',
	['dark'] = 'darkicon',
	['general'] = 'generalicon'
}
]]

function _generate(iconList, iconSetting, iconLinks, tipOverride, iconClass, iconSize)
    if util.exists(iconSetting)  then 
    	iconSetting = util.trim(iconSetting)
        if util.exists(iconSize[iconSetting]) then
            iconSetting = iconSize[iconSetting]
        end
    else
        iconSetting = iconSize["medium"]
    end
    -- This is for calls from other Lua modules as the above will result in nil
    if util.exists(iconSetting) == false then
    	if util.exists(iconSize) then
			iconSetting = iconSize
		else
			iconSetting = 'x14px'
		end
    end
    
    if util.exists(iconLinks) then
        iconLinks = mw.text.split(iconLinks, ",")
    end
        
    if util.exists(tipOverride) then
        tipOverride = mw.text.split(tipOverride, ",")
    end
    
    if util.exists(iconClass)then
    	iconClass = "|class=" .. tostring(iconClass);
    else
    	iconClass = ""
    end

    local result = ""

    for k, v in ipairs(iconList) do
        newIcon = iconData[util.trim(v)]
        if util.exists(newIcon) then
            currentIcon = newIcon.icon
            if util.exists(tipOverride, k) then
                currentTip = tipOverride[k]
            else
                if util.exists(iconLinks, k) then
                    currentTip = iconLinks[k]
                else
                    currentTip = newIcon.tip
                end
            end
        else
            currentIcon = "Icon question.png"
            currentTip = "Unrecognized icon name"
            result = result .. "[[Category:Modules with invalid parameters]]"
        end
        
        --Create wikitext icon
		dataLine = '[[File:' .. currentIcon .. '|' .. iconSetting
        if util.exists(iconLinks, k) then
            dataLine = dataLine .. '|link=' .. iconLinks[k]
        else
            dataLine = dataLine .. '|link='
        end
        if currentTip ~= nil then
            dataLine = dataLine .. '|' .. currentTip
        end

        dataLine = dataLine .. iconClass .. ']]'

		createTip = mw.html.create('span')
        	createTip:addClass( 'va-icon' )
        	:attr('title', currentTip)
        	:wikitext(dataLine)
        result = result .. tostring(createTip)
        
        if k < table.getn(iconList) then
        	result = result .. " "
        end
    end
    
    return result
end

-- Calls from other modules
function p.innerIcon(iconList, iconSetting, iconLinks, tipOverride, iconClass, iconSize)
	iconList = mw.text.split(iconList, ',')
	return _generate(iconList, iconSetting, iconLinks, tipOverride, iconClass, iconSize)
end

function p.Icons(frame) 
	--[[All icons are now lower case to reduce script errors from 
	incorrectly entering the icon code in a different case to the list]]
    local iconList = mw.text.split(string.lower(frame.args[1]), ",")
    local iconSetting = frame.args[2]
    local iconLinks = frame.args[3]
    local tipOverride = frame.args[4]
    local iconClass = frame.args[5]	
	local iconSize = {
    	--[[
    	    All sizes are controlled on the height to ensure a string of icons maintain 
	        a consistent line height
    	]]
    	["small"]           = "x10px",
    	["medium"]          = "x14px",
    	["normal"]          = "x14px",
    	["big"]             = "x20px"
	}
    return _generate(iconList, iconSetting, iconLinks, tipOverride, iconClass, iconSize)    
end

function p.platforms(frame)
	--[[All icons are now lower case to reduce script errors from 
	incorrectly entering the icon code in a different case to the list]]
    local icons = mw.text.split(string.lower(frame.args[1]), ",")
    local result = ""
    
    for k, v in ipairs(icons) do
        currentIcon = iconData[util.trim(v)]
        
        if util.exists(currentIcon, 'platform') == true then
        	
            createSM = mw.html.create('span')
            	createSM:css('display', 'none')
            	:wikitext('[[Has platform::' .. currentIcon.tip .. ']]')
            	:allDone()
            createPlatform = mw.html.create('span')
            	createPlatform:addClass('va-icon')
            		:attr('title', currentIcon.tip)
            		:wikitext('[[File:' 
            			.. currentIcon.icon 
            			.. '|alt='
            			.. currentIcon.tip
            			.. '|x14px|link=]]')
            		:allDone()
            	--[[ Uncommenting tostring(createSM) below will enable 
            	Semantic Mediawiki data tracking]]
            	result = result .. tostring(createSM) 
            			.. tostring(createPlatform)
            if k < table.getn(icons) then
                result = result .. " "
            end
        end
    end
    if result == '' then
        result = '<sup>[Platforms needed]</sup>[[Category:Platforms needed]]'
    end
    
    return result
end    

function p.documentation()
    keys = {}
    for k in pairs(iconData) do
        table.insert(keys, k)
    end
 
    table.sort(keys)
    result = '{| class="va-table va-table-full"\n|-\n !prefix!!Icon!!prefix!!Icon!!prefix!!Icon\n|-\n'
    set = 1
    for k,v in ipairs(keys) do
		i = iconData[v]
        result = result .. "||'''" .. v .. "'''"
        if util.exists(i) == true and i.class ~= nil then
        	newFile = mw.html.create(span)
        		--:addClass(class[i.class])
        		:wikitext('[[File:' .. iconData[v].icon .. '|25px]]')
        else
        	newFile = mw.html.create(span)
        		:wikitext('[[File:' .. iconData[v].icon .. '|25px]]')
        end
        result = result .. '||' .. tostring(newFile)
 
        if set == 3 then
            result = result .. '\n|-\n'
        end
 
        if set < 3 then
            set = set + 1
        else
            set = 1
        end
    end
 
    result = result .. '\n|}'
 
    return result
end

function p.Test(frame) 
	--Please empty when done debugging so other users know it is free to use

end

return p