Permalink
Browse files

Updated History path methods to take a Path instead of String

Added URL.clearParams() as a convenience method to remove existing parameters
  • Loading branch information...
darkfrog26 committed Dec 3, 2018
1 parent ea8b32c commit ba280503558b411335a85138239ce7fb9785bfb9
@@ -56,6 +56,7 @@ case class URL(protocol: Protocol = Protocol.Http,
def paramList(key: String): List[String] = parameters.values(key)
def param(key: String): Option[String] = paramList(key).headOption
def clearParams(): URL = copy(parameters = Parameters.empty)
lazy val base: String = {
val b = new StringBuilder
@@ -1,6 +1,6 @@
package io.youi
import io.youi.net.{Protocol, URL}
import io.youi.net.{Parameters, Path, Protocol, URL}
import org.scalajs.dom._
import reactify.{Channel, Val, Var}
@@ -39,14 +39,24 @@ object History {
case StateType.Pop => back()
}
def setPath(path: String): Unit = set(url.withPath(path))
def setPath(path: Path, keepParams: Boolean = false): Unit = {
set(url.withPath(path) match {
case u if keepParams => u
case u => u.clearParams()
})
}
def set(url: URL): Unit = {
document.location.href = url.toString
stateChange := HistoryStateChange(url, StateType.Set, null)
}
def pushPath(path: String, state: js.Any = null): Unit = push(url.withPath(path), state)
def pushPath(path: Path, keepParams: Boolean = false, state: js.Any = null): Unit = {
push(url.withPath(path) match {
case u if keepParams => u
case u => u.clearParams()
}, state)
}
def push(url: URL, state: js.Any = null): Unit = if (alwaysReload()) {
set(url)
@@ -57,7 +67,12 @@ object History {
stateChange := HistoryStateChange(url, StateType.Push, state)
}
def replacePath(path: String, state: js.Any = null): Unit = replace(url.withPath(path), state)
def replacePath(path: Path, keepParams: Boolean = false, state: js.Any = null): Unit = {
replace(url.withPath(path) match {
case u if keepParams => u
case u => u.clearParams()
}, state)
}
def replace(url: URL, state: js.Any): Unit = if (alwaysReload()) {
set(url)
@@ -1,5 +1,6 @@
package io.youi.activate
import io.youi.net.Path
import io.youi.{History, dom}
import org.scalajs.dom.{Event, document, html, window}
@@ -168,7 +169,7 @@ class OnUnchecked(selector: String, instruction: ActivateInstruction) extends Ac
override def deactivate(): Unit = dom.bySelector[html.Element](selector).head.removeEventListener("change", listener)
}
class Link(path: String) extends ActivateInstruction {
class Link(path: Path) extends ActivateInstruction {
override def activate(): Unit = History.pushPath(path)
override def deactivate(): Unit = {}
@@ -1,6 +1,7 @@
package io.youi.activate
import io.youi.dom._
import io.youi.net.Path
import org.scalajs.dom._
/**
@@ -95,7 +96,7 @@ object ActivationSupport {
case ReplaceAttributeRegex(selector, attributeName, content) => Some(new ReplaceAttributeInstruction(selector, attributeName, content))
case AlertRegex(message) => Some(new AlertInstruction(message))
case CallRegex(code) => Some(new Call(code))
case LinkRegex(path) => Some(new Link(path))
case LinkRegex(path) => Some(new Link(Path.parse(path)))
case OnClickRegex(selector, instruction) => {
val i = parseInstruction(testing, instruction).getOrElse(throw new RuntimeException(s"Failed to parse instruction: [$instruction]"))
Some(new OnClick(selector, i))

0 comments on commit ba28050

Please sign in to comment.