Sorting is working.

Jayden Platell [04-08-14 - 22:01]
Sorting is working.
Filename
Librarian.lua
Librarian.xml
diff --git a/Librarian.lua b/Librarian.lua
index 0916767..e575735 100644
--- a/Librarian.lua
+++ b/Librarian.lua
@@ -12,13 +12,13 @@ ZO_CreateStringId("SI_LIBRARIAN_MARK_READ", "Mark as Read")

 local previousBook
 local scrollChild
+local sortField = "Found"
+local sortAscending = true

 function Librarian:Initialise()
  	scrollChild = LibrarianFrameScrollContainer:GetNamedChild("ScrollChild")
 	self.savedVars = ZO_SavedVars:New("Librarian_SavedVariables", 1, nil, self.defaults, nil)

-	--local manager = ZO_SortFilterList.New(self, LibrarianFrameScrollContainer)
-
 	self:LayoutBooks()

 	self:InitializeKeybindStripDescriptors()
@@ -106,14 +106,19 @@ end

 function Librarian:LayoutBooks()
     ZO_Scroll_ResetToTop(LibrarianFrameScrollContainer)
+    previousBook = nil
     for i, book in ipairs(self.savedVars.books) do
 		self:LayoutBook(i, book)
     end
 end

 function Librarian:LayoutBook(i, book)
-	local bookControl = CreateControlFromVirtual("LibrarianBook", scrollChild, "LibrarianBook", i)
-	bookControl.id = i
+	local bookControl = GetControl("LibrarianBook"..i)
+	if not bookControl then
+		bookControl = CreateControlFromVirtual("LibrarianBook", scrollChild, "LibrarianBook", i)
+		bookControl.id = i
+	end
+
 	bookControl.unread = bookControl:GetNamedChild("Unread")
 	bookControl.found = bookControl:GetNamedChild("Found")
 	bookControl.title = bookControl:GetNamedChild("Title")
@@ -130,6 +135,48 @@ function Librarian:LayoutBook(i, book)
     previousBook = bookControl
 end

+function Librarian:InitialiseSortHeader(control, name, tag)
+	control.tag = tag
+	local nameControl = GetControl(control, "Name")
+    nameControl:SetFont("ZoFontHeader")
+    nameControl:SetText(GetString(name))
+    nameControl:SetHorizontalAlignment(alignment or TEXT_ALIGN_LEFT)
+    control.initialDirection = initialDirection or ZO_SORT_ORDER_DOWN
+    control.usesArrow = true
+end
+
+function Librarian:SortBy(control)
+	local field = control.tag
+	if field == sortField then
+		sortAscending = not sortAscending
+	else
+		sortField = field
+		sortAscending = true
+	end
+
+	if sortField == "Unread" then
+		if sortAscending then
+			table.sort(self.savedVars.books, function(a, b) return a.unread and not b.unread end)
+		else
+			table.sort(self.savedVars.books, function(a, b) return not a.unread and b.unread end)
+		end
+	elseif sortField == "Found" then
+		if sortAscending then
+			table.sort(self.savedVars.books, function(a, b) return a.timeStamp < b.timeStamp end)
+		else
+			table.sort(self.savedVars.books, function(a, b) return a.timeStamp > b.timeStamp end)
+		end
+	elseif sortField == "Title" then
+		if sortAscending then
+			table.sort(self.savedVars.books, function(a, b) return a.title < b.title end)
+		else
+			table.sort(self.savedVars.books, function(a, b) return a.title > b.title end)
+		end
+	end
+
+	self:LayoutBooks()
+end
+
 function Librarian:ReadBook(id)
 	local book = self.savedVars.books[id]
 	LORE_READER:SetupBook(book.title, book.body, book.medium, book.showTitle)
diff --git a/Librarian.xml b/Librarian.xml
index 0cc3be9..512ae2a 100644
--- a/Librarian.xml
+++ b/Librarian.xml
@@ -21,41 +21,53 @@
                     <Anchor point="TOPLEFT" relativeTo="$(parent)" offsetX="19" />
                 </Texture>
                 <Label name="$(parent)Found" font="ZoFontWinH2" wrapMode="ELLIPSIS">
-                    <Anchor point="TOPLEFT" relativeTo="$(parent)" offsetX="70" offsetY="1" />
+                    <Anchor point="TOPLEFT" relativeTo="$(parent)" offsetX="80" offsetY="1" />
                 </Label>
                 <Label name="$(parent)Title" font="ZoFontWinH2" wrapMode="ELLIPSIS">
                     <Anchor point="TOPLEFT" relativeTo="$(parent)" offsetX="270" offsetY="1" />
                 </Label>
             </Controls>
         </Button>
