Skip to content

Instantly share code, notes, and snippets.

@fbettag
Created November 28, 2011 03:52
Show Gist options
  • Save fbettag/1399018 to your computer and use it in GitHub Desktop.
Save fbettag/1399018 to your computer and use it in GitHub Desktop.
Growl-style Notifications with Lift's CometActors
object NotificationCenter extends LiftActor with ListenerManager {
def createUpdate = "dummy"
override def lowPriority = {
case s: NotificationMessage => updateListeners(s)
}
}
case class NotificationMessage(title: String, text: String, image: Box[String], sticky: Boolean, time: Box[Int], customer: Box[Long]) {
def toJsCmd = JsRaw("""
$.gritter.add({
// (string | mandatory) the heading of the notification
title: "%s",
// (string | mandatory) the text inside the notification
text: "%s",
// (string | optional) the image to display on the left
image: "%s",
// (bool | optional) if you want it to fade out on its own or just sit there
sticky: %s,
// (int | optional) the time you want it to be alive for before fading out
time: %s,
// (string | optional) the class name you want to apply to that specific message
class_name: 'sticky'
})
""".format(
title.replaceAll("\"", "\\\""),
text.replaceAll("\"", "\\\""),
image match { case Full(a) => a case _ => "" },
sticky,
time match { case Full(a) => a case _ => "''" }))
}
<span lift="SendNotification">
<h3><lift:loc locid="new.notification">New Notification</lift:loc></h3>
<label><lift:loc locid="title">Title</lift:loc>:</label>
<input class="notification_title"/><br/>
<label><lift:loc locid="text">Text</lift:loc>:</label>
<input class="notification_text"/><br/>
<label><lift:loc locid="sticky">Sticky?</lift:loc>:</label>
<input class="notification_sticky"/><br/>
<label><lift:loc locid="Customer">Customer</lift:loc>:</label>
<input class="notification_customer" placeholder="e.g. 10100"/><br/>
<button class="notification_send"></button>
</span>
class SendNotification extends StatefulSnippet {
def dispatch = {
case "render" => render
}
var title = "Admin Message"
var text = ""
var customer = ""
var sticky = false
def notification = NotificationMessage(
title,
text.replaceAll("\r?\n", "<br/>"),
Full("http://uberblo.gs/me.png"),
sticky,
Empty,
if (customer == "") Empty else Full(customer.toLong))
def render: CssSel =
".notification_title" #> SHtml.ajaxText(title, (v: String) => { title = v; Noop }) &
".notification_text" #> SHtml.ajaxTextarea(text, (v: String) => { text = v; Noop }) &
".notification_sticky" #> SHtml.ajaxCheckbox(sticky, (v: Boolean) => { sticky = v; Noop }) &
".notification_customer" #> SHtml.ajaxText(customer, (v: String) => { customer = v; Noop }) &
".notification_send" #> SHtml.ajaxButton("Send Notification", () => { NotificationCenter ! notification; Noop })
}
<span lift="Users.isLoggedIn">
<span lift="comet?type=UserNotification"></span>
</span>
class UserNotification extends CometActor with CometListener {
def registerWith = NotificationCenter
private var msgs: List[NotificationMessage] = List()
override def lowPriority = {
case v: NotificationMessage => v.customer match {
case Full(c) if User.currentUser != Empty => if (c == User.currentUser.open_!.customer.is) submit(v)
case Full(c) if User.currentUser == Empty =>
case Empty => submit(v)
case _ =>
}
}
private def submit(v: NotificationMessage) {
v :: msgs
partialUpdate(v.toJsCmd)
}
def render = "*" #> ""
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment