Skip to content

Commit

Permalink
GH-156 Spacing Model
Browse files Browse the repository at this point in the history
- add API in attribute service to generate classes,
- add a Spacing attribute worker that generates padding & margin classes
  • Loading branch information
npeltier committed Sep 25, 2020
1 parent 9384a20 commit 839a9ba
Show file tree
Hide file tree
Showing 32 changed files with 443 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.day.cq.wcm.api.WCMMode;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -150,6 +151,11 @@ public Map<String, String> getAttributes(SlingHttpServletRequest request) {
return attributes;
}

@Override
public Collection<String> getClasses(SlingHttpServletRequest request) {
return null;
}

@Override
public String getKey() {
return KEY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public void lockKey() {
assertEquals("authorvh", authorvh.getKey());
}

@Test
public void lockClasses() {
assertNull(authorvh.getClasses(context.request()));
}

@ExtendWith(WCMModeEditContext.class)
@Test
public void testGetAttributes() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
jcr:primaryType="sling:Folder"
styleWorkers="[background,color,shadow,border,flex-general,flex-definitions]"
attributeWorkers="[authorvh]"/>
attributeWorkers="[authorvh,spacing]"/>
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
end="{Long}1199"
key="tablet"
label="Tablet"
inheritBehaviourProp="inheritTablet"
inherit="inheritTablet"
mediaQuery="@media only screen and (min-width: 600px)"
propertySuffix="Tablet"
start="{Long}600"/>
<desktop jcr:primaryType="nt:unstructured"
key="desktop"
label="Desktop"
inheritBehaviourProp="inheritDesktop"
inherit="inheritDesktop"
mediaQuery="@media only screen and (min-width: 1200px)"
propertySuffix="Desktop"
start="{Long}1200"/>
Expand Down
2 changes: 2 additions & 0 deletions apps/docs/content/jcr_root/content/dx-docs/us/en/.content.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
inheritDesktop="{Boolean}true"
inheritTablet="{Boolean}true"
shadowColor="red"
marginLeftDesktop="{Long}100"
marginRightDesktop="{Long}100"
gapMobile="{Long}2"
focusXMobile="{Long}30"
minHeightMobile="custom"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/-->
<sly data-sly-use.model="com.adobe.dx.structure.flex.FlexModel"></sly>
<style data-sly-test="${model.style}">${model.style @ context='unsafe'}</style>
<div id="${model.id}" class="dx-flex">
<div id="${model.id}" class="dx-flex ${model.additionalClasses}">
<div data-sly-element="${dxPolicy.elementName}"
aria-label="${dxPolicy.ariaLabel}"
data-sly-attribute="${model.attributes}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
package com.adobe.dx.structure.flex;

import static com.adobe.dx.inlinestyle.Constants.DEL_SPACE;
import static com.adobe.dx.inlinestyle.Constants.RULE_DELIMITER;
import static com.adobe.dx.structure.flex.FlexModel.PN_MINHEIGHT;
import static com.adobe.dx.utils.CSSConstants.DEL_SPACE;
import static com.adobe.dx.utils.CSSConstants.RULE_DELIMITER;
import static com.adobe.dx.structure.flex.FlexModel.PN_MINHEIGHT_TYPE;
import static com.adobe.dx.structure.flex.FlexModel.PN_MINHEIGHT_VALUE;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
package com.adobe.dx.structure.flex;

import static com.adobe.dx.inlinestyle.Constants.DEL_SPACE;
import static com.adobe.dx.inlinestyle.Constants.PERCENT;
import static com.adobe.dx.inlinestyle.Constants.RULE_DELIMITER;
import static com.adobe.dx.utils.CSSConstants.DEL_SPACE;
import static com.adobe.dx.utils.CSSConstants.PERCENT;
import static com.adobe.dx.utils.CSSConstants.RULE_DELIMITER;
import static com.adobe.dx.structure.flex.FlexModel.PN_MINHEIGHT;
import static com.adobe.dx.structure.flex.FlexModel.PN_MINHEIGHT_TYPE;
import static com.adobe.dx.utils.RequestUtil.getInheritedMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public class FlexModel {

String style;

String additionalClasses;

Map<String, String> attributes;

@PostConstruct
Expand All @@ -64,6 +66,7 @@ void init() {
}
if (attributeService != null) {
attributes = attributeService.getAttributes(request);
additionalClasses = attributeService.getClassesString(request);
}
}

Expand All @@ -75,5 +78,9 @@ public String getStyle() {
return style;
}

public String getAdditionalClasses() {
return additionalClasses;
}

public Map<String, String> getAttributes() { return attributes; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import static org.junit.jupiter.api.Assertions.*;
import com.adobe.dx.testing.AbstractRequestModelTest;
import com.adobe.dx.testing.extensions.WCMModeDisabledContext;
import com.adobe.dx.testing.extensions.WCMModeEditContext;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -49,4 +48,8 @@ public void testAuthorVH() {
assertNull(model.getAttributes());
}

@Test
public void testClasses() {
assertNull(model.getAdditionalClasses());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,12 @@ public interface AttributeService {
*/
@Nullable Map<String, String> getAttributes(SlingHttpServletRequest request);

/**
* Specific case for the "class" attribute as several workers can want to add their contribution here
*
* @param request current component request context
* @return list of classes separated by space, corresponding to current request context
*/
@Nullable String getClassesString(SlingHttpServletRequest request);

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,22 @@

import com.adobe.dx.utils.Worker;

import java.util.Collection;
import java.util.Map;

import org.apache.sling.api.SlingHttpServletRequest;

public interface AttributeWorker extends Worker {

/**
* @return
* @param request current component request
* @return map of attributes from that worker
*/
Map<String, String> getAttributes(SlingHttpServletRequest request);

/**
* @param request current component request
* @return collection of classes from that worker
*/
Collection<String> getClasses(SlingHttpServletRequest request);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@

package com.adobe.dx.domtagging.internal;

import static com.adobe.dx.utils.CSSConstants.SPACE;

import com.adobe.dx.domtagging.AttributeService;
import com.adobe.dx.domtagging.AttributeWorker;
import com.adobe.dx.utils.AbstractWorkerManager;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -53,7 +56,7 @@ public class AttributeServiceImpl extends AbstractWorkerManager<AttributeWorker>
for (String key : getWorkerKeys(request.getResource())) {
AttributeWorker worker = workersMap.get(key);
if (worker != null) {
logger.debug("found worker {}", worker.getKey());
logger.debug("get attributes from worker {}", worker.getKey());
Map<String, String> workerMap = worker.getAttributes(request);
if (attributes == null) {
attributes = workerMap;
Expand All @@ -65,6 +68,23 @@ public class AttributeServiceImpl extends AbstractWorkerManager<AttributeWorker>
return attributes;
}

@Override
public @Nullable String getClassesString(SlingHttpServletRequest request) {
Collection<String> classes = null;
for (String key : getWorkerKeys(request.getResource())) {
AttributeWorker worker = workersMap.get(key);
if (worker != null) {
logger.debug("get classes from worker {}", worker.getKey());
Collection<String> workerClasses = worker.getClasses(request);
if (classes == null) {
classes = new ArrayList<>();
}
classes.addAll(workerClasses);
}
}
return classes != null ? String.join(SPACE, classes) : null;
}

@Override
protected List<AttributeWorker> getWorkers() {
return workers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,70 @@
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
package com.adobe.dx.domtagging.internal;

public class Spacing {
import static com.adobe.dx.utils.CSSConstants.CLASS_DELIMITER;
import static com.adobe.dx.utils.CSSConstants.PN_BOTTOM;
import static com.adobe.dx.utils.CSSConstants.PN_LEFT;
import static com.adobe.dx.utils.CSSConstants.PN_RIGHT;
import static com.adobe.dx.utils.CSSConstants.PN_TOP;

import com.adobe.dx.domtagging.AttributeWorker;
import com.adobe.dx.responsive.Breakpoint;
import com.adobe.dx.responsive.InheritedMap;
import com.adobe.dx.utils.RequestUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.ConfigurationPolicy;

@Component(configurationPolicy = ConfigurationPolicy.REQUIRE)
public class Spacing implements AttributeWorker {

private static final String KEY = "spacing";

private static final List<String> PREFIXES = Arrays.asList("margin", "padding");
private static final List<String> SIDES = Arrays.asList(PN_TOP, PN_RIGHT, PN_BOTTOM, PN_LEFT);

@Override
public Map<String, String> getAttributes(SlingHttpServletRequest request) {
return null;
}

Long getValue(Breakpoint breakpoint, String prefix, String side, InheritedMap map) {
return map.getInheritedValue(prefix + side, prefix, breakpoint,null);
}

@Override
public Collection<String> getClasses(SlingHttpServletRequest request) {
Collection<String> classes = null;
InheritedMap map = RequestUtil.getInheritedMap(request);
for (Breakpoint breakpoint : RequestUtil.getBreakpoints(request)) {
for (String prefix : PREFIXES) {
for (String side : SIDES) {
Long value = getValue(breakpoint, prefix, side, map);
if (value != null) {
if (classes == null) {
classes = new ArrayList<>();
}
classes.add(String.join(CLASS_DELIMITER, Arrays.asList(breakpoint.key(),
prefix,
side.toLowerCase(),
value.toString())));
}
}
}
}
return classes;
}

@Override
public String getKey() {
return KEY;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
package com.adobe.dx.inlinestyle.internal;

import static com.adobe.dx.inlinestyle.Constants.DEL_SPACE;
import static com.adobe.dx.utils.CSSConstants.DEL_SPACE;
import static com.adobe.dx.utils.RequestUtil.getFromRespProps;

import com.adobe.dx.responsive.Breakpoint;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,19 @@
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
package com.adobe.dx.inlinestyle.internal;

import static com.adobe.dx.inlinestyle.Constants.DECLARATION;
import static com.adobe.dx.inlinestyle.Constants.DEL_SPACE;
import static com.adobe.dx.inlinestyle.Constants.PX;
import static com.adobe.dx.inlinestyle.Constants.PX_SPACE;
import static com.adobe.dx.inlinestyle.Constants.SPACE;
import static com.adobe.dx.utils.CSSConstants.BOTTOM;
import static com.adobe.dx.utils.CSSConstants.LEFT;
import static com.adobe.dx.utils.CSSConstants.PN_BOTTOM;
import static com.adobe.dx.utils.CSSConstants.DECLARATION;
import static com.adobe.dx.utils.CSSConstants.DEL_SPACE;
import static com.adobe.dx.utils.CSSConstants.PN_LEFT;
import static com.adobe.dx.utils.CSSConstants.PX;
import static com.adobe.dx.utils.CSSConstants.PX_SPACE;
import static com.adobe.dx.utils.CSSConstants.PN_RIGHT;
import static com.adobe.dx.utils.CSSConstants.RIGHT;
import static com.adobe.dx.utils.CSSConstants.SPACE;
import static com.adobe.dx.utils.CSSConstants.PN_TOP;
import static com.adobe.dx.utils.CSSConstants.TOP;
import static com.adobe.dx.utils.RequestUtil.getPolicy;

import com.adobe.dx.responsive.Breakpoint;
Expand All @@ -45,24 +53,20 @@ public class Border implements InlineStyleWorker {
private static final String STYLE_SUFFIX = "Style";
private static final String WIDTH_SUFFIX = "Width";
private static final String RADIUS = "Radius";
private static final String TOP = "Top";
private static final String RIGHT = "Right";
private static final String LEFT = "Left";
private static final String BOTTOM = "Bottom";
private static final String DECL_RADIUS = "border-radius: ";
private static final String DECL_TOP = DECL_PREFIX + "-top";
private static final String DECL_BOTTOM = DECL_PREFIX + "-bottom";
private static final String DECL_RIGHT = DECL_PREFIX + "-right";
private static final String DECL_LEFT = DECL_PREFIX + "-left";
private static final String DECL_TOP = DECL_PREFIX + TOP;
private static final String DECL_BOTTOM = DECL_PREFIX + BOTTOM;
private static final String DECL_RIGHT = DECL_PREFIX + RIGHT;
private static final String DECL_LEFT = DECL_PREFIX + LEFT;
private static final String ALL = "all";
private static final String EACH = "each";
private static final String ALL_CAP = "All";
private static final String PN_BORDERRADIUS = PREFIX + RADIUS;
private static final String PN_ALLRADIUS = PREFIX + ALL_CAP + RADIUS;
private static final String PN_RADIUS_TOPLEFT = PREFIX + RADIUS + TOP + LEFT;
private static final String PN_RADIUS_TOPRIGHT = PREFIX + RADIUS + TOP + RIGHT;
private static final String PN_RADIUS_BOTTOMLEFT = PREFIX + RADIUS + BOTTOM + LEFT;
private static final String PN_RADIUS_BOTTOMRIGHT = PREFIX + RADIUS + BOTTOM + RIGHT;
private static final String PN_RADIUS_TOPLEFT = PREFIX + RADIUS + PN_TOP + PN_LEFT;
private static final String PN_RADIUS_TOPRIGHT = PREFIX + RADIUS + PN_TOP + PN_RIGHT;
private static final String PN_RADIUS_BOTTOMLEFT = PREFIX + RADIUS + PN_BOTTOM + PN_LEFT;
private static final String PN_RADIUS_BOTTOMRIGHT = PREFIX + RADIUS + PN_BOTTOM + PN_RIGHT;
private static final String PN_SIDES = PREFIX + "Sides";

@Override
Expand Down Expand Up @@ -112,10 +116,10 @@ private String getAllBorders(SlingHttpServletRequest request, ValueMap policy) {

private String getEachBorder(SlingHttpServletRequest request, ValueMap policy) {
List<String> borders = new ArrayList<>();
final String topBorder = getBorderStyle(request, policy, TOP, DECL_TOP);
final String rightBorder = getBorderStyle(request, policy, RIGHT, DECL_RIGHT);
final String bottomBorder = getBorderStyle(request, policy, BOTTOM, DECL_BOTTOM);
final String leftBorder = getBorderStyle(request, policy, LEFT, DECL_LEFT);
final String topBorder = getBorderStyle(request, policy, PN_TOP, DECL_TOP);
final String rightBorder = getBorderStyle(request, policy, PN_RIGHT, DECL_RIGHT);
final String bottomBorder = getBorderStyle(request, policy, PN_BOTTOM, DECL_BOTTOM);
final String leftBorder = getBorderStyle(request, policy, PN_LEFT, DECL_LEFT);
if (topBorder != null) {
borders.add(topBorder);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

package com.adobe.dx.inlinestyle.internal;

import static com.adobe.dx.inlinestyle.Constants.DECLARATION_DELIMITER;
import static com.adobe.dx.inlinestyle.Constants.RULE_DELIMITER;
import static com.adobe.dx.utils.CSSConstants.DECLARATION_DELIMITER;
import static com.adobe.dx.utils.CSSConstants.RULE_DELIMITER;
import static org.apache.commons.lang3.StringUtils.EMPTY;

import com.adobe.dx.responsive.Breakpoint;
Expand Down
Loading

0 comments on commit 839a9ba

Please sign in to comment.