Skip to content

Commit

Permalink
Update V1/V2 NavigationImpl to pass Supplier to item constructors
Browse files Browse the repository at this point in the history
Updates the V1 and V2 `NavigationImpl` models to pass a `Supplier` to
their respective `NavigationItemImpl` constructors.

Removes `Navigation#getExportedType()` because it is exactly the
same as the super method.

----
refs adobe#2526
  • Loading branch information
ky940819 committed Jun 11, 2023
1 parent de66725 commit 7461794
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 22 deletions.
Expand Up @@ -19,6 +19,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
Expand Down Expand Up @@ -56,11 +57,11 @@
@Model(adaptables = SlingHttpServletRequest.class,
adapters = {Navigation.class, ComponentExporter.class},
resourceType = {NavigationImpl.RESOURCE_TYPE})
@Exporter(name = ExporterConstants.SLING_MODEL_EXPORTER_NAME , extensions = ExporterConstants.SLING_MODEL_EXTENSION)
@Exporter(name = ExporterConstants.SLING_MODEL_EXPORTER_NAME, extensions = ExporterConstants.SLING_MODEL_EXTENSION)
public class NavigationImpl extends AbstractComponentImpl implements Navigation {

/**
* The resource navigation component resource type.
* V1 Navigation component resource type.
*/
public static final String RESOURCE_TYPE = "core/wcm/components/navigation/v1/navigation";

Expand Down Expand Up @@ -138,10 +139,11 @@ public class NavigationImpl extends AbstractComponentImpl implements Navigation
@PostConstruct
private void initModel() {
ValueMap properties = this.resource.getValueMap();
structureDepth = properties.get(PN_STRUCTURE_DEPTH, currentStyle.get(PN_STRUCTURE_DEPTH, -1));
boolean collectAllPages = properties.get(PN_COLLECT_ALL_PAGES, currentStyle.get(PN_COLLECT_ALL_PAGES, true));
if (collectAllPages) {
structureDepth = -1;
} else {
structureDepth = properties.get(PN_STRUCTURE_DEPTH, currentStyle.get(PN_STRUCTURE_DEPTH, -1));
}
if (currentStyle.containsKey(PN_STRUCTURE_START) || properties.containsKey(PN_STRUCTURE_START)) {
//workaround to maintain the content of Navigation component of users in case they update to the current i.e. the `structureStart` version.
Expand Down Expand Up @@ -181,14 +183,33 @@ public List<NavigationItem> getItems() {
this.items = Optional.ofNullable(this.getNavigationRoot())
.map(navigationRoot -> getRootItems(navigationRoot, structureStart))
.orElseGet(Stream::empty)
.map(item -> this.createNavigationItem(item, getItems(item)))
.map(item -> this.createNavigationItem(item, () -> getItems(item)))
.collect(Collectors.toList());
}
return Collections.unmodifiableList(items);
}

protected NavigationItem newNavigationItem(Page page, boolean active, boolean current, @NotNull LinkManager linkManager, int level,
List<NavigationItem> children, String parentId, Component component) {
/**
* Create a new navigation item.
*
* @param page The page for which to create a navigation item.
* @param active Flag indicating if the navigation item is active.
* @param current Flag indicating if the navigation item is current page.
* @param linkManager Link manager service.
* @param level Depth level of the navigation item.
* @param children The child navigation items supplier.
* @param parentId ID of the parent navigation component.
* @param component The parent navigation {@link Component}.
* @return The navigation item.
*/
protected NavigationItem newNavigationItem(@NotNull final Page page,
final boolean active,
final boolean current,
@NotNull final LinkManager linkManager,
final int level,
@NotNull final Supplier<@NotNull List<NavigationItem>> children,
final String parentId,
final Component component) {
return new NavigationItemImpl(page, active, current, linkManager, level, children, parentId, component);
}

Expand All @@ -201,12 +222,6 @@ public String getAccessibilityLabel() {
return this.accessibilityLabel;
}

@NotNull
@Override
public String getExportedType() {
return this.resource.getResourceType();
}

/**
* Builds the navigation tree for a {@code navigationRoot} page.
*
Expand All @@ -217,7 +232,7 @@ private List<NavigationItem> getItems(@NotNull final Page subtreeRoot) {
if (this.structureDepth < 0 || subtreeRoot.getDepth() - this.getNavigationRoot().getDepth() < this.structureDepth) {
Iterator<Page> childIterator = subtreeRoot.listChildren(new PageFilter());
return StreamSupport.stream(((Iterable<Page>) () -> childIterator).spliterator(), false)
.map(item -> this.createNavigationItem(item, getItems(item)))
.map(item -> this.createNavigationItem(item, () -> getItems(item)))
.collect(Collectors.toList());
}
return Collections.emptyList();
Expand All @@ -242,11 +257,11 @@ private Stream<Page> getRootItems(@NotNull final Page navigationRoot, final int
/**
* Create a navigation item for the given page/children.
*
* @param page The page for which to create a navigation item.
* @param children The child navigation items.
* @param page The page for which to create a navigation item.
* @param children The child navigation items supplier.
* @return The newly created navigation item.
*/
private NavigationItem createNavigationItem(@NotNull final Page page, @NotNull final List<NavigationItem> children) {
private NavigationItem createNavigationItem(@NotNull final Page page, @NotNull final Supplier<@NotNull List<NavigationItem>> children) {
int level = page.getDepth() - (this.getNavigationRoot().getDepth() + structureStart);
boolean current = checkCurrent(page);
boolean selected = checkSelected(page, current);
Expand All @@ -263,16 +278,26 @@ private NavigationItem createNavigationItem(@NotNull final Page page, @NotNull f
* </ul>
*
* @param page The page to check.
* @param current Flag indicating if the page is the current page.
* @return True if the page is selected, false if not.
*/
private boolean checkSelected(@NotNull final Page page, boolean current) {
return current
|| this.currentPage.getPath().startsWith(page.getPath() + "/");
return current || this.currentPage.getPath().startsWith(page.getPath() + "/");
}

/**
* Checks if the specified page is the current page.
* A page is current if it is either:
* <ul>
* <li>equal to currentPage</li>
* <li>redirects to currentPage</li>
* </ul>
*
* @param page The page to check.
* @return True if the page is current, false if not.
*/
private boolean checkCurrent(@NotNull final Page page) {
return this.currentPage.equals(page)
|| currentPageIsRedirectTarget(page);
return this.currentPage.equals(page) || currentPageIsRedirectTarget(page);
}

/**
Expand Down
Expand Up @@ -16,6 +16,7 @@
package com.adobe.cq.wcm.core.components.internal.models.v2;

import java.util.List;
import java.util.function.Supplier;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.models.annotations.Exporter;
Expand All @@ -30,16 +31,29 @@
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.components.Component;

/**
* V2 Navigation model implementation.
*/
@Model(adaptables = SlingHttpServletRequest.class,
adapters = {Navigation.class, ComponentExporter.class},
resourceType = {NavigationImpl.RESOURCE_TYPE})
@Exporter(name = ExporterConstants.SLING_MODEL_EXPORTER_NAME , extensions = ExporterConstants.SLING_MODEL_EXTENSION)
public class NavigationImpl extends com.adobe.cq.wcm.core.components.internal.models.v1.NavigationImpl {

/**
* V2 Navigation resource type.
*/
public static final String RESOURCE_TYPE = "core/wcm/components/navigation/v2/navigation";

protected NavigationItem newNavigationItem(Page page, boolean active, boolean current, @NotNull LinkManager linkManager, int level,
List<NavigationItem> children, String parentId, Component component) {
@Override
protected NavigationItem newNavigationItem(@NotNull final Page page,
final boolean active,
final boolean current,
@NotNull final LinkManager linkManager,
final int level,
@NotNull final Supplier<@NotNull List<NavigationItem>> children,
final String parentId,
final Component component) {
return new NavigationItemImpl(page, active, current, linkManager, level, children, parentId, component);
}

Expand Down

0 comments on commit 7461794

Please sign in to comment.