# HG changeset patch # User Tomas Zeman # Date 1328863989 -3600 # Node ID 7be37d58997c72e183b28915d002342161080b7e # Parent 995184977e9b1864746cbd0281c6d4fab218f9f3 Generic entity snippet diff -r 995184977e9b -r 7be37d58997c src/main/scala/bootstrap/liftweb/Boot.scala --- a/src/main/scala/bootstrap/liftweb/Boot.scala Fri Feb 10 09:53:09 2012 +0100 +++ b/src/main/scala/bootstrap/liftweb/Boot.scala Fri Feb 10 09:53:09 2012 +0100 @@ -16,7 +16,7 @@ package bootstrap.liftweb import fis.base.model._ -import fis.crm.ui.ContactSnippet +import fis.crm.ui.{ContactSnippet, ContactSnippet2} import net.liftweb.common._ import net.liftweb.db.{DB, ConnectionIdentifier} import net.liftweb.http._ @@ -44,7 +44,7 @@ import Loc._ val menus = List(Menu("/", "FIS Main page") / "index" >> Hidden, - Menu.i("Home") / "" , ContactSnippet.menu) + Menu.i("Home") / "" , ContactSnippet.menu, ContactSnippet2.menu) LiftRules.setSiteMap(SiteMap(menus:_*)) } diff -r 995184977e9b -r 7be37d58997c src/main/scala/fis/base/ui/EntitySnippet.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/scala/fis/base/ui/EntitySnippet.scala Fri Feb 10 09:53:09 2012 +0100 @@ -0,0 +1,101 @@ +/* + * Copyright 2011 Tomas Zeman + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package fis.base.ui + +import net.liftweb.common._ +import net.liftweb.http._ +import net.liftweb.sitemap._ +import net.liftweb.util._ +import net.liftweb.util.Helpers._ +import net.tz.lift.snippet.{A, DataTable, LocTpl, TplPanel, SnippetHelpers} +import scala.xml.{Elem, NodeSeq, Text} + +trait EntityLoc[T] { +} + +case class ListEntity[T]() extends EntityLoc[T] +case class AddEntity[T]() extends EntityLoc[T] +case class ShowEntity[T](e: T) extends EntityLoc[T] +case class EditEntity[T](e: T) extends EntityLoc[T] +case class DeleteEntity[T](e: T) extends EntityLoc[T] + +trait AsEntity[T] { + def asEntity(id: String): Box[T] = asLong(id) flatMap { asEntity(_) } + def asEntity(id: Long): Box[T] +} + +trait EntityTemplate[T] { + def valueTemplate(l: EntityLoc[T]) = LocTpl(l match { + case ListEntity() => tplList + case ShowEntity(_) => tplShow + case AddEntity() => tplAdd + case EditEntity(_) => tplEdit + case DeleteEntity(_) => tplDelete + }) + + def tplList = "entity/list" + def tplShow = "entity/view" + def tplAdd = "entity/form" + def tplEdit = "entity/form" + def tplDelete = "entity/delete" +} + +import Loc._ + +trait Title[T] { + def titleList: NodeSeq + def titleAdd: NodeSeq + def titleShow(e: T): NodeSeq + def titleEdit(e: T): NodeSeq + def titleDelete(e: T): NodeSeq +} + +import net.liftweb.squerylrecord.KeyedRecord + +trait CrudMenu[T <: KeyedRecord[Long]] extends Title[T] with AsEntity[T] with + EntityTemplate[T] { + + def prefix: String + + def listPreMenu = Menu(prefix + ".list", titleList) / prefix + def listMenu = listPreMenu >> LocTpl(tplList) >> Hidden + def addPreMenu = Menu(prefix + ".add", titleAdd) / prefix / "add" + def addMenu = addPreMenu >> LocTpl(tplAdd) + def showPreMenu = Menu.param[T](prefix + ".show", LinkText(titleShow(_)), + asEntity, _.id.toString) / prefix / * + def showMenu = showPreMenu >> LocTpl(tplShow) + def editPreMenu = Menu.param[T](prefix + ".edit", LinkText(titleEdit(_)), + asEntity, _.id.toString) / prefix / * / "edit" + def editMenu = editPreMenu >> LocTpl(tplEdit) + def deletePreMenu = Menu.param[T](prefix + ".delete", LinkText(titleDelete(_)), + asEntity, _.id.toString) / prefix / * / "delete" + def deleteMenu = deletePreMenu >> LocTpl(tplDelete) +} + +trait EntitySnippet[T <: KeyedRecord[Long]] extends SnippetHelpers with + CrudMenu[T] { + + object cur extends RequestVar[EntityLoc[T]](ListEntity[T]()) + + def listsMenu = Menu(prefix + "s", titleList) / (prefix + "s") >> + EarlyResponse(() => Full(RedirectResponse("/" + prefix))) + + lazy val menu: Menu = listsMenu submenus( + listMenu, addMenu, showMenu, editMenu, deleteMenu + ) +} + +// vim: set ts=2 sw=2 et: diff -r 995184977e9b -r 7be37d58997c src/main/scala/fis/crm/ui/ContactSnippet.scala --- a/src/main/scala/fis/crm/ui/ContactSnippet.scala Fri Feb 10 09:53:09 2012 +0100 +++ b/src/main/scala/fis/crm/ui/ContactSnippet.scala Fri Feb 10 09:53:09 2012 +0100 @@ -158,4 +158,17 @@ } } +import fis.base.ui._ + +object ContactSnippet2 extends EntitySnippet[Contact] { + implicit def str2ns(s: String): NodeSeq = Text(s) + def prefix = "contact2" + def asEntity(id: Long): Box[Contact] = Contact.findByKey(id) + def titleList = "Contacts" + def titleAdd = "Create a contact" + def titleShow(c: Contact) = "Contact " + c.linkName + def titleEdit(c: Contact) = "Edit contact " + c.linkName + def titleDelete(c: Contact) = "Delete contact " + c.linkName +} + // vim: set ts=2 sw=2 et: