Skip to content

Commit

Permalink
Use empty list supplier for BreadcrumbItem children.
Browse files Browse the repository at this point in the history
Updates the breadcrumb model implementations by removing the `children` field
from `BreadcrumbItemImpl` constructor because it is always set to empty list (i.e.
breadcrumb items never have children).

The `BreadcrumbItemImpl` will now pass a `Supplier` which
supplies an empty list to the super class `NavigationItemImpl`.

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

----
refs adobe#2526
  • Loading branch information
ky940819 committed Jun 11, 2023
1 parent 7461794 commit 80baa00
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
import com.day.cq.wcm.api.designer.Style;

@Model(adaptables = SlingHttpServletRequest.class,
adapters = {Breadcrumb.class, ComponentExporter.class},
resourceType = {BreadcrumbImpl.RESOURCE_TYPE_V1, BreadcrumbImpl.RESOURCE_TYPE_V2})
adapters = {Breadcrumb.class, ComponentExporter.class},
resourceType = {BreadcrumbImpl.RESOURCE_TYPE_V1, BreadcrumbImpl.RESOURCE_TYPE_V2})
@Exporter(name = ExporterConstants.SLING_MODEL_EXPORTER_NAME, extensions = ExporterConstants.SLING_MODEL_EXTENSION)
public class BreadcrumbImpl extends AbstractComponentImpl implements Breadcrumb {

Expand All @@ -62,9 +62,6 @@ public class BreadcrumbImpl extends AbstractComponentImpl implements Breadcrumb
@ScriptVariable
private Page currentPage;

@Self
private SlingHttpServletRequest request;

@Self
protected LinkManager linkManager;

Expand All @@ -88,12 +85,6 @@ public Collection<NavigationItem> getItems() {
return Collections.unmodifiableList(items);
}

@NotNull
@Override
public String getExportedType() {
return request.getResource().getResourceType();
}

private List<NavigationItem> createItems() {
List<NavigationItem> items = new ArrayList<>();
int currentLevel = currentPage.getDepth();
Expand All @@ -105,8 +96,7 @@ private List<NavigationItem> createItems() {
break;
}
if (checkIfNotHidden(page)) {
NavigationItem navigationItem = newBreadcrumbItem(page, isActivePage, linkManager, currentLevel,
Collections.emptyList(), getId(), component);
NavigationItem navigationItem = newBreadcrumbItem(page, isActivePage, linkManager, currentLevel, getId(), component);
items.add(navigationItem);
}
}
Expand All @@ -115,11 +105,37 @@ private List<NavigationItem> createItems() {
return items;
}

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

private boolean checkIfNotHidden(Page page) {
return !page.isHideInNav() || showHidden;
/**
* Check if a page should be shown in the breadcrumb
* A page should be shown if either
* <ul>
* <li>`showHidden` is set to true for this component; or,</li>
* <li>The page is not configured to be hidden in navigation</li>
* </ul>
*
* @param page The page to check.
* @return True if the page should be shown in the breadcrumb, false if not.
*/
private boolean checkIfNotHidden(@NotNull final Page page) {
return this.showHidden || !page.isHideInNav();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.adobe.cq.wcm.core.components.internal.models.v1;

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

import org.jetbrains.annotations.NotNull;

Expand All @@ -28,12 +28,29 @@

import static org.apache.sling.api.SlingConstants.PROPERTY_PATH;

@JsonIgnoreProperties(value = {"page", "children", "level", "description", "lastModified",PROPERTY_PATH})
/**
* V1 Breadcrumb Item Implementation.
*/
@JsonIgnoreProperties(value = {"page", "children", "level", "description", "lastModified", PROPERTY_PATH})
public class BreadcrumbItemImpl extends NavigationItemImpl implements NavigationItem {

public BreadcrumbItemImpl(Page page, boolean active, @NotNull LinkManager linkManager, int level,
List<NavigationItem> children, String parentId, Component component) {
super(page, active, active, linkManager, level, children, parentId, component);
/**
* Construct a Breadcrumb Item.
*
* @param page The page for which to create a breadcrumb item.
* @param active Flag indicating if the breadcrumb item is active.
* @param linkManager Link manager service.
* @param level Depth level of the navigation item.
* @param parentId ID of the parent navigation component.
* @param component The parent navigation {@link Component}.
*/
public BreadcrumbItemImpl(@NotNull final Page page,
final boolean active,
@NotNull final LinkManager linkManager,
final int level,
final String parentId,
final Component component) {
super(page, active, active, linkManager, level, Collections::emptyList, parentId, component);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
package com.adobe.cq.wcm.core.components.internal.models.v3;

import java.util.List;

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

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

/**
* V3 Breadcrumb component resource type.
*/
protected static final String RESOURCE_TYPE = "core/wcm/components/breadcrumb/v3/breadcrumb";

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.adobe.cq.wcm.core.components.internal.models.v3;

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

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -32,12 +32,29 @@

import static org.apache.sling.api.SlingConstants.PROPERTY_PATH;

@JsonIgnoreProperties(value = {"page", "children", "level", "description", "lastModified",PROPERTY_PATH})
/**
* V3 Breadcrumb Item Implementation.
*/
@JsonIgnoreProperties(value = {"page", "children", "level", "description", "lastModified", PROPERTY_PATH})
public class BreadcrumbItemImpl extends NavigationItemImpl implements NavigationItem {

public BreadcrumbItemImpl(Page page, boolean active, @NotNull LinkManager linkManager, int level,
List<NavigationItem> children, String parentId, Component component) {
super(page, active, active, linkManager, level, children, parentId, component);
/**
* Construct a Breadcrumb Item.
*
* @param page The page for which to create a breadcrumb item.
* @param active Flag indicating if the breadcrumb item is active.
* @param linkManager Link manager service.
* @param level Depth level of the navigation item.
* @param parentId ID of the parent navigation component.
* @param component The parent navigation {@link Component}.
*/
public BreadcrumbItemImpl(@NotNull final Page page,
final boolean active,
@NotNull final LinkManager linkManager,
final int level,
final String parentId,
final Component component) {
super(page, active, active, linkManager, level, Collections::emptyList, parentId, component);
}

@Override
Expand Down

0 comments on commit 80baa00

Please sign in to comment.