-        <TopLevelControl name="LibrarianFrame" inherits="ZO_RightPanelFootPrint" hidden="true">
+        <Button name="SortHeader" resizeToFitDescendents="true" virtual="true">
+            <OnMouseUp>
+                Librarian:SortBy(self)
+            </OnMouseUp>
+            <Controls>
+                <Label name="$(parent)Name" font="ZoFontGame" color="INTERFACE_COLOR_TYPE_TEXT_COLORS:INTERFACE_TEXT_COLOR_NORMAL" modifyTextType="UPPERCASE" horizontalAlignment="CENTER" verticalAlignment="CENTER" wrapMode="ELLIPSIS">
+                    <AnchorFill />
+                </Label>
+            </Controls>
+        </Button>
+        <TopLevelControl name="LibrarianFrame" inherits="ZO_RightPanelFootPrint"  hidden="true">
             <Controls>
-                <Control name="$(parent)SortBy" resizeToFitDescendents="true">
-                    <Anchor point="TOPLEFT" offsetY="10" />
+                <Control name="$(parent)SortBy">
+                    <Anchor point="TOPLEFT" relativeTo="$(parent)" offsetY="10" />
+                    <Anchor point="BOTTOMRIGHT" relativeTo="$(parent)" relativePoint="TOPRIGHT" offsetY="0" />
                     <Controls>
-                        <Control name="$(parent)Unread" inherits="ZO_SortHeader">
-                            <Dimensions x="70" />
-                            <Anchor point="LEFT" />
+                        <Button name="$(parent)Unread" inherits="SortHeader">
+                            <Dimensions x="80" y="20" />
+                            <Anchor point="TOPLEFT" />
                             <OnInitialized>
-                                ZO_PlayerInventory_InitSortHeader(self, SI_LIBRARIAN_SORT_TYPE_UNREAD, TEXT_ALIGN_LEFT, "unread")
+                                Librarian:InitialiseSortHeader(self, SI_LIBRARIAN_SORT_TYPE_UNREAD, "Unread")
                             </OnInitialized>
-                        </Control>
-                        <Control name="$(parent)Time" inherits="ZO_SortHeader">
-                            <Dimensions x="200" />
+                        </Button>
+                        <Button name="$(parent)Time" inherits="SortHeader">
+                            <Dimensions x="200" y="20" />
                             <Anchor point="TOPLEFT" relativeTo="$(parent)Unread" relativePoint="TOPRIGHT" />
                             <OnInitialized>
-                                ZO_PlayerInventory_InitSortHeader(self, SI_LIBRARIAN_SORT_TYPE_FOUND, TEXT_ALIGN_LEFT, "found")
+                                Librarian:InitialiseSortHeader(self, SI_LIBRARIAN_SORT_TYPE_FOUND, "Found")
                             </OnInitialized>
-                        </Control>
-                        <Control name="$(parent)Name" inherits="ZO_SortHeader">
+                        </Button>
+                        <Button name="$(parent)Title" inherits="SortHeader">
                             <Anchor point="TOPLEFT" relativeTo="$(parent)Time" relativePoint="TOPRIGHT" />
+                            <Anchor point="BOTTOMRIGHT" relativeTo="$(parent)" relativePoint="TOPRIGHT" offsetY="20" />
                             <OnInitialized>
-                                ZO_PlayerInventory_InitSortHeader(self, SI_LIBRARIAN_SORT_TYPE_TITLE, TEXT_ALIGN_LEFT, "title")
+                                Librarian:InitialiseSortHeader(self, SI_LIBRARIAN_SORT_TYPE_TITLE, "Title")
                             </OnInitialized>
-                        </Control>
+                        </Button>
                     </Controls>
                 </Control>
-                <Control name="$(parent)ScrollContainer" color="CFDCBD" inherits="ZO_ScrollContainer">
+                <Control name="$(parent)ScrollContainer" inherits="ZO_ScrollContainer">
                     <Anchor point="TOPLEFT" relativeTo="$(parent)SortBy" relativePoint="BOTTOMLEFT" offsetY="30" />
                     <Anchor point="BOTTOMRIGHT" offsetX="-12" offsetY="0" />
                 </Control>