Skip to content

Commit

Permalink
Add video set
Browse files Browse the repository at this point in the history
  • Loading branch information
eschleb committed Feb 27, 2024
1 parent cb9a930 commit 36294bd
Show file tree
Hide file tree
Showing 14 changed files with 740 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.merkle.oss.magnolia.definition.custom.imageset.ImageTypes;
import com.merkle.oss.magnolia.definition.custom.linkset.LinkType;
import com.merkle.oss.magnolia.definition.custom.linkset.LinkTypes;
import com.merkle.oss.magnolia.definition.custom.videoset.VideoType;
import com.merkle.oss.magnolia.definition.custom.videoset.VideoTypes;
import com.merkle.oss.magnolia.powernode.PowerNode;
import info.magnolia.cms.i18n.I18nContentSupport;
import info.magnolia.context.MgnlContext;
Expand Down Expand Up @@ -61,4 +63,9 @@ public LinkType.Resolver linkTypeResolver() {
public ImageType.Resolver imageTypeResolver() {
return ImageTypes::fromValue;
}

@Bean
public VideoType.Resolver videoTypeResolver() {
return VideoTypes::fromValue;
}
}
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;
}
}
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;
}
}
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)
)
);
}
}
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);
}
}
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);
}
}
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));
}
}
Loading

0 comments on commit 36294bd

Please sign in to comment.