Search in sources :

Example 1 with Page

use of com.vaadin.flow.component.page.Page in project flow by vaadin.

the class UIInternals method addExternalDependencies.

private void addExternalDependencies(DependencyInfo dependency) {
    Page page = ui.getPage();
    dependency.getJavaScripts().stream().filter(js -> UrlUtil.isExternal(js.value())).forEach(js -> page.addJavaScript(js.value(), js.loadMode()));
    dependency.getJsModules().stream().filter(js -> UrlUtil.isExternal(js.value())).forEach(js -> page.addJsModule(js.value()));
}
Also used : CssImportData(com.vaadin.flow.server.frontend.FallbackChunk.CssImportData) HeartbeatEvent(com.vaadin.flow.component.HeartbeatEvent) Component(com.vaadin.flow.component.Component) JavaScript(com.vaadin.flow.component.dependency.JavaScript) LoadingIndicatorConfigurationMap(com.vaadin.flow.internal.nodefeature.LoadingIndicatorConfigurationMap) CssImport(com.vaadin.flow.component.dependency.CssImport) ComponentUtil(com.vaadin.flow.component.ComponentUtil) Registration(com.vaadin.flow.shared.Registration) LoggerFactory(org.slf4j.LoggerFactory) BeforeEnterHandler(com.vaadin.flow.router.internal.BeforeEnterHandler) Router(com.vaadin.flow.router.Router) ExtendedClientDetails(com.vaadin.flow.component.page.ExtendedClientDetails) PushConfigurationMap(com.vaadin.flow.internal.nodefeature.PushConfigurationMap) AfterNavigationListener(com.vaadin.flow.router.AfterNavigationListener) PushMode(com.vaadin.flow.shared.communication.PushMode) AfterNavigationHandler(com.vaadin.flow.router.internal.AfterNavigationHandler) Map(java.util.Map) PushConnection(com.vaadin.flow.server.communication.PushConnection) ContinueNavigationAction(com.vaadin.flow.router.BeforeLeaveEvent.ContinueNavigationAction) UI(com.vaadin.flow.component.UI) ConstantPool(com.vaadin.flow.internal.ConstantPool) VaadinSession(com.vaadin.flow.server.VaadinSession) IdentityHashMap(java.util.IdentityHashMap) BeforeLeaveListener(com.vaadin.flow.router.BeforeLeaveListener) BasicElementStateProvider(com.vaadin.flow.dom.impl.BasicElementStateProvider) AnnotationReader(com.vaadin.flow.internal.AnnotationReader) PushConfiguration(com.vaadin.flow.component.PushConfiguration) Set(java.util.Set) Collectors(java.util.stream.Collectors) ReconnectDialogConfigurationMap(com.vaadin.flow.internal.nodefeature.ReconnectDialogConfigurationMap) Serializable(java.io.Serializable) BeforeLeaveHandler(com.vaadin.flow.router.internal.BeforeLeaveHandler) List(java.util.List) VaadinContext(com.vaadin.flow.server.VaadinContext) Stream(java.util.stream.Stream) FallbackChunk(com.vaadin.flow.server.frontend.FallbackChunk) VaadinService(com.vaadin.flow.server.VaadinService) Optional(java.util.Optional) JsModule(com.vaadin.flow.component.dependency.JsModule) UrlUtil(com.vaadin.flow.internal.UrlUtil) HashMap(java.util.HashMap) StateTree(com.vaadin.flow.internal.StateTree) BeforeEnterListener(com.vaadin.flow.router.BeforeEnterListener) ListenerPriority(com.vaadin.flow.router.ListenerPriority) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) NodeFeature(com.vaadin.flow.internal.nodefeature.NodeFeature) ElementUtil(com.vaadin.flow.dom.ElementUtil) JsonCodec(com.vaadin.flow.internal.JsonCodec) Location(com.vaadin.flow.router.Location) HeartbeatListener(com.vaadin.flow.component.HeartbeatListener) Page(com.vaadin.flow.component.page.Page) RouterLayout(com.vaadin.flow.router.RouterLayout) Logger(org.slf4j.Logger) HasElement(com.vaadin.flow.component.HasElement) StyleSheet(com.vaadin.flow.component.dependency.StyleSheet) DependencyInfo(com.vaadin.flow.component.internal.ComponentMetaData.DependencyInfo) ArrayDeque(java.util.ArrayDeque) PollConfigurationMap(com.vaadin.flow.internal.nodefeature.PollConfigurationMap) Collections(java.util.Collections) Push(com.vaadin.flow.component.page.Push) Page(com.vaadin.flow.component.page.Page)

Example 2 with Page

use of com.vaadin.flow.component.page.Page in project flow by vaadin.

the class UIInternals method addComponentDependencies.

/**
 * Adds the dependencies defined using {@link StyleSheet} or
 * {@link JavaScript} on the given Component class.
 *
 * @param componentClass
 *            the component class to read annotations from
 */
public void addComponentDependencies(Class<? extends Component> componentClass) {
    Page page = ui.getPage();
    DependencyInfo dependencies = ComponentUtil.getDependencies(session.getService(), componentClass);
    // In npm mode, add external JavaScripts directly to the page.
    addExternalDependencies(dependencies);
    addFallbackDependencies(dependencies);
    dependencies.getStyleSheets().forEach(styleSheet -> page.addStyleSheet(styleSheet.value(), styleSheet.loadMode()));
}
Also used : Page(com.vaadin.flow.component.page.Page) DependencyInfo(com.vaadin.flow.component.internal.ComponentMetaData.DependencyInfo)

Example 3 with Page

use of com.vaadin.flow.component.page.Page in project flow by vaadin.

the class JavaScriptBootstrapUITest method server_should_not_doClientRoute_when_navigatingToServer.

@Test
public void server_should_not_doClientRoute_when_navigatingToServer() {
    ui.connectClient("foo", "bar", "/clean", "", null);
    assertEquals(Tag.HEADER, ui.wrapperElement.getChild(0).getTag());
    assertEquals(Tag.H2, ui.wrapperElement.getChild(0).getChild(0).getTag());
    ui = Mockito.spy(ui);
    Page page = mockPage();
    ArgumentCaptor<String> execJs = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<String> execArg = ArgumentCaptor.forClass(String.class);
    // Dirty view is allowed after clean view
    ui.navigate("dirty");
    // A server navigation happens
    assertEquals(Tag.SPAN, ui.wrapperElement.getChild(0).getTag());
    Mockito.verify(page).executeJs(execJs.capture(), execArg.capture());
    assertEquals(CLIENT_PUSHSTATE_TO, execJs.getValue());
    final List<String> execValues = execArg.getAllValues();
    assertEquals(2, execValues.size());
    assertNull(execValues.get(0));
    assertEquals("dirty", execArg.getValue());
}
Also used : Page(com.vaadin.flow.component.page.Page) Test(org.junit.Test)

Example 4 with Page

use of com.vaadin.flow.component.page.Page in project flow by vaadin.

the class JavaScriptBootstrapUITest method should_invoke_clientRoute_when_navigationHasNotBeenStarted.

@Test
public void should_invoke_clientRoute_when_navigationHasNotBeenStarted() {
    ui = Mockito.spy(ui);
    Page page = mockPage();
    ArgumentCaptor<String> execJs = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<String> execArg = ArgumentCaptor.forClass(String.class);
    ui.navigate("whatever");
    Mockito.verify(page).executeJs(execJs.capture(), execArg.capture());
    assertEquals(CLIENT_NAVIGATE_TO, execJs.getValue());
    assertEquals("whatever", execArg.getValue());
}
Also used : Page(com.vaadin.flow.component.page.Page) Test(org.junit.Test)

Example 5 with Page

use of com.vaadin.flow.component.page.Page in project flow by vaadin.

the class NavigationStateRendererTest method handle_variousInputs_checkPushStateShouldBeCalledOrNot.

@Test
public // - navigation trigger is PAGE_LOAD, HISTORY, or PROGRAMMATIC
void handle_variousInputs_checkPushStateShouldBeCalledOrNot() {
    // given a service with instantiator
    MockVaadinServletService service = createMockServiceWithInstantiator();
    // given a locked session
    MockVaadinSession session = new AlwaysLockedVaadinSession(service);
    session.setConfiguration(new MockDeploymentConfiguration());
    // given a NavigationStateRenderer mapping to RegularView
    new NavigationStateBuilder(router).withTarget(RegularView.class).build();
    NavigationStateRenderer renderer = new NavigationStateRenderer(navigationStateFromTarget(RegularView.class));
    // given a UI with an instrumented Page that records
    // getHistory().pushState calls
    AtomicBoolean pushStateCalled = new AtomicBoolean(false);
    List<Location> pushStateLocations = new ArrayList<>();
    MockUI ui = new MockUI(session) {

        final Page page = new Page(this) {

            final History history = new History(getUI().get()) {

                @Override
                public void pushState(JsonValue state, Location location) {
                    pushStateCalled.set(true);
                    pushStateLocations.add(location);
                }
            };

            @Override
            public History getHistory() {
                return history;
            }
        };

        @Override
        public Page getPage() {
            return page;
        }
    };
    renderer.handle(new NavigationEvent(new Router(new TestRouteRegistry()), new Location("regular"), ui, NavigationTrigger.UI_NAVIGATE, null, true));
    Assert.assertFalse("No pushState invocation is expected when forwardTo is true.", pushStateCalled.get());
    ui.getInternals().clearLastHandledNavigation();
    renderer.handle(new NavigationEvent(new Router(new TestRouteRegistry()), new Location("regular"), ui, NavigationTrigger.PROGRAMMATIC));
    Assert.assertFalse("No pushState invocation is expected when navigation trigger is PROGRAMMATIC.", pushStateCalled.get());
    ui.getInternals().clearLastHandledNavigation();
    renderer.handle(new NavigationEvent(new Router(new TestRouteRegistry()), new Location("regular"), ui, NavigationTrigger.HISTORY));
    Assert.assertFalse("No pushState invocation is expected when navigation trigger is HISTORY.", pushStateCalled.get());
    ui.getInternals().clearLastHandledNavigation();
    renderer.handle(new NavigationEvent(new Router(new TestRouteRegistry()), new Location("regular"), ui, NavigationTrigger.PAGE_LOAD));
    Assert.assertFalse("No pushState invocation is expected when navigation trigger is PAGE_LOAD.", pushStateCalled.get());
    pushStateCalled.set(false);
    pushStateLocations.clear();
    ui.getInternals().clearLastHandledNavigation();
    renderer.handle(new NavigationEvent(new Router(new TestRouteRegistry()), new Location("regular"), ui, NavigationTrigger.UI_NAVIGATE));
    Assert.assertTrue("pushState invocation is expected.", pushStateCalled.get());
    Assert.assertTrue(pushStateLocations.stream().anyMatch(location -> location.getPath().equals("regular")));
    pushStateCalled.set(false);
    renderer.handle(new NavigationEvent(new Router(new TestRouteRegistry()), new Location("regular"), ui, NavigationTrigger.UI_NAVIGATE));
    Assert.assertFalse("No pushState invocation is expected when navigating to the current location.", pushStateCalled.get());
}
Also used : Arrays(java.util.Arrays) Component(com.vaadin.flow.component.Component) Json(elemental.json.Json) Router(com.vaadin.flow.router.Router) ExtendedClientDetails(com.vaadin.flow.component.page.ExtendedClientDetails) Route(com.vaadin.flow.router.Route) History(com.vaadin.flow.component.page.History) PendingJavaScriptResult(com.vaadin.flow.component.page.PendingJavaScriptResult) JsonValue(elemental.json.JsonValue) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Element(com.vaadin.flow.dom.Element) MockUI(com.vaadin.tests.util.MockUI) UI(com.vaadin.flow.component.UI) NavigationTrigger(com.vaadin.flow.router.NavigationTrigger) AlwaysLockedVaadinSession(com.vaadin.tests.util.AlwaysLockedVaadinSession) MockVaadinContext(com.vaadin.flow.server.MockVaadinContext) Set(java.util.Set) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) NotThreadSafe(net.jcip.annotations.NotThreadSafe) List(java.util.List) NavigationState(com.vaadin.flow.router.NavigationState) ServiceException(com.vaadin.flow.server.ServiceException) MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestRouteRegistry(com.vaadin.flow.router.TestRouteRegistry) MockVaadinSession(com.vaadin.flow.server.MockVaadinSession) ArrayList(java.util.ArrayList) ApplicationRouteRegistry(com.vaadin.flow.server.startup.ApplicationRouteRegistry) Tag(com.vaadin.flow.component.Tag) Location(com.vaadin.flow.router.Location) MockInstantiator(com.vaadin.flow.server.MockInstantiator) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) QueryParameters(com.vaadin.flow.router.QueryParameters) Text(com.vaadin.flow.component.Text) Page(com.vaadin.flow.component.page.Page) RouterLayout(com.vaadin.flow.router.RouterLayout) HasElement(com.vaadin.flow.component.HasElement) RouteRegistry(com.vaadin.flow.server.RouteRegistry) Test(org.junit.Test) Mockito(org.mockito.Mockito) Rule(org.junit.Rule) NavigationStateBuilder(com.vaadin.flow.router.NavigationStateBuilder) RouteConfiguration(com.vaadin.flow.router.RouteConfiguration) PreserveOnRefresh(com.vaadin.flow.router.PreserveOnRefresh) JsonObject(elemental.json.JsonObject) Assert(org.junit.Assert) Collections(java.util.Collections) NavigationEvent(com.vaadin.flow.router.NavigationEvent) ParentLayout(com.vaadin.flow.router.ParentLayout) NavigationEvent(com.vaadin.flow.router.NavigationEvent) MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) ArrayList(java.util.ArrayList) JsonValue(elemental.json.JsonValue) Router(com.vaadin.flow.router.Router) Page(com.vaadin.flow.component.page.Page) History(com.vaadin.flow.component.page.History) TestRouteRegistry(com.vaadin.flow.router.TestRouteRegistry) NavigationStateBuilder(com.vaadin.flow.router.NavigationStateBuilder) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MockUI(com.vaadin.tests.util.MockUI) MockVaadinSession(com.vaadin.flow.server.MockVaadinSession) AlwaysLockedVaadinSession(com.vaadin.tests.util.AlwaysLockedVaadinSession) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

Aggregations

Page (com.vaadin.flow.component.page.Page)13 Location (com.vaadin.flow.router.Location)7 Test (org.junit.Test)7 UI (com.vaadin.flow.component.UI)5 Router (com.vaadin.flow.router.Router)5 Serializable (java.io.Serializable)5 Component (com.vaadin.flow.component.Component)4 NavigationEvent (com.vaadin.flow.router.NavigationEvent)4 HasElement (com.vaadin.flow.component.HasElement)3 ExtendedClientDetails (com.vaadin.flow.component.page.ExtendedClientDetails)3 History (com.vaadin.flow.component.page.History)3 PendingJavaScriptResult (com.vaadin.flow.component.page.PendingJavaScriptResult)3 RouterLayout (com.vaadin.flow.router.RouterLayout)3 TestRouteRegistry (com.vaadin.flow.router.TestRouteRegistry)3 MockVaadinServletService (com.vaadin.flow.server.MockVaadinServletService)3 MockVaadinSession (com.vaadin.flow.server.MockVaadinSession)3 RouteRegistry (com.vaadin.flow.server.RouteRegistry)3 AlwaysLockedVaadinSession (com.vaadin.tests.util.AlwaysLockedVaadinSession)3 MockUI (com.vaadin.tests.util.MockUI)3 ArrayList (java.util.ArrayList)3