Skip to content

Commit

Permalink
KYAN-306 Add Accordion Component (#512)
Browse files Browse the repository at this point in the history
* KYAN-306 Add Accordion Component

* KYAN-306 Add Accordion Component

* KYAN-306 Add Accordion Component
Refactor

* KYAN-306 Add Accordion Component
Added tests

* KYAN-306 Add Accordion Component
Refactor tests
  • Loading branch information
andrechern authored Jan 9, 2025
1 parent ced6367 commit ceb771f
Show file tree
Hide file tree
Showing 17 changed files with 526 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"Kyanite Elements",
"Kyanite Layouts",
"Kyanite Blog Article Components",
"Kyanite In-Text Sections"
"Kyanite In-Text Sections",
"Kyanite Accordion"
]
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (C) 2025 Dynamic Solutions
*
* 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 pl.ds.kyanite.common.components.models;

import static org.apache.sling.models.annotations.DefaultInjectionStrategy.OPTIONAL;
import static pl.ds.kyanite.common.components.utils.SlingUtils.SLING_RESOURCE_TYPE;

import java.util.List;
import java.util.stream.StreamSupport;
import lombok.Getter;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.ChildResource;
import org.apache.sling.models.annotations.injectorspecific.SlingObject;

@Model(adaptables = Resource.class, defaultInjectionStrategy = OPTIONAL)
public class AccordionComponent {

public static final String ACCORDION_ITEM_RESOURCE_TYPE
= "kyanite/common/components/accordion/accordionitem";

@SlingObject
private Resource resource;

@ChildResource
@Getter
private List<AccordionItemComponent> elements;

public List<Resource> getChildrenComponents() {
return StreamSupport
.stream(resource.getChildren()
.spliterator(), false)
.filter(it ->
ACCORDION_ITEM_RESOURCE_TYPE.equals(it.getValueMap()
.get(SLING_RESOURCE_TYPE)))
.toList();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (C) 2025 Dynamic Solutions
*
* 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 pl.ds.kyanite.common.components.models;

import static org.apache.sling.models.annotations.DefaultInjectionStrategy.OPTIONAL;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.Default;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;

@Model(adaptables = Resource.class, defaultInjectionStrategy = OPTIONAL)
public class AccordionItemComponent {

@Getter
@ValueMapValue
@Default(values = "Accordion item title")
private String title;

@ValueMapValue
@Getter
private String size;

@Getter
private List<String> classList = new ArrayList<>();

@PostConstruct
private void init() {
if (StringUtils.isNotBlank(size)) {
classList.add(size);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"isContainer": true,
"group": "Kyanite Components",
"allowedComponents": [
"/apps/kyanite/common/components/accordion/accordionitem",
"/libs/kyanite/common/components/accordion/accordionitem"
],
"sling:resourceType": "ws:Component",
"title": "Accordion"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!--/*
Copyright (C) 2025 Dynamic Solutions
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.
*/-->
<sly data-sly-use.model="pl.ds.kyanite.common.components.models.AccordionComponent">
<sly data-sly-set.cssClasses="accordion"></sly>
<div class="${cssClasses}">
<div data-sly-test="${resource.hasChildren}">
<ul>
<sly data-sly-list.cell="${model.childrenComponents}">
<sly data-sly-resource="${cell}"></sly>
</sly>
</ul>
</div>
</div>

<sly data-sly-test="${!resource.hasChildren}">
<sly data-sly-use.lib="/libs/wcm/foundation/components/commons/templates.html"
data-sly-call="${lib.placeholder @ classes=cssClasses}">
</sly>
</sly>
</sly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"group": "Kyanite sub-elements",
"isContainer": true,
"sling:resourceType": "ws:Component",
"allowedComponents": [
"Kyanite Columns",
"Kyanite Components",
"Kyanite Elements",
"Kyanite Layouts",
"Kyanite In-Text Sections",
"Kyanite Accordion"
],
"title": "Accordion Item"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!--/*
Copyright (C) 2025 Dynamic Solutions
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.
*/-->
<sly data-sly-use.model="pl.ds.kyanite.common.components.models.AccordionItemComponent">
<sly data-sly-test="${resource.hasChildren}">
<li class="accordion-item ${wcmmode.isEdit ? 'opened' : ''}">
<button aria-expanded="false" class="accordion-item__title title ${model.classList @ join=' '}">
${model.title}
<i class="mdi mdi-chevron-down mdi-36px"></i>
</button>
<div role="region" class="accordion-item__content">
<sly data-sly-list="${resource.children}">
<sly data-sly-resource="${item}"></sly>
</sly>
</div>
</li>
</sly>
<sly data-sly-test="${!resource.hasChildren}">
<sly
data-sly-use.lib="/libs/wcm/foundation/components/commons/templates.html"
data-sly-call="${lib.placeholder @ classes=classes}">
</sly>
</sly>
</sly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"sling:resourceType": "wcm/dialogs/dialog",
"tabs": {
"sling:resourceType": "wcm/dialogs/components/tabs",
"generalTab": {
"sling:resourceType": "wcm/dialogs/components/tab",
"label": "General",
"title": {
"sling:resourceType": "wcm/dialogs/components/textfield",
"name": "title",
"label": "Title"
},
"size": {
"sling:resourceType": "wcm/dialogs/components/include",
"path": "/libs/kyanite/common/components/common/issize"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jcr:primaryType": "nt:unstructured",
"sling:resourceType": "kyanite/common/components/accordion/accordionitem",
"size": "is-4"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"jcr:primaryType": "nt:unstructured",
"cell_1": {
"title": "Title",
"size": "is-4",
"jcr:primaryType": "nt:unstructured",
"sling:resourceType": "kyanite/common/components/accordion/accordionitem",
"content": {
"jcr:primaryType": "nt:unstructured",
"size": "is-normal",
"text": "\u003Cp\u003ELorem ipsum dolor sit amet, consectetur adipiscing elit.\u003C/p\u003E",
"sling:resourceType": "kyanite/common/components/content",
"textWeight": "has-text-weight-normal"
}
},
"cell_2": {
"title": "Title",
"size": "is-4",
"jcr:primaryType": "nt:unstructured",
"sling:resourceType": "kyanite/common/components/accordion/accordionitem",
"content": {
"jcr:primaryType": "nt:unstructured",
"size": "is-normal",
"text": "\u003Cp\u003ELorem ipsum dolor sit amet, consectetur adipiscing elit.\u003C/p\u003E",
"sling:resourceType": "kyanite/common/components/content",
"textWeight": "has-text-weight-normal"
}
},
"cell_3": {
"title": "Title",
"size": "is-4",
"jcr:primaryType": "nt:unstructured",
"sling:resourceType": "kyanite/common/components/accordion/accordionitem",
"content": {
"jcr:primaryType": "nt:unstructured",
"size": "is-normal",
"text": "\u003Cp\u003ELorem ipsum dolor sit amet, consectetur adipiscing elit.\u003C/p\u003E",
"sling:resourceType": "kyanite/common/components/content",
"textWeight": "has-text-weight-normal"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (C) 2025 Dynamic Solutions
*
* 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 pl.ds.kyanite.common.components.models;

import static java.util.Objects.requireNonNull;
import static org.assertj.core.api.Assertions.assertThat;

import java.util.List;

import org.apache.sling.api.resource.Resource;
import org.apache.sling.testing.mock.sling.ResourceResolverType;
import org.apache.sling.testing.mock.sling.junit5.SlingContext;
import org.apache.sling.testing.mock.sling.junit5.SlingContextExtension;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

@ExtendWith(SlingContextExtension.class)
public class AccordionComponentTest {

private static final String PATH = "/content/accordion";
public static final String IS_5 = "is-5";

private final SlingContext context = new SlingContext(ResourceResolverType.RESOURCERESOLVER_MOCK);

@BeforeEach
public void init() {
context.addModelsForClasses(AccordionComponent.class);
context.addModelsForClasses(AccordionItemComponent.class);
context.addModelsForClasses(ContentComponent.class);
context.load().json(requireNonNull(
Thread.currentThread().getContextClassLoader().getResourceAsStream("accordion.json")), PATH);
}

@Test
void defaultAccordionComponentModelTest() {
AccordionComponent model = context.resourceResolver().getResource(PATH + "/default")
.adaptTo(AccordionComponent.class);
assertThat(model).isNotNull();
List<Resource> childrenComponents = model.getChildrenComponents();
assertThat(childrenComponents).hasSize(2);
AccordionItemComponent accordionItemComponent = childrenComponents.get(1).adaptTo(AccordionItemComponent.class);
assertThat(accordionItemComponent).isNotNull();
assertThat(accordionItemComponent.getClassList()).containsExactly(IS_5);
assertThat(accordionItemComponent.getSize()).isEqualTo(IS_5);
assertThat(accordionItemComponent.getTitle()).isEqualTo("Title2");
Resource content = childrenComponents.get(0).getChild("content");
ContentComponent contentComponent = content.adaptTo(ContentComponent.class);
assertThat(contentComponent.getText()).isEqualTo("<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>");
}
}
35 changes: 35 additions & 0 deletions applications/common/backend/src/test/resources/accordion.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"default": {
"sling:resourceType": "kyanite/common/components/accordion",
"jcr:primaryType": "nt:unstructured",
"cell_1": {
"sling:resourceType": "kyanite/common/components/accordion/accordionitem",
"jcr:primaryType": "nt:unstructured",
"size": "is-4",
"title": "Title",
"content": {
"sling:resourceType": "kyanite/common/components/content",
"jcr:primaryType": "nt:unstructured",
"size": "is-normal",
"text": "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>",
"textWeight": "has-text-weight-normal"
}
},
"cell_2": {
"sling:resourceType": "kyanite/common/components/accordion/accordionitem",
"jcr:primaryType": "nt:unstructured",
"size": "is-5",
"title": "Title2",
"content": {
"sling:resourceType": "kyanite/common/components/content",
"jcr:primaryType": "nt:unstructured",
"size": "is-normal",
"text": "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit2.</p>",
"textWeight": "has-text-weight-normal"
}
},
"text": {
"title": "Title2"
}
}
}
Loading

0 comments on commit ceb771f

Please sign in to comment.