Created
November 28, 2011 03:52
-
-
Save fbettag/1399018 to your computer and use it in GitHub Desktop.
Growl-style Notifications with Lift's CometActors
This file contains hidden or 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
object NotificationCenter extends LiftActor with ListenerManager { | |
def createUpdate = "dummy" | |
override def lowPriority = { | |
case s: NotificationMessage => updateListeners(s) | |
} | |
} |
This file contains hidden or 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
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 _ => "''" })) | |
} |
This file contains hidden or 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
<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> |
This file contains hidden or 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
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 }) | |
} |
This file contains hidden or 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
<span lift="Users.isLoggedIn"> | |
<span lift="comet?type=UserNotification"></span> | |
</span> |
This file contains hidden or 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
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