-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAHPriceMemory_UI.lua
More file actions
712 lines (608 loc) · 25.8 KB
/
AHPriceMemory_UI.lua
File metadata and controls
712 lines (608 loc) · 25.8 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
-- Create the library table
if not AHPriceMemory_UI then
AHPriceMemory_UI = {}
end
local lib = AHPriceMemory_UI
-- Local reference to constants
local colors = AHPriceMemory.COLORS
local colorsRGB = AHPriceMemory.COLORS_RGB
local uiConstants = AHPriceMemory.UI_CONSTANTS
local strings = AHPriceMemory.STRINGS
local rarityTypes = AHPriceMemory.RARITY_TYPES
local rarityNames = AHPriceMemory.RARITY_NAMES
local sortTypes = AHPriceMemory.SORT_TYPES
local sortNames = AHPriceMemory.SORT_NAMES
local rowColors = AHPriceMemory.ROW_COLORS
-- Helper function to extract itemID from itemLink (compatible with Lua 5.0)
function lib.ExtractItemID(itemLink)
if not itemLink then return nil end
-- Find "item:" in the string
local _, _, itemID = string.find(itemLink, "item:(%d+)")
if itemID then
return tonumber(itemID)
end
return nil
end
-- Helper function to format money
function lib.FormatMoney(price)
local gold = floor(price / 10000)
local silver = floor((price - (gold * 10000)) / 100)
local copper = price - (gold * 10000) - (silver * 100)
local priceString = ""
if gold > 0 then
priceString = gold .. "|c" .. colors.GOLD .. "g|r"
if silver > 0 or copper > 0 then
priceString = priceString .. " "
end
end
if silver > 0 then
priceString = priceString .. silver .. "|c" .. colors.SILVER .. "s|r"
if copper > 0 then
priceString = priceString .. " "
end
end
if copper > 0 or (gold == 0 and silver == 0) then
priceString = priceString .. copper .. "|c" .. colors.COPPER .. "c|r"
end
return priceString
end
-- Helper function to check if item matches search
local function ItemMatchesSearch(itemName, itemID, searchText)
if searchText == "" then
return true
end
local itemIDStr = tostring(itemID)
-- Search by ID
if string.find(itemIDStr, searchText) then
return true
end
-- Search by name (case insensitive)
if itemName and string.find(string.lower(itemName), searchText) then
return true
end
return false
end
-- Helper function for sorting with secondary sort by stackSize
local function CreateSortFunction(field, ascending, customCompare)
return function(a, b)
local compareResult
if customCompare then
-- For custom sorting (bid, buyout)
local aGreater = customCompare(a.data, b.data)
local bGreater = customCompare(b.data, a.data)
-- If neither is greater, they're equal
if not aGreater and not bGreater then
compareResult = nil -- Equal, use secondary sort
else
compareResult = aGreater
end
else
-- For simple field sorting (id, name)
local aValue = a[field]
local bValue = b[field]
if aValue == bValue then
compareResult = nil -- Equal, use secondary sort
else
compareResult = aValue < bValue
end
end
-- If values are equal, sort by stackSize
if compareResult == nil then
return a.stackSize < b.stackSize
end
-- Apply ascending/descending
if ascending then
return compareResult
else
return not compareResult
end
end
end
-- Create item frame (reusable)
local function CreateItemFrame(parent, config)
local itemFrame = CreateFrame("Frame", nil, parent)
itemFrame:SetWidth(parent:GetWidth())
itemFrame:SetHeight(uiConstants.ITEM_HEIGHT)
-- Background texture
itemFrame.bgTexture = itemFrame:CreateTexture(nil, "BACKGROUND")
itemFrame.bgTexture:SetAllPoints(itemFrame)
-- Item text
itemFrame.itemText = itemFrame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
itemFrame.itemText:SetPoint("LEFT", itemFrame, "LEFT", 0, 0)
itemFrame.itemText:SetWidth(config.itemTextWidth or 520)
itemFrame.itemText:SetJustifyH("LEFT")
-- Cache text (for "not in cache")
itemFrame.cacheText = itemFrame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
itemFrame.cacheText:SetPoint("LEFT", itemFrame.itemText, "LEFT", 0, 0)
itemFrame.cacheText:SetTextColor(colorsRGB.NOT_IN_CACHE[1], colorsRGB.NOT_IN_CACHE[2], colorsRGB.NOT_IN_CACHE[3])
-- Price text
itemFrame.priceText = itemFrame:CreateFontString(nil, "OVERLAY", "NumberFontNormal")
itemFrame.priceText:SetPoint("RIGHT", itemFrame, "RIGHT", 0, 0)
if config.priceTextWidth then
itemFrame.priceText:SetWidth(config.priceTextWidth)
itemFrame.priceText:SetJustifyH("RIGHT")
end
-- Tooltip functionality
itemFrame:EnableMouse(true)
itemFrame:SetScript("OnEnter", function()
if this.itemLink then
GameTooltip:SetOwner(this, "ANCHOR_CURSOR")
GameTooltip:SetHyperlink(this.itemLink)
if config.onTooltip and this.itemData then
GameTooltip:AddLine(" ", 1, 1, 1)
config.onTooltip(GameTooltip, this.itemData)
end
GameTooltip:Show()
end
end)
itemFrame:SetScript("OnLeave", function()
GameTooltip:Hide()
end)
return itemFrame
end
-- Update item frame with data
local function UpdateItemFrame(itemFrame, item, visualIndex, config)
local yOffset = -5 - ((visualIndex - 1) * (uiConstants.ITEM_HEIGHT + 2))
itemFrame:SetPoint("TOPLEFT", itemFrame:GetParent(), "TOPLEFT", 0, yOffset)
itemFrame:Show()
-- Update background
if mod(item.originalIndex, 2) == 0 then
itemFrame.bgTexture:SetTexture(rowColors.EVEN[1], rowColors.EVEN[2], rowColors.EVEN[3], rowColors.EVEN[4])
else
itemFrame.bgTexture:SetTexture(rowColors.ODD[1], rowColors.ODD[2], rowColors.ODD[3], rowColors.ODD[4])
end
-- Update item text
local r, g, b, hex = GetItemQualityColor(item.rarity)
itemFrame.itemText:SetTextColor(r, g, b)
-- Build display text - use custom formatter if provided
local displayText
if config.formatItemName then
displayText = config.formatItemName(item.data, item.name, item.id)
else
displayText = item.name .. " [ID " .. item.id .. "]"
end
itemFrame.itemText:SetText(displayText)
-- Update cache text
if item.notInCache then
itemFrame.cacheText:SetPoint("LEFT", itemFrame.itemText, "LEFT", itemFrame.itemText:GetStringWidth(), 0)
itemFrame.cacheText:SetText(strings.NOT_IN_CACHE)
itemFrame.cacheText:Show()
else
itemFrame.cacheText:Hide()
end
-- Update price
itemFrame.priceText:SetText(config.formatPrice(item.data))
-- Store data for tooltip
itemFrame.itemLink = item.link
itemFrame.itemData = item.data
end
-- Update visible frames based on scroll position
local function UpdateVisibleFrames(window)
if not window.filteredItems then return end
local scrollOffset = window.scrollFrame:GetVerticalScroll()
local scrollHeight = window.scrollFrame:GetHeight()
local itemHeight = uiConstants.ITEM_HEIGHT + 2
-- Calculate visible range with buffer
local firstVisible = math.max(1, math.floor(scrollOffset / itemHeight))
local lastVisible = math.min(table.getn(window.filteredItems), math.ceil((scrollOffset + scrollHeight) / itemHeight) + 1)
-- Hide all frames first
for _, frame in ipairs(window.itemFramePool) do
frame:Hide()
end
-- Show and update only visible frames
local visibleIndex = 1
for i = firstVisible, lastVisible do
local item = window.filteredItems[i]
if item then
local frame = window.itemFramePool[visibleIndex]
if not frame then
frame = CreateItemFrame(window.content, window.config)
window.itemFramePool[visibleIndex] = frame
end
item.originalIndex = i
UpdateItemFrame(frame, item, i, window.config)
visibleIndex = visibleIndex + 1
end
end
end
-- Create and show the database window
-- Parameters:
-- config = {
-- windowName = "MyAddon_Window", -- Unique frame name
-- title = "My Database", -- Window title
-- data = myDataTable, -- Table with item data (itemID as key)
-- width = 750, -- Window width (optional, default 750)
-- height = 600, -- Window height (optional, default 600)
-- showAutoFillCheckbox = true, -- Show auto-fill checkbox (optional)
-- autoFillEnabled = true, -- Initial state of auto-fill (optional)
-- onAutoFillChanged = function(enabled) end, -- Callback when auto-fill state changes (optional)
-- showMessagesCheckbox = true, -- Show messages checkbox (optional)
-- showMessagesEnabled = true, -- Initial state of show messages (optional)
-- onShowMessagesChanged = function(enabled) end, -- Callback when show messages state changes (optional)
-- getItemData = function(itemID, rawData) -- Function to extract item info
-- return {
-- itemID = rawData.itemID, -- Required for custom formatItemName
-- price = rawData.vendorPrice or 0,
-- itemName = rawData.itemName,
-- -- any other fields you want
-- }
-- end,
-- formatPrice = function(itemData) -- Function to format price display
-- return lib.FormatMoney(itemData.price)
-- end,
-- formatItemName = function(itemData, itemName, itemID) -- Optional: Custom item name formatting
-- return itemName .. " [ID " .. itemID .. "]"
-- end,
-- onTooltip = function(tooltip, itemData) -- Function to add custom tooltip lines
-- tooltip:AddLine("Vendor Price:", 1, 1, 1)
-- tooltip:AddLine(lib.FormatMoney(itemData.price), 1, 1, 1)
-- end,
-- sortByPrice = function(a, b) -- Function to compare prices for sorting
-- return a.price < b.price
-- end,
-- defaultSort = "id" -- Default sort field (optional, default "id")
-- }
function lib.ShowWindow(config)
local windowName = config.windowName
local existingWindow = _G[windowName]
if existingWindow then
if existingWindow:IsShown() then
existingWindow:Hide()
else
existingWindow:Show()
lib.RefreshWindow(windowName, config)
end
return
end
-- Window dimensions
local windowWidth = config.width or 750
local windowHeight = config.height or 600
-- Create main frame
local window = CreateFrame("Frame", windowName, UIParent)
window:SetWidth(windowWidth)
window:SetHeight(windowHeight)
window:SetPoint("CENTER", UIParent, "CENTER", 0, 50)
window:SetBackdrop({
bgFile = "Interface/DialogFrame/UI-DialogBox-Background",
edgeFile = "Interface/DialogFrame/UI-DialogBox-Border",
tile = true, tileSize = 32, edgeSize = 32,
insets = { left = 11, right = 12, top = 12, bottom = 11 }
})
window:SetMovable(true)
window:EnableMouse(true)
window:RegisterForDrag("LeftButton")
window:SetScript("OnDragStart", function() this:StartMoving() end)
window:SetScript("OnDragStop", function() this:StopMovingOrSizing() end)
-- Store config in window for later use
window.config = config
window.currentSort = config.defaultSort or "id"
window.sortAscending = true
window.currentRarityFilter = -1
window.searchText = ""
window.itemFramePool = {}
-- Title
local title = window:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
title:SetPoint("TOP", window, "TOP", 0, -15)
title:SetText(config.title)
-- Close button
local closeBtn = CreateFrame("Button", nil, window, "UIPanelCloseButton")
closeBtn:SetPoint("TOPRIGHT", window, "TOPRIGHT", -5, -5)
-- Auto-fill checkbox (if enabled)
if config.showAutoFillCheckbox then
local autoFillCheckbox = CreateFrame("CheckButton", windowName .. "_AutoFillCheckbox", window, "UICheckButtonTemplate")
autoFillCheckbox:SetWidth(24)
autoFillCheckbox:SetHeight(24)
autoFillCheckbox:SetPoint("TOPRIGHT", window, "TOPRIGHT", -35, -10)
autoFillCheckbox:SetChecked(config.autoFillEnabled)
-- Checkbox text
local checkboxText = autoFillCheckbox:CreateFontString(nil, "OVERLAY", "GameFontNormal")
checkboxText:SetPoint("RIGHT", autoFillCheckbox, "LEFT", -5, 0)
checkboxText:SetText(strings.AUTO_FILL)
-- Checkbox script
autoFillCheckbox:SetScript("OnClick", function()
local enabled = this:GetChecked() == 1 -- Convert to boolean
-- Update the state using the global function
if AHPriceMemory_SetAutoFillState then
AHPriceMemory_SetAutoFillState(enabled)
end
-- Call the callback for messages
if config.onAutoFillChanged then
config.onAutoFillChanged(enabled)
end
end)
-- Tooltip
autoFillCheckbox:SetScript("OnEnter", function()
GameTooltip:SetOwner(this, "ANCHOR_NONE")
GameTooltip:SetPoint("TOPRIGHT", this, "BOTTOMRIGHT", 0, -5)
GameTooltip:SetText(strings.AUTO_FILL_TOOLTIP_TITLE, 1, 1, 1)
GameTooltip:AddLine(strings.AUTO_FILL_TOOLTIP_DESC, 0.9, 0.9, 0.9, 1)
GameTooltip:Show()
end)
autoFillCheckbox:SetScript("OnLeave", function()
GameTooltip:Hide()
end)
-- Store reference to checkbox in window
window.autoFillCheckbox = autoFillCheckbox
end
-- Show Messages checkbox (if enabled) - positioned below Auto-fill checkbox
if config.showMessagesCheckbox then
local showMessagesCheckbox = CreateFrame("CheckButton", windowName .. "_ShowMessagesCheckbox", window, "UICheckButtonTemplate")
showMessagesCheckbox:SetWidth(24)
showMessagesCheckbox:SetHeight(24)
-- Position it below the Auto-fill checkbox if it exists
if window.autoFillCheckbox then
showMessagesCheckbox:SetPoint("TOPRIGHT", window.autoFillCheckbox, "BOTTOMRIGHT", 0, -5)
else
showMessagesCheckbox:SetPoint("TOPRIGHT", window, "TOPRIGHT", -35, -10)
end
showMessagesCheckbox:SetChecked(config.showMessagesEnabled)
-- Checkbox text
local checkboxText = showMessagesCheckbox:CreateFontString(nil, "OVERLAY", "GameFontNormal")
checkboxText:SetPoint("RIGHT", showMessagesCheckbox, "LEFT", -5, 0)
checkboxText:SetText(strings.SHOW_MESSAGES)
-- Checkbox script
showMessagesCheckbox:SetScript("OnClick", function()
local enabled = this:GetChecked() == 1 -- Convert to boolean
-- Update the state using the global function
if AHPriceMemory_SetShowMessagesState then
AHPriceMemory_SetShowMessagesState(enabled)
end
-- Call the callback for messages
if config.onShowMessagesChanged then
config.onShowMessagesChanged(enabled)
end
end)
-- Tooltip
showMessagesCheckbox:SetScript("OnEnter", function()
GameTooltip:SetOwner(this, "ANCHOR_NONE")
GameTooltip:SetPoint("TOPRIGHT", this, "BOTTOMRIGHT", 0, -5)
GameTooltip:SetText(strings.SHOW_MESSAGES_TOOLTIP_TITLE, 1, 1, 1)
GameTooltip:AddLine(strings.SHOW_MESSAGES_TOOLTIP_DESC, 0.9, 0.9, 0.9, 1)
GameTooltip:Show()
end)
showMessagesCheckbox:SetScript("OnLeave", function()
GameTooltip:Hide()
end)
-- Store reference to checkbox in window
window.showMessagesCheckbox = showMessagesCheckbox
end
-- Search box label
local searchLabel = window:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
searchLabel:SetPoint("TOPLEFT", window, "TOPLEFT", 20, -50)
searchLabel:SetText(strings.SEARCH)
searchLabel:SetTextColor(colorsRGB.LABEL[1], colorsRGB.LABEL[2], colorsRGB.LABEL[3])
-- Search box
local searchBox = CreateFrame("EditBox", windowName .. "_SearchBox", window, "InputBoxTemplate")
searchBox:SetWidth(150)
searchBox:SetHeight(20)
searchBox:SetPoint("TOPLEFT", searchLabel, "TOPRIGHT", 10, 3)
searchBox:SetAutoFocus(false)
searchBox:SetScript("OnTextChanged", function()
window.searchText = string.lower(this:GetText())
lib.RefreshWindow(windowName, config)
end)
searchBox:SetScript("OnEscapePressed", function()
this:ClearFocus()
end)
searchBox:SetScript("OnEnterPressed", function()
this:ClearFocus()
end)
-- Clear search button
local clearBtn = CreateFrame("Button", nil, window, "UIPanelButtonTemplate")
clearBtn:SetWidth(50)
clearBtn:SetHeight(20)
clearBtn:SetPoint("LEFT", searchBox, "RIGHT", 5, 0)
clearBtn:SetText(strings.CLEAR)
clearBtn:SetScript("OnClick", function()
searchBox:SetText("")
window.searchText = ""
lib.RefreshWindow(windowName, config)
end)
-- Sort by label
local sortLabel = window:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
sortLabel:SetPoint("TOPLEFT", window, "TOPLEFT", 20, -80)
sortLabel:SetText(strings.SORT_BY)
sortLabel:SetTextColor(colorsRGB.LABEL[1], colorsRGB.LABEL[2], colorsRGB.LABEL[3])
-- Sort buttons
local sortButtons = {}
for i, sortType in ipairs(sortTypes) do
local btn = CreateFrame("Button", nil, window, "UIPanelButtonTemplate")
btn:SetWidth(60)
btn:SetHeight(20)
if i == 1 then
btn:SetPoint("LEFT", sortLabel, "RIGHT", 5, 0)
else
btn:SetPoint("LEFT", sortButtons[i-1], "RIGHT", 5, 0)
end
btn:SetText(sortType.label)
btn.sortKey = sortType.key
btn:SetScript("OnClick", function()
-- If clicking the same sort, toggle direction
if window.currentSort == this.sortKey then
window.sortAscending = not window.sortAscending
else
-- New sort type, default to ascending
window.currentSort = this.sortKey
window.sortAscending = true
end
lib.RefreshWindow(windowName, config)
end)
sortButtons[i] = btn
end
-- Rarity filter label
local rarityLabel = window:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
rarityLabel:SetPoint("TOPLEFT", window, "TOPLEFT", 20, -110)
rarityLabel:SetText(strings.RARITY)
rarityLabel:SetTextColor(colorsRGB.LABEL[1], colorsRGB.LABEL[2], colorsRGB.LABEL[3])
-- Rarity filter buttons
local rarityButtons = {}
for i, rarityType in ipairs(rarityTypes) do
local btn = CreateFrame("Button", nil, window, "UIPanelButtonTemplate")
btn:SetWidth(80)
btn:SetHeight(20)
if i == 1 then
btn:SetPoint("LEFT", rarityLabel, "RIGHT", 5, 0)
else
btn:SetPoint("LEFT", rarityButtons[i-1], "RIGHT", 3, 0)
end
btn:SetText(rarityType.label)
btn.rarityKey = rarityType.key
-- Color the button text
local btnText = btn:GetFontString()
btnText:SetTextColor(rarityType.color[1], rarityType.color[2], rarityType.color[3])
btn:SetScript("OnClick", function()
window.currentRarityFilter = this.rarityKey
lib.RefreshWindow(windowName, config)
end)
rarityButtons[i] = btn
end
-- Item count text
window.countText = window:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
window.countText:SetPoint("TOP", window, "TOP", 0, -145)
window.countText:SetText(strings.ITEMS .. ": 0")
window.countText:SetTextColor(1, 1, 1)
-- Scroll frame
local scrollFrame = CreateFrame("ScrollFrame", windowName .. "_ScrollFrame", window, "UIPanelScrollFrameTemplate")
scrollFrame:SetPoint("TOPLEFT", window, "TOPLEFT", 20, -165)
scrollFrame:SetPoint("BOTTOMRIGHT", window, "BOTTOMRIGHT", -30, 15)
-- Content frame
local content = CreateFrame("Frame", windowName .. "_ContentFrame", scrollFrame)
content:SetWidth(windowWidth - 50)
content:SetHeight(1)
scrollFrame:SetScrollChild(content)
-- Add scroll event handler
scrollFrame:SetScript("OnVerticalScroll", function()
UpdateVisibleFrames(window)
end)
window.content = content
window.scrollFrame = scrollFrame
window:Show()
lib.RefreshWindow(windowName, config)
end
-- Refresh the window content
function lib.RefreshWindow(windowName, config)
local window = _G[windowName]
if not window then return end
-- Update config if provided
if config then
window.config = config
end
config = window.config
-- Refresh data if refreshData function is provided
if config.refreshData then
config.data = config.refreshData()
end
-- Reset scroll to top
if window.scrollFrame then
window.scrollFrame:SetVerticalScroll(0)
end
-- Collect items with their info
local items = {}
if config.data then
for key, rawData in pairs(config.data) do
-- Get item data from config function
local itemData = config.getItemData(key, rawData)
-- Extract numeric itemID from itemData (not from key!)
local itemID = itemData.itemID
if type(itemID) ~= "number" then
itemID = tonumber(itemID)
end
-- Skip if we couldn't get a valid itemID
if itemID then
local itemName, itemLink, itemRarity = GetItemInfo(itemID)
local notInCache = false
-- Use saved name if item is not in cache
if not itemName then
notInCache = true
if itemData.itemName then
itemName = itemData.itemName
else
itemName = strings.ITEM_PREFIX .. itemID
end
end
-- Apply rarity filter
if window.currentRarityFilter == -1 or (itemRarity and itemRarity == window.currentRarityFilter) then
-- Apply search filter
if ItemMatchesSearch(itemName, itemID, window.searchText) then
table.insert(items, {
id = itemID,
key = key,
name = itemName,
link = itemLink,
rarity = itemRarity or 1,
notInCache = notInCache,
data = itemData,
stackSize = itemData.stackSize or 1
})
end
end
end
end
end
-- Sort items based on current sort and direction
local sortFunction
if window.currentSort == "id" then
sortFunction = CreateSortFunction("id", window.sortAscending)
elseif window.currentSort == "name" then
sortFunction = CreateSortFunction("name", window.sortAscending)
elseif window.currentSort == "buyout" then
sortFunction = CreateSortFunction(nil, window.sortAscending, config.sortByBuyout)
elseif window.currentSort == "bid" then
sortFunction = CreateSortFunction(nil, window.sortAscending, config.sortByBid)
elseif window.currentSort == "rarity" then
-- Rarity needs special handling with name as secondary, stackSize as tertiary
sortFunction = function(a, b)
if a.rarity ~= b.rarity then
if window.sortAscending then
return a.rarity < b.rarity
else
return a.rarity > b.rarity
end
end
-- Secondary: name
if a.name ~= b.name then
return a.name < b.name
end
-- Tertiary: stackSize
return a.stackSize < b.stackSize
end
end
if sortFunction then
table.sort(items, sortFunction)
end
-- Store filtered items in window
window.filteredItems = items
-- Update count with all filter information
local totalCount = 0
if config.data then
for _ in pairs(config.data) do
totalCount = totalCount + 1
end
end
local filterInfo = ""
-- Add sort information
local sortDirection = window.sortAscending and strings.ASCENDING or strings.DESCENDING
filterInfo = " (" .. strings.SORTED_BY .. " " .. sortNames[window.currentSort] .. " " .. sortDirection .. ")"
-- Add rarity filter
if window.currentRarityFilter ~= -1 then
filterInfo = filterInfo .. " (" .. strings.FILTERED_BY .. " " .. rarityNames[window.currentRarityFilter + 1] .. ")"
end
-- Add search filter
if window.searchText ~= "" then
filterInfo = filterInfo .. " (" .. strings.SEARCH_FILTER .. " '" .. window.searchText .. "')"
end
window.countText:SetText(strings.SHOWING .. " " .. table.getn(items) .. " " .. strings.OF .. " " .. totalCount .. " " .. strings.ITEMS .. filterInfo)
-- Update content height
local numItems = table.getn(items)
local itemHeight = uiConstants.ITEM_HEIGHT + 2
local contentHeight = math.max(1, numItems * itemHeight + 10)
window.content:SetHeight(contentHeight)
-- Update visible frames
UpdateVisibleFrames(window)
-- Update scroll
local scrollHeight = window.scrollFrame:GetHeight()
if contentHeight <= scrollHeight then
window.scrollFrame:SetVerticalScroll(0)
end
window.scrollFrame:UpdateScrollChildRect()
end