It occurs to me to post more code, so this is my entire opening and closing of faction headers, plus setting of the faction by factionID. If anyone wants to see all of the code, I'll be happy to post that too.
Lua Code:
local repsCollapsed = {} -- Obey user's settings about headers opened or closed
-- Open all faction headers
function RepByZone:OpenAllFactionHeaders()
local i = 1
while i <= GetNumFactions() do
local name, _, _, _, _, _, _, _, isHeader, isCollapsed = GetFactionInfo(i)
if isHeader then
repsCollapsed[name] = isCollapsed
if name == FACTION_INACTIVE then
if not isCollapsed then
CollapseFactionHeader(i)
end
break
elseif isCollapsed then
ExpandFactionHeader(i)
end
end
i = i + 1
end
end
-- Close all faction headers
function RepByZone:CloseAllFactionHeaders()
local i = 1
while i <= GetNumFactions() do
local name, _, _, _, _, _, _, _, isHeader, isCollapsed = GetFactionInfo(i)
if isHeader then
if isCollapsed and not repsCollapsed[name] then
ExpandFactionHeader(i)
elseif repsCollapsed[name] and not isCollapsed then
CollapseFactionHeader(i)
end
end
i = i + 1
end
wipe(repsCollapsed)
end
function RepByZone:GetAllFactions()
-- Will not return factions the user has marked as inactive
self:OpenAllFactionHeaders()
local factionList = {}
for i = 1, GetNumFactions() do
local name, _, _, _, _, _, _, _, isHeader, _, _, _, _, factionID = GetFactionInfo(i)
if not isHeader then
factionList[factionID] = name
end
end
self:CloseAllFactionHeaders()
return factionList
end
-- Blizzard sets watched faction by index, not by factionID so create our own API
function RepByZone:SetWatchedFactionByFactionID(id)
if type(id) == "table" then id = tonumber(id) end
if type(id) ~= "number" then return end
self:OpenAllFactionHeaders()
for i = 1, GetNumFactions() do
local name, _, standingID, _, _, _, _, _, isHeader, _, _, isWatched, _, factionID = GetFactionInfoByID(id)
if id == factionID then
self:Print("DEBUG: SetWatchedFactionByFactionID name:", name)
self:Print("DEBUG: SetWatchedFactionByFactionID index:", i)
if not isWatched then
SetWatchedFactionIndex(i)
end
self:CloseAllFactionHeaders()
return name, id
else
break
end
end
self:CloseAllFactionHeaders()
end
-- Player switched zones, set watched faction
function RepByZone:SwitchedZones()
local UImapID = IsInInstance() and select(8, GetInstanceInfo()) or C_Map.GetBestMapForUnit("player")
local locationsAndFactions = IsInInstance() and self:InstanceAndFactionList() or self:ZoneAndFactionList()
-- local mapID = C_Map.GetMapInfo(UImapID).mapID
self:Print("DEBUG: Current UImapID is", UImapID)
-- self:Print("DEBUG: Current mapID is", mapID)
self:SetWatchedFactionByFactionID(db.defaultRepID)
for zoneID, factionID in pairs(locationsAndFactions) do
if zoneID == UImapID then
self:Print("DEBUG: zoneID and UImapID match")
self:SetWatchedFactionByFactionID(factionID)
break
end
end
end