Secondary navigation
authorTomas Zeman <tzeman@volny.cz>
Thu, 12 Apr 2012 10:52:42 +0200
changeset 49 c24112f0f084
parent 48 366e1f8d93bb
child 50 d67905a2c39d
Secondary navigation
src/main/scala/bootstrap/liftweb/Boot.scala
src/main/scala/fis/base/ui/SecNav.scala
src/main/webapp/css/base.css
src/main/webapp/templates-hidden/default.html
src/main/webapp/templates-hidden/secnav.html
--- a/src/main/scala/bootstrap/liftweb/Boot.scala	Thu Apr 12 10:52:42 2012 +0200
+++ b/src/main/scala/bootstrap/liftweb/Boot.scala	Thu Apr 12 10:52:42 2012 +0200
@@ -16,6 +16,7 @@
 package bootstrap.liftweb
 
 import fis.base.model._
+import fis.base.ui._
 import fis.crm.ui.ContactSnippet
 import fis.db.SquerylTxMgr
 import net.liftweb.common._
@@ -36,6 +37,8 @@
 
     import Loc._
 
+    SecNav.init()
+
     val menus = List(Menu("/", "FIS Main page") / "index" >> Hidden,
       Menu.i("Home") / "" , ContactSnippet.menu)
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/scala/fis/base/ui/SecNav.scala	Thu Apr 12 10:52:42 2012 +0200
@@ -0,0 +1,87 @@
+/*
+ * 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.base.ui
+
+import net.liftweb.common._
+import net.liftweb.http._
+import net.liftweb.sitemap._
+import net.liftweb.sitemap.Loc._
+import net.liftweb.sitemap.Menu._
+import net.liftweb.util._
+import net.liftweb.util.Helpers._
+import net.tz.lift.snippet._
+import scala.xml.NodeSeq
+
+/**
+ * Secondary navigation snippet.
+ */
+object SecNav extends SecNav(Nil) {
+
+  def init() {
+    LiftRules.snippets.append {
+      case List("secnav") => ClearNodes
+    }
+  }
+
+  val TAG = "secnav"
+
+  def apply[T](m: Menuable): SecNav = this + m
+  def apply[T](p: ParamMenuable[T]): SecNav = this + p
+}
+
+class SecNav(val navs: Seq[SecNavItem]) {
+
+  def + (i: SecNavItem): SecNav = new SecNav(navs :+ i)
+
+  def + (m: Menuable): SecNav = this + (new SecNavMenu(m))
+
+  def +[T] (p: ParamMenuable[T]): SecNav = this + (new SecNavLoc(p))
+
+  def toCssTr: CssTr = ".items" #> (navs flatMap { _.toHtml })
+
+  def build = Snippet(SecNav.TAG, toCssTr)
+}
+
+sealed abstract class SecNavItem {
+
+  def toHtml: NodeSeq
+
+  protected def item(href: String, cnt: NodeSeq) =
+    <li><a href={href}>{cnt}</a></li>
+}
+
+class SecNavMenu(m: Menuable) extends SecNavItem {
+
+  def toHtml = (for {
+    l <- SiteMap.findAndTestLoc(m.name)
+  } yield {
+    item(l.calcDefaultHref, m.linkText.text())
+  }) toSeq
+}
+
+class SecNavLoc[T](p: ParamMenuable[T]) extends SecNavItem {
+
+  def toHtml = (for {
+    cur <- S.location
+    value <- cur.currentValue
+    l <- SiteMap.findAndTestLoc(p.name)
+    p <- tryo { (l.asInstanceOf[Loc[T]], value.asInstanceOf[T]) }
+  } yield {
+    item(p._1.calcHref(p._2), p._1.linkText(p._2))
+  }) toSeq
+}
+
+// vim: set ts=2 sw=2 et:
--- a/src/main/webapp/css/base.css	Thu Apr 12 10:52:42 2012 +0200
+++ b/src/main/webapp/css/base.css	Thu Apr 12 10:52:42 2012 +0200
@@ -56,6 +56,11 @@
   font-weight: bold;
 }
 
+#secnav li {
+  float: left;
+  padding: 0px 10px 10px 0;
+}
+
 /* fix for active sf-menu item in span tag */
 .sf-menu span {
   padding: 0.75em 1em;
--- a/src/main/webapp/templates-hidden/default.html	Thu Apr 12 10:52:42 2012 +0200
+++ b/src/main/webapp/templates-hidden/default.html	Thu Apr 12 10:52:42 2012 +0200
@@ -33,15 +33,13 @@
           </div>
         </div>
       </div>
-      <div id="secnav" class="row">
-        <ul>
-          <span class="lift:secnav"></span>
-        </ul>
-      </div>
-      <div class="lift:msgs?showAll=false row">
-        <lift:error_class>alert-error</lift:error_class>
-        <lift:warning_class>alert</lift:warning_class>
-        <lift:notice_class>alert-success</lift:notice_class>
+      <span class="lift:embed?what=/templates-hidden/secnav"></span>
+      <div class="row span11">
+        <span class="lift:msgs?showAll=false">
+          <lift:error_class>alert-error</lift:error_class>
+          <lift:warning_class>alert</lift:warning_class>
+          <lift:notice_class>alert-success</lift:notice_class>
+        </span>
       </div>
       <div class="row">
         <div class="span12">
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/webapp/templates-hidden/secnav.html	Thu Apr 12 10:52:42 2012 +0200
@@ -0,0 +1,7 @@
+<span class="lift:secnav">
+  <div id="secnav" class="row span10">
+    <ul class="nav">
+      <span class="items"></span>
+    </ul>
+  </div>
+</span>