Skip to content

Commit

Permalink
Swapped the custom Loc sample over to CSS selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
timperrett committed Aug 24, 2011
1 parent 856b57c commit 8d4bd69
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 119 deletions.
75 changes: 37 additions & 38 deletions chapter-7/src/main/scala/sample/lib/Wiki.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import scala.xml.{Text, NodeSeq}
import net.liftweb.common.{Box,Full,Empty,Failure}
import net.liftweb.util.NamedPF
import net.liftweb.util.Helpers._
import net.liftweb.http.{SHtml,RewriteRequest,RewriteResponse,ParsePath}
import net.liftweb.http.{SHtml,S,RewriteRequest,RewriteResponse,ParsePath}
import net.liftweb.sitemap.Loc
import net.liftweb.mapper.{OrderBy,Ascending,By}
import net.liftweb.textile.TextileParser
Expand All @@ -18,30 +18,30 @@ import sample.model.WikiEntry
* @param page - the name of the page
* @param edit - are we viewing or editing the page?
*/
case class WikiLoc(page: String, edit: Boolean) {
case class Page(title: String, edit: Boolean) {

/**
* Get the underly database record for this page.
* When requested, the page will go to the database and look
* for an entry who's name matches that passed as the
* paramerter to the WikiLoc instance.
* paramerter to the Page instance.
*/
lazy val record: WikiEntry =
WikiEntry.find(By(WikiEntry.name, page)) openOr WikiEntry.create.name(page)
lazy val data: WikiEntry =
WikiEntry.find(By(WikiEntry.name, title)) openOr WikiEntry.create.name(title)
}

/**
* The WikiStuff object that provides menu, URL rewriting,
* and snippet support for the page that displays wiki contents
*/
object Wiki extends Loc[WikiLoc] {
object AllLoc extends WikiLoc("all", false)
object Wiki extends Loc[Page] {
object AllLoc extends Page("all", false)

// the name of the page
def name = "wiki"

// the default parameters (used for generating the menu listing)
def defaultValue = Full(WikiLoc("HomePage", false))
def defaultValue = Full(Page("HomePage", false))

def params = Nil

Expand All @@ -59,37 +59,37 @@ object Wiki extends Loc[WikiLoc] {
*/
override val snippets: SnippetTest = {
case ("wiki", Full(AllLoc)) => showAll _
case ("wiki", Full(wp @ WikiLoc(_ , true))) => editRecord(wp.record) _
case ("wiki", Full(wp @ WikiLoc(_ , false))) if !wp.record.saved_? => editRecord(wp.record) _
case ("wiki", Full(wp: WikiLoc)) => displayRecord(wp.record) _
case ("wiki", Full(wp @ Page(_ , true))) => editRecord(wp.data)
case ("wiki", Full(wp @ Page(_ , false))) if !wp.data.saved_? => editRecord(wp.data)
case ("wiki", Full(wp: Page)) => displayRecord(wp.data)
}

/**
* Generate a link based on the current page
*/
val link = new Loc.Link[WikiLoc](List("wiki"), false){
override def createLink(in: WikiLoc) =
if(in.edit) Full(Text("/wiki/edit/"+urlEncode(in.page)))
else Full(Text("/wiki/"+urlEncode(in.page)))
val link = new Loc.Link[Page](List("wiki"), false){
override def createLink(in: Page) =
if(in.edit) Full(Text("/wiki/edit/"+urlEncode(in.title)))
else Full(Text("/wiki/"+urlEncode(in.title)))
}

/**
* What's the text of the link?
*/
val text = new Loc.LinkText(calcLinkText _)
def calcLinkText(in: WikiLoc): NodeSeq =
if(in.edit) Text("Wiki edit "+in.page) else Text("Wiki "+in.page)
def calcLinkText(in: Page): NodeSeq =
if(in.edit) Text("Wiki edit "+in.title) else Text("Wiki "+in.title)

/**
* Rewrite the request so that we a) get friendly URLs and
* b) get the appropriate page and context (edit or view) params
* in order to pass them to the WikiLoc
* in order to pass them to the Page
*/
override val rewrite: LocRewrite = Full(NamedPF("Wiki Rewrite"){
case RewriteRequest(ParsePath("wiki" :: "edit" :: page :: Nil, _, _,_),_, _) =>
(RewriteResponse("wiki" :: Nil), WikiLoc(page, true))
case RewriteRequest(ParsePath("wiki" :: page :: Nil, _, _,_),_,_) =>
(RewriteResponse("wiki" :: Nil), WikiLoc(page, false))
case RewriteRequest(ParsePath("wiki" :: "edit" :: title :: Nil, _, _,_),_, _) =>
(RewriteResponse("wiki" :: Nil), Page(title, true))
case RewriteRequest(ParsePath("wiki" :: title :: Nil, _, _,_),_,_) =>
(RewriteResponse("wiki" :: Nil), Page(title, false))
})

/**
Expand All @@ -100,37 +100,36 @@ object Wiki extends Loc[WikiLoc] {


/**
* Make the URL to be accessed based up the page in the WikiLoc.
* Make the URL to be accessed based up the page in the Page.
* The createLink method already ensures that spaces etc are encoded
*/
def url(page: String) = createLink(WikiLoc(page, false))
def url(title: String) = createLink(Page(title, false))

/**
* The render bind for actually displaying the wiki contents. This will
* look just like what you are familiar with for creating snippets
*/
def editRecord(r: WikiEntry)(xhtml: NodeSeq): NodeSeq = {
def editRecord(r: WikiEntry) = {
val isNew = !r.saved_?
val pageName = r.name.is
<form action={url(pageName)} method="post">{
bind("e", chooseTemplate("display","edit", xhtml),
"message" -> (if(isNew) Text("Create Entry named '"+pageName+"'")
else Text("Edit entry named '"+pageName+"'")),
"cancel" -> <a href={url(pageName)}>Cancel</a>,
"textarea" -> r.entry.toForm,
"submit" -> SHtml.submit(isNew ? "Add" | "Save", () => r.save)
)}</form>
"a [href]" #> url(pageName) &
"form [action]" #> url(pageName) &
"textarea" #> r.entry.toForm &
"type=submit" #> SHtml.submit(isNew ? "Add" | "Save", () => r.save) &
"message" #>
(if(isNew) Text("Create Entry named '"+pageName+"'")
else Text("Edit entry named '"+pageName+"'")) &
"edit ^*" #> NodeSeq.Empty
}

/**
* Pretty much the same as the editRecord method, but obviously
* minus the err, editing.
*/
def displayRecord(entry: WikiEntry)(xhtml: NodeSeq): NodeSeq =
bind("v",chooseTemplate("display","view", xhtml),
"content" -> TextileParser.toHtml(entry.entry, textileWriter),
"edit" -> <a href={createLink(WikiLoc(entry.name, true))}>Edit</a>
)
def displayRecord(entry: WikiEntry) =
"content" #> TextileParser.toHtml(entry.entry, textileWriter) &
"a [href]" #> createLink(Page(entry.name, true)) &
"view ^*" #> NodeSeq.Empty

private val textileWriter = Some((info: WikiURLInfo) =>
info match {
Expand Down
22 changes: 12 additions & 10 deletions chapter-7/src/main/webapp/wiki.html
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
<lift:surround with="default" at="content">
<p><a href="/wiki/all">Show All Pages</a></p>

<lift:wiki>
<p><a href="/wiki/all">Show All Pages</a></p>

<!-- Contents to display for edit page -->
<display:edit>
<p><e:message /><br /><e:textarea /></p>
<p><e:cancel /> <e:submit /></p>
</display:edit>
<edit>
<form action="" method="post">
<p><message /><br /><textarea rows="8" cols="40"></textarea></p>
<p><a href="">Cancel</a> <input type="submit" /></p>
</form>
</edit>

<!-- Content to display for view page -->
<display:view>
<v:content />
<p><v:edit /></p>
</display:view>
<view>
<content />
<p><a href="">Edit</a></p>
</view>

</lift:wiki>
</lift:surround>
82 changes: 11 additions & 71 deletions lift-in-action.tmproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<plist version="1.0">
<dict>
<key>currentDocument</key>
<string>project/plugins/Plugins.scala</string>
<string>chapter-14/src/main/scala/sample/lib/DependencyInjection.scala</string>
<key>documents</key>
<array>
<dict>
Expand All @@ -21,104 +21,44 @@
<integer>449</integer>
<key>metaData</key>
<dict>
<key>chapter-5/src/main/scala/example/travel/lib/PaypalHandler.scala</key>
<key>chapter-14/src/main/scala/sample/lib/DependencyInjection.scala</key>
<dict>
<key>caret</key>
<dict>
<key>column</key>
<integer>10</integer>
<key>line</key>
<integer>42</integer>
</dict>
<key>firstVisibleColumn</key>
<integer>0</integer>
<key>firstVisibleLine</key>
<integer>8</integer>
</dict>
<key>chapter-5/src/main/scala/example/travel/snippet/OrderSummary.scala</key>
<dict>
<key>caret</key>
<dict>
<key>column</key>
<integer>33</integer>
<key>line</key>
<integer>20</integer>
</dict>
<key>firstVisibleColumn</key>
<integer>0</integer>
<key>firstVisibleLine</key>
<integer>7</integer>
</dict>
<key>chapter-5/src/main/webapp/summary.html</key>
<dict>
<key>caret</key>
<dict>
<key>column</key>
<integer>51</integer>
<key>line</key>
<integer>27</integer>
</dict>
<key>firstVisibleColumn</key>
<integer>0</integer>
<key>firstVisibleLine</key>
<integer>9</integer>
</dict>
<key>chapter-5/src/main/webapp/templates-hidden/_basket.html</key>
<dict>
<key>caret</key>
<dict>
<key>column</key>
<integer>4</integer>
<integer>16</integer>
<key>line</key>
<integer>4</integer>
<integer>32</integer>
</dict>
<key>columnSelection</key>
<false/>
<key>firstVisibleColumn</key>
<integer>0</integer>
<key>firstVisibleLine</key>
<integer>0</integer>
<integer>19</integer>
<key>selectFrom</key>
<dict>
<key>column</key>
<integer>24</integer>
<integer>10</integer>
<key>line</key>
<integer>17</integer>
<integer>32</integer>
</dict>
<key>selectTo</key>
<dict>
<key>column</key>
<integer>4</integer>
<key>line</key>
<integer>4</integer>
</dict>
</dict>
<key>project/plugins/Plugins.scala</key>
<dict>
<key>caret</key>
<dict>
<key>column</key>
<integer>0</integer>
<integer>20</integer>
<key>line</key>
<integer>12</integer>
<integer>32</integer>
</dict>
<key>firstVisibleColumn</key>
<integer>0</integer>
<key>firstVisibleLine</key>
<integer>0</integer>
</dict>
</dict>
<key>openDocuments</key>
<array>
<string>chapter-5/src/main/scala/example/travel/snippet/OrderSummary.scala</string>
<string>chapter-5/src/main/webapp/templates-hidden/_basket.html</string>
<string>chapter-5/src/main/webapp/summary.html</string>
<string>chapter-5/src/main/scala/example/travel/lib/PaypalHandler.scala</string>
<string>project/plugins/Plugins.scala</string>
<string>chapter-14/src/main/scala/sample/lib/DependencyInjection.scala</string>
</array>
<key>showFileHierarchyDrawer</key>
<false/>
<key>windowFrame</key>
<string>{{176, 116}, {1240, 622}}</string>
<string>{{200, 67}, {1240, 622}}</string>
</dict>
</plist>

0 comments on commit 8d4bd69

Please sign in to comment.