changeset 13:0ca5ec0b7502

We have an ldb tooltip now! Still need to make it pretty.
author wobin
date Thu, 23 Apr 2009 17:00:45 +1000
parents d069c3e6379b
children 18c33a32ba31
files Squawk.lua
diffstat 1 files changed, 68 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/Squawk.lua	Thu Apr 23 04:26:34 2009 +1000
+++ b/Squawk.lua	Thu Apr 23 17:00:45 2009 +1000
@@ -73,14 +73,31 @@
 --		- The next time Follower is online and recieves a Squawk we check if there
 --		is a Settings.Requested for that name, and if so assume they have approved
 --		and clear/add records appropriately.
+--
+--		For updating, there can be a few methods.
+--
+--		Guild open: you're not private, you broadcast to the guild your last X
+--		squawks on login
+--
+--		followers:
 --]]
 Model.Squawk = {}
 local Squawk = Model.Squawk
-
+local function wrap(str, limit)
+  limit = limit or 72
+  local here = 1
+  return str:gsub("(%s+)()(%S+)()",
+                          function(sp, st, word, fi)
+                            if fi-here > limit then
+                              here = st 
+                              return "\n"..word
+                            end
+                          end)
+end
 function Squawk:new(Message, Owner)
 	local o = {}
 	o.Owner = Owner or UnitName("player")
-	o.Message = Message
+	o.Message = wrap(Message)
 	o.Time = time()
 	local reply, to = strsplit("@", ((strsplit(" ", Message))))
 	if reply == "" then
@@ -332,14 +349,60 @@
 	end
 end
 
+local TimeSpan = {	[1] = {"second",  60, 1},
+										[2] = {"minute", 3600, 60}, 
+										[3] = {"hour", 86400, 3600} }
+
+function View:GetTime(stime)
+	local lapsed = difftime(time(), stime)
+	if lapsed < 86400 then -- if we're still in the same day...
+		for _,span in ipairs(TimeSpan) do
+			if lapsed < span[2] then
+				local timespan = math.floor(lapsed/span[3])
+				if timespan == 1 then
+					timespan = timespan .." ".. span[1]
+				else
+					timespan = timespan .. " ".. span[1].."s"
+				end
+				return timespan.. " ago"
+			end
+		end
+	end
+	return date("%I:%M %p %b %d", stime)
+end
+
 local LDBFeed = LibStub("LibDataBroker-1.1"):NewDataObject("Squawk", {type = "data source", text = "Awk!"})
 local QTip = LibStub("LibQTip-1.0")
 
+local function AddLine(tooltip, Line, Number, Owner, TimeStamp)
+	if #Line < 79 then
+		tooltip:AddLine(Number, Owner, Line, TimeStamp)
+	else
+		tooltip:AddLine(Number, Owner, Line:sub(1, 80).."-", TimeStamp)
+		AddLine(tooltip, Line:sub(81))
+	end
+end
+
 function LDBFeed:OnEnter()
-	local tooltip = QTip:Acquire("Squawk",3, "LEFT", "LEFT", "RIGHT")
+	local tooltip = QTip:Acquire("Squawk",4, "LEFT", "LEFT", "LEFT", "RIGHT")
 	self.tooltip = tooltip
-	tooltip:AddHeader('Name')
-	tooltip:AddLine('testUser', 'Squawk!', '3 min ago')
+	tooltip:AddHeader('Squawk')
+	for i,squawk in ipairs(Model.Squawks) do
+		if #squawk.Message > 79 then
+			local head = true
+			local message = {strsplit("\n",squawk.Message)}
+			for _,line in ipairs(message) do
+				if head then
+					AddLine(tooltip, line, i..".", squawk.Owner, View:GetTime(squawk.Time))
+					head = false
+				else
+					AddLine(tooltip, line)
+				end
+			end
+		else
+			tooltip:AddLine(i..".", squawk.Owner, squawk.Message, View:GetTime(squawk.Time))
+		end
+	end
 	tooltip:SmartAnchorTo(self)
 	tooltip:Show()
 end