|
28 | 28 | import java.util.regex.Pattern; |
29 | 29 | import java.util.stream.Stream; |
30 | 30 |
|
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; |
48 | 31 | import org.slf4j.Logger; |
49 | 32 | import org.slf4j.LoggerFactory; |
50 | 33 |
|
| 34 | +import com.vaadin.flow.component.dependency.JsModule; |
| 35 | +import com.vaadin.flow.component.internal.AllowInert; |
| 36 | +import com.vaadin.flow.component.internal.JavaScriptNavigationStateRenderer; |
51 | 37 | import com.vaadin.flow.component.internal.UIInternalUpdater; |
52 | 38 | import com.vaadin.flow.component.internal.UIInternals; |
| 39 | +import com.vaadin.flow.component.page.History; |
53 | 40 | import com.vaadin.flow.component.page.LoadingIndicatorConfiguration; |
54 | 41 | import com.vaadin.flow.component.page.Page; |
55 | 42 | import com.vaadin.flow.dom.Element; |
56 | 43 | import com.vaadin.flow.function.SerializableConsumer; |
57 | 44 | import com.vaadin.flow.function.SerializableRunnable; |
58 | 45 | import com.vaadin.flow.i18n.I18NProvider; |
| 46 | +import com.vaadin.flow.i18n.LocaleChangeEvent; |
59 | 47 | import com.vaadin.flow.internal.CurrentInstance; |
60 | 48 | import com.vaadin.flow.internal.ExecutionContext; |
61 | 49 | import com.vaadin.flow.internal.StateNode; |
62 | 50 | import com.vaadin.flow.internal.StateTree.ExecutionRegistration; |
63 | 51 | import com.vaadin.flow.internal.nodefeature.ElementData; |
64 | 52 | import com.vaadin.flow.internal.nodefeature.LoadingIndicatorConfigurationMap; |
| 53 | +import com.vaadin.flow.internal.nodefeature.NodeProperties; |
65 | 54 | import com.vaadin.flow.internal.nodefeature.PollConfigurationMap; |
66 | 55 | import com.vaadin.flow.internal.nodefeature.ReconnectDialogConfigurationMap; |
67 | 56 | import com.vaadin.flow.router.AfterNavigationListener; |
68 | 57 | import com.vaadin.flow.router.BeforeEnterListener; |
69 | 58 | import com.vaadin.flow.router.BeforeLeaveListener; |
| 59 | +import com.vaadin.flow.router.ErrorNavigationEvent; |
| 60 | +import com.vaadin.flow.router.ErrorParameter; |
70 | 61 | import com.vaadin.flow.router.EventUtil; |
71 | 62 | import com.vaadin.flow.router.HasUrlParameter; |
72 | 63 | 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; |
73 | 67 | import com.vaadin.flow.router.NavigationTrigger; |
| 68 | +import com.vaadin.flow.router.NotFoundException; |
74 | 69 | import com.vaadin.flow.router.QueryParameters; |
75 | 70 | import com.vaadin.flow.router.RouteConfiguration; |
| 71 | +import com.vaadin.flow.router.RouteNotFoundError; |
| 72 | +import com.vaadin.flow.router.RouteParameters; |
76 | 73 | import com.vaadin.flow.router.Router; |
77 | 74 | 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; |
78 | 78 | import com.vaadin.flow.router.internal.PathUtil; |
79 | 79 | import com.vaadin.flow.server.Command; |
80 | 80 | import com.vaadin.flow.server.ErrorEvent; |
@@ -1195,6 +1195,30 @@ public boolean isNavigationSupported() { |
1195 | 1195 | return true; |
1196 | 1196 | } |
1197 | 1197 |
|
| 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 | + |
1198 | 1222 | /** |
1199 | 1223 | * Gets the router used for navigating in this UI. |
1200 | 1224 | * |
|
0 commit comments