Skip to content

Commit

Permalink
#376 [Tabs][Carousel] Panel title should not override child item title
Browse files Browse the repository at this point in the history
  • Loading branch information
richardhand committed Nov 14, 2018
1 parent b3e5791 commit 1ce0c9a
Show file tree
Hide file tree
Showing 16 changed files with 123 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public class Item {
protected String iconPath;
protected String iconAbbreviation;

/**
* Name of the resource property that defines a panel title
*/
String PN_PANEL_TITLE = "cq:panelTitle";

/**
* Name of the resource property that defines a component icon
*/
Expand Down Expand Up @@ -72,7 +77,8 @@ public Item(SlingHttpServletRequest request, Resource resource) {
name = resource.getName();
ValueMap vm = resource.adaptTo(ValueMap.class);
if (vm != null) {
value = vm.get(JcrConstants.JCR_TITLE, String.class);
String jcrTitle = vm.get(JcrConstants.JCR_TITLE, String.class);
value = vm.get(PN_PANEL_TITLE, jcrTitle);
}
}
ComponentManager componentManager = request.getResourceResolver().adaptTo(ComponentManager.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import org.apache.sling.models.annotations.injectorspecific.Self;
import org.apache.sling.models.annotations.injectorspecific.SlingObject;

import com.adobe.cq.wcm.core.components.models.ListItem;
import com.adobe.cq.wcm.core.components.models.Container;
import com.adobe.cq.wcm.core.components.models.ContainerItem;
import com.day.cq.wcm.api.components.Component;
import com.day.cq.wcm.api.components.ComponentManager;

Expand All @@ -39,17 +39,17 @@ public abstract class AbstractContainerImpl implements Container {
@Self
private SlingHttpServletRequest request;

private List<ListItem> items;
private List<ContainerItem> items;

private List<ListItem> readItems() {
List<ListItem> items = new ArrayList<>();
private List<ContainerItem> readItems() {
List<ContainerItem> items = new ArrayList<>();
if (resource != null) {
ComponentManager componentManager = request.getResourceResolver().adaptTo(ComponentManager.class);
if (componentManager != null) {
for (Resource res : resource.getChildren()) {
Component component = componentManager.getComponentOfResource(res);
if (component != null) {
items.add(new ResourceListItemImpl(request, res));
items.add(new ContainerItemImpl(request, res));
}
}
}
Expand All @@ -58,7 +58,7 @@ private List<ListItem> readItems() {
}

@Override
public List<ListItem> getItems() {
public List<ContainerItem> getItems() {
if (items == null) {
items = readItems();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ Copyright 2017 Adobe Systems Incorporated
~ Copyright 2018 Adobe Systems Incorporated
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
Expand All @@ -15,8 +15,6 @@
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
package com.adobe.cq.wcm.core.components.internal.models;

import java.util.Calendar;

import javax.annotation.Nonnull;

import org.apache.sling.api.resource.Resource;
Expand All @@ -25,33 +23,24 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.adobe.cq.wcm.core.components.models.ListItem;
import com.adobe.cq.wcm.core.components.models.ContainerItem;
import com.day.cq.commons.jcr.JcrConstants;

public class ResourceListItemImpl implements ListItem {
public class ContainerItemImpl implements ContainerItem {

private static final Logger LOGGER = LoggerFactory.getLogger(ResourceListItemImpl.class);
private static final Logger LOGGER = LoggerFactory.getLogger(ContainerItemImpl.class);

protected String url;
protected SlingHttpServletRequest request;
protected String title;
protected String description;
protected Calendar lastModified;
protected String path;
protected String name;

public ResourceListItemImpl(@Nonnull SlingHttpServletRequest request, @Nonnull Resource resource) {
public ContainerItemImpl(@Nonnull SlingHttpServletRequest request, @Nonnull Resource resource) {
ValueMap valueMap = resource.adaptTo(ValueMap.class);
if (valueMap != null) {
title = valueMap.get(JcrConstants.JCR_TITLE, String.class);
description = valueMap.get(JcrConstants.JCR_DESCRIPTION, String.class);
lastModified = valueMap.get(JcrConstants.JCR_LASTMODIFIED, Calendar.class);
String jcrTitle = valueMap.get(JcrConstants.JCR_TITLE, String.class);
title = valueMap.get(ContainerItem.PN_PANEL_TITLE, jcrTitle);
}
path = resource.getPath();
url = null;
}

@Override
public String getURL() {
return url;
name = resource.getName();
}

@Override
Expand All @@ -60,18 +49,8 @@ public String getTitle() {
}

@Override
public String getDescription() {
return description;
}

@Override
public Calendar getLastModified() {
return lastModified;
}

@Override
public String getPath() {
return path;
public String getName() {
return name;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ public class TabsImpl extends AbstractContainerImpl implements Tabs {
@ValueMapValue(optional = true)
private String activeItem;

private String activeItemPath;
private String activeName;

@Override
public String getActiveItem() {
if (activeItemPath == null) {
if (activeName == null) {
Resource active = resource.getChild(activeItem);
if (active != null) {
activeItemPath = active.getPath();
activeName = activeItem;
}
}
return activeItemPath;
return activeName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public interface Carousel extends Container {
/**
* Name of the resource property that indicates whether automatic pause on hovering the carousel is disabled, or not.
*
* @since com.adobe.cq.wcm.core.components.models 12.6.0
* @since com.adobe.cq.wcm.core.components.models 13.0.0
*/
String PN_AUTOPAUSE_DISABLED = "autopauseDisabled";

Expand Down Expand Up @@ -70,7 +70,7 @@ default Long getDelay() {
* Indicates whether automatic pause on hovering the carousel is disabled, or not.
*
* @return {@code true} if automatic pause on hovering the carousel should be disabled; {@code false} otherwise
* @since com.adobe.cq.wcm.core.components.models 12.6.0
* @since com.adobe.cq.wcm.core.components.models 13.0.0
*/
default boolean getAutopauseDisabled() {
throw new UnsupportedOperationException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@
public interface Container extends ContainerExporter {

/**
* Returns a list of component items
* Returns a list of container items
*
* @return List of component items
* @return List of container items
* @since com.adobe.cq.wcm.core.components.models 12.5.0
*/
default List<ListItem> getItems() {
default List<ContainerItem> getItems() {
throw new UnsupportedOperationException();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ Copyright 2018 Adobe Systems Incorporated
~
~ 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.adobe.cq.wcm.core.components.models;

import javax.annotation.Nullable;

import org.osgi.annotation.versioning.ConsumerType;

/**
* Interface for a generic container item, used by the {@link Tabs} and {@link Carousel} models.
*
* @since com.adobe.cq.wcm.core.components.models 13.0.0
*/
@ConsumerType
public interface ContainerItem {

/**
* Name of the resource property that defines the container item title
*
* @since com.adobe.cq.wcm.core.components.models 13.0.0
*/
String PN_PANEL_TITLE = "cq:panelTitle";

/**
* Returns the title of this {@code ContainerItem}.
*
* @return the title of this container item or {@code null}
* @since com.adobe.cq.wcm.core.components.models 13.0.0
*/
@Nullable
default String getTitle() {
throw new UnsupportedOperationException();
}

/**
* Returns the name of this {@code ContainerItem}.
*
* @return the container item name or {@code null}
* @since com.adobe.cq.wcm.core.components.models 13.0.0
*/
@Nullable
default String getName() {
throw new UnsupportedOperationException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* version, is bound to this proxy component resource type.
* </p>
*/
@Version("12.6.0")
@Version("13.0.0")
package com.adobe.cq.wcm.core.components.models;

import org.osgi.annotation.versioning.Version;
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

import com.adobe.cq.sightly.WCMBindings;
import com.adobe.cq.wcm.core.components.context.CoreComponentTestContext;
import com.adobe.cq.wcm.core.components.models.ListItem;
import com.adobe.cq.wcm.core.components.models.ContainerItem;
import com.adobe.cq.wcm.core.components.models.Carousel;
import com.day.cq.wcm.api.designer.Style;
import io.wcm.testing.mock.aem.junit.AemContext;
Expand Down Expand Up @@ -59,16 +59,16 @@ public static void init() {
@Test
public void testEmptyCarousel() {
Carousel carousel = new CarouselImpl();
List<ListItem> items = carousel.getItems();
List<ContainerItem> items = carousel.getItems();
assertTrue("", items == null || items.size() == 0);
}

@Test
public void testCarouselWithItems() {
Carousel carousel = getCarouselUnderTest(CAROUSEL_1);
Object[][] expectedItems = {
{"/content/carousel/jcr:content/root/responsivegrid/carousel-1/item_1", "Teaser 1", "Teaser 1 description"},
{"/content/carousel/jcr:content/root/responsivegrid/carousel-1/item_2", "Teaser 2", "Teaser 2 description"},
{"item_1", "Teaser 1"},
{"item_2", "Teaser 2"},
};
verifyCarouselItems(expectedItems, carousel.getItems());
//Utils.testJSONExport(carousel, Utils.getTestExporterJSONPath(TEST_BASE, "carousel1"));
Expand Down Expand Up @@ -104,16 +104,14 @@ private Carousel getCarouselUnderTest(String resourcePath) {
return request.adaptTo(Carousel.class);
}

private void verifyCarouselItems(Object[][] expectedItems, List<ListItem> items) {
private void verifyCarouselItems(Object[][] expectedItems, List<ContainerItem> items) {
assertEquals("The carousel contains a different number of items than expected.", expectedItems.length, items.size());
int index = 0;
for (ListItem item : items) {
assertEquals("The carousel item's path is not what was expected.",
expectedItems[index][0], item.getPath());
for (ContainerItem item : items) {
assertEquals("The carousel item's name is not what was expected.",
expectedItems[index][0], item.getName());
assertEquals("The carousel item's title is not what was expected: " + item.getTitle(),
expectedItems[index][1], item.getTitle());
assertEquals("The carousel item's description is not what was expected: " + item.getDescription(),
expectedItems[index][2], item.getDescription());
index++;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import com.adobe.cq.sightly.WCMBindings;
import com.adobe.cq.wcm.core.components.Utils;
import com.adobe.cq.wcm.core.components.context.CoreComponentTestContext;
import com.adobe.cq.wcm.core.components.models.ListItem;
import com.adobe.cq.wcm.core.components.models.ContainerItem;
import com.adobe.cq.wcm.core.components.models.Tabs;
import io.wcm.testing.mock.aem.junit.AemContext;

Expand All @@ -56,19 +56,19 @@ public static void init() {
@Test
public void testEmptyTabs() {
Tabs tabs = new TabsImpl();
List<ListItem> items = tabs.getItems();
List<ContainerItem> items = tabs.getItems();
Assert.assertTrue("", items == null || items.size() == 0);
}

@Test
public void testTabsWithItems() {
Tabs tabs = getTabsUnderTest(TABS_1);
Object[][] expectedItems = {
{"/content/tabs/jcr:content/root/responsivegrid/tabs-1/item_1", "Teaser 1", "Teaser 1 description"},
{"/content/tabs/jcr:content/root/responsivegrid/tabs-1/item_2", "Teaser 2", "Teaser 2 description"},
{"item_1", "Teaser 1"},
{"item_2", "Teaser 2"},
};
verifyTabItems(expectedItems, tabs.getItems());
assertEquals("/content/tabs/jcr:content/root/responsivegrid/tabs-1/item_2", tabs.getActiveItem());
assertEquals("item_2", tabs.getActiveItem());
Utils.testJSONExport(tabs, Utils.getTestExporterJSONPath(TEST_BASE, "tabs1"));
}

Expand All @@ -89,16 +89,14 @@ private Tabs getTabsUnderTest(String resourcePath) {
return request.adaptTo(Tabs.class);
}

private void verifyTabItems(Object[][] expectedItems, List<ListItem> items) {
private void verifyTabItems(Object[][] expectedItems, List<ContainerItem> items) {
assertEquals("The tabs contains a different number of items than expected.", expectedItems.length, items.size());
int index = 0;
for (ListItem item : items) {
assertEquals("The tabs item's path is not what was expected.",
expectedItems[index][0], item.getPath());
for (ContainerItem item : items) {
assertEquals("The tabs item's name is not what was expected.",
expectedItems[index][0], item.getName());
assertEquals("The tabs item's title is not what was expected: " + item.getTitle(),
expectedItems[index][1], item.getTitle());
assertEquals("The tabs item's description is not what was expected: " + item.getDescription(),
expectedItems[index][2], item.getDescription());
index++;
}
}
Expand Down
16 changes: 5 additions & 11 deletions bundles/core/src/test/resources/tabs/exporter-tabs1.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
{
"items": [
{
"url": null,
"title": "Teaser 1",
"description": "Teaser 1 description",
"lastModified": null,
"path": "/content/tabs/jcr:content/root/responsivegrid/tabs-1/item_1"
"name": "item_1",
"title": "Teaser 1"
},
{
"url": null,
"title": "Teaser 2",
"description": "Teaser 2 description",
"lastModified": null,
"path": "/content/tabs/jcr:content/root/responsivegrid/tabs-1/item_2"
"name": "item_2",
"title": "Teaser 2"
}
],
"activeItem":"/content/tabs/jcr:content/root/responsivegrid/tabs-1/item_2"
"activeItem": "item_2"
}
Loading

0 comments on commit 1ce0c9a

Please sign in to comment.