forked from Auctionator/LibAHTab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLibAHTab.lua
101 lines (77 loc) · 3.07 KB
/
LibAHTab.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
local lib = LibStub:NewLibrary("LibAHTab-1-0", 2)
if not lib or lib.internalState then return end
local MIN_TAB_WIDTH = 70
local TAB_PADDING = 20
local tabIDErrorMessage = "tabID should be a string"
function lib:DoesIDExist(tabID)
assert(type(tabID) == "string", tabIDErrorMessage)
return lib.internalState and lib.internalState.usedIDs[tabID] ~= nil
end
function lib:CreateTab(tabID, attachedFrame, displayText, tabHeader)
assert(AuctionHouseFrame, "Wait for the AH to open before creating your tab")
assert(type(tabID) == "string", tabIDErrorMessage)
assert(type(attachedFrame) == "table" and attachedFrame.IsObjectType and attachedFrame:IsObjectType("Frame"), "attachedFrame should be a frame")
assert(type(displayText) == "string", "displayText should be a string")
assert(tabHeader == nil or type(tabHeader) == "string", "tabHeader should be a string")
if not lib.internalState then
lib.internalState = {
Tabs = {},
usedIDs = {},
selectedTab = nil,
}
lib.internalState.rootFrame = CreateFrame("Frame", nil, AuctionHouseFrame)
lib.internalState.rootFrame:SetSize(10, 10)
lib.internalState.rootFrame:SetPoint("TOPLEFT", AuctionHouseFrame.Tabs[#AuctionHouseFrame.Tabs], "TOPRIGHT")
hooksecurefunc(AuctionHouseFrame, "SetDisplayMode", function(self, mode)
if mode ~= nil and #mode > 0 then
for _, tab in ipairs(lib.internalState.Tabs) do
tab.frameRef:Hide()
PanelTemplates_DeselectTab(tab)
end
end
end)
end
if lib:DoesIDExist(tabID) then
error("The tab id already exists")
end
local newTab = CreateFrame("Button", nil, lib.internalState.rootFrame, "AuctionHouseFrameDisplayModeTabTemplate")
table.insert(lib.internalState.Tabs, newTab)
newTab:SetText(displayText)
lib.internalState.usedIDs[tabID] = newTab
PanelTemplates_TabResize(newTab, TAB_PADDING, nil, MIN_TAB_WIDTH)
if #lib.internalState.Tabs > 1 then
newTab:SetPoint("TOPLEFT", lib.internalState.Tabs[#lib.internalState.Tabs - 1], "TOPRIGHT", 3, 0)
else
newTab:SetPoint("TOPLEFT", lib.internalState.rootFrame, "TOPLEFT", 3, 0)
end
PanelTemplates_DeselectTab(newTab)
newTab.frameRef = attachedFrame
newTab.tabHeader = tabHeader or displayText
attachedFrame:Hide()
newTab:SetScript("OnClick", function()
lib:SetSelected(tabID)
end)
end
function lib:GetButton(tabID)
assert(type(tabID) == "string", tabIDErrorMessage)
return lib.internalState.usedIDs[tabID]
end
function lib:SetSelected(tabID)
assert(type(tabID) == "string", tabIDErrorMessage)
if lib.internalState == nil or not lib:DoesIDExist(tabID) then
error("Tab doesn't exist")
end
AuctionHouseFrame:SetDisplayMode({})
AuctionHouseFrame.displayMode = nil
for _, tab in ipairs(lib.internalState.Tabs) do
tab.frameRef:Hide()
PanelTemplates_DeselectTab(tab)
end
for _, tab in ipairs(AuctionHouseFrame.Tabs) do
PanelTemplates_DeselectTab(tab)
end
local selectedTab = lib:GetButton(tabID)
PanelTemplates_SelectTab(selectedTab)
AuctionHouseFrame:SetTitle(selectedTab.tabHeader)
selectedTab.frameRef:Show()
end