Permalink
Browse files

Added better support to History for anchor SPA integration

  • Loading branch information...
darkfrog26 committed Dec 5, 2018
1 parent dbed7e2 commit d3a30c45ebcd6e9bc382b86e464bd21d857595c1
Showing with 18 additions and 10 deletions.
  1. +18 −10 dom/src/main/scala/io/youi/History.scala
@@ -93,17 +93,25 @@ object History {
* Updates all anchors on the page to internal links to push history instead of loading another page. Essentially
* converts all links to be single-page-app compliant. May be run multiple times and will only change new links.
*/
def fixAnchors(): Unit = {
dom.byTag[html.Anchor]("a").foreach { anchor =>
if (Option(anchor.onclick).isEmpty && linkType(anchor.href) == LinkType.Internal) {
anchor.onclick = (evt: Event) => {
evt.preventDefault()
evt.stopPropagation()
push(URL(anchor.href))
}
}
def fixAnchors(): Unit = dom.byTag[html.Anchor]("a").foreach(fixAnchor)
def fixAnchor(anchor: html.Anchor): html.Anchor = {
if (Option(anchor.onclick).isEmpty && linkType(anchor.href) == LinkType.Internal) {
this.anchor(anchor, anchor.href)
} else {
anchor
}
}
def anchor(anchor: html.Anchor, location: String): html.Anchor = {
anchor.href = location
anchor.onclick = (evt: Event) => {
evt.preventDefault()
evt.stopPropagation()
push(URL(anchor.href))
}
anchor
}
def linkType(href: String): LinkType = if (href.trim.isEmpty) {

0 comments on commit d3a30c4

Please sign in to comment.