Skip to content

Commit

Permalink
adobeGH-24 make StyleService responsive
Browse files Browse the repository at this point in the history
  • Loading branch information
npeltier committed Sep 14, 2020
1 parent 2b73736 commit 16fffdb
Show file tree
Hide file tree
Showing 10 changed files with 313 additions and 98 deletions.
11 changes: 6 additions & 5 deletions bundles/core/src/main/java/com/adobe/dx/style/StyleWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
package com.adobe.dx.style;

import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import com.adobe.dx.responsive.Breakpoint;

import org.apache.sling.api.SlingHttpServletRequest;
import org.jetbrains.annotations.Nullable;

/**
Expand All @@ -32,10 +33,10 @@ public interface StyleWorker {
/**
* Generates a declaration specific to that generator, for an upper rule
*
* @param resource current component resource,
* @param dxPolicy policy, could be obtained from above resource, but in the signature for practical reason,
* @param breakpoint breakpoint if declaration should be specific to one, null otherwise
* @param request current request
* @return single or several declarations split by ';', or null if not necessary or able to generate some
*/
@Nullable String getDeclaration(Resource resource, ValueMap dxPolicy);
@Nullable String getDeclaration(@Nullable Breakpoint breakpoint, SlingHttpServletRequest request);

}
29 changes: 17 additions & 12 deletions bundles/core/src/main/java/com/adobe/dx/style/internal/Border.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@
import static com.adobe.dx.style.Constants.PX;
import static com.adobe.dx.style.Constants.PX_SPACE;
import static com.adobe.dx.style.Constants.SPACE;
import static com.adobe.dx.utils.RequestUtil.getPolicy;

import com.adobe.dx.responsive.Breakpoint;
import com.adobe.dx.style.StyleWorker;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.ValueMap;
import org.jetbrains.annotations.Nullable;
import org.osgi.service.component.annotations.Component;
Expand Down Expand Up @@ -68,18 +70,21 @@ public String getKey() {
}

