Module:Icons/Sandbox: Difference between revisions

_Currency: Format negative maxAmount values also, and refactor to avoid repeated string concatenation
(Add red colouring for negative currencies)
(_Currency: Format negative maxAmount values also, and refactor to avoid repeated string concatenation)
 
Line 844: Line 844:


function p._Currency(fileName, link, altText, amount, maxAmount)
function p._Currency(fileName, link, altText, amount, maxAmount)
local amountText = nil
local ret = {}
local maxAmountText = nil
table.insert(ret, '<span style="display:inline-block">')
 
-- Currency icon
if tonumber(amount) ~= nil then
amountText = formatnum(amount)
if (maxAmount ~= nil and maxAmount >= amount) then
maxAmountText = ' - ' .. formatnum(maxAmount)
end
end
 
local fileText = ''
if fileName ~= nil then
if fileName ~= nil then
fileText = '[[File:' .. fileName .. '|25px'
table.insert(ret, '[[File:' .. fileName .. '|25px')
if link ~= nil then
if link ~= nil then
fileText = fileText .. '|link=' .. link
table.insert(ret, '|link=' .. link)
end
end
fileText = fileText .. '|alt=' .. ((altText == nil and '') or altText) .. ']]'
table.insert(ret, '|alt=' .. ((altText == nil and '') or altText) .. ']]')
end
end
 
-- Currency amounts
local ret = {}
if tonumber(amount) ~= nil then
table.insert(ret, '<span style="display:inline-block">')
local function numColour(amount)
table.insert(ret, fileText)
if amount < 0 then
return '<span style="color:red;">' .. formatnum(amount) .. '</span>'
if amountText ~= nil then
else
-- Colour number negative, if it's a number below 0
return formatnum(amount)
local amountNum = tonumber(amount)
end
if amountNum < 0 then
end
table.insert(ret, '<span style="color:red;">&nbsp;' .. amountText .. '</span>')
table.insert(ret, '&nbsp;' .. numColour(amount))
else
if (tonumber(maxAmount) ~= nil and maxAmount > amount) then
-- Otherwise, just add the number as is.
table.insert(ret, ' - ' .. numColour(maxAmount))
table.insert(ret, '&nbsp;' .. amountText)
end
end
end
end
table.insert(ret, maxAmountText or '')
table.insert(ret, '</span>')
table.insert(ret, '</span>')