Quantcast

Alts : Fix sort when clikcing headers

urnati [04-19-26 - 14:15]
Alts : Fix sort when clikcing headers
Filename
TitanAlts/TitanAlts.lua
diff --git a/TitanAlts/TitanAlts.lua b/TitanAlts/TitanAlts.lua
index bba9524..da54207 100755
--- a/TitanAlts/TitanAlts.lua
+++ b/TitanAlts/TitanAlts.lua
@@ -334,15 +334,10 @@ local function Cell_tt_hide(self, text, parent)
 	tt:Hide()
 end

-local function SortDB(self, tt_key)
-
-	if alts_tt_sort_col == tt_key then
-		alts_tt_sort_ascend = not alts_tt_sort_ascend -- flip the sort
-	else
-		alts_tt_sort_ascend = true
-	end
-	alts_tt_sort_col = tt_key
+---Local Sort the alts list per col and sort order set.
+local function Sort_DB()

+	local tt_key = alts_tt_sort_col
 	local sorter = nil
 	if alts_tt_sort_ascend then
 		sorter = function(a, b)
@@ -357,10 +352,37 @@ local function SortDB(self, tt_key)
 	for idx = 1, #alts_tt do
 		table.sort(alts_tt, sorter)
 	end
+end
+
+---Local Called when user clicks a sortable col header. Ensure sort col and order are set then redraw.
+---@param self any
+---@param tt_key string
+local function Sort_Click(self, tt_key)
+
+	if alts_tt_sort_col == tt_key then
+		alts_tt_sort_ascend = not alts_tt_sort_ascend -- flip the sort
+	else
+		alts_tt_sort_ascend = true
+	end
+	alts_tt_sort_col = tt_key

+	-- Redraw tooltip
 	Alts.GenTooltip(_G[TITAN_BUTTON])
 end

+---Local Called on display of tooltip; Ensure col and sort order set if first time otherwise use as is
+local function Sort_Display()
+
+	if alts_tt_sort_col == "" then
+		alts_tt_sort_col = 'name_titan'
+		alts_tt_sort_ascend = true
+	else
+		-- use it
+	end
+
+	Sort_DB()
+end
+
 -- Grab the button text to display
 local function GetButtonText(id)
 	local avgItemLevel, avgItemLevelEquipped, avgItemLevelPvp = GetAverageItemLevel()
@@ -411,7 +433,7 @@ local function GenToonName(self, row, col, action, tt_info, toon_info)
 	local next = nil
 	if action == 'header' then
 		row, next = self.qtooltip:SetCell(row, col, tt_info.title, headerFont, header_justify)
-		self.qtooltip:SetCellScript(row, col, "OnMouseDown", SortDB, tt_info.tt_key)
+		self.qtooltip:SetCellScript(row, col, "OnMouseDown", Sort_Click, tt_info.tt_key)
 		total_shown = 0
 	elseif action == 'row' then
 		local cell = (toon_info[tt_info.tt_key] or UNK)
@@ -430,7 +452,7 @@ local function GenLevel(self, row, col, action, tt_info, toon_info)
 	local next = nil
 	if action == 'header' then
 		row, next = self.qtooltip:SetCell(row, col, tt_info.title, headerFont, header_justify);
-		self.qtooltip:SetCellScript(row, col, "OnMouseDown", SortDB, tt_info.tt_key)
+		self.qtooltip:SetCellScript(row, col, "OnMouseDown", Sort_Click, tt_info.tt_key)
 	elseif action == 'row' then
 		local cell = (toon_info[tt_info.tt_key] or UNK) -- whole number or not known
 		row, next = self.qtooltip:SetCell(row, col, cell, "RIGHT")
@@ -447,7 +469,7 @@ local function GenILevel(self, row, col, action, tt_info, toon_info)
 	local next = nil
 	if action == 'header' then
 		row, next = self.qtooltip:SetCell(row, col, tt_info.title, headerFont, header_justify)
-		self.qtooltip:SetCellScript(row, col, "OnMouseDown", SortDB, tt_info.tt_key)
+		self.qtooltip:SetCellScript(row, col, "OnMouseDown", Sort_Click, tt_info.tt_key)
 	elseif action == 'row' then
 		local cell = toon_info[tt_info.tt_key] -- need as number to format
 		local str = ""
@@ -517,7 +539,7 @@ local function GenMoney(self, row, col, action, tt_info, toon_info)
 	local str = ""
 	if action == 'header' then
 		row, next = self.qtooltip:SetCell(row, col, tt_info.title, headerFont, header_justify)
-		self.qtooltip:SetCellScript(row, col, "OnMouseDown", SortDB, tt_info.tt_key)
+		self.qtooltip:SetCellScript(row, col, "OnMouseDown", Sort_Click, tt_info.tt_key)
 		total_gold = 0
 	elseif action == 'row' then
 		local cell = toon_info[tt_info.tt_key] -- need number to format
@@ -545,7 +567,7 @@ local function GenPlayed(self, row, col, action, tt_info, toon_info)
 	local str = ""
 	if action == 'header' then
 		row, next = self.qtooltip:SetCell(row, col, tt_info.title, headerFont, header_justify)
-		self.qtooltip:SetCellScript(row, col, "OnMouseDown", SortDB, tt_info.tt_key)
+		self.qtooltip:SetCellScript(row, col, "OnMouseDown", Sort_Click, tt_info.tt_key)
 		total_played = 0
 	elseif action == 'row' then
 		local cell = toon_info[tt_info.tt_key] -- need number to format
@@ -778,17 +800,7 @@ local function GenDB(self, action)
 			end
 		end

-		-- Sort by name initially
-		-- cobbled from SortDB :)
-		local tt_key = "name_titan"
-		alts_tt_sort_col = tt_key
-		local sorter = function(a, b)
-			return a[tt_key] < b[tt_key]
-		end;
-
-		for idx = 1, #alts_tt do
-			table.sort(alts_tt, sorter)
-		end
+		Sort_Display()
 	end
 end

@@ -1195,10 +1207,10 @@ Note : Each routine has 3 sections to process the column:
 - row : How each cell should look; add tooltip if needed
 - total : total OR x / total OR blank

-Note : Sort is auto-magic. SortDB allows only one column to be sorted at a time.
-SortDB sorts the 'row' data using tt_key field; not header ot total.
+Note : Sort is auto-magic. Sort_Click allows only one column to be sorted at a time.
+Sort_Click sorts the 'row' data using tt_key field; not header ot total.
 Alphabetic sort only - Deep in QTip all cells are strings.
-SortDB 'flips' between ascending and descending. Usually ascending (A-Z) first but
+Sort_Click 'flips' between ascending and descending. Usually ascending (A-Z) first but
 if the user is clicking a lot, it is possible they need to click twice to get the order they want.