@Override
public @Nullable String getDeclaration(Resource resource, ValueMap dxPolicy) {
List<String> declarations = null;
String border = buildBorder(dxPolicy);
String radius = buildRadius(dxPolicy);
if (border != null) {
if (radius != null) {
return border + DEL_SPACE + radius;
} else {
return border;
public @Nullable String getDeclaration(Breakpoint breakpoint, SlingHttpServletRequest request) {
if (breakpoint == null) {
//we only do border for all
ValueMap dxPolicy = getPolicy(request);
String border = buildBorder(dxPolicy);
String radius = buildRadius(dxPolicy);
if (border != null) {
if (radius != null) {
return border + DEL_SPACE + radius;
} else {
return border;
}
} else if (radius != null) {
return radius;
}
} else if (radius != null) {
return radius;
}
return null;
}
Expand Down
34 changes: 20 additions & 14 deletions bundles/core/src/main/java/com/adobe/dx/style/internal/Shadow.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
package com.adobe.dx.style.internal;

import static com.adobe.dx.style.Constants.PX_SPACE;
import static com.adobe.dx.utils.RequestUtil.getPolicy;

import com.adobe.dx.responsive.Breakpoint;
import com.adobe.dx.style.StyleWorker;

import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.ValueMap;
import org.jetbrains.annotations.Nullable;
import org.osgi.service.component.annotations.Component;
Expand All @@ -45,20 +47,24 @@ public String getKey() {
}

@Override
public @Nullable String getDeclaration(Resource resource, ValueMap dxPolicy) {
String color = dxPolicy.get(PN_COLOR, String.class);
if (color != null) {
StringBuilder sb = new StringBuilder();
sb.append(RULE)
.append(dxPolicy.get(PN_OFFSETX, 0L)).append(PX_SPACE)
.append(dxPolicy.get(PN_OFFSETY, 0L)).append(PX_SPACE)
.append(dxPolicy.get(PN_BLUR, 0L)).append(PX_SPACE)
.append(dxPolicy.get(PN_SPREAD, 0L)).append(PX_SPACE)
.append(color);
if (dxPolicy.containsKey(PN_INSET)) {
sb.append(INSET_SUFFIX);
public @Nullable String getDeclaration(Breakpoint breakpoint, SlingHttpServletRequest request) {
if (breakpoint == null) {
//we only do border for all
ValueMap dxPolicy = getPolicy(request);
String color = dxPolicy.get(PN_COLOR, String.class);
if (color != null) {
StringBuilder sb = new StringBuilder();
sb.append(RULE)
.append(dxPolicy.get(PN_OFFSETX, 0L)).append(PX_SPACE)
.append(dxPolicy.get(PN_OFFSETY, 0L)).append(PX_SPACE)
.append(dxPolicy.get(PN_BLUR, 0L)).append(PX_SPACE)
.append(dxPolicy.get(PN_SPREAD, 0L)).append(PX_SPACE)
.append(color);
if (dxPolicy.containsKey(PN_INSET)) {
sb.append(INSET_SUFFIX);
}
return sb.toString();
}
return sb.toString();
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@
package com.adobe.dx.style.internal;

import static com.adobe.dx.style.Constants.DECLARATION_DELIMITER;
import static com.day.cq.wcm.commons.Constants.EMPTY_STRING_ARRAY;
import static org.apache.commons.lang3.StringUtils.EMPTY;

import com.adobe.dx.bindings.internal.DxBindingsValueProvider;
import com.adobe.dx.responsive.Breakpoint;
import com.adobe.dx.style.StyleWorker;
import com.adobe.dx.style.StyleService;
import com.adobe.dx.utils.RequestUtil;

import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -30,10 +33,11 @@

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.record.PageBreakRecord;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.scripting.SlingBindings;
import org.jetbrains.annotations.NotNull;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.ConfigurationPolicy;
import org.osgi.service.component.annotations.Reference;
Expand All @@ -47,6 +51,7 @@
public class StyleServiceImpl implements StyleService {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
private static final String FORMAT_ID = "#%s {%s}";
private static final String FORMAT_BP = "\n%s {\n%s\n}";
private static final String SLASH = "/";
private static final String TYPE_PREFIX = "/apps/";
private static final String STYLEWORKERS_SUFFIX = "/styleWorkers";
Expand All @@ -59,33 +64,49 @@ public class StyleServiceImpl implements StyleService {

Map<String, StyleWorker> workerMap = MapUtils.EMPTY_MAP;

String getStylePerBreakpoint(String id, Breakpoint breakpoint, String[] keys, SlingHttpServletRequest request) {
List<String> declarations = null;
for (String workerKey : keys) {
if (workerMap.containsKey(workerKey)) {
logger.debug("found {} worker", workerKey);
String declaration = workerMap.get(workerKey).getDeclaration(breakpoint, request);
if (StringUtils.isNotBlank(declaration)) {
logger.debug("generated {}", declaration);
if (declarations == null) {
declarations = new ArrayList<>();
}
declarations.add(declaration);
}
}
}
if (declarations != null && !declarations.isEmpty()) {
String concat = String.join(DECLARATION_DELIMITER, declarations);
return StringUtils.isNotBlank(id) ? String.format(FORMAT_ID, id, concat) : concat;
}
return EMPTY;
}

@Override
public String getLocalStyle(String id, SlingHttpServletRequest request) {
List<String> declarations = null;
Resource resource = request.getResource();
String[] keys = getWorkerKeys(resource);
if (keys != null) {
SlingBindings bindings = (SlingBindings)request.getAttribute(SlingBindings.class.getName());
ValueMap dxPolicy = bindings != null ? (ValueMap)bindings.get(DxBindingsValueProvider.POLICY_KEY) : ValueMap.EMPTY;
for (String workerKey : keys) {
StyleWorker worker = workerMap.get(workerKey);
if (worker != null) {
String declaration = worker.getDeclaration(resource, dxPolicy);
if (StringUtils.isNotBlank(declaration)) {
logger.debug("generated {} from {}", declaration, request);
if (declarations == null) {
declarations = new ArrayList<>();
}
declarations.add(declaration);
if (keys.length > 0) {
StringBuilder style = new StringBuilder();
String defaultStyle = getStylePerBreakpoint(id, null, keys, request);
if (StringUtils.isNotBlank(defaultStyle)) {
style.append(defaultStyle);
}
Breakpoint[] breakpoints = RequestUtil.getBreakpoints(request);
if (breakpoints != null) {
for (Breakpoint breakpoint : breakpoints) {
String bpStyle = getStylePerBreakpoint(id, breakpoint, keys, request);
if (StringUtils.isNotBlank(bpStyle)) {
style.append(String.format(FORMAT_BP, breakpoint.mediaQuery(), bpStyle));
}
} else {
logger.debug("{} was required resource type {}, but no associated worker is registered", workerKey,
resource.getResourceType());
}
}
if (declarations != null && !declarations.isEmpty()) {
String concat = String.join(DECLARATION_DELIMITER, declarations);
return StringUtils.isNotBlank(id) ? String.format(FORMAT_ID, id, concat) : concat;
if (style.length() > 0) {
return style.toString();
}
}
return EMPTY;
Expand All @@ -94,14 +115,14 @@ public String getLocalStyle(String id, SlingHttpServletRequest request) {
/**
* returns ordered list of workers for that given resource (or null)
*/
String[] getWorkerKeys(Resource resource) {
@NotNull String[] getWorkerKeys(Resource resource) {
String type = resource.getResourceType();
String typePath = (type.startsWith(SLASH) ? type : TYPE_PREFIX + type) + STYLEWORKERS_SUFFIX;
Resource keys = resource.getResourceResolver().getResource(typePath);
if (keys != null) {
return keys.adaptTo(String[].class);
}
return null;
return EMPTY_STRING_ARRAY;
}

void refreshWorkers() {
Expand Down
65 changes: 65 additions & 0 deletions bundles/core/src/main/java/com/adobe/dx/utils/RequestUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ Copyright 2020 Adobe
~
~ 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.dx.utils;

import com.adobe.dx.bindings.internal.DxBindingsValueProvider;
import com.adobe.dx.responsive.Breakpoint;

import java.util.Map;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.scripting.SlingBindings;

/**
* Utility class for fetching utility objects from request
*/
public class RequestUtil {
private RequestUtil() {
}

/**
* @param request current request
* @return sling bindings
*/
public static final SlingBindings getBindings(SlingHttpServletRequest request) {
return (SlingBindings)request.getAttribute(SlingBindings.class.getName());
}

/**
* @param request current request
* @return current DX policy
*/
public static final ValueMap getPolicy(SlingHttpServletRequest request) {
return (ValueMap)getBindings(request).get(DxBindingsValueProvider.POLICY_KEY);
}

/**
* @param request current request
* @return current set of breakpoints
*/
public static final Breakpoint[] getBreakpoints(SlingHttpServletRequest request) {
return (Breakpoint[]) getBindings(request).get(DxBindingsValueProvider.BP_KEY);
}

/**
* @param request current request
* @return current set of responsive properties
*/
public static final Map<String, Object> getResponsiveProperties(SlingHttpServletRequest request) {
return (Map<String, Object>) getBindings(request).get(DxBindingsValueProvider.RESP_PROPS_KEY);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public static ResponsiveConfiguration initResponsiveConfiguration(AemContext con
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");
.resource("2", "propertySuffix", "Tablet", "key", "tablet", "mediaQuery", "@media screen and (min-width: 600px)")
.resource("3", "propertySuffix", "Desktop", "key", "desktop", "mediaQuery", "@media screen and (min-width: 1200px)");
MockContextAwareConfig.registerAnnotationClasses(context, ResponsiveConfiguration.class);
MockContextAwareConfig.registerAnnotationClasses(context, Breakpoint.class);
context.create().resource(CONTENT_ROOT, "sling:configRef", CONF_ROOT);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*******************************************************************************
*
* ADOBE CONFIDENTIAL
* __________________
*
* Copyright 2019 Adobe
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe and its suppliers, if any. The intellectual
* and technical concepts contained herein are proprietary to Adobe
* and its suppliers and are protected by all applicable intellectual
* property laws, including trade secret and copyright laws.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe.
******************************************************************************/

package com.adobe.dx.style.internal;

import com.adobe.dx.bindings.internal.DxBindingsValueProvider;
import com.adobe.dx.responsive.Breakpoint;
import com.adobe.dx.style.StyleWorker;
import com.adobe.dx.testing.AbstractTest;

import org.apache.sling.api.scripting.SlingBindings;

public abstract class AbstractStyleWorkerTest extends AbstractTest {

abstract StyleWorker getWorker();

String getDeclaration() {
return getDeclaration(null);
}

String getDeclaration(Breakpoint breakpoint) {

SlingBindings bindings = (SlingBindings)context.request().getAttribute(SlingBindings.class.getName());
bindings.put(DxBindingsValueProvider.POLICY_KEY, getVM(CONTENT_ROOT));
return getWorker().getDeclaration(breakpoint, context.request());
}
}
Loading

0 comments on commit 16fffdb

Please sign in to comment.