-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle RouterLink with fragment properly (#641)
RouterLinks with target inside page navigation don't go to server. RouterLinks with fragment trigger scroll to fragment and hashchangeevent. Does not handle initial page scroll to fragment, that will be handled by prerending. Fixes #339
- Loading branch information
Showing
6 changed files
with
446 additions
and
25 deletions.
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
hummingbird-client/src/main/java/com/vaadin/client/hummingbird/FragmentHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* Copyright 2000-2016 Vaadin Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package com.vaadin.client.hummingbird; | ||
|
||
import java.util.Objects; | ||
|
||
import com.google.web.bindery.event.shared.HandlerRegistration; | ||
import com.vaadin.client.Registry; | ||
import com.vaadin.client.communication.ResponseHandlingEndedEvent; | ||
|
||
import elemental.client.Browser; | ||
|
||
/** | ||
* Handler that makes sure that scroll to fragment and hash change event work | ||
* when there has been navigation via {@link RouterLinkHandler router link} to a | ||
* path with fragment. | ||
* <p> | ||
* This class will trigger scroll to fragment and hash change event once the | ||
* response from server has been processed, but only if the server did not | ||
* override the location. | ||
* | ||
* @author Vaadin Ltd | ||
*/ | ||
public class FragmentHandler { | ||
|
||
private final String previousHref; | ||
private final String newHref; | ||
|
||
private HandlerRegistration handlerRegistration; | ||
|
||
/** | ||
* Creates a new fragment handler for the given locations. | ||
* | ||
* @param previousHref | ||
* the href before the navigation | ||
* @param newHref | ||
* the href being navigated into | ||
*/ | ||
public FragmentHandler(String previousHref, String newHref) { | ||
assert previousHref != null; | ||
assert newHref != null; | ||
|
||
this.previousHref = previousHref; | ||
this.newHref = newHref; | ||
} | ||
|
||
/** | ||
* Adds a request response tracker to the given registry for making sure the | ||
* fragment is handled correctly if the location has not been updated during | ||
* the response. | ||
* | ||
* @param registry | ||
* the registry to bind to | ||
*/ | ||
public void bind(Registry registry) { | ||
handlerRegistration = registry.getRequestResponseTracker() | ||
.addResponseHandlingEndedHandler(this::onResponseHandlingEnded); | ||
} | ||
|
||
private void onResponseHandlingEnded( | ||
ResponseHandlingEndedEvent responseHandlingEndedEvent) { | ||
assert handlerRegistration != null; | ||
|
||
String currentHref = Browser.getWindow().getLocation().getHref(); | ||
|
||
if (Objects.equals(currentHref, newHref)) { | ||
// trigger possible scroll to fragment identifier | ||
Browser.getWindow().getLocation().replace(newHref); | ||
// fire fragment change event | ||
fireHashChangeEvent(previousHref, newHref); | ||
} | ||
|
||
handlerRegistration.removeHandler(); | ||
} | ||
|
||
/* | ||
* This method is used instead because Elemental's | ||
* HashChangeEvent.initHashChange gives errors. | ||
*/ | ||
private static native void fireHashChangeEvent(String oldUrl, String newUrl) | ||
/*-{ | ||
var event = new HashChangeEvent('hashchange', { | ||
'view': window, | ||
'bubbles': true, | ||
'cancelable': false, | ||
'oldURL': oldUrl, | ||
'newURL': newUrl | ||
}); | ||
window.dispatchEvent(event); | ||
}-*/; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...ts/test-root-context/src/main/java/com/vaadin/hummingbird/uitest/ui/FragmentLinkView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package com.vaadin.hummingbird.uitest.ui; | ||
|
||
import com.vaadin.hummingbird.dom.Element; | ||
import com.vaadin.hummingbird.dom.ElementFactory; | ||
import com.vaadin.ui.History.HistoryStateChangeHandler; | ||
|
||
public class FragmentLinkView extends AbstractDivView { | ||
|
||
public FragmentLinkView() { | ||
Element bodyElement = getElement(); | ||
bodyElement.getStyle().set("margin", "1em"); | ||
|
||
Element scrollLocator = ElementFactory.createDiv() | ||
.setAttribute("id", "scrollLocator") | ||
.setTextContent("Scroll locator"); | ||
scrollLocator.getStyle().set("position", "fixed").set("top", "0") | ||
.set("right", "0"); | ||
|
||
Element placeholder = ElementFactory.createDiv("Hash Change Events") | ||
.setAttribute("id", "placeholder"); | ||
|
||
bodyElement.appendChild(scrollLocator, placeholder, new Element("p")); | ||
|
||
Element scrollToLink = ElementFactory.createRouterLink( | ||
"/view/com.vaadin.hummingbird.uitest.ui.FragmentLinkView#Scroll_Target", | ||
"Scroller link"); | ||
Element scrollToLink2 = ElementFactory.createRouterLink( | ||
"/view/com.vaadin.hummingbird.uitest.ui.FragmentLinkView#Scroll_Target2", | ||
"Scroller link 2"); | ||
Element scrollToLinkAnotherView = ElementFactory.createRouterLink( | ||
"/view/com.vaadin.hummingbird.uitest.ui.FragmentLinkView2#Scroll_Target", | ||
"Scroller link with different view"); | ||
Element linkThatIsOverridden = ElementFactory.createRouterLink( | ||
"./override#Scroll_Target", "Link that server overrides"); | ||
|
||
Element scrollTarget = ElementFactory.createHeading1("Scroll Target") | ||
.setAttribute("id", "Scroll_Target"); | ||
Element scrollTarget2 = ElementFactory.createHeading2("Scroll Target 2") | ||
.setAttribute("id", "Scroll_Target2"); | ||
|
||
bodyElement.appendChild(scrollToLink, new Element("p"), scrollToLink2, | ||
new Element("p"), scrollToLinkAnotherView, new Element("p"), | ||
linkThatIsOverridden, new Element("p"), createSpacer(), | ||
scrollTarget, createSpacer(), scrollTarget2, createSpacer()); | ||
|
||
} | ||
|
||
@Override | ||
protected void onAttach() { | ||
getUI().get().getPage() | ||
.executeJavaScript("var i = 0;" | ||
+ "window.addEventListener('hashchange', function(event) {" | ||
+ "var x = document.createElement('span');" | ||
+ "x.textContent = ' ' + i;" + "i++;" | ||
+ "x.class = 'hashchange';" | ||
+ "document.getElementById('placeholder').appendChild(x);}," | ||
+ " false);"); | ||
|
||
HistoryStateChangeHandler current = getUI().get().getPage().getHistory() | ||
.getHistoryStateChangeHandler(); | ||
getUI().get().getPage().getHistory() | ||
.setHistoryStateChangeHandler(event -> { | ||
if (event.getLocation().equals("override")) { | ||
event.getSource().replaceState(null, | ||
"overridden#Scroll_Target2"); | ||
} else { | ||
current.onHistoryStateChange(event); | ||
} | ||
}); | ||
} | ||
|
||
private Element createSpacer() { | ||
Element spacer = ElementFactory.createDiv().setTextContent("spacer"); | ||
spacer.getStyle().set("height", "1000px"); | ||
return spacer; | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
...s/test-root-context/src/main/java/com/vaadin/hummingbird/uitest/ui/FragmentLinkView2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.vaadin.hummingbird.uitest.ui; | ||
|
||
import com.vaadin.hummingbird.dom.Element; | ||
|
||
public class FragmentLinkView2 extends FragmentLinkView { | ||
|
||
public FragmentLinkView2() { | ||
getElement().insertChild(0, new Element("div").setTextContent("VIEW 2") | ||
.setAttribute("id", "view2")); | ||
} | ||
|
||
@Override | ||
protected void onAttach() { | ||
// do not call super onAttach since it adds a hashchangelistener | ||
} | ||
} |
Oops, something went wrong.