Module:ElectoralCalendarMap
Documentation for this module may be created at Module:ElectoralCalendarMap/doc
local p = {}
function p.load(frame)
local dataPage = "Data:Election_Data.json"
local rowsPerPage = 7
local currentPage = tonumber(frame.args.page) or 1
local dataTitle = mw.title.new(dataPage)
if not dataTitle then
return "could not find data page '" .. dataPage .. "'!"
end
local dataContent = dataTitle:getContent()
if not dataContent then
return "could not retrieve content from '" .. dataPage .. "'!"
end
local APIData = mw.text.jsonDecode(dataContent)
if not APIData then
return "invalid JSON content in '" .. dataPage .. "'!"
end
local electionColors = {
General = "#4682b4", -- blue
Local = "#8a2be2", -- purple
Statewide = "#008000", -- green
Referendum = "#ffa500" -- orange
}
local allElections = {}
for region, elections in pairs(APIData) do
for _, election in ipairs(elections) do
table.insert(allElections, {
region = region,
election = election
})
end
end
table.sort(allElections, function(a, b)
return a.election.date > b.election.date
end)
local totalRows = #allElections
local totalPages = math.ceil(totalRows / rowsPerPage)
if currentPage > totalPages then currentPage = totalPages end
if currentPage < 1 then currentPage = 1 end
local startRow = (currentPage - 1) * rowsPerPage + 1
local endRow = math.min(startRow + rowsPerPage - 1, totalRows)
local paginatedElections = {}
for i = startRow, endRow do
table.insert(paginatedElections, allElections[i])
end
local combinedJSON = {}
for _, entry in ipairs(paginatedElections) do
local region = entry.region
local election = entry.election
local mapDataTitle = mw.title.new("MapData:" .. region .. ".map")
if mapDataTitle then
local content = mapDataTitle:getContent()
if content then
local parsedContent = mw.text.jsonDecode(content)
if parsedContent and type(parsedContent.features) == "table" then
for _, feature in ipairs(parsedContent.features) do
feature.properties.title = string.gsub(region, "_", " ")
feature.properties.description = string.format(
"<b>%s</b><br>%s<br>%s",
election.election_name,
election.date,
election.description
)
local fillColor = electionColors[election.election_type] or "#404040"
feature.properties.fill = fillColor
feature.properties["fill-opacity"] = 0.8
table.insert(combinedJSON, feature)
end
else
mw.log("invalid or missing features in mapdata for region '" .. region .. "'!")
end
else
mw.log("warning: couldn't fetch mapdata for region '" .. region .. "'!")
end
else
mw.log("warning: couldn't create title object for region '" .. region .. "'!")
end
end
local combinedMapData = mw.text.jsonEncode({
type = "FeatureCollection",
features = combinedJSON
})
local mapFrame = '<mapframe width="100%" height="400" zoom="3" longitude="0" latitude="0">' .. combinedMapData .. '</mapframe>'
return frame:preprocess(mapFrame)
end
return p