Skip to content

Commit

Permalink
feat: add MenuConfiguration (#20138)
Browse files Browse the repository at this point in the history
* feat: add MenuConfiguration

New public API for building application menu: adds `MenuConfiguration`, `MenuOptions` and `MenuOption` where `MenuConfiguration` is the main entry point to access menu data to build main menu.

Fixes: #20063

* chore: renamed classes and removed MenuOptions

Renamed MenuOption to MenuEntry.

* chore: updated javadocs

* chore: moved MenuRegistry to internal package

Removed Serializable from MenuConfiguration.

* chore: added javadoc and deprecated MenuData constructor

* chore: use new constructor

* chore: make MenuConfiguration final
  • Loading branch information
tltv authored and vaadin-bot committed Oct 4, 2024
1 parent a310a43 commit 86b20c7
Show file tree
Hide file tree
Showing 15 changed files with 464 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* the License.
*/

package com.vaadin.flow.server.menu;
package com.vaadin.flow.internal.menu;

import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -54,6 +54,8 @@
import com.vaadin.flow.server.VaadinRequest;
import com.vaadin.flow.server.VaadinService;
import com.vaadin.flow.server.VaadinSession;
import com.vaadin.flow.server.menu.AvailableViewInfo;
import com.vaadin.flow.server.menu.RouteParamType;

import static com.vaadin.flow.server.frontend.FrontendUtils.GENERATED;

Expand All @@ -63,6 +65,8 @@
*
* Only returns views that are accessible at the moment and leaves out routes
* that require path parameters.
* <p>
* For internal use only. May be renamed or removed in a future release.
*/
public class MenuRegistry {

Expand Down Expand Up @@ -346,7 +350,7 @@ private static void collectClientViews(String basePath,
if (viewConfig.menu() == null) {
// create MenuData anyway to avoid need for null checking
viewConfig = copyAvailableViewInfo(viewConfig,
new MenuData(viewConfig.title(), null, false, null));
new MenuData(viewConfig.title(), null, false, null, null));
}
configurations.put(path, viewConfig);
if (viewConfig.children() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.TreeMap;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
Expand All @@ -38,12 +36,8 @@
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.Html;
import com.vaadin.flow.component.Tag;
import com.vaadin.flow.di.Lookup;
import com.vaadin.flow.router.internal.ClientRoutesProvider;
import com.vaadin.flow.server.HttpStatusCode;
import com.vaadin.flow.server.VaadinService;
import com.vaadin.flow.server.frontend.FrontendUtils;
import com.vaadin.flow.server.menu.MenuRegistry;

/**
* This is abstract error view for routing exceptions.
Expand Down
41 changes: 25 additions & 16 deletions flow-server/src/main/java/com/vaadin/flow/router/MenuData.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,34 @@
import java.io.Serializable;
import java.util.Objects;

import com.vaadin.flow.component.Component;

/**
* Data class for menu item information.
* <p>
* Only for read as data is immutable.
*/
public record MenuData(String title, Double order, boolean exclude, String icon) implements Serializable {
public record MenuData(String title, Double order, boolean exclude, String icon, Class<? extends Component> menuClass) implements Serializable {

/**
* MenuData constructor.
*
* @param title
* title of the menu item
* @param order
* order of the menu item
* @param exclude
* whether the menu item should be excluded
* @param icon
* the icon of the menu item
*
* @deprecated Use {@link #MenuData(String, Double, boolean, String, Class)}
* instead.
*/
@Deprecated(forRemoval = true)
public MenuData(String title, Double order, boolean exclude, String icon) {
this(title, order, exclude, icon, null);
}

/**
* Gets the title of the menu item.
Expand Down Expand Up @@ -65,20 +87,7 @@ public String getIcon() {
@Override
public String toString() {
return "MenuData{" + "title='" + title + '\'' + ", order=" + order
+ ", exclude=" + exclude + ", icon='" + icon + '\'' + '}';
}

@Override
public boolean equals(Object obj) {
return obj instanceof MenuData other
&& Objects.equals(title, other.title)
&& Objects.equals(order, other.order)
&& Objects.equals(exclude, other.exclude)
&& Objects.equals(icon, other.icon);
}

@Override
public int hashCode() {
return Objects.hash(title, order, exclude, icon);
+ ", exclude=" + exclude + ", icon='" + icon + "', menuClass='"
+ menuClass + "'" + '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
import com.vaadin.flow.server.Constants;
import com.vaadin.flow.server.HttpStatusCode;
import com.vaadin.flow.server.VaadinSession;
import com.vaadin.flow.server.menu.MenuRegistry;
import com.vaadin.flow.internal.menu.MenuRegistry;

/**
* Base class for navigation handlers that target a navigation state.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
import com.vaadin.flow.server.auth.NavigationContext;
import com.vaadin.flow.server.auth.ViewAccessChecker;
import com.vaadin.flow.router.Layout;
import com.vaadin.flow.server.menu.MenuRegistry;
import com.vaadin.flow.internal.menu.MenuRegistry;
import com.vaadin.flow.shared.Registration;

import static java.util.stream.Collectors.toList;
Expand Down Expand Up @@ -324,7 +324,7 @@ private void populateRegisteredRoutes(ConfiguredRoutes configuration,
(Objects.equals(menu.order(), Double.MIN_VALUE)) ? null
: menu.order(),
excludeFromMenu,
(menu.icon().isBlank() ? null : menu.icon())))
(menu.icon().isBlank() ? null : menu.icon()), target))
.orElse(null);

RouteData route = new RouteData(parentLayouts, template, parameters,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package com.vaadin.flow.router.internal;

import java.util.Collections;
import java.util.List;

import org.slf4j.LoggerFactory;

Expand All @@ -25,11 +24,8 @@
import com.vaadin.flow.router.NavigationStateBuilder;
import com.vaadin.flow.router.NotFoundException;
import com.vaadin.flow.router.RouteResolver;
import com.vaadin.flow.router.RouterLayout;
import com.vaadin.flow.server.RouteRegistry;
import com.vaadin.flow.server.VaadinSession;
import com.vaadin.flow.server.menu.AvailableViewInfo;
import com.vaadin.flow.server.menu.MenuRegistry;
import com.vaadin.flow.internal.menu.MenuRegistry;

/**
* Default implementation of the {@link RouteResolver} interface.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,12 @@
import com.vaadin.flow.internal.Pair;
import com.vaadin.flow.internal.StringUtil;
import com.vaadin.flow.internal.hilla.EndpointRequestUtil;
import com.vaadin.flow.router.internal.ClientRoutesProvider;
import com.vaadin.flow.server.AbstractConfiguration;
import com.vaadin.flow.server.Constants;
import com.vaadin.flow.server.VaadinRequest;
import com.vaadin.flow.server.VaadinService;
import com.vaadin.flow.server.VaadinServlet;
import com.vaadin.flow.server.VaadinSession;
import com.vaadin.flow.server.frontend.scanner.ClassFinder;
import com.vaadin.flow.server.menu.MenuRegistry;
import com.vaadin.flow.internal.menu.MenuRegistry;

import elemental.json.JsonObject;
import static com.vaadin.flow.server.Constants.COMPATIBILITY_RESOURCES_FRONTEND_DEFAULT;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2000-2024 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package com.vaadin.flow.server.menu;

import java.io.Serializable;
import java.util.List;
import java.util.Locale;

import com.vaadin.flow.internal.menu.MenuRegistry;

/**
* Menu configuration helper class to retrieve available menu entries for
* application main menu.
*
* @since 24.5
*/
public final class MenuConfiguration {

/**
* Collect ordered list of menu entries for menu population. All client
* views are collected and any accessible server views.
*
* @return ordered list of {@link MenuEntry} instances
*/
public static List<MenuEntry> getMenuEntries() {
return MenuRegistry.collectMenuItemsList().stream()
.map(MenuConfiguration::createMenuEntry).toList();
}

/**
* Collect ordered list of menu entries for menu population. All client
* views are collected and any accessible server views.
*
* @param locale
* locale to use for ordering. null for default locale.
*
* @return ordered list of {@link MenuEntry} instances
*/
public static List<MenuEntry> getMenuEntries(Locale locale) {
return MenuRegistry.collectMenuItemsList(locale).stream()
.map(MenuConfiguration::createMenuEntry).toList();
}

private static MenuEntry createMenuEntry(AvailableViewInfo viewInfo) {
if (viewInfo.menu() == null) {
return new MenuEntry(viewInfo.route(), viewInfo.title(), null,
false, null, null);
}
return new MenuEntry(viewInfo.route(),
(viewInfo.menu().title() != null
&& !viewInfo.menu().title().isBlank()
? viewInfo.menu().title()
: viewInfo.title()),
viewInfo.menu().order(), viewInfo.menu().exclude(),
viewInfo.menu().icon(), viewInfo.menu().menuClass());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2000-2024 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package com.vaadin.flow.server.menu;

import java.io.Serializable;

import com.vaadin.flow.component.Component;

/**
* Menu entry for the main menu.
*
* @param path
* the path to navigate to
* @param title
* the title to display
* @param order
* the order in the menu or null for default order
* @param exclude
* whether to exclude the menu entry
* @param icon
* Icon to use in the menu or null for no icon. Value can go inside a
* {@code <vaadin-icon>} element's {@code icon} attribute which
* accepts icon group and name like 'vaadin:file'. Or it can go to a
* {@code <vaadin-icon>} element's {@code src} attribute which takes
* path to the icon. E.g. 'line-awesome/svg/lock-open-solid.svg'.
* @param menuClass
* the source class with {@link com.vaadin.flow.router.Menu}
* annotation or null if not available. Always null for
* Hilla/TypeScript client views.
*/
public record MenuEntry(String path, String title, Double order,
boolean exclude, String icon, Class<? extends Component> menuClass) implements Serializable {
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@
import com.vaadin.flow.server.MockServletServiceSessionSetup;
import com.vaadin.flow.server.VaadinService;
import com.vaadin.flow.server.VaadinSession;
import com.vaadin.flow.server.VaadinSessionState;
import com.vaadin.flow.server.menu.MenuRegistry;
import com.vaadin.flow.internal.menu.MenuRegistry;
import com.vaadin.tests.util.MockDeploymentConfiguration;

public class JavaScriptBootstrapUITest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import com.vaadin.flow.router.internal.ResolveRequest;
import com.vaadin.flow.server.InvalidRouteConfigurationException;
import com.vaadin.flow.server.RouteRegistry;
import com.vaadin.flow.server.menu.MenuRegistry;
import com.vaadin.flow.internal.menu.MenuRegistry;

public class DefaultRouteResolverTest extends RoutingTestBase {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

import com.fasterxml.jackson.annotation.JsonProperty;
import net.bytebuddy.ByteBuddy;
import net.bytebuddy.description.modifier.SyntheticState;
import net.bytebuddy.description.modifier.Visibility;
Expand Down Expand Up @@ -81,8 +80,7 @@
import com.vaadin.flow.server.ServiceException;
import com.vaadin.flow.server.WrappedSession;
import com.vaadin.flow.server.menu.AvailableViewInfo;
import com.vaadin.flow.server.menu.MenuRegistry;
import com.vaadin.flow.server.menu.RouteParamType;
import com.vaadin.flow.internal.menu.MenuRegistry;
import com.vaadin.flow.server.startup.ApplicationRouteRegistry;
import com.vaadin.tests.util.AlwaysLockedVaadinSession;
import com.vaadin.tests.util.MockDeploymentConfiguration;
Expand Down
Loading

0 comments on commit 86b20c7

Please sign in to comment.