forked from shirsig/aux-addon-TBC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauction_listing.lua
1071 lines (982 loc) · 37.3 KB
/
auction_listing.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
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
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module 'aux.gui.auction_listing'
include 'T'
include 'aux'
local info = require 'aux.util.info'
local sort_util = require 'aux.util.sort'
local money = require 'aux.util.money'
local history = require 'aux.core.history'
local gui = require 'aux.gui'
local search_tab = require 'aux.tabs.search'
local tooltip = require 'aux.core.tooltip'
local cache = require 'aux.core.cache'
local price_per_unit = false
local HEAD_HEIGHT = 27
local HEAD_SPACE = 2
local TIME_LEFT_STRINGS = {
color.red'30m', -- Short
color.orange'2h', -- Medium
color.yellow(_VERSION and '12h' or '8h'), -- Long
color.blue(_VERSION and '48h' or '24h'), -- Very Long
}
function item_column_init(rt, cell)
local spacer = CreateFrame('Frame', nil, cell)
spacer:SetPoint('TOPLEFT', 0, 0)
spacer:SetHeight(rt.ROW_HEIGHT)
spacer:SetWidth(1)
cell.spacer = spacer
local iconBtn = CreateFrame('Button', nil, cell)
iconBtn:SetPoint('TOPLEFT', spacer, 'TOPRIGHT')
iconBtn:SetHeight(rt.ROW_HEIGHT)
iconBtn:SetWidth(rt.ROW_HEIGHT)
iconBtn:SetScript('OnEnter', rt.OnIconEnter)
iconBtn:SetScript('OnLeave', rt.OnIconLeave)
iconBtn:SetScript('OnClick', rt.OnIconClick)
iconBtn:SetScript('OnDoubleClick', rt.OnIconDoubleClick)
local icon = iconBtn:CreateTexture(nil, 'ARTWORK')
icon:SetPoint('TOPLEFT', 2, -2)
icon:SetPoint('BOTTOMRIGHT', -2, 2)
icon:SetTexCoord(.08, .92, .08, .92)
cell.iconBtn = iconBtn
cell.icon = icon
cell.text:ClearAllPoints()
cell.text:SetPoint('TOPLEFT', iconBtn, 'TOPRIGHT', 2, 0)
cell.text:SetPoint('BOTTOMRIGHT', 0, 0)
end
function item_column_fill(cell, record, _, _, _, indented)
cell.icon:SetTexture(record.texture)
if indented then
cell.spacer:SetWidth(10)
cell.icon:SetAlpha(.5)
cell.text:SetAlpha(.7)
else
cell.spacer:SetWidth(1)
cell.icon:SetAlpha(1)
cell.text:SetAlpha(1)
end
cell.text:SetText(gsub(record.link, '[%[%]]', ''))
end
M.search_columns = {
{
title = 'Item',
width = .35,
init = item_column_init,
fill = item_column_fill,
cmp = function(record_a, record_b, desc)
return sort_util.compare(record_a.name, record_b.name, desc)
end,
},
{
title = 'Lvl',
width = .035,
align = 'CENTER',
fill = function(cell, record)
local display_level = max(record.level, 1)
display_level = UnitLevel'player' < record.level and color.red(display_level) or display_level
cell.text:SetText(display_level)
end,
cmp = function(record_a, record_b, desc)
return sort_util.compare(record_a.level, record_b.level, desc)
end,
},
{
title = 'Auctions',
width = .06,
align = 'CENTER',
fill = function(cell, record, count, own, expandable)
local numAuctionsText = expandable and color.link(count) or count
if own > 0 then
numAuctionsText = numAuctionsText .. (' ' .. color.yellow('(' .. own .. ')'))
end
cell.text:SetText(numAuctionsText)
end,
cmp = function(record_a, record_b, desc)
return sort_util.EQ
-- if sortKey == 'numAuctions' then
-- if a.children then
-- aVal = a.totalAuctions
-- bVal = b.totalAuctions
-- else
-- aVal = a.numAuctions
-- bVal = b.numAuctions
-- end
-- end
end,
},
{
title = 'Stack\nSize',
width = .055,
align = 'CENTER',
fill = function(cell, record)
cell.text:SetText(record.aux_quantity)
end,
cmp = function(record_a, record_b, desc)
return sort_util.compare(record_a.aux_quantity, record_b.aux_quantity, desc)
end,
},
{
title = 'Time\nLeft',
width = .04,
align = 'CENTER',
fill = function(cell, record)
cell.text:SetText(TIME_LEFT_STRINGS[record.duration or 0] or '?')
end,
cmp = function(record_a, record_b, desc)
return sort_util.compare(record_a.duration, record_b.duration, desc)
end,
},
{
title = 'Seller',
width = .13,
align = 'CENTER',
fill = function(cell, record)
cell.text:SetText(cache.is_player(record.owner) and (color.yellow(record.owner)) or (record.owner or '?'))
end,
cmp = function(record_a, record_b, desc)
if not record_a.owner and not record_b.owner then
return sort_util.EQ
elseif not record_a.owner then
return sort_util.GT
elseif not record_b.owner then
return sort_util.LT
else
return sort_util.compare(record_a.owner, record_b.owner, desc)
end
end,
},
{
title = {'Auction Bid\n(per item)', 'Auction Bid\n(per stack)'},
width = .125,
align = 'RIGHT',
isPrice = true,
fill = function(cell, record)
local price_color
if record.high_bidder then
price_color = color.green
elseif record.high_bid ~= 0 then
price_color = color.orange
end
local price
if record.high_bidder then
price = price_per_unit and ceil(record.high_bid / record.aux_quantity) or record.high_bid
else
price = price_per_unit and ceil(record.unit_bid_price) or record.bid_price
end
cell.text:SetText(money.to_string(price, true, false, price_color))
end,
cmp = function(record_a, record_b, desc)
local price_a
if record_a.high_bidder then
price_a = price_per_unit and record_a.high_bid / record_a.aux_quantity or record_a.high_bid
else
price_a = price_per_unit and record_a.unit_bid_price or record_a.bid_price
end
local price_b
if record_b.high_bidder then
price_b = price_per_unit and record_b.high_bid / record_b.aux_quantity or record_b.high_bid
else
price_b = price_per_unit and record_b.unit_bid_price or record_b.bid_price
end
if record_a.high_bidder and not record_b.high_bidder then
return sort_util.GT
elseif record_b.high_bidder and not record_a.high_bidder then
return sort_util.LT
end
if price_a == price_b then
if record_a.high_bid == 0 and record_b.high_bid ~= 0 then
return sort_util.GT
elseif record_b.high_bid == 0 and record_a.high_bid ~= 0 then
return sort_util.LT
end
end
return sort_util.compare(price_a, price_b, desc)
end,
},
{
title = {'Auction Buyout\n(per item)', 'Auction Buyout\n(per stack)'},
width = .125,
align = 'RIGHT',
isPrice = true,
fill = function(cell, record)
local price = price_per_unit and ceil(record.unit_buyout_price) or record.buyout_price
cell.text:SetText(price > 0 and money.to_string(price, true) or '---')
end,
cmp = function(record_a, record_b, desc)
local price_a = price_per_unit and record_a.unit_buyout_price or record_a.buyout_price
local price_b = price_per_unit and record_b.unit_buyout_price or record_b.buyout_price
price_a = price_a > 0 and price_a or (desc and -huge or huge)
price_b = price_b > 0 and price_b or (desc and -huge or huge)
return sort_util.compare(price_a, price_b, desc)
end,
},
{
title = '% Hist.\nValue',
width = .08,
align = 'CENTER',
fill = function(cell, record)
local pct, bidPct = record_percentage(record)
cell.text:SetText((pct or bidPct) and percentage_historical(pct or bidPct, not pct) or '?')
end,
cmp = function(record_a, record_b, desc)
local pct_a = record_percentage(record_a) or (desc and -huge or huge)
local pct_b = record_percentage(record_b) or (desc and -huge or huge)
return sort_util.compare(pct_a, pct_b, desc)
end,
},
}
M.auctions_columns = {
{
title = 'Item',
width = .35,
init = item_column_init,
fill = item_column_fill,
cmp = function(record_a, record_b, desc)
return sort_util.compare(record_a.name, record_b.name, desc)
end,
},
{
title = 'Lvl',
width = .035,
align = 'CENTER',
fill = function(cell, record)
local display_level = max(record.level, 1)
display_level = UnitLevel('player') < record.level and color.red(display_level) or display_level
cell.text:SetText(display_level)
end,
cmp = function(record_a, record_b, desc)
return sort_util.compare(record_a.level, record_b.level, desc)
end,
},
{
title = 'Auctions',
width = .06,
align = 'CENTER',
fill = function(cell, record, count, own, expandable)
local numAuctionsText = expandable and color.link(count) or count
cell.text:SetText(numAuctionsText)
end,
cmp = function(record_a, record_b, desc)
return sort_util.EQ
-- if sortKey == 'numAuctions' then
-- if a.children then
-- aVal = a.totalAuctions
-- bVal = b.totalAuctions
-- else
-- aVal = a.numAuctions
-- bVal = b.numAuctions
-- end
-- end
end,
},
{
title = 'Stack\nSize',
width = .055,
align = 'CENTER',
fill = function(cell, record)
cell.text:SetText(record.aux_quantity)
end,
cmp = function(record_a, record_b, desc)
return sort_util.compare(record_a.aux_quantity, record_b.aux_quantity, desc)
end,
},
{
title = 'Time\nLeft',
width = .04,
align = 'CENTER',
fill = function(cell, record)
cell.text:SetText(TIME_LEFT_STRINGS[record.duration or 0] or '?')
end,
cmp = function(record_a, record_b, desc)
return sort_util.compare(record_a.duration, record_b.duration, desc)
end,
},
{
title = {'Auction Bid\n(per item)', 'Auction Bid\n(per stack)'},
width = .125,
align = 'RIGHT',
isPrice = true,
fill = function(cell, record)
local price
if record.high_bidder then
price = price_per_unit and ceil(record.high_bid / record.aux_quantity) or record.high_bid
else
price = price_per_unit and ceil(record.start_price / record.aux_quantity) or record.start_price
end
cell.text:SetText(money.to_string(price, true))
end,
cmp = function(record_a, record_b, desc)
local price_a
if record_a.high_bidder then
price_a = price_per_unit and record_a.high_bid / record_a.aux_quantity or record_a.high_bid
else
price_a = price_per_unit and record_a.start_price / record_b.aux_quantity or record_a.start_price
end
local price_b
if record_b.high_bidder then
price_b = price_per_unit and record_b.high_bid / record_b.aux_quantity or record_b.high_bid
else
price_b = price_per_unit and record_b.start_price / record_b.aux_quantity or record_b.start_price
end
return sort_util.compare(price_a, price_b, desc)
end,
},
{
title = {'Auction Buyout\n(per item)', 'Auction Buyout\n(per stack)'},
width = .125,
align = 'RIGHT',
isPrice = true,
fill = function(cell, record)
local price = price_per_unit and ceil(record.unit_buyout_price) or record.buyout_price
cell.text:SetText(price > 0 and money.to_string(price, true) or '---')
end,
cmp = function(record_a, record_b, desc)
local price_a = price_per_unit and record_a.unit_buyout_price or record_a.buyout_price
local price_b = price_per_unit and record_b.unit_buyout_price or record_b.buyout_price
price_a = price_a > 0 and price_a or (desc and -huge or huge)
price_b = price_b > 0 and price_b or (desc and -huge or huge)
return sort_util.compare(price_a, price_b, desc)
end,
},
{
title = 'High Bidder',
width = .21,
align = 'CENTER',
fill = function(cell, record)
cell.text:SetText(record.high_bidder or color.red 'No Bids')
end,
cmp = function(record_a, record_b, desc)
if not record_a.high_bidder and not record_b.high_bidder then
return sort_util.EQ
elseif not record_a.high_bidder then
return sort_util.GT
elseif not record_b.high_bidder then
return sort_util.LT
else
return sort_util.compare(record_a.high_bidder, record_b.high_bidder, desc)
end
end,
},
}
M.bids_columns = {
{
title = 'Item',
width = .35,
init = item_column_init,
fill = item_column_fill,
cmp = function(record_a, record_b, desc)
return sort_util.compare(record_a.name, record_b.name, desc)
end,
},
{
title = 'Auctions',
width = .06,
align = 'CENTER',
fill = function(cell, record, count, own, expandable)
local numAuctionsText = expandable and color.link(count) or count
cell.text:SetText(numAuctionsText)
end,
cmp = function(record_a, record_b, desc)
return sort_util.EQ
-- if sortKey == 'numAuctions' then
-- if a.children then
-- aVal = a.totalAuctions
-- bVal = b.totalAuctions
-- else
-- aVal = a.numAuctions
-- bVal = b.numAuctions
-- end
-- end
end,
},
{
title = 'Stack\nSize',
width = .055,
align = 'CENTER',
fill = function(cell, record)
cell.text:SetText(record.aux_quantity)
end,
cmp = function(record_a, record_b, desc)
return sort_util.compare(record_a.aux_quantity, record_b.aux_quantity, desc)
end,
},
{
title = 'Time\nLeft',
width = .04,
align = 'CENTER',
fill = function(cell, record)
cell.text:SetText(TIME_LEFT_STRINGS[record.duration or 0] or '?')
end,
cmp = function(record_a, record_b, desc)
return sort_util.compare(record_a.duration, record_b.duration, desc)
end,
},
{
title = 'Seller',
width = .13,
align = 'CENTER',
fill = function(cell, record)
cell.text:SetText(cache.is_player(record.owner) and (color.yellow(record.owner)) or (record.owner or '?'))
end,
cmp = function(record_a, record_b, desc)
if not record_a.owner and not record_b.owner then
return sort_util.EQ
elseif not record_a.owner then
return sort_util.GT
elseif not record_b.owner then
return sort_util.LT
else
return sort_util.compare(record_a.owner, record_b.owner, desc)
end
end,
},
{
title = {'Auction Bid\n(per item)', 'Auction Bid\n(per stack)'},
width = .125,
align = 'RIGHT',
isPrice = true,
fill = function(cell, record)
local price
if record.high_bidder then
price = price_per_unit and ceil(record.high_bid / record.aux_quantity) or record.high_bid
else
price = price_per_unit and ceil(record.unit_bid_price) or record.bid_price
end
cell.text:SetText(money.to_string(price))
end,
cmp = function(record_a, record_b, desc)
local price_a
if record_a.high_bidder then
price_a = price_per_unit and record_a.high_bid / record_a.aux_quantity or record_a.high_bid
else
price_a = price_per_unit and record_a.unit_bid_price or record_a.bid_price
end
local price_b
if record_b.high_bidder then
price_b = price_per_unit and record_b.high_bid / record_b.aux_quantity or record_b.high_bid
else
price_b = price_per_unit and record_b.unit_bid_price or record_b.bid_price
end
return sort_util.compare(price_a, price_b, desc)
end,
},
{
title = {'Auction Buyout\n(per item)', 'Auction Buyout\n(per stack)'},
width = .125,
align = 'RIGHT',
isPrice = true,
fill = function(cell, record)
local price = price_per_unit and ceil(record.unit_buyout_price) or record.buyout_price
cell.text:SetText(price > 0 and money.to_string(price, true) or '---')
end,
cmp = function(record_a, record_b, desc)
local price_a = price_per_unit and record_a.unit_buyout_price or record_a.buyout_price
local price_b = price_per_unit and record_b.unit_buyout_price or record_b.buyout_price
price_a = price_a > 0 and price_a or (desc and -huge or huge)
price_b = price_b > 0 and price_b or (desc and -huge or huge)
return sort_util.compare(price_a, price_b, desc)
end,
},
{
title = 'Status',
width = .115,
align = 'CENTER',
fill = function(cell, record)
local status
if record.high_bidder then
status = color.yellow'High Bidder'
else
status = color.red'Outbid'
end
cell.text:SetText(status)
end,
cmp = function(record_a, record_b, desc)
return sort_util.compare(record_a.high_bidder and 1 or 0, record_b.high_bidder and 1 or 0, desc)
end,
},
}
function record_percentage(record)
if not record then return end
local historical_value = history.value(record.item_key) or 0
if historical_value > 0 then
if record.unit_buyout_price > 0 then
return round(100 * record.unit_buyout_price / historical_value)
end
return nil, round(100 * record.unit_bid_price / historical_value)
end
end
function M.percentage_historical(pct, bid)
local text = (pct > 10000 and '>10000' or pct) .. '%'
if bid then
return color.gray(text)
elseif pct < 50 then
return color.blue(text)
elseif pct < 80 then
return color.green(text)
elseif pct < 110 then
return color.yellow(text)
elseif pct < 135 then
return color.orange(text)
else
return color.red(text)
end
end
function M.time_left(code)
return TIME_LEFT_STRINGS[code]
end
local methods = {
ResizeColumns = function(self)
local weight = 0
for _, cell in pairs(self.headCells) do
weight = weight + cell.info.width
end
weight = (self.contentFrame:GetRight() - self.contentFrame:GetLeft()) / weight
for i, cell in pairs(self.headCells) do
local width = cell.info.width * weight
cell:SetWidth(width)
for _, row in pairs(self.rows) do
row.cells[i]:SetWidth(width)
end
end
end,
OnHeadColumnClick = function()
local button = arg1
local rt = this.rt
if button == 'RightButton' and rt.headCells[this.columnIndex].info.isPrice then
price_per_unit = not price_per_unit
for _, cell in pairs(rt.headCells) do
if cell.info.isPrice then
cell:SetText(cell.info.title[price_per_unit and 1 or 2])
end
end
rt:SetSort()
return
end
local descending = false
if getn(rt.sorts) > 0 and rt.sorts[1].index == this.columnIndex then
descending = not rt.sorts[1].descending
end
rt:SetSort((descending and -1 or 1) * this.columnIndex)
end,
OnIconEnter = function()
local rt = this:GetParent().row.rt
local row = this:GetParent().row
if row.record then
GameTooltip:SetOwner(this, 'ANCHOR_RIGHT')
info.load_tooltip(GameTooltip, row.record.tooltip)
tooltip.extend_tooltip(GameTooltip, row.record.link, row.record.aux_quantity)
info.set_shopping_tooltip(row.record.slot)
end
end,
OnIconLeave = function()
GameTooltip:Hide()
end,
OnEnter = function()
local rt = this.rt
if rt.expanded[this.expandKey] then
GameTooltip_SetDefaultAnchor(GameTooltip, UIParent)
GameTooltip:AddLine('Double-click to collapse this item and show only the cheapest auction.', 1, 1, 1, true)
GameTooltip:Show()
elseif this.expandable then
GameTooltip_SetDefaultAnchor(GameTooltip, UIParent)
GameTooltip:AddLine('Double-click to expand this item and show all the auctions.', 1, 1, 1, true)
GameTooltip:Show()
end
this.highlight:Show()
end,
OnLeave = function()
GameTooltip:Hide()
if not this.rt.selected or this.rt.selected.search_signature ~= this.record.search_signature then
this.highlight:Hide()
end
end,
OnClick = function()
local button = arg1
if IsControlKeyDown() then
DressUpItemLink(this.record.link)
elseif IsShiftKeyDown() and ChatFrameEditBox:IsVisible() then
ChatFrameEditBox:Insert(this.record.link)
elseif not modified and button == 'RightButton' then -- TODO not when alt (how?)
tab = 1
search_tab.filter = strlower(info.item(this.record.item_id).name) .. '/exact'
search_tab.execute(nil, false)
else
local selection = this.rt:GetSelection()
if not selection or selection.record ~= this.record then
this.rt:SetSelectedRecord(this.record)
elseif this.rt.handlers.OnClick then
this.rt.handlers.OnClick(this, button)
end
end
end,
OnDoubleClick = function()
local rt = this.rt
local expand = not rt.expanded[this.expandKey]
rt.expanded[this.expandKey] = expand
rt:UpdateRowInfo()
rt:UpdateRows()
if not this.indented then
rt:SetSelectedRecord(this.record)
end
end,
UpdateRowInfo = function(self)
for _, v in ipairs(self.rowInfo) do
if type(v) == 'table' then
for _, child in pairs(v.children) do
release(child)
end
release(v.children)
release(v)
end
end
wipe(self.rowInfo)
self.rowInfo.numDisplayRows = 0
self.isSorted = nil
self:SetSelectedRecord(nil, true)
local records = self.records
local single_item = all(records, function(record) return record.item_key == records[1].item_key end)
sort(records, function(a, b) return a.search_signature < b.search_signature or a.search_signature == b.search_signature and tostring(a) < tostring(b) end)
for i = 1, getn(records) do
local record = records[i]
local prevRecord = records[i - 1]
if prevRecord and record.search_signature == prevRecord.search_signature then
-- it's an identical auction to the previous row so increment the number of auctions
self.rowInfo[getn(self.rowInfo)].children[getn(self.rowInfo[getn(self.rowInfo)].children)].count = self.rowInfo[getn(self.rowInfo)].children[getn(self.rowInfo[getn(self.rowInfo)].children)].count + 1
elseif not single_item and prevRecord and record.item_key == prevRecord.item_key then
-- it's the same base item as the previous row so insert a new auction
tinsert(self.rowInfo[getn(self.rowInfo)].children, O('count', 1, 'record', record))
if self.expanded[self.rowInfo[getn(self.rowInfo)].expandKey] then
self.rowInfo.numDisplayRows = self.rowInfo.numDisplayRows + 1
end
else
-- it's a different base item from the previous row
tinsert(self.rowInfo, O('item_key', record.item_key, 'expandKey', record.item_key, 'children', A(O('count', 1, 'record', record))))
self.rowInfo.numDisplayRows = self.rowInfo.numDisplayRows + 1
end
end
for _, v in ipairs(self.rowInfo) do
local totalAuctions, totalPlayerAuctions = 0, 0
for _, childInfo in pairs(v.children) do
totalAuctions = totalAuctions + childInfo.count
if cache.is_player(childInfo.record.owner) then
totalPlayerAuctions = totalPlayerAuctions + childInfo.count
end
end
v.totalAuctions = totalAuctions
v.totalPlayerAuctions = totalPlayerAuctions
end
end,
UpdateRows = function(self)
if self.rowInfo.numDisplayRows > getn(self.rows) then
self.contentFrame:SetPoint('BOTTOMRIGHT', -15, 0)
else
self.contentFrame:SetPoint('BOTTOMRIGHT', 0, 0)
end
self:ResizeColumns()
FauxScrollFrame_Update(self.scrollFrame, self.rowInfo.numDisplayRows, getn(self.rows), self.ROW_HEIGHT)
local maxOffset = max(self.rowInfo.numDisplayRows - getn(self.rows), 0)
if FauxScrollFrame_GetOffset(self.scrollFrame) > maxOffset then
FauxScrollFrame_SetOffset(self.scrollFrame, maxOffset)
end
for _, cell in pairs(self.headCells) do
local tex = cell:GetNormalTexture()
tex:SetTexture[[Interface\AddOns\aux-AddOn\WorldStateFinalScore-Highlight]]
tex:SetTexCoord(.017, 1, .083, .909)
tex:SetAlpha(.5)
end
if getn(self.sorts) > 0 then
local last_sort = self.sorts[1]
if last_sort.descending then
self.headCells[last_sort.index]:GetNormalTexture():SetTexture(.8, .6, 1, .8)
else
self.headCells[last_sort.index]:GetNormalTexture():SetTexture(.6, .8, 1, .8)
end
end
if not self.isSorted then
local function sort_helper(a, b)
local record_a, record_b
if a.children then
record_a = a.children[1].record
record_b = b.children[1].record
else
record_a = a.record
record_b = b.record
end
for _, sort in pairs(self.sorts) do
local ordering = self.columns[sort.index].cmp and self.columns[sort.index].cmp(record_a, record_b, sort.descending) or sort_util.EQ
if ordering == sort_util.LT then
return true
elseif ordering == sort_util.GT then
return false
end
end
return tostring(a) < tostring(b)
end
for _, v in ipairs(self.rowInfo) do
sort(v.children, sort_helper)
end
sort(self.rowInfo, sort_helper)
self.isSorted = true
end
for _, row in pairs(self.rows) do
row:Hide()
end
local rowIndex = 1 - FauxScrollFrame_GetOffset(self.scrollFrame)
for _, v in ipairs(self.rowInfo) do
if self.expanded[v.expandKey] then
for j, childInfo in ipairs(v.children) do
self:SetRowInfo(rowIndex, childInfo.record, childInfo.count, 0, j > 1, false, v.expandKey)
rowIndex = rowIndex + 1
end
else
self:SetRowInfo(rowIndex, v.children[1].record, v.totalAuctions, getn(v.children) > 1 and v.totalPlayerAuctions or 0, false, getn(v.children) > 1, v.expandKey)
rowIndex = rowIndex + 1
end
end
end,
SetRowInfo = function(self, rowIndex, record, totalAuctions, totalPlayerAuctions, indented, expandable, expandKey)
if rowIndex <= 0 or rowIndex > getn(self.rows) then return end
local row = self.rows[rowIndex]
row:Show()
if self.selected and record.search_signature == self.selected.search_signature then
row.highlight:Show()
else
row.highlight:Hide()
end
row.record = record
row.expandable = expandable
row.indented = indented
row.expandKey = expandKey
for i, column in pairs(self.columns) do
column.fill(row.cells[i], record, totalAuctions, totalPlayerAuctions, expandable, indented)
end
end,
SetSelectedRecord = function(self, record, silent)
self.selected = record
local selectedData = self:GetSelection()
self.selected = selectedData and self.selected or nil
for _, row in pairs(self.rows) do
if self.selected and row.record and row.record.search_signature == self.selected.search_signature then
row.highlight:Show()
else
row.highlight:Hide()
end
end
if not silent and self.handlers.OnSelectionChanged then
self.handlers.OnSelectionChanged(self, selectedData or nil)
end
end,
Reset = function(self)
wipe(self.expanded)
self:UpdateRowInfo()
self:UpdateRows()
self:SetSelectedRecord()
end,
SetDatabase = function(self, database)
if database and database ~= self.records then
self.records = database
end
local prevSelectedIndex
if self.selected then
for i, row in pairs(self.rows) do
if row:IsVisible() and row.record == self.selected then
prevSelectedIndex = i
end
end
end
self:UpdateRowInfo()
self:UpdateRows()
if not self.selected and prevSelectedIndex then
-- try to select the same row
local row = self.rows[prevSelectedIndex]
if row and row:IsVisible() and row.record then
self:SetSelectedRecord(row.record)
end
if not self.selected then
-- select the first row
row = self.rows[1]
if row and row:IsVisible() and row.record then
self:SetSelectedRecord(row.record)
end
end
end
end,
RemoveAuctionRecord = function(self, record)
local index = key(self.records, record)
if index then
tremove(self.records, index)
end
self:SetDatabase()
end,
ContainsRecord = function(self, record)
if key(self.records, record) then
return true
end
end,
SetSort = vararg-function(arg)
local self = tremove(arg, 1)
for _, v in ipairs(arg) do
for i, sort in pairs(self.sorts) do
if sort.index == abs(v) then
tremove(self.sorts, i)
break
end
end
tinsert(self.sorts, 1, {index=abs(v), descending=v < 0})
end
self.isSorted = nil
self:UpdateRows()
end,
SetHandler = function(self, event, handler)
self.handlers[event] = handler
end,
GetSelection = function(self)
if not self.selected then return end
local selectedData
for _, v in ipairs(self.rowInfo) do
for _, childInfo in pairs(v.children) do
if childInfo.record.search_signature == self.selected.search_signature then
selectedData = childInfo
break
end
end
end
return selectedData
end,
}
function M.new(parent, rows, columns)
local rt = CreateFrame('Frame', nil, parent)
rt.columns = columns
rt.ROW_HEIGHT = (parent:GetHeight() - HEAD_HEIGHT - HEAD_SPACE) / rows
rt.expanded = {}
rt.handlers = {}
rt.sorts = {}
rt.records = {}
rt.rowInfo = {numDisplayRows=0}
for name, func in pairs(methods) do
rt[name] = func
end
rt:SetScript('OnShow', function()
for _, cell in pairs(this.headCells) do
if cell.info.isPrice then
cell:SetText(cell.info.title[price_per_unit and 1 or 2])
end
end
end)
local contentFrame = CreateFrame('Frame', nil, rt)
contentFrame:SetPoint('TOPLEFT', 0, 0)
contentFrame:SetPoint('BOTTOMRIGHT', 0, 0)
rt.contentFrame = contentFrame
local scrollFrame = CreateFrame('ScrollFrame', gui.unique_name, rt, 'FauxScrollFrameTemplate')
scrollFrame:SetScript('OnVerticalScroll', function()
FauxScrollFrame_OnVerticalScroll(rt.ROW_HEIGHT, function() rt:UpdateRows() end)
end)
scrollFrame:SetAllPoints(contentFrame)
rt.scrollFrame = scrollFrame
FauxScrollFrame_Update(rt.scrollFrame, 0, rows, rt.ROW_HEIGHT)
local scrollBar = _G[scrollFrame:GetName() .. 'ScrollBar']
scrollBar:ClearAllPoints()
scrollBar:SetPoint('TOPRIGHT', rt, -4, -HEAD_HEIGHT)
scrollBar:SetPoint('BOTTOMRIGHT', rt, -4, 4)
scrollBar:SetWidth(10)
local thumbTex = scrollBar:GetThumbTexture()
thumbTex:SetPoint('CENTER', 0, 0)
thumbTex:SetTexture(color.content.background())
thumbTex:SetHeight(150)
thumbTex:SetWidth(scrollBar:GetWidth())
_G[scrollBar:GetName() .. 'ScrollUpButton']:Hide()
_G[scrollBar:GetName() .. 'ScrollDownButton']:Hide()
rt.headCells = {}
for i, column in ipairs(rt.columns) do
local cell = CreateFrame('Button', nil, rt.contentFrame)
cell:SetHeight(HEAD_HEIGHT)
if i == 1 then
cell:SetPoint('TOPLEFT', 0, 0)
else
cell:SetPoint('TOPLEFT', rt.headCells[i - 1], 'TOPRIGHT')
end
cell.info = column
cell.rt = rt
cell.columnIndex = i
cell:RegisterForClicks('LeftButtonUp', 'RightButtonUp')
cell:SetScript('OnClick', rt.OnHeadColumnClick)
local text = cell:CreateFontString()
text:SetJustifyH('CENTER')
text:SetFont(gui.font, 12)
text:SetTextColor(color.label.enabled())
cell:SetFontString(text)
if not column.isPrice then cell:SetText(column.title or '') end -- TODO
text:SetAllPoints()
local tex = cell:CreateTexture()
tex:SetAllPoints()
tex:SetTexture([[Interface\AddOns\aux-AddOn\WorldStateFinalScore-Highlight]])
tex:SetTexCoord(.017, 1, .083, .909)
tex:SetAlpha(.5)
cell:SetNormalTexture(tex)
local tex = cell:CreateTexture()
tex:SetAllPoints()
tex:SetTexture([[Interface\Buttons\UI-Listbox-Highlight]])
tex:SetTexCoord(.025, .957, .087, .931)
tex:SetAlpha(.2)
cell:SetHighlightTexture(tex)
tinsert(rt.headCells, cell)