Skip to content

Commit 5734f85

Browse files
authored
feat: added a commonly needed helper method to get current route target (#15709)
1 parent 23c57c8 commit 5734f85

3 files changed

Lines changed: 66 additions & 20 deletions

File tree

‎flow-server/src/main/java/com/vaadin/flow/component/UI.java‎

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,53 +28,53 @@
2828
import java.util.regex.Pattern;
2929
import java.util.stream.Stream;
3030

31-
import com.vaadin.flow.component.dependency.JsModule;
32-
import com.vaadin.flow.component.internal.AllowInert;
33-
import com.vaadin.flow.component.internal.JavaScriptNavigationStateRenderer;
34-
import com.vaadin.flow.component.page.History;
35-
import com.vaadin.flow.i18n.LocaleChangeEvent;
36-
import com.vaadin.flow.internal.nodefeature.NodeProperties;
37-
import com.vaadin.flow.router.ErrorNavigationEvent;
38-
import com.vaadin.flow.router.ErrorParameter;
39-
import com.vaadin.flow.router.NavigationEvent;
40-
import com.vaadin.flow.router.NavigationState;
41-
import com.vaadin.flow.router.NavigationStateBuilder;
42-
import com.vaadin.flow.router.NotFoundException;
43-
import com.vaadin.flow.router.RouteNotFoundError;
44-
import com.vaadin.flow.router.RouteParameters;
45-
import com.vaadin.flow.router.internal.ErrorStateRenderer;
46-
import com.vaadin.flow.router.internal.ErrorTargetEntry;
47-
import com.vaadin.flow.router.internal.HasUrlParameterFormat;
4831
import org.slf4j.Logger;
4932
import org.slf4j.LoggerFactory;
5033

34+
import com.vaadin.flow.component.dependency.JsModule;
35+
import com.vaadin.flow.component.internal.AllowInert;
36+
import com.vaadin.flow.component.internal.JavaScriptNavigationStateRenderer;
5137
import com.vaadin.flow.component.internal.UIInternalUpdater;
5238
import com.vaadin.flow.component.internal.UIInternals;
39+
import com.vaadin.flow.component.page.History;
5340
import com.vaadin.flow.component.page.LoadingIndicatorConfiguration;
5441
import com.vaadin.flow.component.page.Page;
5542
import com.vaadin.flow.dom.Element;
5643
import com.vaadin.flow.function.SerializableConsumer;
5744
import com.vaadin.flow.function.SerializableRunnable;
5845
import com.vaadin.flow.i18n.I18NProvider;
46+
import com.vaadin.flow.i18n.LocaleChangeEvent;
5947
import com.vaadin.flow.internal.CurrentInstance;
6048
import com.vaadin.flow.internal.ExecutionContext;
6149
import com.vaadin.flow.internal.StateNode;
6250
import com.vaadin.flow.internal.StateTree.ExecutionRegistration;
6351
import com.vaadin.flow.internal.nodefeature.ElementData;
6452
import com.vaadin.flow.internal.nodefeature.LoadingIndicatorConfigurationMap;
53+
import com.vaadin.flow.internal.nodefeature.NodeProperties;
6554
import com.vaadin.flow.internal.nodefeature.PollConfigurationMap;
6655
import com.vaadin.flow.internal.nodefeature.ReconnectDialogConfigurationMap;
6756
import com.vaadin.flow.router.AfterNavigationListener;
6857
import com.vaadin.flow.router.BeforeEnterListener;
6958
import com.vaadin.flow.router.BeforeLeaveListener;
59+
import com.vaadin.flow.router.ErrorNavigationEvent;
60+
import com.vaadin.flow.router.ErrorParameter;
7061
import com.vaadin.flow.router.EventUtil;
7162
import com.vaadin.flow.router.HasUrlParameter;
7263
import com.vaadin.flow.router.Location;
64+
import com.vaadin.flow.router.NavigationEvent;
65+
import com.vaadin.flow.router.NavigationState;
66+
import com.vaadin.flow.router.NavigationStateBuilder;
7367
import com.vaadin.flow.router.NavigationTrigger;
68+
import com.vaadin.flow.router.NotFoundException;
7469
import com.vaadin.flow.router.QueryParameters;
7570
import com.vaadin.flow.router.RouteConfiguration;
71+
import com.vaadin.flow.router.RouteNotFoundError;
72+
import com.vaadin.flow.router.RouteParameters;
7673
import com.vaadin.flow.router.Router;
7774
import com.vaadin.flow.router.RouterLayout;
75+
import com.vaadin.flow.router.internal.ErrorStateRenderer;
76+
import com.vaadin.flow.router.internal.ErrorTargetEntry;
77+
import com.vaadin.flow.router.internal.HasUrlParameterFormat;
7878
import com.vaadin.flow.router.internal.PathUtil;
7979
import com.vaadin.flow.server.Command;
8080
import com.vaadin.flow.server.ErrorEvent;
@@ -1195,6 +1195,30 @@ public boolean isNavigationSupported() {
11951195
return true;
11961196
}
11971197

1198+
/**
1199+
* Returns the currently active route aka navigation target shown in this
1200+
* UI.
1201+
* <p>
1202+
* Note, that certain UIs, like embedded apps, don't support routing and for
1203+
* those an exception will be thrown.
1204+
* <p>
1205+
* Also, the current route might not be initialized if this method is called
1206+
* while still building the view chain, for example in the constructor of
1207+
* layouts. Thus, consider postponing the usage of this method to for
1208+
* example AfterNavigationEvent.
1209+
*
1210+
* @return the currently active route instance if available
1211+
* @throws IllegalStateException
1212+
* if current view is not yet available
1213+
*/
1214+
public Component getCurrentView() {
1215+
if (getInternals().getActiveRouterTargetsChain().isEmpty()) {
1216+
throw new IllegalStateException(
1217+
"Routing is not in use or not yet initialized. If you are not using embedded UI, try postponing the call to an onAttach method or to an AfterNavigationEvent listener.");
1218+
}
1219+
return (Component) getInternals().getActiveRouterTargetsChain().get(0);
1220+
}
1221+
11981222
/**
11991223
* Gets the router used for navigating in this UI.
12001224
*

‎flow-server/src/test/java/com/vaadin/flow/component/UITest.java‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,8 @@ public void locationAfterServerNavigation()
300300
List<HasElement> chain = ui.getInternals()
301301
.getActiveRouterTargetsChain();
302302
Assert.assertEquals(1, chain.size());
303-
MatcherAssert.assertThat(chain.get(0),
303+
Component currentRoute = ui.getCurrentView();
304+
MatcherAssert.assertThat(currentRoute,
304305
CoreMatchers.instanceOf(FooBarNavigationTarget.class));
305306
}
306307

@@ -1491,6 +1492,28 @@ public void threeModalComponents_topComponentRemoved_onlyTopMostNotInert() {
14911492
verifyInert(secondModal, false);
14921493
}
14931494

1495+
@Test
1496+
public void getCurrentView_routingInitialized_getsCurrentRouteComponent()
1497+
throws InvalidRouteConfigurationException {
1498+
UI ui = new UI();
1499+
initUI(ui, "", null);
1500+
Component currentRoute = ui.getCurrentView();
1501+
MatcherAssert.assertThat(currentRoute,
1502+
CoreMatchers.instanceOf(RootNavigationTarget.class));
1503+
1504+
ui.navigate("foo/bar");
1505+
currentRoute = ui.getCurrentView();
1506+
MatcherAssert.assertThat(currentRoute,
1507+
CoreMatchers.instanceOf(FooBarNavigationTarget.class));
1508+
}
1509+
1510+
@Test
1511+
public void getCurrentView_routingNotInitialized_throws()
1512+
throws InvalidRouteConfigurationException {
1513+
UI ui = new UI();
1514+
Assert.assertThrows(IllegalStateException.class, ui::getCurrentView);
1515+
}
1516+
14941517
private void verifyInert(Component component, boolean inert) {
14951518
Assert.assertEquals("Invalid inert state", inert,
14961519
component.getElement().getNode().isInert());

‎flow-server/src/test/java/com/vaadin/flow/router/internal/NavigationStateRendererTest.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,7 @@ public void handle_preserveOnRefreshAndWindowNameKnown_componentIsCachedRetrieve
312312
"ROOT.123", new Location("preserved")).isPresent());
313313

314314
// given the recently instantiated view
315-
final Component view = (Component) ui1.getInternals()
316-
.getActiveRouterTargetsChain().get(0);
315+
final Component view = ui1.getCurrentView();
317316

318317
// given a new UI with the same window name
319318
MockUI ui2 = new MockUI(session);

0 commit comments

Comments
 (0)