Skip to content

Commit

Permalink
adobeGH-24 introduces StyleService with shadow & border
Browse files Browse the repository at this point in the history
- StyleService checks for current component's type styleWorker property, and registered workers, and computes one style for all,
- Shadow worker + UT
- Border worker + UT
  • Loading branch information
npeltier committed Sep 3, 2020
1 parent 4cf2922 commit c8020d1
Show file tree
Hide file tree
Showing 23 changed files with 840 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
<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="cq:Component"
componentGroup="DX"
jcr:title="Flex"/>
jcr:title="Flex"
styleWorkers="[shadow,border]"/>
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@
</sly>
</sly>
<sly data-sly-use.colorTpl="color.html" data-sly-call="${colorTpl.background @ id = model.id}"></sly>
<!--/* Background Image (Must be set after Background Colors to override background-image) */-->
<sly data-sly-test="${imageModel.src}" data-sly-use.backgroundTpl="background.html"
data-sly-call="${backgroundTpl.styles @ colorModel = colorModel, imageModel = imageModel, idGeneratorModel = idGeneratorModel}"></sly>
<sly data-sly-test="${colorModel.foreground}"
data-sly-use.colorTpl="${rtPro.componentPath}/color.html"
data-sly-call="${colorTpl.foreground @ colorModel = colorModel, imageModel = imageModel, idGeneratorModel = idGeneratorModel}"></sly>
<!--/* Border */-->
<sly data-sly-test="${model.border || model.borderRadius}"
data-sly-use.borderTpl="${rtPro.componentPath}/border.html"
data-sly-call="${borderTpl.border @ idGeneratorModel = idGeneratorModel, position = model}">
</sly>
<!--/* Shadow & Border */-->
<sly data-sly-test="${model.style}">${model.style @ context='styleString'}</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
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
~ See the License for the specific language governing permissions and
~ limitations under the License.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

package com.adobe.dx.structure.flex;

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

