Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EAK-504] Limited Parsys: add functionality to limit parsys-like components child component count #505

Merged
merged 13 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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.exadel.aem.toolkit.api.annotations.policies;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MaxChildren {
/**
* Specifies the maximum number of children allowed for container component
* @return Long value, 0 or greater
*/
long value();
ala-n marked this conversation as resolved.
Show resolved Hide resolved

/**
* Used to specify target node for max children limit. The limitation applies to the current annotated component
* if set to {@link PolicyTarget#CURRENT}. Otherwise, the limitation is applied to a container nested within the current
* component. E.g. if the current component is an inheritor of parsys, setting {@code targetContainer} to {@link
* PolicyTarget#CURRENT} means that the limitation affects which components can be added to the current
* one. But if the current component contains a parsys inside you need to skip {@code targetContainer} or set it to
* {@link PolicyTarget#CHILD} so that the limitation applies to the parsys.
* @return {@link PolicyTarget} value
*/
PolicyTarget targetContainer() default PolicyTarget.CHILD;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
package com.exadel.aem.toolkit.api.annotations.policies;

/**
* Defines possible values of {@link AllowedChildren#targetContainer()} property
* Defines possible values of {@link AllowedChildren#targetContainer()} and {@link MaxChildren#targetContainer()} properties
*/
public enum PolicyTarget {
CHILD,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.exadel.aem.toolkit.plugin.handlers.common.CqDialogHandler;
import com.exadel.aem.toolkit.plugin.handlers.common.CqEditConfigHandler;
import com.exadel.aem.toolkit.plugin.handlers.common.CqHtmlTagHandler;
import com.exadel.aem.toolkit.plugin.handlers.common.MaxChildrenHandler;
import com.exadel.aem.toolkit.plugin.handlers.common.PropertyMappingHandler;
import com.exadel.aem.toolkit.plugin.handlers.dependson.DependsOnHandler;
import com.exadel.aem.toolkit.plugin.handlers.editconfig.DropTargetsHandler;
Expand All @@ -54,12 +55,17 @@ public class Handlers {
private static final BiConsumer<Source, Target> CASUAL_ANNOTATIONS_HANDLER = new CasualAnnotationsHandler();
private static final BiConsumer<Source, Target> PROPERTY_MAPPING_HANDLER = new PropertyMappingHandler();
private static final BiConsumer<Source, Target> ALLOWED_CHILDREN_HANDLER = new AllowedChildrenHandler();
private static final BiConsumer<Source, Target> MAX_CHILDREN_HANDLER = new MaxChildrenHandler();

// UI-specific handlers
private static final BiConsumer<Source, Target> CHILD_EDIT_CONFIG_HANDLER = new CqChildEditConfigHandler().andThen(ALLOWED_CHILDREN_HANDLER);
private static final BiConsumer<Source, Target> CHILD_EDIT_CONFIG_HANDLER = new CqChildEditConfigHandler()
.andThen(ALLOWED_CHILDREN_HANDLER)
.andThen(MAX_CHILDREN_HANDLER);
private static final BiConsumer<Source, Target> COMPONENT_HANDLER = new ComponentHandler();
private static final BiConsumer<Source, Target> DIALOG_HANDLER = new CqDialogHandler();
private static final BiConsumer<Source, Target> EDIT_CONFIG_HANDLER = new CqEditConfigHandler().andThen(ALLOWED_CHILDREN_HANDLER);
private static final BiConsumer<Source, Target> EDIT_CONFIG_HANDLER = new CqEditConfigHandler()
.andThen(ALLOWED_CHILDREN_HANDLER)
.andThen(MAX_CHILDREN_HANDLER);
private static final BiConsumer<Source, Target> HTML_TAG_HANDLER = new CqHtmlTagHandler();
private static final Map<String, BiConsumer<Source, Target>> UI_HANDLERS = ImmutableMap.<String, BiConsumer<Source, Target>>builder()
.put(Scopes.COMPONENT, COMPONENT_HANDLER)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* 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.exadel.aem.toolkit.plugin.handlers.common;

import com.exadel.aem.toolkit.api.annotations.meta.Scopes;
import com.exadel.aem.toolkit.api.annotations.policies.MaxChildren;
import com.exadel.aem.toolkit.api.annotations.policies.PolicyTarget;
import com.exadel.aem.toolkit.api.handlers.Handler;
import com.exadel.aem.toolkit.api.handlers.Source;
import com.exadel.aem.toolkit.api.handlers.Target;
import com.exadel.aem.toolkit.plugin.utils.DialogConstants;

/**
* Implements {@code BiConsumer} to populate a {@link Target} instance with properties originating from a {@link Source}
* object. The source refers to the {@code resolvemaxchildren} listener of the {@code
* cq:editConfig} node of an AEM component
*/
public class MaxChildrenHandler implements Handler {
/**
* Name of the listener to resolve max children limit for `policies` js module
*/
public static final String MAX_LIMIT_RESOLVER_NAME = "resolvemaxchildren";
ala-n marked this conversation as resolved.
Show resolved Hide resolved

/**
* Format of default max children limit resolver
*/
private static final String MAX_LIMIT_RESOLVER_FORMAT = "() => %d";


/**
* Processes data that can be extracted from the given {@code Source} and stores it into the provided {@code
* Target}
* @param source {@code Source} object used for data retrieval
* @param target Resulting {@code Target} object
*/
@Override
public void accept(Source source, Target target) {
source.tryAdaptTo(MaxChildren.class).ifPresent(adaptation -> populate(adaptation, target));
}

/**
* Processes data from {@code MaxChildren} annotation and stores it into 'cq:listeners' node of the provided
* {@code Target}
* @param rule {@code MaxChildren} object used for data retrieval
* @param target Resulting {@code Target} object
*/
private static void populate(MaxChildren rule, Target target) {
if (isEditConfig(target) == (PolicyTarget.CURRENT == rule.targetContainer())) {
target
.attribute(DialogConstants.PN_PRIMARY_TYPE, DialogConstants.NT_EDIT_CONFIG)
.getOrCreateTarget(DialogConstants.NN_LISTENERS)
.attribute(DialogConstants.PN_PRIMARY_TYPE, DialogConstants.NT_LISTENERS)
.attribute(MAX_LIMIT_RESOLVER_NAME, String.format(MAX_LIMIT_RESOLVER_FORMAT, rule.value()));
}
}

/**
* Gets whether the given {@link Target} is a representation of a {@code cq:editConfig} node of an AEM component
* @param target {@code Target} instance
* @return True if the target is a {@code cq:editConfig} node; otherwise, false
*/
private static boolean isEditConfig(Target target) {
return Scopes.CQ_EDIT_CONFIG.equals(target.getScope());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.exadel.aem.toolkit.api.annotations.editconfig.ChildEditConfig;
import com.exadel.aem.toolkit.api.annotations.meta.Scopes;
import com.exadel.aem.toolkit.api.annotations.policies.AllowedChildren;
import com.exadel.aem.toolkit.api.annotations.policies.MaxChildren;
import com.exadel.aem.toolkit.api.annotations.policies.PolicyTarget;
import com.exadel.aem.toolkit.api.handlers.Source;

Expand Down Expand Up @@ -55,6 +56,8 @@ boolean canProcess(Source source) {
Class<?> componentClass = (Class<?>) source.adaptTo(Class.class);
return componentClass.isAnnotationPresent(ChildEditConfig.class)
|| Arrays.stream(componentClass.getAnnotationsByType(AllowedChildren.class))
.anyMatch(ac -> PolicyTarget.CHILD.equals(ac.targetContainer()))
|| Arrays.stream(componentClass.getAnnotationsByType(MaxChildren.class))
.anyMatch(ac -> PolicyTarget.CHILD.equals(ac.targetContainer()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.exadel.aem.toolkit.api.annotations.editconfig.EditConfig;
import com.exadel.aem.toolkit.api.annotations.meta.Scopes;
import com.exadel.aem.toolkit.api.annotations.policies.AllowedChildren;
import com.exadel.aem.toolkit.api.annotations.policies.MaxChildren;
import com.exadel.aem.toolkit.api.annotations.policies.PolicyTarget;
import com.exadel.aem.toolkit.api.handlers.Source;

Expand Down Expand Up @@ -56,6 +57,8 @@ boolean canProcess(Source source) {
Class<?> componentClass = (Class<?>) source.adaptTo(Class.class);
return componentClass.isAnnotationPresent(EditConfig.class)
|| Arrays.stream(componentClass.getAnnotationsByType(AllowedChildren.class))
.anyMatch(ac -> PolicyTarget.CURRENT.equals(ac.targetContainer()))
|| Arrays.stream(componentClass.getAnnotationsByType(MaxChildren.class))
.anyMatch(ac -> PolicyTarget.CURRENT.equals(ac.targetContainer()));
}
}
2 changes: 2 additions & 0 deletions plugin/src/test/com/exadel/aem/toolkit/plugin/AllTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.exadel.aem.toolkit.plugin.handlers.common.ComponentsTest;
import com.exadel.aem.toolkit.plugin.handlers.common.EditConfigTest;
import com.exadel.aem.toolkit.plugin.handlers.common.IgnoreFreshnessTest;
import com.exadel.aem.toolkit.plugin.handlers.common.MaxChildrenTest;
import com.exadel.aem.toolkit.plugin.handlers.common.WriteModeTest;
import com.exadel.aem.toolkit.plugin.handlers.dependson.DependsOnTest;
import com.exadel.aem.toolkit.plugin.handlers.placement.CoincidenceTest;
Expand Down Expand Up @@ -63,6 +64,7 @@
AllowedChildrenTest.class,
DependsOnTest.class,
IgnoreFreshnessTest.class,
MaxChildrenTest.class,

LayoutTest.class,
ReplacementTest.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.exadel.aem.toolkit.plugin.handlers.common;

import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;

import com.exadel.aem.toolkit.plugin.handlers.common.cases.maxchildren.MaxChildrenTestCases;
import com.exadel.aem.toolkit.plugin.maven.FileSystemRule;
import com.exadel.aem.toolkit.plugin.maven.PluginContextRenderingRule;

public class MaxChildrenTest {

@ClassRule
public static FileSystemRule fileSystemHost = new FileSystemRule();

@Rule
public PluginContextRenderingRule pluginContext = new PluginContextRenderingRule(fileSystemHost.getFileSystem());

@Test
public void testSimpleAnnotation() {
pluginContext.test(MaxChildrenTestCases.SimpleMaxLimitAnnotation.class, "handlers/common/maxChildren/simple");
}

@Test
public void testAllowedChildrenWithMaxLimitConflict() {
pluginContext.test(MaxChildrenTestCases.AllowedChildrenWithMaxLimit.class, "handlers/common/maxChildren/withAllowedChildren");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.exadel.aem.toolkit.plugin.handlers.common.cases.maxchildren;

import com.exadel.aem.toolkit.api.annotations.main.AemComponent;
import com.exadel.aem.toolkit.api.annotations.policies.AllowedChildren;
import com.exadel.aem.toolkit.api.annotations.policies.MaxChildren;
import com.exadel.aem.toolkit.api.annotations.policies.PolicyMergeMode;
import com.exadel.aem.toolkit.plugin.handlers.common.cases.components.ComplexComponent1;
import com.exadel.aem.toolkit.plugin.handlers.common.cases.components.ComplexComponent2;
import com.exadel.aem.toolkit.plugin.maven.TestConstants;

public class MaxChildrenTestCases {
@AemComponent(
title = TestConstants.DEFAULT_COMPONENT_TITLE,
path = TestConstants.DEFAULT_COMPONENT_NAME
)
@MaxChildren(5)
public static class SimpleMaxLimitAnnotation {
}

@AemComponent(
title = TestConstants.DEFAULT_COMPONENT_TITLE,
path = TestConstants.DEFAULT_COMPONENT_NAME
)
@AllowedChildren(
classes = {ComplexComponent1.class, ComplexComponent2.class},
pagePaths = {"page/Path1, page/Path2"},
mode = PolicyMergeMode.MERGE
)
@MaxChildren(1)
public static class AllowedChildrenWithMaxLimit {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<jcr:root xmlns:jcr="http://www.jcp.org/jcr/1.0"
xmlns:cq="http://www.day.com/jcr/cq/1.0"
jcr:primaryType="cq:Component"
jcr:title="Test Component"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:cq="http://www.day.com/jcr/cq/1.0"
xmlns:jcr="http://www.jcp.org/jcr/1.0"
jcr:primaryType="cq:EditConfig">
<cq:listeners jcr:primaryType="cq:EditListenersConfig"
resolvemaxchildren="() => 5"/>
</jcr:root>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<jcr:root xmlns:jcr="http://www.jcp.org/jcr/1.0"
xmlns:cq="http://www.day.com/jcr/cq/1.0"
jcr:primaryType="cq:Component"
jcr:title="Test Component"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:cq="http://www.day.com/jcr/cq/1.0"
xmlns:jcr="http://www.jcp.org/jcr/1.0"
jcr:primaryType="cq:EditConfig">
<cq:listeners jcr:primaryType="cq:EditListenersConfig"
resolvemaxchildren="() => 1"
updatecomponentlist="Granite.PolicyResolver.build('{&quot;isEditConfig&quot;:false,&quot;rules&quot;:[{&quot;value&quot;:[&quot;/apps/eak/test-component&quot;],&quot;pagePaths&quot;:[&quot;page/Path1&quot;,&quot;page/Path2&quot;],&quot;mode&quot;:&quot;MERGE&quot;}]}')"/>
</jcr:root>
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
xmlns:jcr="http://www.jcp.org/jcr/1.0"
jcr:primaryType="cq:ClientLibraryFolder"
categories="[cq.authoring.dialog]"
dependencies="[eak.dependson,eak.coral-overlay,eak.lists.item-dialog,eak.policy-management]"
dependencies="[eak.dependson,eak.coral-overlay,eak.lists.item-dialog,eak.policies]"
/>
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
xmlns:jcr="http://www.jcp.org/jcr/1.0"
jcr:primaryType="cq:ClientLibraryFolder"
categories="[cq.authoring.dependson,eak.dependson]"
dependencies="[granite.jquery,granite.utils]"
dependencies="[eak.utils]"
jsProcessor="[min:gcc;languageIn=ECMASCRIPT_2015,compilationLevel=advanced,languageOut=ECMASCRIPT5]"
/>
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
<jcr:root xmlns:cq="http://www.day.com/jcr/cq/1.0"
xmlns:jcr="http://www.jcp.org/jcr/1.0"
jcr:primaryType="cq:ClientLibraryFolder"
categories="[eak.policy-management]"
categories="[eak.policies]"
dependencies="[eak.utils]"
jsProcessor="[min:gcc;languageIn=ECMASCRIPT_2015,compilationLevel=advanced,languageOut=ECMASCRIPT5]"
/>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
css/maxChildren.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* Blocked text */
.cq-Overlay--placeholder[data-max-children-marker]::before {
content: attr(data-max-children-marker);
color: #d24637;

/* Position fix */
line-height: 100%;
vertical-align: middle;
}

/* Blocked hover state */
.cq-Overlay--placeholder[data-max-children-marker]:hover {
border-color: #d24637;
background-color: rgba(203, 156, 150, 0.6);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
js/policyUtils.js
js/maxChildren.js
js/policyManagement.js
Loading
Loading