blob: 76d05eb706098be37c8cd462cf4c730f0f057e15 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
package reading.utils
import scala.util.{ Failure, Success }
import org.scalajs.dom.Element
import scalatags.JsDom.all._
import rx._
import Ctx.Owner.Unsafe._
object RxTag {
def apply(r: Ctx.Data => HtmlTag): HtmlTag =
rxMod(Rx(r(implicitly[Ctx.Data])))
private def rxMod(r: Rx[HtmlTag]): HtmlTag = {
def rSafe = r.toTry match {
case Success(v) => v.render
case Failure(e) => span(e.toString, backgroundColor := "red").render
}
var last = rSafe
r.trigger {
val newLast = rSafe
Option(last.parentElement).foreach {
_.replaceChild(newLast, last)
}
last = newLast
}
span(
bindNode(last)
)
}
}
object RxAttr {
def apply[Builder, T: AttrValue](attr: scalatags.generic.Attr, v: Rx[T]) = {
val attrValue = new AttrValue[Rx[T]] {
def apply(t: Element, a: Attr, r: Rx[T]): Unit = {
val _ = r.trigger { implicitly[AttrValue[T]].apply(t, a, r.now) }
}
}
scalatags.generic.AttrPair(attr, v, attrValue)
}
}
|