From b46d2f02db57cefe4bc1e420b53b2267c45cdb1a Mon Sep 17 00:00:00 2001 From: Dom Christie Date: Tue, 16 Jan 2018 17:06:22 +0000 Subject: [PATCH 1/5] Fix behavior when tapping on a same-page anchor link. Do not start a visit if proposed location is an anchor on the same-page. --- Turbolinks/WebView.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Turbolinks/WebView.js b/Turbolinks/WebView.js index 3e39cd6..8551015 100644 --- a/Turbolinks/WebView.js +++ b/Turbolinks/WebView.js @@ -51,10 +51,18 @@ } }, + locationIsSamePageAnchor: function (location) { + return location.anchor && location.requestURL == this.currentVisit.location.requestURL + }, + // Adapter interface visitProposedToLocationWithAction: function(location, action) { - this.postMessage("visitProposed", { location: location.absoluteURL, action: action }) + if (this.locationIsSamePageAnchor(location)) { + this.controller.scrollToAnchor(location.anchor) + } else { + this.postMessage("visitProposed", { location: location.absoluteURL, action: action }) + } }, visitStarted: function(visit) { From a599e94f7afca03b2af3fa88ddb2ffd4fe3bb8a9 Mon Sep 17 00:00:00 2001 From: Dom Christie Date: Wed, 17 Jan 2018 16:19:29 +0000 Subject: [PATCH 2/5] Demonstrate same-page anchor behaviour --- TurbolinksDemo/server/lib/turbolinks_demo/views/index.erb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/TurbolinksDemo/server/lib/turbolinks_demo/views/index.erb b/TurbolinksDemo/server/lib/turbolinks_demo/views/index.erb index 99f4e7d..0163d6c 100644 --- a/TurbolinksDemo/server/lib/turbolinks_demo/views/index.erb +++ b/TurbolinksDemo/server/lib/turbolinks_demo/views/index.erb @@ -6,6 +6,11 @@
  • Trigger an HTTP 404 to see how Turbolinks handles error responses.
  • Visit a protected area to see how to handle an unauthorized session using a separate web view.
  • Load a native view controller to see how to intercept the URL and handle it natively.
  • +
  • Scroll to an element on this page.
  • Follow an external link to open it in Safari.
  • Post a message from JavaScript to see how it can be handled natively.
  • + +
    + Same-page anchor. Back to top. +
    From d07e6250d2d81a8a42697da321cde06f4f3d6b9c Mon Sep 17 00:00:00 2001 From: Dom Christie Date: Wed, 17 Jan 2018 16:20:57 +0000 Subject: [PATCH 3/5] Use window.location to cover cases when currentVisit is undefined --- Turbolinks/WebView.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Turbolinks/WebView.js b/Turbolinks/WebView.js index 8551015..294e4bf 100644 --- a/Turbolinks/WebView.js +++ b/Turbolinks/WebView.js @@ -52,7 +52,7 @@ }, locationIsSamePageAnchor: function (location) { - return location.anchor && location.requestURL == this.currentVisit.location.requestURL + return location.anchor && location.requestURL == window.location.href.split('#')[0] }, // Adapter interface From f44b3ee4c3cbe0180d75b71ea8f2b5fec8ec38ac Mon Sep 17 00:00:00 2001 From: Dom Christie Date: Wed, 17 Jan 2018 16:25:37 +0000 Subject: [PATCH 4/5] Fix behaviour of empty anchors --- Turbolinks/WebView.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Turbolinks/WebView.js b/Turbolinks/WebView.js index 294e4bf..c3b40a6 100644 --- a/Turbolinks/WebView.js +++ b/Turbolinks/WebView.js @@ -52,7 +52,7 @@ }, locationIsSamePageAnchor: function (location) { - return location.anchor && location.requestURL == window.location.href.split('#')[0] + return location.anchor != null && location.requestURL == window.location.href.split('#')[0] }, // Adapter interface From a50049e0af349584b470780df445ff895b4ec2ff Mon Sep 17 00:00:00 2001 From: Dom Christie Date: Wed, 12 Sep 2018 22:10:15 +0100 Subject: [PATCH 5/5] Check same-page anchor using controller method for consistency --- Turbolinks/WebView.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Turbolinks/WebView.js b/Turbolinks/WebView.js index c3b40a6..caccd9b 100644 --- a/Turbolinks/WebView.js +++ b/Turbolinks/WebView.js @@ -51,14 +51,10 @@ } }, - locationIsSamePageAnchor: function (location) { - return location.anchor != null && location.requestURL == window.location.href.split('#')[0] - }, - // Adapter interface visitProposedToLocationWithAction: function(location, action) { - if (this.locationIsSamePageAnchor(location)) { + if (this.controller.locationIsSamePageAnchor(location)) { this.controller.scrollToAnchor(location.anchor) } else { this.postMessage("visitProposed", { location: location.absoluteURL, action: action })