City UI
authorTomas Zeman <tzeman@volny.cz>
Fri, 20 Apr 2012 08:26:23 +0200
changeset 68 f8f8cac057a7
parent 67 a62b754b945a
child 69 b1dc0efd1303
City UI
src/main/scala/bootstrap/liftweb/Boot.scala
src/main/scala/fis/geo/ui/CitySnipppet.scala
src/main/webapp/templates-hidden/_resources.html
src/main/webapp/templates-hidden/_resources_cs.html
--- a/src/main/scala/bootstrap/liftweb/Boot.scala	Fri Apr 20 08:26:23 2012 +0200
+++ b/src/main/scala/bootstrap/liftweb/Boot.scala	Fri Apr 20 08:26:23 2012 +0200
@@ -19,7 +19,7 @@
 import fis.base.ui._
 import fis.aaa.ui.UserSnippet
 import fis.crm.ui.ContactSnippet
-import fis.geo.ui.CountrySnippet
+import fis.geo.ui.{CitySnippet, CountrySnippet}
 import fis.db.SquerylTxMgr
 import net.liftweb.common._
 import net.liftweb.http._
@@ -44,7 +44,8 @@
     val menus = List(Menu("/", "FIS Main page") / "index" >> Hidden,
       Menu.i("Home") / "" , ContactSnippet.menu,
       UserSnippet.menu,
-      CountrySnippet.menu)
+      CountrySnippet.menu,
+      CitySnippet.menu)
 
     LiftRules.setSiteMap(SiteMap(menus:_*))
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/scala/fis/geo/ui/CitySnipppet.scala	Fri Apr 20 08:26:23 2012 +0200
@@ -0,0 +1,124 @@
+/*
+ * Copyright 2012 Tomas Zeman <tzeman@volny.cz>
+ *
+ * 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.geo.ui
+
+import fis.base.ui._
+import fis.geo.model._
+import net.liftweb.common._
+import net.liftweb.http._
+import net.liftweb.sitemap._
+import net.liftweb.sitemap.Loc._
+import net.liftweb.util._
+import net.liftweb.util.Helpers._
+import net.tz.lift.model._
+import net.tz.lift.snippet._
+import scala.xml.{Elem, NodeSeq, Text}
+
+object CitySnippet extends CityCrud with EntitySnippet[City] {
+  val prefix = "city"
+
+  private val listPre = Menu("city.list", l10n("Cities")) / prefix >>
+    Title(_ => i18n("Cities")) >>
+    locTpl("entity/list") >> Snippet("list", list)
+
+  private val createPre = Menu("city.create", l10n("Create")) / prefix / ADD >>
+    Title(_ => i18n("Create city")) >>
+    locTpl("entity/form") >> Snippet("form", form) >> Hidden
+
+  private val viewPre = Menu.param[City]("city.view", l10n("City"), parse,
+    encode) / prefix / * >> Title(c => i18n("City %s", c.linkName)) >>
+    locTpl("entity/view") >> Snippet("panel", panel) >> Hidden
+
+  private val editPre = Menu.param[City]("city.edit", l10n("Edit"), parse,
+    encode) / prefix / * / EDIT >>
+    Title(c => i18n("Edit city %s", c.linkName)) >>
+    locTpl("entity/form") >> Snippet("form", form) >> Hidden
+
+  private val deletePre = Menu.param[City]("city.delete", l10n("Delete"),
+    parse, encode) / prefix / * / DELETE >>
+    Title(c => i18n("Delete city %s", c.linkName)) >>
+    locTpl("entity/delete") >> Snippet("form", deleteF) >> Hidden
+
+  private val listM = listPre >> SecNav(createPre).build
+  private val createM = createPre >> SecNav(listPre).build
+  private val viewM = viewPre >> (SecNav(editPre) + deletePre).build
+  private val editM = editPre >> SecNav(viewPre).build
+  private val deleteM = deletePre >> SecNav(viewPre).build
+
+  private lazy val viewLoc = viewM.toLoc
+  private lazy val editLoc = editM.toLoc
+  private lazy val deleteLoc = deleteM.toLoc
+
+  val menu = listM submenus(viewM, editM, createM, deleteM)
+
+  private def cur = viewLoc.currentValue or editLoc.currentValue or
+    deleteLoc.currentValue
+
+  private def list: CssTr = { _ => CityTable(City.cities()) }
+
+  private def panel: CssTr = "*" #> cur.map { c => ViewPanel(fields(c)) }
+
+  object url {
+    def view: City => Box[String] = (viewLoc.calcHref _) andThen (Box !! _)
+  }
+
+  private def fields(c: City) = List(c.name, c.country, c.note)
+
+  private object CityTable extends FieldTable[City] {
+    def fields(c: City) = EntityLink(c) ++ Seq(c.country, c.note)
+    def apply(l: Iterable[City]) = build(City, l)
+  }
+
+  private case class CityLink(c: City) extends EntityLink[City](c, url.view)
+
+  EntityLink.register[City](CityLink(_))
+
+  private object form extends HorizontalScreen with CancelButton with SaveButton {
+
+    private object city extends ScreenVar[City](City.createRecord)
+
+    override def screenFields: List[BaseField] = fields(city)
+
+    override def localSetup() {
+      cur.foreach(city(_))
+    }
+
+    def finish() { save(city) foreach { v =>
+      S notice l10n("City %s saved.", v.linkName)
+      S.redirectTo(viewLoc.calcHref(v))
+    }}
+  }
+
+  private object deleteF extends HorizontalScreen with CancelButton with
+    DeleteButton {
+
+    val confirm = field(l10n("Really delete this city?"), false)
+
+    def finish() {
+      for {
+        c <- deleteLoc.currentValue if confirm
+        r <- delete(c)
+        n <- r.box(c.linkName)
+      } {
+        S notice l10n("City %s deleted.", n)
+        S redirectTo listM.loc.calcDefaultHref
+      }
+    }
+  }
+
+}
+
+// vim: set ts=2 sw=2 et:
--- a/src/main/webapp/templates-hidden/_resources.html	Fri Apr 20 08:26:23 2012 +0200
+++ b/src/main/webapp/templates-hidden/_resources.html	Fri Apr 20 08:26:23 2012 +0200
@@ -79,6 +79,25 @@
   <res name="country.iso2" lang="en" default="true">ISO2</res>
   <res name="country.iso3" lang="en" default="true">ISO3</res>
 
+
+  <!-- city
+    default strings:
+      Cities
+      Create city
+      City
+      City %s
+      Edit city %s
+      Delete city %s
+      City %s saved.
+      Really delete this city?
+      City %s deleted.
+  -->
+  <!-- city fields -->
+  <res name="city.name" lang="en" default="true">Name</res>
+  <res name="city.note" lang="en" default="true">Note</res>
+  <res name="city.country" lang="en" default="true">Country</res>
+
+
 <!--
   vim: et sw=2 ts=2
 -->
--- a/src/main/webapp/templates-hidden/_resources_cs.html	Fri Apr 20 08:26:23 2012 +0200
+++ b/src/main/webapp/templates-hidden/_resources_cs.html	Fri Apr 20 08:26:23 2012 +0200
@@ -78,6 +78,23 @@
   <res name="country.iso3" lang="cs">ISO3 kód</res>
 
 
+  <!-- city -->
+  <res name="Cities" lang="cs">Města</res>
+  <res name="Create city" lang="cs">Vytvořit město</res>
+  <res name="City %s" lang="cs">Město %s</res>
+  <res name="City" lang="cs">Město</res>
+  <res name="Edit city %s" lang="cs">Upravit město %s</res>
+  <res name="Delete city %s" lang="cs">Smazat město %s</res>
+  <res name="City %s saved." lang="cs">Město %s uloženo.</res>
+  <res name="Really delete this city?" lang="cs">Skutečně smazat město?</res>
+  <res name="City %s deleted." lang="cs">Město %s smazáno.</res>
+
+  <!-- city fields -->
+  <res name="city.name" lang="en" default="true">Název</res>
+  <res name="city.note" lang="en" default="true">Poznámka</res>
+  <res name="city.country" lang="en" default="true">Stát</res>
+
+
 <!--
   vim: et sw=2 ts=2
 -->