-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMerchantPlusItemList.lua
161 lines (134 loc) · 4.48 KB
/
MerchantPlusItemList.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
--
-- Merchant Plus
-- A Modern Scrollable UI for Merchants
--
-- Copyright 2023 - 2024 SimGuy
--
-- Use of this source code is governed by an MIT-style
-- license that can be found in the LICENSE file or at
-- https://opensource.org/licenses/MIT.
--
local _, Shared = ...
-- Import a shared trace function if one exists
local trace = Shared.Trace or function() end
MerchantPlusItemListMixin = {}
-- On load, set up the nineslice. The nineslice doesn't seem to work if defined in XML.
function MerchantPlusItemListMixin:OnLoad()
trace("called: OnLoad")
self.NineSlice:ClearAllPoints()
self.NineSlice:SetPoint("TOPLEFT", self.HeaderContainer, "BOTTOMLEFT")
self.NineSlice:SetPoint("BOTTOMRIGHT")
end
-- Before the widget is shown, set it up to show results
function MerchantPlusItemListMixin:OnShow()
trace("called: OnShow")
self:Init()
self:UpdateTableBuilderLayout()
self:RefreshScrollFrame()
self.ScrollBox:ScrollToBegin()
end
-- Before the widget is hidden, clean up stuff
function MerchantPlusItemListMixin:OnHide()
trace("called: OnHide")
end
-- On init, we will need to create various structures
function MerchantPlusItemListMixin:Init()
trace("called: Init")
if self.initialized then
return
end
local view = CreateScrollBoxListLinearView()
view:SetElementFactory(function(factory)
factory("MerchantPlusItemListLineTemplate", function() end)
end)
ScrollUtil.InitScrollBoxListWithScrollBar(self.ScrollBox, self.ScrollBar, view)
local tableBuilder = CreateTableBuilder(nil)
self.tableBuilder = tableBuilder
ScrollUtil.RegisterTableBuilder(self.ScrollBox, tableBuilder, function(elementData)
return elementData
end)
self.tableBuilder:SetHeaderContainer(self.HeaderContainer)
self.tableBuilder:SetDataProvider(self.GetDataRow)
self.initialized = true
end
-- Update the layout of the table.
function MerchantPlusItemListMixin:UpdateTableBuilderLayout()
trace("called: UpdateTableBuilderLayout")
if not self.initialized then
return
end
self.tableBuilder:Reset()
if self.SetTableLayout then
self:SetTableLayout()
end
self.tableBuilder:SetTableWidth(self.ScrollBox:GetWidth())
self.tableBuilder:Arrange()
end
-- Add a column to the TableBuilder
function MerchantPlusItemListMixin:AddColumn(key, title, celltype, fixed, width, left, right, col, options)
local tableBuilder = self.tableBuilder
local column = tableBuilder:AddColumn()
column:ConstructHeader("BUTTON", "MerchantPlusTableHeaderStringTemplate", key, title)
column:ConstructCells("FRAME", celltype, col, options)
if fixed then
column:SetFixedConstraints(width, 0)
else
column:SetFillConstraints(width, 0)
end
column:SetCellPadding(left, right)
return column
end
-- Update the contents of the table.
function MerchantPlusItemListMixin:RefreshScrollFrame()
trace("called: RefreshScrollFrame")
if not self.initialized or not self:IsVisible() then
return
end
local count = self.GetDataCount and self:GetDataCount() or 0
if count > 0 and self.GetData and self.Sort then
self.ResultsText:Hide()
local dataProvider = CreateDataProvider(self:GetData())
self.ScrollBox:SetDataProvider(dataProvider, ScrollBoxConstants.RetainScrollPosition)
self.ScrollBox:GetDataProvider():SetSortComparator(function(lhs, rhs)
return self:Sort(lhs, rhs)
end)
else
self.ResultsText:Show()
self.ResultsText:SetText(BROWSE_NO_RESULTS)
if self.ScrollBox.ClearDataProvider then
self.ScrollBox:ClearDataProvider()
else
self.ScrollBox:FlushDataProvider()
end
end
end
-- Return the ElementData back directly to the TableBuilder
function MerchantPlusItemListMixin:GetDataRow()
return self
end
-- Set the sort to the header that was selected, or if it's already selected,
-- reverse it
function MerchantPlusItemListMixin:SetSortOrder(key, state)
trace("called: SetSortOrder")
if self.sortOrder == key and key ~= "" and state == nil then
if self.sortOrderState == 1 then
self.sortOrderState = 0
else
self.sortOrderState = 1
end
else
self.sortOrder = key
self.sortOrderState = state or 0
end
if self.SortCallback then
self:SortCallback()
end
if self.initialized and self:IsVisible() and self.ScrollBox:HasDataProvider() then
self.ScrollBox:GetDataProvider():Sort()
self.ScrollBox:ScrollToBegin()
end
end
-- Returns a table containing the sort order and state
function MerchantPlusItemListMixin:GetSortOrder()
return self.sortOrder, self.sortOrderState
end