package com.campudus.scycle.dom

import com.campudus.scycle.Driver
import org.scalajs.dom._
import rx.{Ctx, Rx, Var}

class DomDriver(selector: String, input: Rx[Element])(implicit ctx: Ctx.Owner) extends Driver {

  println("initializing driver...")

  Rx {
    console.log("selecting again", document.querySelector("#app"))
    val container = document.querySelector(selector)
    val oldChild = container.firstChild
    val newChild = input()
    console.log("replacing", oldChild, "in", container, "with", newChild)

    container.replaceChild(newChild, oldChild)
  }

  def selectEvents(selector: String, event: String)(implicit ctx: Ctx.Owner): Rx[Event] = {
    val eventVar = Var[Event](null)
    document.addEventListener(event, (e: Event) => {
      console.log("got an event", e)
      if (e.srcElement == document.querySelector(selector)) {
        console.log("pushing it through!")
        eventVar() = e
      }
    })
    eventVar
  }

}


package com.campudus.scycle.dom

import com.campudus.scycle.Driver
import org.scalajs.dom._
import rx.{Ctx, Rx, Var}

class DomDriver(selector: String, input: Rx[Element])(implicit ctx: Ctx.Owner) extends Driver {

  println("initializing driver...")
  val container = document.querySelector(selector)

  Rx {
//    container.innerHTML = input().outerHTML
    container.parentNode.replaceChild(input(), container)
  }

  def selectEvents(selector: String, event: String)(implicit ctx: Ctx.Owner): Rx[Event] = {
    val eventVar = Var[Event](null)
    document.addEventListener(event, (e: Event) => {
      console.log("got an event", e)
      if (e.srcElement == document.querySelector(selector)) {
        console.log("pushing it through!")
        eventVar() = e
      }
    })
    eventVar
  }

}


--- 
+++ 
@@ -7,11 +7,15 @@
 class DomDriver(selector: String, input: Rx[Element])(implicit ctx: Ctx.Owner) extends Driver {
 
   println("initializing driver...")
-  val container = document.querySelector(selector)
 
   Rx {
-//    container.innerHTML = input().outerHTML
-    container.parentNode.replaceChild(input(), container)
+    console.log("selecting again", document.querySelector("#app"))
+    val container = document.querySelector(selector)
+    val oldChild = container.firstChild
+    val newChild = input()
+    console.log("replacing", oldChild, "in", container, "with", newChild)
+
+    container.replaceChild(newChild, oldChild)
   }
 
   def selectEvents(selector: String, event: String)(implicit ctx: Ctx.Owner): Rx[Event] = {
Replace children correctly - focus problem again

Signed-off-by: Joern Bernhardt <7b85a41a628204b76aba4326273a3ccc74bd009a@campudus.com>
