Add path merger, and routers with parents
This commit is contained in:
parent
e3f195ca49
commit
0220140ae0
@ -7,6 +7,7 @@ import java.nio.{ByteBuffer, CharBuffer}
|
|||||||
import java.nio.charset.Charset
|
import java.nio.charset.Charset
|
||||||
import java.nio.charset.StandardCharsets.UTF_8
|
import java.nio.charset.StandardCharsets.UTF_8
|
||||||
|
|
||||||
|
|
||||||
/** Base class for path extractors. */
|
/** Base class for path extractors. */
|
||||||
trait Path {
|
trait Path {
|
||||||
def /(child: String) = new /(this, child)
|
def /(child: String) = new /(this, child)
|
||||||
@ -63,6 +64,9 @@ object Path {
|
|||||||
def apply(list: List[String]): Path =
|
def apply(list: List[String]): Path =
|
||||||
list.foldLeft(Root: Path)(_ / _)
|
list.foldLeft(Root: Path)(_ / _)
|
||||||
|
|
||||||
|
def apply(left: Path, right: Path): Path =
|
||||||
|
right.toList.foldLeft(left)(_ / _)
|
||||||
|
|
||||||
def unapplySeq(path: Path): Some[List[String]] =
|
def unapplySeq(path: Path): Some[List[String]] =
|
||||||
Some(path.toList)
|
Some(path.toList)
|
||||||
//
|
//
|
||||||
|
@ -3,7 +3,8 @@ package outwatch.router
|
|||||||
import cats.effect.LiftIO
|
import cats.effect.LiftIO
|
||||||
import monix.execution.Scheduler
|
import monix.execution.Scheduler
|
||||||
import org.scalajs.dom.window
|
import org.scalajs.dom.window
|
||||||
import outwatch.dom._, dsl._
|
import outwatch.dom._
|
||||||
|
import monix.reactive.Observable
|
||||||
import outwatch.util.Store
|
import outwatch.util.Store
|
||||||
|
|
||||||
sealed trait Action
|
sealed trait Action
|
||||||
@ -11,11 +12,10 @@ final case class Replace(path: Path) extends Action
|
|||||||
|
|
||||||
final case class RouterState[P](page: P)
|
final case class RouterState[P](page: P)
|
||||||
|
|
||||||
class AppRouter[F[_]: LiftIO, P](f: Path => P) {
|
class AppRouter[F[_]: LiftIO, P](root: Path, f: Path => P) {
|
||||||
def routerReducer(state: RouterState[P], action: Action): RouterState[P] = action match {
|
def routerReducer(state: RouterState[P], action: Action): RouterState[P] = action match {
|
||||||
case Replace(path) =>
|
case Replace(path) =>
|
||||||
println(s"Going to $path")
|
Path.unapplySeq(Path(root, path)).foreach(p => window.history.replaceState("", "", p.mkString("/")))
|
||||||
Path.unapplySeq(path).foreach(p => window.history.replaceState("", "", p.mkString("/")))
|
|
||||||
state.copy(page = f(path))
|
state.copy(page = f(path))
|
||||||
case _ => state
|
case _ => state
|
||||||
}
|
}
|
||||||
@ -30,11 +30,14 @@ class AppRouter[F[_]: LiftIO, P](f: Path => P) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
object AppRouter{
|
object AppRouter{
|
||||||
def render[P](resolver: RouterResolve[P])(implicit store: RouterStore[P]): VDomModifier =
|
def render[P](resolver: RouterResolve[P])(implicit store: RouterStore[P]): Observable[VDomModifier] =
|
||||||
div(store.map(state => resolver(state.page)))
|
store.map(state => resolver(state.page))
|
||||||
|
|
||||||
def create[F[_]: LiftIO, P](notFound: P)(f: PartialFunction[Path, P]): AppRouter[F, P] =
|
def create[F[_]: LiftIO, P](notFound: P)(f: PartialFunction[Path, P]): AppRouter[F, P] =
|
||||||
new AppRouter[F, P](f.lift.andThen(_.getOrElse(notFound)))
|
create[F, P](Root, notFound)(f)
|
||||||
|
|
||||||
|
def create[F[_]: LiftIO, P](parent: Path, notFound: P)(f: PartialFunction[Path, P]): AppRouter[F, P] =
|
||||||
|
new AppRouter[F, P](parent, f.lift.andThen(_.getOrElse(notFound)))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
object Version{
|
object Version{
|
||||||
val version = "0.0.2"
|
val version = "0.0.3"
|
||||||
val scalaVersion = "2.12.8"
|
val scalaVersion = "2.12.8"
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user