Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@PerWiklander
Created July 30, 2015 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PerWiklander/900e1f71a8478d7b4434 to your computer and use it in GitHub Desktop.
Save PerWiklander/900e1f71a8478d7b4434 to your computer and use it in GitHub Desktop.
package scalatags.Text
import scalatags.Text.Aggregate
import scalatags.Text.attrs._
trait ClassSet extends Aggregate {
final def compositeAttr[A](k: Attr, f: (A, List[A]) => A, e: => Modifier) = new CompositeAttr(k, f, e)
val classSwitch = compositeAttr[String](cls, (h, t) => (h::t).mkString(" ").trim, cls:="")
@inline final def classSet(ps: (String, Boolean)*): Modifier =
classSwitch(ps.map(p => if (p._2) Some(p._1) else None): _*)
@inline final def classSetS(cl: String*): Modifier =
classSet(cl.map((_, true)): _*)
@inline final def classSet1(a: String, ps: (String, Boolean)*): Modifier =
classSet((a, true) +: ps: _*)
@inline final def classSetM(ps: Map[String, Boolean]): Modifier =
classSet(ps.toSeq: _*)
@inline final def classSet1M(a: String, ps: Map[String, Boolean]): Modifier =
classSet1(a, ps.toSeq: _*)
final class CompositeAttr[A](k: Attr, f: (A, List[A]) => A, e: => Modifier) {
def apply(as: Option[A]*)(implicit ev: AttrValue[A]): Modifier =
as.toList.filter(_.isDefined).map(_.get) match {
case h :: t => k := f(h, t)
case Nil => e
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment