Search in sources :

Example 1 with NavigationTrigger

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

the class NavigationRpcHandler method handle.

@Override
public Optional<Runnable> handle(UI ui, JsonObject invocationJson) {
    History history = ui.getPage().getHistory();
    HistoryStateChangeHandler historyStateChangeHandler = history.getHistoryStateChangeHandler();
    if (historyStateChangeHandler != null) {
        JsonValue state = invocationJson.get(JsonConstants.RPC_NAVIGATION_STATE);
        String location = invocationJson.getString(JsonConstants.RPC_NAVIGATION_LOCATION);
        boolean triggeredByLink = invocationJson.hasKey(JsonConstants.RPC_NAVIGATION_ROUTERLINK);
        NavigationTrigger trigger = triggeredByLink ? NavigationTrigger.ROUTER_LINK : NavigationTrigger.HISTORY;
        // as it would be dubious and even impossible on the client side.
        if (trigger == NavigationTrigger.ROUTER_LINK && ui.getElement().getNode().isInert()) {
            LoggerFactory.getLogger(NavigationRpcHandler.class.getName()).trace("Ignored router link click for location '{}' because the UI is inert.", location);
            return Optional.empty();
        }
        HistoryStateChangeEvent event = new HistoryStateChangeEvent(history, state, new Location(location), trigger);
        historyStateChangeHandler.onHistoryStateChange(event);
    }
    return Optional.empty();
}
Also used : HistoryStateChangeEvent(com.vaadin.flow.component.page.History.HistoryStateChangeEvent) HistoryStateChangeHandler(com.vaadin.flow.component.page.History.HistoryStateChangeHandler) NavigationTrigger(com.vaadin.flow.router.NavigationTrigger) JsonValue(elemental.json.JsonValue) History(com.vaadin.flow.component.page.History) Location(com.vaadin.flow.router.Location)

Example 2 with NavigationTrigger

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

the class ApplicationRunnerServlet method createServletService.

@SuppressWarnings("serial")
@Override
protected VaadinServletService createServletService(DeploymentConfiguration deploymentConfiguration) throws ServiceException {
    // service doesn't use router actually. UI class is responsible to show
    // and update the content by itself with only root route available
    VaadinServletService service = new VaadinServletService(this, deploymentConfiguration) {

        @Override
        public Router getRouter() {
            Router router = new Router(getRouteRegistry()) {

                @Override
                public int navigate(UI ui, Location location, NavigationTrigger trigger, JsonValue state) {
                    ui.getPage().getHistory().pushState(null, location);
                    return HttpServletResponse.SC_OK;
                }
            };
            return router;
        }
    };
    service.init();
    final SystemMessagesProvider provider = service.getSystemMessagesProvider();
    service.setSystemMessagesProvider((SystemMessagesProvider) systemMessagesInfo -> {
        if (systemMessagesInfo.getRequest() == null) {
            return provider.getSystemMessages(systemMessagesInfo);
        }
        Object messages = systemMessagesInfo.getRequest().getAttribute(CUSTOM_SYSTEM_MESSAGES_PROPERTY);
        if (messages instanceof SystemMessages) {
            return (SystemMessages) messages;
        }
        return provider.getSystemMessages(systemMessagesInfo);
    });
    return service;
}
Also used : VaadinServletContext(com.vaadin.flow.server.VaadinServletContext) Proxy(java.lang.reflect.Proxy) ServletException(javax.servlet.ServletException) SystemMessagesProvider(com.vaadin.flow.server.SystemMessagesProvider) URL(java.net.URL) CurrentInstance(com.vaadin.flow.internal.CurrentInstance) URISyntaxException(java.net.URISyntaxException) LoggerFactory(org.slf4j.LoggerFactory) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration) Router(com.vaadin.flow.router.Router) JsonValue(elemental.json.JsonValue) HttpServletRequest(javax.servlet.http.HttpServletRequest) DefaultDeploymentConfiguration(com.vaadin.flow.server.DefaultDeploymentConfiguration) Map(java.util.Map) Location(com.vaadin.flow.router.Location) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) URI(java.net.URI) UI(com.vaadin.flow.component.UI) Method(java.lang.reflect.Method) Path(java.nio.file.Path) LinkedHashSet(java.util.LinkedHashSet) NavigationTrigger(com.vaadin.flow.router.NavigationTrigger) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) HttpSession(javax.servlet.http.HttpSession) VaadinSession(com.vaadin.flow.server.VaadinSession) SystemMessages(com.vaadin.flow.server.SystemMessages) ServletConfig(javax.servlet.ServletConfig) Properties(java.util.Properties) Logger(org.slf4j.Logger) Files(java.nio.file.Files) VaadinServlet(com.vaadin.flow.server.VaadinServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) Set(java.util.Set) IOException(java.io.IOException) Attributes(com.vaadin.flow.server.Attributes) Field(java.lang.reflect.Field) File(java.io.File) Serializable(java.io.Serializable) WebServlet(javax.servlet.annotation.WebServlet) FileVisitResult(java.nio.file.FileVisitResult) ApplicationConfiguration(com.vaadin.flow.server.startup.ApplicationConfiguration) Conf(com.vaadin.flow.uitest.servlet.CustomDeploymentConfiguration.Conf) VaadinService(com.vaadin.flow.server.VaadinService) InvocationHandler(java.lang.reflect.InvocationHandler) ServiceException(com.vaadin.flow.server.ServiceException) Collections(java.util.Collections) VaadinServletService(com.vaadin.flow.server.VaadinServletService) UI(com.vaadin.flow.component.UI) SystemMessagesProvider(com.vaadin.flow.server.SystemMessagesProvider) NavigationTrigger(com.vaadin.flow.router.NavigationTrigger) JsonValue(elemental.json.JsonValue) VaadinServletService(com.vaadin.flow.server.VaadinServletService) Router(com.vaadin.flow.router.Router) SystemMessages(com.vaadin.flow.server.SystemMessages) Location(com.vaadin.flow.router.Location)

Aggregations

Location (com.vaadin.flow.router.Location)2 NavigationTrigger (com.vaadin.flow.router.NavigationTrigger)2 JsonValue (elemental.json.JsonValue)2 UI (com.vaadin.flow.component.UI)1 History (com.vaadin.flow.component.page.History)1 HistoryStateChangeEvent (com.vaadin.flow.component.page.History.HistoryStateChangeEvent)1 HistoryStateChangeHandler (com.vaadin.flow.component.page.History.HistoryStateChangeHandler)1 DeploymentConfiguration (com.vaadin.flow.function.DeploymentConfiguration)1 CurrentInstance (com.vaadin.flow.internal.CurrentInstance)1 Router (com.vaadin.flow.router.Router)1 Attributes (com.vaadin.flow.server.Attributes)1 DefaultDeploymentConfiguration (com.vaadin.flow.server.DefaultDeploymentConfiguration)1 ServiceException (com.vaadin.flow.server.ServiceException)1 SystemMessages (com.vaadin.flow.server.SystemMessages)1 SystemMessagesProvider (com.vaadin.flow.server.SystemMessagesProvider)1 VaadinService (com.vaadin.flow.server.VaadinService)1 VaadinServlet (com.vaadin.flow.server.VaadinServlet)1 VaadinServletContext (com.vaadin.flow.server.VaadinServletContext)1 VaadinServletRequest (com.vaadin.flow.server.VaadinServletRequest)1 VaadinServletService (com.vaadin.flow.server.VaadinServletService)1