Permalink
Browse files

Examples up through Ajax/Comet

  • Loading branch information...
1 parent 8f3cd74 commit 48b0dea3557004e37f2abb3b3d6466fd499b8797 @dpp dpp committed Jan 21, 2011
@@ -22,12 +22,14 @@ class Boot {
// Build SiteMap
def sitemap = SiteMap(
- Menu.i("Home") / "index", // the simple way to declare a menu
-
- // more complex because this menu allows anything in the
- // /static path to be visible
- Menu(Loc("Static", Link(List("static"), true, "/static/index"),
- "Static Content")))
+ Menu.i("Home") / "index",
+ Menu("lazy", "Lazy Loading") / "lazy",
+ Menu("parallel", "Parallel Rendering") / "parallel",
+ Menu("comet", "Comet & Ajax") / "comet",
+ Menu("wiring", "Wiring") / "wiring",
+ Menu("tempales", "Designer Friendly Templates") / "templates",
+ Menu("wizard", "Wizard") / "wizard",
+ Menu("security", "Security") / "security")
// set the sitemap. Note if you don't want access control for
// each page, just comment this line out.
@@ -0,0 +1,43 @@
+package net.liftweb.seventhings
+package comet
+
+import net.liftweb._
+import http._
+import actor._
+import util._
+import Helpers._
+
+
+/**
+ * The comet chat component
+ */
+class Chat extends CometActor with CometListener {
+ private var msgs: Vector[String] = Vector() // private state
+
+ // register this component
+ def registerWith = ChatServer
+
+ // listen for messages
+ override def lowPriority = {
+ case v: Vector[String] => msgs = v; reRender()
+ }
+
+ // render the component
+ def render = ClearClearable & "li *" #> msgs
+}
+
+
+/**
+ * The chat server
+ */
+object ChatServer extends LiftActor with ListenerManager {
+ private var msgs = Vector("Welcome") // the private data
+
+ // what we send to listeners on update
+ def createUpdate = msgs
+
+ // handle incoming messages
+ override def lowPriority = {
+ case s: String => msgs :+= s; updateListeners()
+ }
+}
@@ -1,57 +0,0 @@
-package code {
-package lib {
-
-import net.liftweb._
-import http._
-import util._
-import common._
-import _root_.java.util.Date
-
-/**
- * A factory for generating new instances of Date. You can create
- * factories for each kind of thing you want to vend in your application.
- * An example is a payment gateway. You can change the default implementation,
- * or override the default implementation on a session, request or current call
- * stack basis.
- */
-object DependencyFactory extends Factory {
- implicit object time extends FactoryMaker(Helpers.now _)
-
- /**
- * objects in Scala are lazily created. The init()
- * method creates a List of all the objects. This
- * results in all the objects getting initialized and
- * registering their types with the dependency injector
- */
- private def init() {
- List(time)
- }
- init()
-}
-
-/*
-/**
- * Examples of changing the implementation
- */
-sealed abstract class Changer {
- def changeDefaultImplementation() {
- DependencyFactory.time.default.set(() => new Date())
- }
-
- def changeSessionImplementation() {
- DependencyFactory.time.session.set(() => new Date())
- }
-
- def changeRequestImplementation() {
- DependencyFactory.time.request.set(() => new Date())
- }
-
- def changeJustForCall(d: Date) {
- DependencyFactory.time.doWith(d) {
- // perform some calculations here
- }
- }
-}
-*/
-}
-}
@@ -0,0 +1,23 @@
+package net.liftweb.seventhings
+package snippet
+
+import comet._
+
+import net.liftweb._
+import util._
+import Helpers._
+import http._
+import js.JsCmds._
+import js.JE._
+
+/**
+ * Handle input by sending the input line
+ * to the ChatServer
+ */
+object ChatIn {
+ def render =
+ "*" #> SHtml.onSubmit(s => {
+ ChatServer ! s // send the message
+ SetValById("chat_in", "") // clear the input box
+ })
+}
@@ -0,0 +1,19 @@
+package net.liftweb.seventhings
+package snippet
+
+import net.liftweb._
+import util._
+import Helpers._
+
+/**
+ * Fetch an ad from an ad server (takes about 1/2 second
+ */
+object FetchAd {
+ def render = {
+ // sleep for 1/2 second
+ Thread.sleep(500 millis)
+
+ // send the result back
+ ".ad" #> Thread.currentThread.getName
+ }
+}
@@ -1,16 +0,0 @@
-package net.liftweb.seventhings
-package snippet
-
-import scala.xml.{NodeSeq, Text}
-import net.liftweb.util._
-import net.liftweb.common._
-import java.util.Date
-import code.lib._
-import Helpers._
-
-class HelloWorld {
- lazy val date: Box[Date] = DependencyFactory.inject[Date] // inject the date
-
- // replace the contents of the element with id "time" with the date
- def howdy = "#time *" #> date.map(_.toString)
-}
@@ -0,0 +1,23 @@
+package net.liftweb.seventhings
+package snippet
+
+import net.liftweb._
+import util._
+import Helpers._
+
+/**
+ * Do something that takes a long time to do
+ */
+object LongTime {
+ def render = {
+ // capture the start time
+ val start = now
+
+ // sleep for up to 15 seconds
+ Thread.sleep(randomLong(15 seconds))
+
+ // send the result back
+ "#start" #> start.toString &
+ "#end" #> now.toString
+ }
+}
@@ -3,27 +3,65 @@ package snippet
import net.liftweb._
import http._
+import util._
+import Helpers._
import common._
import scala.xml.NodeSeq
+/**
+ * A snippet that will show part or all of a source file
+ * that's part of this project
+ */
object ShowCode {
def render: NodeSeq =
for {
- fileName <- S.attr("name") ?~
- "Name missing"
+ fileName <- S.attr("name") ?~ "Name missing"
- code <- LiftRules.loadResource("/src/scala"+fileName) ?~
+ code <- (LiftRules.loadResource("/src/scala"+fileName) or
+ LiftRules.loadResource("/src/webapp"+fileName)) ?~
("Cannot find "+fileName)
- } yield <div style="text-align: center"><tt>Listing: <a href={"https://github.com/lift/seventhings/tree/master/src/main/scala"+fileName}>{fileName}</a></tt>
- <pre class="listing brush: scala">{new String(code, "UTF-8")}</pre>
- </div>
+ } yield {
+ def calcBrush = if (fileName.endsWith(".html") ||
+ fileName.endsWith(".xml")) "html" else "scala"
+
+ def gitDir = if (fileName.endsWith(".html") ||
+ fileName.endsWith(".xml")) "webapp" else "scala"
+
+ val start = S.attr("start")
+ val end = S.attr("end")
+
+ val rawCode = (new String(code, "UTF-8").
+ split("""\n""").
+ toList.map(_.replace("\t", " "))).
+ dropWhile(s => start.map(st => s.indexOf(st) < 0) openOr false) match {
+ case Nil => Nil
+ case x :: xs => x :: xs.
+ takeWhile(s => end.map(en => s.indexOf(en) < 0) openOr true)
+ }
+
+
+
+
+ <div style="text-align: center"><tt>Listing:
+ <a href={"https://github.com/lift/seventhings/tree/master/src/main/"+
+ gitDir+fileName}>{fileName}</a></tt>
+ <pre class={"listing brush: "+calcBrush}>{rawCode.mkString("\n")}</pre>
+ </div>
+ }
private implicit def boxNStoNS(in: Box[NodeSeq]): NodeSeq =
in match {
case Full(ns) => ns
- case Failure(msg, _, _) => throw new SnippetExecutionException(msg)
- case _ => throw new SnippetExecutionException("Couldn't calculate value")
+ case Failure(msg, _, _) =>
+ <div style="display: block; margin: 8px; border: 2px solid red">
+ Failed to look up source: {msg}
+ </div>
+ case _ => <div style="display: block; margin: 8px; border: 2px solid red">
+ Failed to look up source
+ </div>
+
+
}
}
Oops, something went wrong.

0 comments on commit 48b0dea

Please sign in to comment.