liftweb-uirouter

angularjs ui-router module for scala liftweb framework

git clone https://9o.is/git/liftweb-uirouter.git

UiSurround.scala

(1461B)


      1 package net.liftmodules.uirouter
      2 package snippet
      3 
      4 import xml._
      5 import net.liftweb._
      6 import http._
      7 import util._
      8 import Helpers._
      9 
     10 trait UiSurround extends SnippetHelper {
     11 
     12   /**
     13    * Conditional Template Surround Snippet. Allows pages to be surrounded by a 
     14    * different template if it's accessed with ajax.
     15    * 
     16    * Needed attributes are:
     17    * - with: template name to surround page.
     18    * - at: id of element in template to place the page.
     19    * - withAjax: template name to surround page when accessed with ajax.
     20    * 
     21    * <div data-lift="UiRouter.surround?withAjax=no-base&with=base-wrap&at=content">
     22    * 
     23    * When accessing with ajax, assure URL query parameter 'ajax' exists.
     24    * 
     25    * NOTE: default.html in templates-hidden cannot exist 
     26    * (else Lift will auto-surround everything with default)
     27    */
     28   def surround(ns: NodeSeq): NodeSeq = 
     29     for {
     30       surroundWith <- S.attr("with") ?~ "Surround with not specified"
     31       surroundWithAjax <- S.attr("withAjax") ?~ "Surround with ajax not specified"
     32       at <- S.attr("at") ?~ "Surround at not specified"
     33     } yield {
     34       if(S.param("ajax").isDefined)
     35         Templates("templates-hidden" :: surroundWithAjax :: Nil) map {
     36           s"#$at" #> ns
     37         } openOr Text("Template '"+surroundWithAjax+"' not found")
     38       else
     39         Templates("templates-hidden" :: surroundWith :: Nil) map {
     40           s"#$at" #> ns
     41         } openOr Text("Template '"+surroundWith+"' not found") 
     42     }
     43 }