Search in sources :

Example 26 with Location

use of com.vaadin.flow.router.Location in project flow by vaadin.

the class ViewRenderer method createEvent.

private LocationChangeEvent createEvent(NavigationEvent event, List<View> viewChain) {
    String route = getRoute();
    Map<String, String> routePlaceholders;
    if (route != null) {
        routePlaceholders = extractRoutePlaceholders(event.getLocation(), new RouteLocation(new Location(route)));
    } else {
        routePlaceholders = Collections.emptyMap();
    }
    return new LocationChangeEvent(event.getSource(), event.getUI(), event.getTrigger(), event.getLocation(), viewChain, routePlaceholders);
}
Also used : Location(com.vaadin.flow.router.Location)

Example 27 with Location

use of com.vaadin.flow.router.Location in project flow by vaadin.

the class UI method navigate.

/**
 * Updates this UI to show the view corresponding to the given location and
 * query parameters. The location must be a relative path without any ".."
 * segments.
 * <p>
 * Besides the navigation to the {@code location} this method also updates
 * the browser location (and page history).
 *
 * @see #navigate(String)
 * @see RouterInterface#navigate(UI, Location, NavigationTrigger)
 *
 * @param location
 *            the location to navigate to, not {@code null}
 * @param queryParameters
 *            query parameters that are used for navigation, not
 *            {@code null}
 */
public void navigate(String location, QueryParameters queryParameters) {
    if (location == null) {
        throw new IllegalArgumentException("Location may not be null");
    }
    if (queryParameters == null) {
        throw new IllegalArgumentException("Query parameters may not be null");
    }
    if (!getRouterInterface().isPresent()) {
        throw new IllegalStateException("Can't navigate when UI has no router");
    }
    Location navigationLocation = new Location(location, queryParameters);
    if (!internals.hasLastHandledLocation() || !navigationLocation.getPathWithQueryParameters().equals(internals.getLastHandledLocation().getPathWithQueryParameters())) {
        // Enable navigating back
        getPage().getHistory().pushState(null, navigationLocation);
    }
    getRouterInterface().get().navigate(this, navigationLocation, NavigationTrigger.PROGRAMMATIC);
}
Also used : Location(com.vaadin.flow.router.Location)

Example 28 with Location

use of com.vaadin.flow.router.Location in project flow by vaadin.

the class NavigationStateRendererTest method instantiatorUse.

@Test
public void instantiatorUse() throws ServiceException {
    MockVaadinServletService service = new MockVaadinServletService();
    service.init(new MockInstantiator() {

        @Override
        public <T extends HasElement> T createRouteTarget(Class<T> routeTargetType, NavigationEvent event) {
            Assert.assertEquals(Component.class, routeTargetType);
            return (T) new Text("foo");
        }
    });
    MockUI ui = new MockUI(new MockVaadinSession(service));
    NavigationEvent event = new NavigationEvent(new Router(new TestRouteRegistry()), new Location(""), ui, NavigationTrigger.PAGE_LOAD);
    NavigationStateRenderer renderer = new NavigationStateRenderer(navigationStateFromTarget(ChildConfiguration.class));
    Component routeTarget = renderer.getRouteTarget(Component.class, event);
    Assert.assertEquals(Text.class, routeTarget.getClass());
    UI.setCurrent(null);
}
Also used : NavigationEvent(com.vaadin.flow.router.NavigationEvent) MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) Router(com.vaadin.flow.router.Router) Text(com.vaadin.flow.component.Text) TestRouteRegistry(com.vaadin.flow.router.TestRouteRegistry) MockUI(com.vaadin.tests.util.MockUI) MockVaadinSession(com.vaadin.flow.server.MockVaadinSession) MockInstantiator(com.vaadin.flow.server.MockInstantiator) Component(com.vaadin.flow.component.Component) Location(com.vaadin.flow.router.Location) NavigationStateRenderer(com.vaadin.flow.router.internal.NavigationStateRenderer) Test(org.junit.Test)

Example 29 with Location

use of com.vaadin.flow.router.Location in project flow by vaadin.

the class LocationTest method parseLocation.

@Test
public void parseLocation() {
    Location location = new Location("foo/bar/baz");
    assertEquals(Arrays.asList("foo", "bar", "baz"), location.getSegments());
    assertEquals("foo/bar/baz", location.getPath());
}
Also used : Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

Example 30 with Location

use of com.vaadin.flow.router.Location in project flow by vaadin.

the class LocationTest method locationWithParametersPath_withoutTrailingSlash.

@Test
public void locationWithParametersPath_withoutTrailingSlash() {
    String initialPath = "foo/bar";
    QueryParameters queryParams = getQueryParameters();
    Location location = new Location(initialPath, queryParams);
    String pathWithParameters = location.getPathWithQueryParameters();
    assertEquals(initialPath + '?' + queryParams.getQueryString(), pathWithParameters);
}
Also used : QueryParameters(com.vaadin.flow.router.QueryParameters) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

Aggregations

Location (com.vaadin.flow.router.Location)51 Test (org.junit.Test)40 UI (com.vaadin.flow.component.UI)15 Router (com.vaadin.flow.router.legacy.Router)15 NavigationEvent (com.vaadin.flow.router.NavigationEvent)10 NavigationHandler (com.vaadin.flow.router.NavigationHandler)8 RouterConfiguration (com.vaadin.flow.router.legacy.RouterConfiguration)7 HistoryStateChangeEvent (com.vaadin.flow.component.page.History.HistoryStateChangeEvent)6 TestView (com.vaadin.flow.router.legacy.ViewRendererTest.TestView)6 NavigationTrigger (com.vaadin.flow.router.NavigationTrigger)5 QueryParameters (com.vaadin.flow.router.QueryParameters)5 DefaultErrorView (com.vaadin.flow.router.legacy.DefaultErrorView)5 VaadinRequest (com.vaadin.flow.server.VaadinRequest)5 VaadinResponse (com.vaadin.flow.server.VaadinResponse)5 Optional (java.util.Optional)4 CurrentInstance (com.vaadin.flow.internal.CurrentInstance)3 RouterInterface (com.vaadin.flow.router.RouterInterface)3 ImmutableRouterConfiguration (com.vaadin.flow.router.legacy.ImmutableRouterConfiguration)3 Resolver (com.vaadin.flow.router.legacy.Resolver)3 RouteSegmentVisitor (com.vaadin.flow.router.legacy.RouteLocation.RouteSegmentVisitor)3