Module:ElementTooltip

From Oxygen Not Included Wiki
Jump to navigation Jump to search

This Module is called by Template:ElementTooltip, given the displayname of an element, it finds the corresponding element from one of the following the gamefiles:

and localization files

After the element is found, all relevant information about it is gathered, including basic information about related elements it can turn into upon phase transition (e.g. a solid melting into a liquid). The data is then sorted into data-attributes of a <span>, which are later read and formatted by the Javascript Tooltip.js.


local p = {}

-- misc functions
local eleFromPagename = require("Module:ElementNames").elementFromPagename
local eleFromEleId = require("Module:ElementNames").elementFromElementId
local pagenameFromEle = require("Module:ElementNames").pagenameFromElement
local i18nde = require("Module:I18n/strings/elements")
local function _allCapsNoSpace(str) return str:upper():gsub("%s+", "") end
local _e = function(...) return i18nde["en"][(({...})[1])] end
local function _areSame(s1, s2) return (string.lower(s1)):match("([^%s]+)$") == (string.lower(s2)):match("([^%s]+)$") end

local function _trim(str) return (str:gsub("^%s*(.-)%s*$", "%1")) end
local function _replaceStyledCaption(str) 
    return str
    :gsub('<style="KKeyword">(.-)</style>', '[[%1]]')   -- pray and hope all ingame codex-entries are pagenames
    :gsub('<b>(.-)</b>', "'''%1'''")                    -- wikitext bold
    :gsub('<i>(.-)</i>', "''%1''")                      -- wikitext italics
    :gsub('<sub>(.-)</sub>', '--%1--')                  -- this needs to be fixed by javascript later
end

local function _extractChemicalSymbol(caption)
    if caption:sub(1, 1) == "(" then -- if caption starts with "(" 
        local match = caption:match("%b()") -- extract 
        if match then return match:sub(2, -2) end -- Remove the surrounding brackets
    end
    return nil
end

local function _fetchPhaseTransitionData(eleID)
    local e = eleFromEleId(eleID)
    local t = {}
    t.name = pagenameFromEle(e)
    t.state = e.state
    t.SHC = e.specificHeatCapacity
    t.TC = e.thermalConductivity
    return t
end

local function _transitionName(e,t,k)
    local transitionName = {
        Solid = { 
            Liquid  = { high = 'melts' }, 
            Gas     = { high = 'sublimates' }, 
            Solid   = { high = 'cooks' } 
        },
        Liquid = { 
            Gas     = { low = 'exsolutes',   high = 'vaporizes' },
            Solid   = { low = 'freezes',     high = 'exsolutes' }
        },
        Gas = { 
            Liquid  = { low = 'condenses' },
            Solid   = { low = 'depositions' }
        }
    }
    if transitionName[e] then if transitionName[e][t] then if transitionName[e][t][k] then 
        return transitionName[e][t][k] .. ' into'
    end end end
    return 'transitions into'
end

local function _addPhaseTransitionData(e)
    for _, i in ipairs({'high', 'low'}) do
        for _, j in ipairs({'OreId', 'Target'}) do
            if e[i .. 'TempTransition' .. j] then
                local t = _fetchPhaseTransitionData(e[i .. 'TempTransition' .. j])
                e[i .. 'TempTransition' .. j .. 'Name'] = t.name
                e[i .. 'TempTransition' .. j .. 'SHC'] = t.SHC
                e[i .. 'TempTransition' .. j .. 'TC'] = t.TC
                e[i .. 'TempTransitionName'] = _transitionName(e.state,t.state,i)
            end
        end
    end
    return e
end

local function _addpic(p,q)
    if p then
        return '<span class="element-tooltip--related-pic" data-picof="'.. q ..'" > [[File:' .. _trim(p) .. '.png|24px|link=]]</span>'
    end
    return ''
end

local function _primeElementData(pagename)
    local e = eleFromPagename(pagename)
    e.elementname = pagenameFromEle(e)

    -- fetch Description
    e.caption = _e("STRINGS.ELEMENTS." .. _allCapsNoSpace(e.elementId) .. ".DESC") or ""
    e.caption = _replaceStyledCaption(e.caption)
    e.tags = nil

    -- add additional Data
    e = _addPhaseTransitionData(e)
    if _extractChemicalSymbol(e.caption) then e.chem = _extractChemicalSymbol(e.caption) end

    if e.elementname == "Void" and pagename ~= "Void" then 
        return nil
    end
    return e
end

function p.main(frame)
    local e = _primeElementData(_trim(frame.args.elementname))
    if e == nil then return '[[Category:Pages that use ElementTooltip incorrectly]]' end
    
    local output = mw.html.create('')
	output:wikitext('<span class="advanced-tooltip--content element-tooltip--content" ')
    for k, v in pairs(e) do
        output:wikitext('data-' .. k .. '="' .. tostring(v) .. '" ')
    end
    output:wikitext('>')

    output:wikitext(_addpic(e.highTempTransitionTargetName,'highTempTransitionTargetName'))
    output:wikitext(_addpic(e.highTempTransitionOreIdName,'highTempTransitionOreIdName'))
    output:wikitext(_addpic(e.lowTempTransitionTargetName,'lowTempTransitionTargetName'))
    output:wikitext(_addpic(e.lowTempTransitionOreIdName,'lowTempTransitionOreIdName'))
    output:wikitext('</span>')

    return tostring(output)
end
return p