pyc-website

main website for pyc inc.

git clone https://9o.is/git/pyc-website.git

NgAlert.scala

(1702B)


      1 package inc.pyc
      2 package snippet
      3 
      4 import xml._
      5 import net.liftweb._
      6 import util._
      7 import common._
      8 import json.JsonAST._
      9 
     10 /**
     11  * Builds Alert JSON messages for server-side responses.
     12  */
     13 object NgAlert extends Logger {
     14   
     15   val thumbsUp = <i class="fa-fw fa fa-thumbs-o-up"></i>
     16   val thumbsDown = <i class="fa-fw fa fa-thumbs-o-down"></i>
     17     
     18   private def msgBox(msgType: String, msg: NodeSeq, data: Option[JValue] = None): JValue = 
     19     JObject(List(
     20         JField("msg_type", JString(msgType)), 
     21         JField("msg", JString(msg.toString)),
     22         JField("data", data.getOrElse(JNull))))
     23   
     24   def success(msg: NodeSeq): JValue = msgBox("success", thumbsUp ++ msg)
     25   def success(msg: String): JValue = success(Text(msg))
     26   def success(data: JValue): JValue = msgBox("success", Text(""), Some(data))
     27   def success(msg: NodeSeq, data: JValue): JValue = msgBox("success", msg, Some(data))
     28   def success: JValue = success(Text(""))
     29   
     30   
     31   def danger(msg: NodeSeq, errors: List[FieldError]): JValue = {
     32     debug(errors)
     33     
     34     msgBox("danger", thumbsDown ++ msg ++ 
     35         errors.foldLeft(<ul></ul>)((el,err) => el.copy(child = el.child :+ <li>{" - "+err.msg}</li>)))
     36   }
     37   
     38   def danger(msg: String, errors: List[FieldError] = Nil): JValue = danger(Text(msg), errors)
     39   def danger(data: JValue): JValue = msgBox("danger", Text(""), Some(data))
     40   def danger: JValue = danger(Text(""), Nil)
     41   
     42   def info(msg: NodeSeq): JValue = msgBox("info", msg)
     43   def info(msg: String): JValue = info(Text(msg))
     44   def info: JValue = info(Text(""))
     45   
     46   def warning(msg: NodeSeq): JValue = msgBox("warning", msg)
     47   def warning(msg: String): JValue = warning(Text(msg))
     48   def warning: JValue = warning(Text(""))
     49 }