Module:CircularVote

From OpenStreetMap Foundation
Jump to navigation Jump to search

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

local p = {}

function p.show(frame)
    local args = frame.args
    local list = args.list or ""
    local default = args.defaultcolor or "lightgray"

    local people = mw.text.split(list, "%s*,%s*")
    table.sort(people)

    local out = {}
    for _, initials in ipairs(people) do
        initials = mw.text.trim(initials)
        local fullname = args["name_" .. initials] or initials
        local circle_color = default

        local vote = (args[initials] or ""):lower()

        local vote_color = ({
            yes = "green",
            no = "red",
            abstention = "yellow",
            abstain = "yellow",
            none = "gray",
            ["did not vote"] = "gray"
        })[vote] or "gray"

        local vote_symbol = ({
            yes = "✓",
            no = "✗",
            abstention = "–",
            abstain = "–",
            none = "",
            ["did not vote"] = ""
        })[vote] or ""

        local symbol_color = vote_color == "yellow" and "black" or "white"

        table.insert(out, string.format([[
<div title="%s" style="position:relative; display:inline-flex; justify-content:center; align-items:center; width:4em; height:4em; border-radius:50%%; background:%s; color:white; font-weight:bold; font-size:0.75em; text-align:center; white-space:nowrap; overflow:hidden;">
  %s
  <div style="position:absolute; bottom:0.1em; right:0.1em; width:1em; height:1em; border-radius:50%%; background:%s; border:2px solid white; display:flex; align-items:center; justify-content:center; font-size:0.75em; color:%s;">
    %s
  </div>
</div>
        ]],
        mw.text.encode(fullname),
        circle_color,
        initials,
        vote_color,
        symbol_color,
        vote_symbol))
    end

    return table.concat(out, "")
end

return p