Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
dpp committed Feb 23, 2011
1 parent 929074e commit 0dd2453
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 51 deletions.
2 changes: 1 addition & 1 deletion samples/shopwithme/project/build/LiftProject.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class LiftProject(info: ProjectInfo) extends DefaultWebProject(info) {
val liftVersion = "2.3-SNAPSHOT"

// uncomment the following if you want to use the snapshot repo
val scalatoolsSnapshot = ScalaToolsSnapshots
// val scalatoolsSnapshot = ScalaToolsSnapshots

// The local maven repo to get immediate fixes
// val mavenLocal = "Local Maven Repository" at
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import http._
import sitemap._
import Loc._

import code.snippet._


/**
* A class that's instantiated early and run. It allows the application
Expand All @@ -21,7 +23,11 @@ class Boot {

// Build SiteMap
def sitemap = SiteMap(
Menu.i("Home") / "index")
Menu.i("Home") / "index",
SearchPage.menu,
AllItemsPage.menu,
AnItemPage.menu
)

// set the sitemap. Note if you don't want access control for
// each page, just comment this line out.
Expand Down
48 changes: 29 additions & 19 deletions samples/shopwithme/src/main/scala/code/comet/ListPages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,34 @@ class ListPages extends CometActor {
* Draw yourself
*/
def render = {
"#listPagesContents" #> (
"tbody" #>
Helpers.findOrCreateId(id => // make sure tbody has an id
// when the cart contents updates
WiringUI.history(SessionPresenceInfo.pages) {
(old, nw, ns) => {
// capture the tr part of the template
val theTR = ("tr ^^" #> "**")(ns)

// build a row out of the page view info
def html(ci: (String, ParsePath)): NodeSeq =
("tr [id]" #> ci._1 & "td *" #> ci._2.toString)(theTR)

// calculate the delta between the lists and
// based on the deltas, emit the current jQuery
// stuff to update the display
JqWiringSupport.calculateDeltas(old, nw, id)(_._1, html _)
}
}))
"tbody *" #> SessionPresenceInfo.pages.map{
_._2.wholePath.mkString("/", "/", "")
}.map {
path => "td *" #> path
}
}

override def localSetup() {
// make sure we're a dependent on the ValueCell
// by default, the dependency info will be removed
// on a reRender
SessionPresenceInfo.pages.addDependent(this)
}

/**
* By default, Lift deals with managing wiring dependencies.
* This means on each full render (a full render will
* happen on reRender() or on a page load if there have been
* partial updates.) You may want to manually deal with
* wiring dependencies. If you do, override this method
* and return true
*/
override protected def manualWiringDependencyManagement = true

// we'll be poked on the predicate update. reRender
// rather than the partial update which is oriented toward
// the WiringUI world
override def poke() {
reRender(false)
}
}
72 changes: 65 additions & 7 deletions samples/shopwithme/src/main/scala/code/comet/WhatPage.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import lib._
import net.liftweb._
import common._
import http._
import js.JE._
import js.JsCmds._
import util._
import Helpers._
import scala.xml.NodeSeq
Expand All @@ -15,6 +17,7 @@ import scala.xml.NodeSeq
*/
class WhatPage extends CometActor {
private var thePath: Box[ParsePath] = Empty
private var lastToken = Helpers.nextFuncName

override def lifespan = Full(3 seconds)

Expand Down Expand Up @@ -45,32 +48,87 @@ class WhatPage extends CometActor {
*/
override protected def captureInitialReq(initialReq: Box[Req]) {
thePath = initialReq.map(_.path)
this.addPresence()
}

private def addPresence() {
// add the parse path to the list of pages being viewed
// by this session
thePath.foreach(p => SessionPresenceInfo.pages.
atomicUpdate(lst => (uniqueId -> p) :: lst))
atomicUpdate(_ + (uniqueId -> p)))

}

private def removePresence() {
SessionPresenceInfo.pages.atomicUpdate(_.filter(_._1 != uniqueId))
}

println("Initied "+name+" path "+thePath)
private def heartBeat() {
Schedule.schedule(() => this ! HeartBeat(), 5 seconds)
}

override def localSetup() {
heartBeat()
}

override def localShutdown() {
// remove the page from the list of pages
SessionPresenceInfo.pages.atomicUpdate(_.filter(_._1 != uniqueId))
println("Bye "+name+" "+SessionPresenceInfo.pages.get)
removePresence()
}

override def localSetup() {
println("Setting up in "+this+" 888888888888* ")
/**
* This method will be called when there's a change in the long poll
* listeners. The method does nothing, but allows you to get a granular
* sense of how many browsers care about this CometActor. Note that
* this method should not block for any material time and if there's
* any processing to do, use Scheduler.schedule or send a message to this
* CometActor. Do not change the Actor's state from this method.
*/
override protected def listenerTransition(): Unit = {
if (cometListeners.isEmpty) {
// we've got zero listeners. If we have this for more
// than 3 seconds, remove us
val token = lastToken // snapshot this
Schedule.schedule(() => this ! CheckToken(token), 3 seconds)
} else {
// update the token to indicate that we've
// had a listener transition and should not
// remove ourselves
lastToken = Helpers.nextFuncName
this.addPresence() // make sure we're in the list
}
}

override def lowPriority = {
case HeartBeat() => {
// a Noop JavaScript function
partialUpdate(JsRaw(uniqueId+" = 1;"))
heartBeat()
}

case CheckToken(token) => {
// the token hasn't changed in 3 seconds (no
// transition to having listeners), so
// we remove ourselves from the list
if (token == lastToken) {
this.removePresence()
}
}
}
}

private case class CheckToken(token: String)

private case class HeartBeat()

/**
* Capture the list of all the pages that
* are being viewed
*/
class PresenceInfo {
val pages = ValueCell[List[(String, ParsePath)]](Nil)
val pages = ValueCell[Set[(String, ParsePath)]](Set())
}



object SessionPresenceInfo extends SessionVar(new PresenceInfo)
23 changes: 0 additions & 23 deletions samples/shopwithme/src/main/webapp/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,6 @@
<body class="lift:content_id=main">
<div id="main" class="lift:surround?with=default;at=content">

<div>
I'm on pages:

<div lift="comet?type=ListPages">
<table id="listPagesContents">
<tbody>
<tr><td>an item</td></tr>
</tbody>
</table>
</div>
</div>

<div lift="comet?type=CometCart">
Contents:
<table id="contents">
<tbody>
<tr><td>an item</td></tr>
</tbody>
</table>

Subtotal: <span id="total">0</span>
</div>

<div lift="AddRandom">Add something</div>

</div>
Expand Down
28 changes: 28 additions & 0 deletions samples/shopwithme/src/main/webapp/templates-hidden/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,34 @@ <h1 class="alt">app<img alt="" id="ajax-loader" style="display:none; margin-bott
<hr class="space" />

<div class="lift:comet?type=WhatPage;randomname=true"></div>

<div class="lift:comet?type=CometCart" style="border: 1px solid #ccc">
Cart:
<table id="contents">
<tbody id="contents_tbody">
<tr><td>an item</td></tr>
</tbody>
</table>
<hr/>
Subtotal: <span id="total">0</span>
</div>

<br/>

<div style="border: 1px solid #ccc">
I'm on pages:

<div lift="comet?type=ListPages" >
<table id="listPagesContents">
<tbody id="page_list_tbody">
<tr><td>an item</td></tr>
</tbody>
</table>
</div>
</div>

<br>

</div>

<div class="column span-17 last">
Expand Down

0 comments on commit 0dd2453

Please sign in to comment.