Skip to content

Commit

Permalink
adobeGH-24 add minHeight, gap & definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
npeltier committed Aug 31, 2020
1 parent 1dd01ce commit 4cf2922
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,23 @@
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/-->
<template data-sly-template.background="${ @ id}">
<sly data-sly-repeat.bp="${breakpoints}">

<sly data-sly-test="${resprops.backgroundColor[bp.key] || resprops.gradient[bp.key]}">
<sly data-sly-test="${bp.mediaQuery}">
${bp.mediaQuery}{
</sly>
<sly data-sly-test="${bp.mediaQuery}">${bp.mediaQuery}{</sly>
${'#{0}' @ format=id, context='styleString'} {
<sly data-sly-test="${resprops.backgroundColor[bp.key]}">
${'background-color: {0}; ' @ format=resprops.backgroundColor[bp.key], context='styleString'}
</sly>
<sly data-sly-test="${resprops.gradient[bp.key]}">
${'background-image: {0};' @ format=[resprops.gradient[bp.key]], context='styleString'}
</sly>
}
<sly data-sly-test="${bp.mediaQuery}">}</sly>
</sly>}<sly data-sly-test="${bp.mediaQuery}">}</sly>
</sly>
</sly>
</template>
<template data-sly-template.foreground="${ @ id}">
<sly data-sly-repeat.bp="${breakpoints}"
data-sly-test="${resprops.foregroundColor[bp.key]}">
<sly data-sly-test.mediaQuery="${bp.mediaQuery}">
${bp.mediaQuery}{
</sly>
${'#{0}' @ format=id, context='styleString'} {
${'color: {0}; ' @ format=resprops.foregroundColor[bp.key], context='styleString'}
}
<sly data-sly-test.mediaQuery="${bp.mediaQuery}">${bp.mediaQuery}{</sly>
${'#{0}' @ format=id, context='styleString'} { ${'color: {0}; ' @ format=resprops.foregroundColor[bp.key], context='styleString'}}
<sly data-sly-test="${bp.mediaQuery}">}</sly>
</sly>
</template>
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,27 @@
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/-->
<sly data-sly-use.model="com.adobe.dx.structure.flex.FlexModel"></sly>
<style data-sly-test="${model.styleNeeded}">
<sly data-sly-use.colorTpl="color.html" data-sly-call="${colorTpl.background @ id = model.id}"></sly>
<sly data-sly-repeat.bp="${breakpoints}">
<sly data-sly-test="${!resprops.inherit[bp] && (resprops.minHeight[bp] || resprops.gapContainer[bp] || resprops.gap[bp] || resprops.definitions[bp])}">
<sly data-sly-test="${bp.mediaQuery}">${bp.mediaQuery}{</sly>
${'#{0}' @ format=model.id, context='styleString'} > .dx-flex-items {
${resprops.minHeight[bp] @ context = 'styleString'}
${resprops.gapContainer[bp] @ context = 'styleString'}
}
<sly data-sly-test="${resprops.gap[bp]}">
${'#{0}' @ format=model.id, context='styleString'} > .dx-flex-items > * {${model.gap[bp] @ context = 'styleString'}}
</sly>
<sly data-sly-repeat.definition="${model.definitions[bp]}">
${'#{0}' @ format=model.id, context='styleString'} > .dx-flex-items > *:nth-child(${model.definitions[bp].size @ context='unsafe' }) {
${definition.width}
${definition.minHeight}
${definition.order}
}
</sly>
<sly data-sly-test="${bp.mediaQuery}">}</sly>
</sly>
</sly>
<sly data-sly-use.colorTpl="color.html" data-sly-call="${colorTpl.background @ id = model.id}"></sly>
</style>
<div id="${model.id}" class="dx-flex">
<div class="dx-flex-items" data-sly-resource="${'./items' @ resourceType='dx/structure/components/parlite'}"></div>
Expand Down
2 changes: 1 addition & 1 deletion apps/structure/core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ Bundle-DocURL:
<dependency>
<groupId>com.adobe.dx</groupId>
<artifactId>testing</artifactId>
<version>0.0.10</version>
<version>0.0.11-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.adobe.dx</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,57 @@
package com.adobe.dx.structure.flex;

import com.adobe.dx.domtagging.IDTagger;
import com.adobe.dx.responsive.Breakpoint;
import com.day.cq.wcm.api.policies.ContentPolicy;
import com.day.cq.wcm.api.policies.ContentPolicyManager;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import javax.annotation.PostConstruct;

import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.OSGiService;
import org.apache.sling.models.annotations.injectorspecific.ScriptVariable;
import org.apache.sling.models.annotations.injectorspecific.SlingObject;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;

@Model(adaptables = SlingHttpServletRequest.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class FlexModel {
private static final String NN_DEFINITIONS = "definitions";

@SlingObject
protected SlingHttpServletRequest request;

@OSGiService
IDTagger idTagger;

@ScriptVariable
Breakpoint[] breakpoints;

Map<String, Breakpoint> bpMap;
Map<String, List<ValueMap>> definitionsMap = new HashMap<>();

String id;

@PostConstruct
void init() {
bpMap = new HashMap<>();
if (breakpoints != null) {
for (Breakpoint breakpoint : breakpoints) {
bpMap.put(breakpoint.key(), breakpoint);
}
}
}

public boolean isStyleNeeded() {
return true;
}
Expand All @@ -46,4 +78,40 @@ public String getId() {
}
return id;
}

private Resource getPolicyResource(String name) {
ResourceResolver resolver = request.getResourceResolver();
ContentPolicyManager policyManager = resolver.adaptTo(ContentPolicyManager.class);
if (policyManager != null) {
ContentPolicy contentPolicy = policyManager.getPolicy(request.getResource());
if (contentPolicy != null) {
String path = contentPolicy.getPath();
return resolver.getResource(path + "/" + name);
}
}
return null;
}

private String computeResponsiveResourceName(String name, Breakpoint breakpoint) {
return name + breakpoint.propertySuffix();
}

public List<ValueMap> getDefinitions(String breakpointKey) {
if (definitionsMap.containsKey(breakpointKey)) {
return definitionsMap.get(breakpointKey);
}
if (bpMap.containsKey(breakpointKey)) {
String resourceName = computeResponsiveResourceName(NN_DEFINITIONS, bpMap.get(breakpointKey));
Resource parent = request.getResource().getChild(resourceName);
if (parent == null) {
parent = getPolicyResource(resourceName);
}
if (parent != null) {
definitionsMap.put(breakpointKey, StreamSupport.stream(parent.getChildren().spliterator(), false)
.map(Resource::getValueMap)
.collect(Collectors.toList()));
}
}
return definitionsMap.get(breakpointKey);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
~ See the License for the specific language governing permissions and
~ limitations under the License.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
@Version("0.0.1")
@Version("0.0.2")
package com.adobe.dx.structure.flex;

import org.osgi.annotation.versioning.Version;
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,18 @@
import static org.mockito.Mockito.when;

import com.adobe.dx.domtagging.IDTagger;
import com.adobe.dx.responsive.Breakpoint;
import com.adobe.dx.responsive.ResponsiveConfiguration;
import com.adobe.dx.testing.AbstractRequestModelTest;
import com.day.cq.wcm.api.policies.ContentPolicy;
import com.day.cq.wcm.api.policies.ContentPolicyManager;

import java.util.List;

import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.wrappers.CompositeValueMap;
import org.apache.sling.caconfig.ConfigurationBuilder;
import org.apache.sling.testing.mock.caconfig.MockContextAwareConfig;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -32,8 +42,34 @@ class FlexModelTest extends AbstractRequestModelTest {

@BeforeEach
public void setup() throws ReflectiveOperationException {
context.build().resource(CONTENT_ROOT,"title", "flex test").commit();
context.build().resource(CONF_ROOT + "/sling:configs/" + ResponsiveConfiguration.class.getName() + "/breakpoints")
.siblingsMode()
.resource("1","propertySuffix", "Mobile", "key", "mobile")
.resource("2", "propertySuffix", "Tablet", "key", "tablet")
.resource("3", "propertySuffix", "Desktop", "key", "desktop");
MockContextAwareConfig.registerAnnotationClasses(context, ResponsiveConfiguration.class);
MockContextAwareConfig.registerAnnotationClasses(context, Breakpoint.class);
context.create().resource(CONTENT_ROOT, "sling:configRef", CONF_ROOT);
ResponsiveConfiguration configuration = context.resourceResolver()
.getResource(CONTENT_ROOT)
.adaptTo(ConfigurationBuilder.class)
.as(ResponsiveConfiguration.class);
context.build().resource(CONTENT_ROOT,
"sling:resourceType", "dx/structure/components/flex",
"title", "dx flex component")
.resource("definitionsMobile")
.siblingsMode()
.resource("1", "minHeight", "custom")
.resource("2", "minHeight", "custom");
context.currentResource(CONTENT_ROOT);
context.contentPolicyMapping("dx/structure/components/flex", "blah", "blah");
ContentPolicy policy = context.resourceResolver()
.adaptTo(ContentPolicyManager.class).getPolicy(context.currentResource());
context.build().resource(policy.getPath() + "/definitionsTablet/items0",
"minHeight", "custom");
model = getModel(FlexModel.class, CONTENT_ROOT);
model.breakpoints = configuration.breakpoints();
model.init();
}

@Test
Expand All @@ -48,9 +84,20 @@ public void testNonWorkingId() {
assertNull(model.getId());
}


@Test
public void testIsNeeded() throws ReflectiveOperationException {
public void testIsNeeded() {
assertTrue(model.isStyleNeeded());
}

@Test
public void testGetDefinitions() {
List<ValueMap> mobile = model.getDefinitions("mobile");
List<ValueMap> tablet = model.getDefinitions("tablet");
assertNotNull(mobile);
assertNotNull(tablet);
assertNull(model.getDefinitions("desktop"));
assertEquals(2, mobile.size());
assertEquals(1, tablet.size());
assertEquals("custom", mobile.get(0).get("minHeight"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,17 @@ public ResponsiveProperties(final ResponsiveConfiguration configuration, ValueMa
this.properties = properties;
}

private String computeResponsiveResourceName(String name, Breakpoint breakpoint) {
return name + breakpoint.propertySuffix();
}

@Override
public Object get(Object key) {
if (key != null) {
boolean empty = true;
LinkedHashMap<String,String> breakpointValues = new LinkedHashMap<>();
for (Breakpoint breakpoint : breakpoints) {
String respKey = key + breakpoint.propertySuffix();
String respKey = computeResponsiveResourceName(key.toString(), breakpoint);
String value = properties.get(respKey, String.class);
empty &= StringUtils.isBlank(value);
breakpointValues.put(breakpoint.key(), value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.adobe.dx.testing;

import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.scripting.SlingBindings;
import org.apache.sling.models.factory.ModelFactory;

public class AbstractRequestModelTest extends AbstractTest {
Expand All @@ -34,4 +35,9 @@ protected <T> T getModel(final Class<T> type, String path) throws ReflectiveOper
return getModel(type);
}

protected void addBinding(String binding, Object value) {
SlingBindings bindings = (SlingBindings) context.request().getAttribute(SlingBindings.class.getName());
bindings.put(binding, value);
}

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

import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.scripting.SlingBindings;
import org.apache.sling.testing.mock.sling.ResourceResolverType;
import org.junit.jupiter.api.extension.ExtendWith;

Expand Down

0 comments on commit 4cf2922

Please sign in to comment.