Quantcast

Make columns clickable to sort by action text or binding

James Whitehead II [10-02-10 - 13:14]
Make columns clickable to sort by action text or binding
Filename
Clique.xml
CliqueOptions.lua
CliqueUtils.lua
diff --git a/Clique.xml b/Clique.xml
index 1e83fd3..ad3ee54 100755
--- a/Clique.xml
+++ b/Clique.xml
@@ -79,6 +79,7 @@
         <Scripts>
             <OnClick>
                 PlaySound("igMainMenuOptionCheckBoxOn");
+                CliqueConfig:Column_OnClick(self, button)
             </OnClick>
         </Scripts>
     </Button>
diff --git a/CliqueOptions.lua b/CliqueOptions.lua
index 332752f..143278e 100755
--- a/CliqueOptions.lua
+++ b/CliqueOptions.lua
@@ -36,12 +36,22 @@ function CliqueConfig:SetupGUI()
     self.page1.column1:SetText(L["Action"])
     self.page1.column2:SetText(L["Binding"])

+    -- Set columns up to handle sorting
+    self.page1.column1.sortType = "name"
+    self.page1.column2.sortType = "key"
+    self.page1.sortType = self.page1.column2.sortType
+
     self.button_add:SetText(L["Add binding"])
     self.button_edit:SetText(L["Edit"])

     self.page1:Show()
 end

+function CliqueConfig:Column_OnClick(frame, button)
+    self.page1.sortType = frame.sortType
+    self:UpdateList()
+end
+
 function CliqueConfig:HijackSpellbook()
     self.spellbookButtons = {}

@@ -172,7 +182,18 @@ end})

 local compareFunctions = {
     name = function(a, b)
-        return a.binding < b.binding
+        local texta = addon:GetBindingActionText(a)
+        local textb = addon:GetBindingActionText(b)
+        return texta < textb
+    end,
+    key = function(a, b)
+        local keya = addon:GetBindingKey(a)
+        local keyb = addon:GetBindingKey(b)
+        if keya == keyb then
+            return memoizeBindings[a] < memoizeBindings[b]
+        else
+            return keya < keyb
+        end
     end,
     binding = function(a, b)
         local mem = memoizeBindings
@@ -195,7 +216,12 @@ function CliqueConfig:UpdateList()
     for uid, entry in pairs(binds) do
         sort[#sort + 1] = entry
     end
-    table.sort(sort, compareFunctions.binding)
+
+    if page.sortType then
+        table.sort(sort, compareFunctions[page.sortType])
+    else
+        table.sort(sort, compareFunctions.key)
+    end

     -- Enable or disable the scroll bar
     if #sort > MAX_ROWS - 1 then
diff --git a/CliqueUtils.lua b/CliqueUtils.lua
index cd43a6b..775dc3c 100644
--- a/CliqueUtils.lua
+++ b/CliqueUtils.lua
@@ -110,6 +110,11 @@ function addon:GetBindingActionText(binding)
     end
 end

+function addon:GetBindingKey(binding)
+    local key = binding.key:match("[^%-]+$")
+    return key
+end
+
 local binMap = {
     ALT = 1,
     LALT = 2,