Created
August 16, 2010 01:32
-
-
Save milesegan/526222 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package code.comet | |
import net.liftweb._ | |
import http._ | |
import actor._ | |
import js._ | |
import JsCmds._ | |
import js.jquery.JqJsCmds.{AppendHtml, FadeOut, Hide, FadeIn} | |
import java.util.Date | |
import scala.xml._ | |
import util.Helpers._ | |
object ChatServer extends LiftActor with ListenerManager { | |
private var message: ChatCmd = _ | |
def createUpdate = message | |
override def lowPriority = { | |
case s: String => message = AddMessage(nextFuncName, s, new Date) ; updateListeners() | |
case d: RemoveMessage => message = d; updateListeners() | |
} | |
} | |
sealed trait ChatCmd | |
final case class AddMessage(guid: String, msg: String, date: Date) extends ChatCmd | |
final case class RemoveMessage(guid: String) extends ChatCmd | |
class Chat extends CometActor with CometListener { | |
def registerWith = ChatServer | |
override def lowPriority = { | |
case m: ChatCmd => { | |
updateMsg(m) | |
} | |
} | |
def updateMsg(msg: ChatCmd) { | |
partialUpdate(msg match { | |
case m: AddMessage => | |
Noop & AppendHtml("ul_dude", doLine(m)) & | |
Hide(m.guid) & FadeIn(m.guid, TimeSpan(0),TimeSpan(500)) | |
case RemoveMessage(guid) => | |
Noop & FadeOut(guid,TimeSpan(0),TimeSpan(500)) & | |
After(TimeSpan(500),Replace(guid, NodeSeq.Empty)) | |
}) | |
} | |
def render = | |
bind("chat", // the namespace for binding | |
"line" -> Nil, | |
"input" -> SHtml.text("", s => ChatServer ! s)) // the input | |
private def doLine(m: AddMessage): NodeSeq = { | |
val xml = <li><chat:msg/><chat:btn/></li> | |
bind("chat", addId(xml, m.guid), | |
"msg" -> m.msg, | |
"btn" -> SHtml.ajaxButton("delete", | |
() => { | |
ChatServer ! | |
RemoveMessage(m.guid) | |
Noop})) | |
} | |
private def addId(in: NodeSeq, id: String): NodeSeq = in map { | |
case e: Elem => e % ("id" -> id) | |
case x => x | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment