-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
eschleb
committed
Feb 27, 2024
1 parent
cb9a930
commit 36294bd
Showing
14 changed files
with
740 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
...com/merkle/oss/magnolia/definition/custom/videoset/AbstractVideoSetDefinitionBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package com.merkle.oss.magnolia.definition.custom.videoset; | ||
|
||
import com.merkle.oss.magnolia.definition.builder.complex.AbstractConfiguredComplexPropertyDefinitionBuilder; | ||
import com.merkle.oss.magnolia.definition.builder.simple.TextFieldDefinitionBuilder; | ||
import com.merkle.oss.magnolia.definition.custom.imageset.AbstractImageSetDefinitionBuilder; | ||
import com.merkle.oss.magnolia.definition.custom.imageset.ImageType; | ||
import com.merkle.oss.magnolia.definition.custom.switchable.FieldOption; | ||
import com.merkle.oss.magnolia.definition.custom.switchable.SwitchableDefinitionBuilder; | ||
import info.magnolia.ui.field.FieldValidatorDefinition; | ||
|
||
import javax.annotation.Nullable; | ||
import javax.jcr.Node; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
public abstract class AbstractVideoSetDefinitionBuilder<B extends AbstractVideoSetDefinitionBuilder<B>> extends AbstractConfiguredComplexPropertyDefinitionBuilder<Node, VideoSetDefinition, B> { | ||
public static final String SELECTION_SUFFIX = "_selection"; | ||
public static final String ALT_TEXT_SUFFIX = "_alt"; | ||
public static final String PREVIEW_IMAGE_SUFFIX = "_previewImage"; | ||
private final AbstractImageSetDefinitionBuilder<?> imageSetDefinitionBuilder; | ||
private final String labelPrefix; | ||
@Nullable | ||
private List<VideoType> videoOptions; | ||
@Nullable | ||
private Boolean readOnly; | ||
@Nullable | ||
private Boolean required; | ||
@Nullable | ||
private List<FieldValidatorDefinition> validators; | ||
|
||
protected AbstractVideoSetDefinitionBuilder( | ||
final AbstractImageSetDefinitionBuilder<?> imageSetDefinitionBuilder, | ||
final String labelPrefix | ||
) { | ||
this.labelPrefix = labelPrefix; | ||
this.imageSetDefinitionBuilder = imageSetDefinitionBuilder; | ||
videoOptions(Arrays.stream(VideoTypes.values()).collect(Collectors.toList())); | ||
} | ||
|
||
protected abstract FieldOption<VideoType> createFieldOption(VideoType videoType); | ||
|
||
public B videoOption(final VideoType videoOption) { | ||
return videoOptions(Stream.concat( | ||
Stream.ofNullable(videoOptions).flatMap(Collection::stream), | ||
Stream.of(videoOption) | ||
).collect(Collectors.toList())); | ||
} | ||
|
||
public B videoOptions(final List<VideoType> videoOptions) { | ||
this.videoOptions = videoOptions; | ||
return self(); | ||
} | ||
|
||
public B previewImageOption(final ImageType imageOption) { | ||
imageSetDefinitionBuilder.imageOption(imageOption); | ||
return self(); | ||
} | ||
|
||
public B previewImageOptions(final List<ImageType> imageOptions) { | ||
imageSetDefinitionBuilder.imageOptions(imageOptions); | ||
return self(); | ||
} | ||
|
||
public B readOnly() { | ||
return readOnly(true); | ||
} | ||
|
||
public B readOnly(final boolean readOnly) { | ||
this.readOnly = readOnly; | ||
return self(); | ||
} | ||
|
||
public B required() { | ||
return required(true); | ||
} | ||
|
||
public B required(final boolean required) { | ||
this.required = required; | ||
return self(); | ||
} | ||
|
||
public B validator(final FieldValidatorDefinition validator) { | ||
return validators(Stream.concat( | ||
Stream.ofNullable(validators).flatMap(Collection::stream), | ||
Stream.of(validator) | ||
).collect(Collectors.toList())); | ||
} | ||
|
||
public B validators(final List<FieldValidatorDefinition> validators) { | ||
this.validators = validators; | ||
return self(); | ||
} | ||
|
||
public VideoSetDefinition build(final String name) { | ||
final VideoSetDefinition definition = new VideoSetDefinition( | ||
new SwitchableDefinitionBuilder<VideoType>(labelPrefix) | ||
.optionPropertyNameDecorator(source -> source + SELECTION_SUFFIX) | ||
.fieldOptions(Stream.ofNullable(videoOptions).flatMap(Collection::stream).map(this::createFieldOption).collect(Collectors.toList())) | ||
.label(labelPrefix + "video.label") | ||
.build(name), | ||
new TextFieldDefinitionBuilder() | ||
.label(labelPrefix + "altText.label") | ||
.build(name + ALT_TEXT_SUFFIX), | ||
imageSetDefinitionBuilder | ||
.label(labelPrefix + "previewImage.label") | ||
.build(name + PREVIEW_IMAGE_SUFFIX) | ||
); | ||
super.populate(definition, name); | ||
Optional.ofNullable(readOnly).ifPresent(definition::setReadOnly); | ||
Optional.ofNullable(required).ifPresent(definition::setRequired); | ||
Optional.ofNullable(validators).ifPresent(definition::setValidators); | ||
return definition; | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
.../src/main/java/com/merkle/oss/magnolia/definition/custom/videoset/VideoSetDefinition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package com.merkle.oss.magnolia.definition.custom.videoset; | ||
|
||
import com.merkle.oss.magnolia.definition.custom.imageset.ImageSetDefinition; | ||
import com.merkle.oss.magnolia.definition.custom.switchable.SwitchableDefinition; | ||
import info.magnolia.ui.editor.CurrentItemProviderDefinition; | ||
import info.magnolia.ui.editor.FormDefinition; | ||
import info.magnolia.ui.editor.FormView; | ||
import info.magnolia.ui.field.ConfiguredComplexPropertyDefinition; | ||
import info.magnolia.ui.field.EditorPropertyDefinition; | ||
import info.magnolia.ui.field.FieldValidatorDefinition; | ||
import info.magnolia.ui.field.TextFieldDefinition; | ||
import info.magnolia.ui.framework.layout.FieldLayoutDefinition; | ||
import info.magnolia.ui.framework.layout.StackedLayoutProducer; | ||
|
||
import javax.annotation.Nullable; | ||
import javax.jcr.Node; | ||
import java.util.List; | ||
|
||
public class VideoSetDefinition extends ConfiguredComplexPropertyDefinition<Node> implements FormDefinition<Node> { | ||
private final SwitchableDefinition video; | ||
private final TextFieldDefinition altText; | ||
private final ImageSetDefinition previewImage; | ||
private boolean readOnly; | ||
private boolean required; | ||
@Nullable | ||
private List<FieldValidatorDefinition> validators; | ||
|
||
public VideoSetDefinition( | ||
final SwitchableDefinition video, | ||
final TextFieldDefinition altText, | ||
final ImageSetDefinition previewImage | ||
) { | ||
this.video = video; | ||
this.altText = altText; | ||
this.previewImage = previewImage; | ||
setImplementationClass((Class) FormView.class); | ||
setItemProvider(new CurrentItemProviderDefinition<>()); | ||
} | ||
|
||
public SwitchableDefinition getVideo() { | ||
return video; | ||
} | ||
|
||
public TextFieldDefinition getAltTextField() { | ||
return altText; | ||
} | ||
|
||
public ImageSetDefinition getPreviewImage() { | ||
return previewImage; | ||
} | ||
|
||
@Override | ||
public List<EditorPropertyDefinition> getProperties() { | ||
return List.of(video, altText, previewImage); | ||
} | ||
|
||
@Override | ||
public FieldLayoutDefinition<?> getLayout() { | ||
return new StackedLayoutProducer.Definition(); | ||
} | ||
|
||
@Override | ||
public void setI18n(final boolean i18n) { | ||
super.setI18n(i18n); | ||
video.setI18n(i18n); | ||
altText.setI18n(i18n); | ||
previewImage.setI18n(i18n); | ||
} | ||
|
||
public void setReadOnly(final boolean readOnly) { | ||
this.readOnly = readOnly; | ||
video.setReadOnly(readOnly); | ||
altText.setReadOnly(readOnly); | ||
previewImage.setReadOnly(readOnly); | ||
} | ||
|
||
public boolean isReadOnly() { | ||
return readOnly; | ||
} | ||
|
||
public void setRequired(final boolean required) { | ||
this.required = required; | ||
video.setRequired(required); | ||
} | ||
|
||
public boolean isRequired() { | ||
return required; | ||
} | ||
|
||
public void setValidators(final List<FieldValidatorDefinition> validators){ | ||
this.validators = validators; | ||
video.setValidators(validators); | ||
} | ||
|
||
public List<FieldValidatorDefinition> getValidators() { | ||
return validators; | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
...in/java/com/merkle/oss/magnolia/definition/custom/videoset/VideoSetDefinitionBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.merkle.oss.magnolia.definition.custom.videoset; | ||
|
||
import com.merkle.oss.magnolia.definition.builder.simple.AssetLinkDefinitionBuilder; | ||
import com.merkle.oss.magnolia.definition.builder.simple.TextFieldDefinitionBuilder; | ||
import com.merkle.oss.magnolia.definition.custom.imageset.AbstractImageSetDefinitionBuilder; | ||
import com.merkle.oss.magnolia.definition.custom.imageset.ImageSetDefinitionBuilder; | ||
import com.merkle.oss.magnolia.definition.custom.switchable.FieldOption; | ||
import com.merkle.oss.magnolia.definition.custom.switchable.SingleSwitchableForm; | ||
|
||
import javax.inject.Provider; | ||
|
||
public class VideoSetDefinitionBuilder extends AbstractVideoSetDefinitionBuilder<VideoSetDefinitionBuilder> { | ||
private static final String LABEL_PREFIX = "merkle.customDefinitions.videoSet."; | ||
|
||
public VideoSetDefinitionBuilder() { | ||
this(new ImageSetDefinitionBuilder()); | ||
} | ||
|
||
public VideoSetDefinitionBuilder(final AbstractImageSetDefinitionBuilder<?> imageSetDefinitionBuilder) { | ||
super(imageSetDefinitionBuilder, LABEL_PREFIX); | ||
} | ||
|
||
@Override | ||
protected FieldOption<VideoType> createFieldOption(final VideoType videoType) { | ||
if(VideoTypes.DAM.equals(videoType)) { | ||
return dam(); | ||
} | ||
if(VideoTypes.YOUTUBE.equals(videoType)) { | ||
return youtube(); | ||
} | ||
if(VideoTypes.VIMEO.equals(videoType)) { | ||
return vimeo(); | ||
} | ||
throw new IllegalArgumentException("Unsupported video type " + videoType); | ||
} | ||
|
||
private FieldOption<VideoType> vimeo() { | ||
return new FieldOption<>( | ||
VideoTypes.VIMEO, | ||
name -> new SingleSwitchableForm<>( | ||
new TextFieldDefinitionBuilder() | ||
.label(LABEL_PREFIX + VideoTypes.VIMEO.getLabel()) | ||
.converterClass(VimeoTextValueConverter.class) | ||
.build(name) | ||
) | ||
); | ||
} | ||
|
||
private FieldOption<VideoType> youtube() { | ||
return new FieldOption<>( | ||
VideoTypes.YOUTUBE, | ||
name -> new SingleSwitchableForm<>( | ||
new TextFieldDefinitionBuilder() | ||
.label(LABEL_PREFIX + VideoTypes.YOUTUBE.getLabel()) | ||
.converterClass(YoutubeTextValueConverter.class) | ||
.build(name) | ||
) | ||
); | ||
} | ||
|
||
private FieldOption<VideoType> dam() { | ||
return new FieldOption<>( | ||
VideoTypes.DAM, | ||
name -> new SingleSwitchableForm<>( | ||
new AssetLinkDefinitionBuilder() | ||
.label(LABEL_PREFIX + VideoTypes.DAM.getLabel()) | ||
.build(name) | ||
) | ||
); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...finitions/src/main/java/com/merkle/oss/magnolia/definition/custom/videoset/VideoType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.merkle.oss.magnolia.definition.custom.videoset; | ||
|
||
import com.merkle.oss.magnolia.definition.builder.datasource.OptionListDefinitionBuilder.OptionEnum; | ||
|
||
import java.util.Optional; | ||
|
||
public interface VideoType extends OptionEnum { | ||
interface Resolver { | ||
Optional<VideoType> resolve(String value); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...initions/src/main/java/com/merkle/oss/magnolia/definition/custom/videoset/VideoTypes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.merkle.oss.magnolia.definition.custom.videoset; | ||
|
||
import java.util.Arrays; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
public enum VideoTypes implements VideoType { | ||
DAM("videoType.dam.label", "dam"), | ||
YOUTUBE("videoType.youtube.label", "youtube"), | ||
VIMEO("videoType.vimeo.label", "vimeo"), | ||
; | ||
|
||
private final String label; | ||
private final String value; | ||
|
||
VideoTypes(final String label, final String value) { | ||
this.label = label; | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public String getLabel() { | ||
return label; | ||
} | ||
|
||
@Override | ||
public String getValue() { | ||
return value; | ||
} | ||
|
||
public static Optional<VideoType> fromValue(final String value) { | ||
return Arrays.stream(values()) | ||
.filter(type -> Objects.equals(type.getValue(), value)) | ||
.findFirst() | ||
.map(videoType -> videoType); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...main/java/com/merkle/oss/magnolia/definition/custom/videoset/VimeoTextValueConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.merkle.oss.magnolia.definition.custom.videoset; | ||
|
||
import com.vaadin.data.Converter; | ||
import com.vaadin.data.Result; | ||
import com.vaadin.data.ValueContext; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
public class VimeoTextValueConverter implements Converter<String, String> { | ||
public static final String PREFIX = "vimeo:"; | ||
|
||
@Override | ||
public Result<String> convertToModel(final String value, final ValueContext context) { | ||
final String trimmed = StringUtils.trim(value); | ||
if(StringUtils.isNotEmpty(trimmed)) { | ||
return Result.ok(PREFIX + trimmed); | ||
} | ||
return Result.ok(trimmed); | ||
} | ||
|
||
@Override | ||
public String convertToPresentation(final String value, final ValueContext context) { | ||
return StringUtils.trim(StringUtils.removeStart(value, PREFIX)); | ||
} | ||
} |
Oops, something went wrong.