Expand Down Expand Up @@ -50,6 +50,9 @@ public class FlexModel {
@OSGiService
IDTagger idTagger;

@OSGiService
StyleService styleService;

@ScriptVariable
Breakpoint[] breakpoints;

Expand All @@ -58,6 +61,8 @@ public class FlexModel {

String id;

String style;

@PostConstruct
void init() {
bpMap = new HashMap<>();
Expand All @@ -66,6 +71,9 @@ void init() {
bpMap.put(breakpoint.key(), breakpoint);
}
}
if (styleService != null) {
style = styleService.getLocalStyle(getId(), request);
}
}

public boolean isStyleNeeded() {
Expand All @@ -79,6 +87,10 @@ public String getId() {
return id;
}

public String getStyle() {
return style;
}

private Resource getPolicyResource(String name) {
ResourceResolver resolver = request.getResourceResolver();
ContentPolicyManager policyManager = resolver.adaptTo(ContentPolicyManager.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.apache.sling.models.annotations.injectorspecific.SlingObject;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;

@Model(adaptables = { SlingHttpServletRequest.class, Resource.class }, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
@Model(adaptables = SlingHttpServletRequest.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class BackgroundGradient {

private static final String CQ_STYLEGUIDE_BUCKETNAME = "cq:styleguide";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*******************************************************************************
*
* 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.structure;

import com.adobe.dx.bindings.internal.DxBindingsValueProvider;
import com.adobe.dx.responsive.Breakpoint;
import com.adobe.dx.responsive.ResponsiveConfiguration;
import com.adobe.dx.testing.AbstractRequestModelTest;

import java.util.Map;

import javax.script.Bindings;
import javax.script.SimpleBindings;

import org.apache.sling.api.scripting.SlingBindings;
import org.apache.sling.testing.mock.caconfig.MockContextAwareConfig;
import org.junit.jupiter.api.BeforeEach;

import io.wcm.testing.mock.aem.junit5.AemContext;

public class AbstractStructureModelTest extends AbstractRequestModelTest {

protected SlingBindings getBindings(AemContext context) {
return (SlingBindings) context.request().getAttribute(SlingBindings.class.getName());
}

protected static final String MODEL_PATH = CONTENT_ROOT + "/model";

@BeforeEach
public void beforeEach() {
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);
}

@Override
protected <T> T getModel(Class<T> type) {
DxBindingsValueProvider provider = new DxBindingsValueProvider();
Bindings bindings = new SimpleBindings();
for (Map.Entry<String, Object> entry : getBindings(context).entrySet()) {
bindings.put(entry.getKey(), entry.getValue());
}
bindings.put(SlingBindings.RESOURCE, context.currentResource());
provider.addBindings(bindings);
SlingBindings slingBindings = getBindings(context);
slingBindings.put(DxBindingsValueProvider.POLICY_KEY, bindings.get(DxBindingsValueProvider.POLICY_KEY));
slingBindings.put(DxBindingsValueProvider.BP_KEY, bindings.get(DxBindingsValueProvider.BP_KEY));
slingBindings.put(DxBindingsValueProvider.RESP_PROPS_KEY, bindings.get(DxBindingsValueProvider.RESP_PROPS_KEY));
return super.getModel(type);
}

@Override
protected <T> T getModel(Class<T> type, String path) {
context.currentResource(path);
return getModel(type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,55 +21,36 @@
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.adobe.dx.structure.AbstractStructureModelTest;
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;

class FlexModelTest extends AbstractRequestModelTest {
class FlexModelTest extends AbstractStructureModelTest {

FlexModel model;

@BeforeEach
public void setup() throws ReflectiveOperationException {
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,
context.build().resource(MODEL_PATH,
"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.currentResource(MODEL_PATH);
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();
model = getModel(FlexModel.class, MODEL_PATH);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@
*/
public class DxBindingsValueProvider implements BindingsValuesProvider {

private static final String POLICY_KEY = "dxPolicy";
public static final String POLICY_KEY = "dxPolicy";

private static final String RESP_PROPS_KEY = "resprops";
public static final String RESP_PROPS_KEY = "resprops";

private static final String BP_KEY = "breakpoints";
public static final String BP_KEY = "breakpoints";

@Override
public void addBindings(@NotNull Bindings bindings) {
Expand Down
28 changes: 28 additions & 0 deletions bundles/core/src/main/java/com/adobe/dx/style/Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ 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.style;

public class Constants {
private Constants() {
}
public static final String DECLARATION_DELIMITER = ";";
public static final String DECLARATION = ": ";
public static final String SPACE = " ";
public static final String PX = "px";
public static final String PX_SPACE = PX + SPACE;
public static final String DEL_SPACE = DECLARATION_DELIMITER + SPACE;

}
32 changes: 32 additions & 0 deletions bundles/core/src/main/java/com/adobe/dx/style/StyleService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ 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.style;

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

public interface StyleService {

/**
* Computes a list of CSS declarations relatives of the given request. If an id is provided,
* encapsulate those declarations in that id rule (for nested usage).
*
* @param id optional ID to encapsulate declarations with
* @param request current request
* @return declaration set, or local rule
*/
String getLocalStyle(@Nullable String id, SlingHttpServletRequest request);
}
41 changes: 41 additions & 0 deletions bundles/core/src/main/java/com/adobe/dx/style/StyleWorker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ 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.style;

import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.jetbrains.annotations.Nullable;

/**
* Single CSS rule generator, for very specific usage in a style tag, modified by the component itself
*/
public interface StyleWorker {

/**
* @return key with which the worker can be identified
*/
String getKey();

/**
* 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,
* @return single or several declarations split by ';', or null if not necessary or able to generate some
*/
@Nullable String getDeclaration(Resource resource, ValueMap dxPolicy);

}
Loading

0 comments on commit c8020d1

Please sign in to comment.