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

ImageChoice Component #1366

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
Expand Up @@ -106,6 +106,9 @@ private FormConstants() {
/** The resource type for image v1 */
public static final String RT_FD_FORM_IMAGE_V1 = RT_FD_FORM_PREFIX + "image/v1/image";

/** The resource type for image choice v1 */
public static final String RT_FD_FORM_IMAGECHOICE_V1 = RT_FD_FORM_PREFIX + "imagechoice/v1/imagechoice";

/** The resource type for title v1 */
public static final String RT_FD_FORM_TITLE_V1 = RT_FD_FORM_PREFIX + "title/v1/title";

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ Copyright 2024 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.cq.forms.core.components.internal.models.v1.form;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Nullable;
import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.Exporter;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.Optional;
import org.apache.sling.models.annotations.Via;
import org.apache.sling.models.annotations.injectorspecific.InjectionStrategy;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.adobe.cq.export.json.ComponentExporter;
import com.adobe.cq.export.json.ExporterConstants;
import com.adobe.cq.forms.core.components.internal.form.FormConstants;
import com.adobe.cq.forms.core.components.models.form.CheckBox;
import com.adobe.cq.forms.core.components.models.form.ImageChoice;
import com.adobe.cq.forms.core.components.util.AbstractFieldImpl;
import com.adobe.cq.forms.core.components.util.ComponentUtils;
import com.fasterxml.jackson.annotation.JsonIgnore;

@Model(
adaptables = { SlingHttpServletRequest.class, Resource.class },
adapters = { ImageChoice.class, ComponentExporter.class },
resourceType = { FormConstants.RT_FD_FORM_IMAGECHOICE_V1 })
@Exporter(name = ExporterConstants.SLING_MODEL_EXPORTER_NAME, extensions = ExporterConstants.SLING_MODEL_EXTENSION)
public class ImageChoiceImpl extends AbstractFieldImpl implements ImageChoice {

private final Logger logger = LoggerFactory.getLogger(getClass());
@ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL, name = "orientation")
@Nullable
protected String orientationJcr;
private CheckBox.Orientation orientation;

@ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL, name = "selectionType")
@Nullable
protected String selectionType;

@Inject
@Via("resource")
@Optional
protected List<ImageItem> options = new ArrayList<>();

@PostConstruct
private void initImageChoiceModel() {
orientation = CheckBox.Orientation.fromString(orientationJcr);
}

@Override
public Map<String, Object> getCustomLayoutProperties() {
Map<String, Object> customLayoutProperties = super.getCustomLayoutProperties();
if (orientation != null) {
customLayoutProperties.put("orientation", orientation);
}
if (selectionType != null) {
customLayoutProperties.put("selectionType", selectionType);
}
return customLayoutProperties;
}

@JsonIgnore
public CheckBox.Orientation getOrientation() {
return orientation;
}

@JsonIgnore
public String getSelectionType() {
return selectionType;
}

public List<ImageItem> getOptions() {
options = setImageValueBasedOnSubmissionDataType();
return options;
}

private Map<Object, ImageItem> removeDuplicates() {
LinkedHashMap<Object, ImageItem> map = new LinkedHashMap<>();
if (options != null) {
for (ImageItem item : options) {
map.put(item.getValue(), item);
}
}
return map;
}

private List<ImageItem> setImageValueBasedOnSubmissionDataType() {
if (options == null) {
return null;
} else {
Map<Object, ImageItem> map = removeDuplicates();
List<ImageItem> updatedOptions = new ArrayList<>();
for (Map.Entry<Object, ImageItem> entry : map.entrySet()) {
Object key = entry.getKey();
ImageItem item = entry.getValue();
Object coercedValue = coerceImageValue(type, key);
item.setImageValue(coercedValue);
updatedOptions.add(item);
}
return updatedOptions;
}
}

private Object coerceImageValue(Type type, Object value) {
if (type.equals(Type.NUMBER) || type.equals(Type.NUMBER_ARRAY)) {
return Long.parseLong(value.toString());
} else if (type.equals(Type.BOOLEAN) || type.equals(Type.BOOLEAN_ARRAY)) {
return Boolean.parseBoolean(value.toString());
}
return value;
}

@Override
public Type getType() {
return super.getType();
}

@Override
public Object[] getDefault() {
Object[] typedDefaultValue = null;
if (defaultValue != null) {
typedDefaultValue = ComponentUtils.coerce(type, defaultValue);
}
return typedDefaultValue;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ Copyright 2024 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.cq.forms.core.components.internal.models.v1.form;

import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.InjectionStrategy;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;

import com.fasterxml.jackson.annotation.JsonIgnore;

@Model(adaptables = Resource.class)
public class ImageItem {
@ValueMapValue(name = "imageSrc")
private String imageSrc;

@JsonIgnore
@ValueMapValue(name = "imageValue")
private String value;

private Object imageValue;

@ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL, name = "altText")
private String altText;

public String getImageSrc() {
return imageSrc;
}

public void setImageSrc(String imageSrc) {
this.imageSrc = imageSrc;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

public Object getImageValue() {
return imageValue;
}

public void setImageValue(Object imageValue) {
this.imageValue = imageValue;
}

public String getAltText() {
return altText;
}

public void setAltText(String altText) {
this.altText = altText;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public enum FieldType {
PANEL("panel"),
FORM("form"),
CHECKBOX_GROUP("checkbox-group"),
IMAGE("image");
IMAGE("image"),
IMAGECHOICE("imagechoice");

private String value;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ Copyright 2024 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.cq.forms.core.components.models.form;

import java.util.List;

import org.osgi.annotation.versioning.ConsumerType;

import com.adobe.cq.forms.core.components.internal.models.v1.form.ImageItem;

/**
* Defines the form {@code ImageChoice} Sling Model used for the {@code /apps/core/fd/components/form/imagechoice/v1/imagechoice}
* component.
*
* @since com.adobe.cq.forms.core.components.models.form 2.0.0
*/
@ConsumerType
public interface ImageChoice extends Field {

/**
* Returns the type of selection for the image choice component, either single or multiple selection.
*
* @return the type of selection
* @since com.adobe.cq.forms.core.components.models.form 2.0.0
*/
String getSelectionType();

/**
* Returns the list of options associated with the image choice component.
*
* @return the list of options
* @since com.adobe.cq.forms.core.components.models.form 2.0.0
*/
List<ImageItem> getOptions();
}
Loading
Loading