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

add video to teaser component #4

Merged
merged 5 commits into from
Nov 22, 2023
Merged
Changes from 1 commit
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
Next Next commit
add video to teaser component
QuangTranXuan committed Nov 19, 2023

Verified

This commit was signed with the committer’s verified signature.
Dragon-Seeker Blodhgarm
commit afbdf9c0e74075e9fe9ed3663d3a749599dc0807
82 changes: 63 additions & 19 deletions core/src/main/java/nz/co/manawabay/core/models/Teaser.java
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
package nz.co.manawabay.core.models;

import com.adobe.cq.wcm.core.components.commons.link.Link;
import com.adobe.cq.wcm.core.components.models.ListItem;
import nz.co.manawabay.core.internal.resource.CoreResourceWrapper;
import com.adobe.cq.wcm.core.components.models.Image;
import com.day.cq.commons.jcr.JcrConstants;
import com.day.cq.wcm.api.components.Component;
import com.adobe.cq.wcm.core.components.models.ListItem;
import com.day.cq.commons.DownloadResource;
import com.day.cq.commons.jcr.JcrConstants;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.DamConstants;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.components.Component;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import nz.co.manawabay.core.internal.resource.CoreResourceWrapper;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.commons.mime.MimeTypeService;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.Via;
import org.apache.sling.models.annotations.injectorspecific.ScriptVariable;
import org.apache.sling.models.annotations.injectorspecific.Self;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;
import org.apache.sling.models.annotations.injectorspecific.*;
import org.apache.sling.models.annotations.via.ResourceSuperType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.PostConstruct;
import java.util.*;
import java.util.List;

import java.util.*;


@Model(adaptables = SlingHttpServletRequest.class,
adapters = com.adobe.cq.wcm.core.components.models.Teaser.class,
resourceType = "manawabay/components/teaser",
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
@Slf4j
public class Teaser implements com.adobe.cq.wcm.core.components.models.Teaser {

//LOGGER
private static final Logger LOGGER = LoggerFactory.getLogger(Teaser.class);
public static final String PN_IMAGE_DELEGATE = "imageDelegate";
public static final String PN_PAGE_PUBLISHDATE = "publishDate";

@@ -56,9 +56,16 @@ public class Teaser implements com.adobe.cq.wcm.core.components.models.Teaser {
@ScriptVariable
protected Component component;

@SlingObject
private ResourceResolver resourceResolver;

@Self
@Via(type = ResourceSuperType.class)
private com.adobe.cq.wcm.core.components.models.Teaser teaser;

@OSGiService
private MimeTypeService mimeTypeService;

private Resource iconResource;
private Resource brandImageResource;
@ValueMapValue
@@ -79,6 +86,7 @@ public class Teaser implements com.adobe.cq.wcm.core.components.models.Teaser {
protected boolean showBrandImage;
@ValueMapValue
private String publishDateFormatString;

public Calendar getPublishDate() {
if (publishDate == null) {
publishDate = pageProperties.get(PN_PAGE_PUBLISHDATE, Calendar.class);
@@ -89,18 +97,23 @@ public Calendar getPublishDate() {
public boolean getShowPublishDate() {
return showPublishDate;
}

public boolean getShowIcon() {
return showIcon;
}

public boolean getHideTitle() {
return hideTitle;
}

public boolean getHideImage() {
return hideImage;
}

public boolean getShowDescription() {
return showDescription;
}

public boolean getShowBrandImage() {
return showBrandImage;
}
@@ -109,6 +122,9 @@ public String getPublishDateFormatString() {
return publishDateFormatString;
}

@Getter
private String videoUrl;

@JsonIgnore
public Resource getIconImage(@NotNull Page page) {
if (this.iconResource == null) {
@@ -131,7 +147,7 @@ public Resource getIconResource() {
if (iconResource != null && component != null) {
String delegateResourceType = component.getProperties().get(PN_IMAGE_DELEGATE, String.class);
if (StringUtils.isEmpty(delegateResourceType)) {
LOGGER.error("In order for list rendering delegation to work correctly you need to set up the teaserDelegate property on" +
log.error("In order for list rendering delegation to work correctly you need to set up the teaserDelegate property on" +
" the {} component; its value has to point to the resource type of a teaser component.", component.getPath());
} else {
ValueMap valueMap = iconResource.getValueMap();
@@ -158,7 +174,7 @@ public Resource getBrandImageResource() {
if (brandImageResource != null && component != null) {
String delegateResourceType = component.getProperties().get(PN_IMAGE_DELEGATE, String.class);
if (StringUtils.isEmpty(delegateResourceType)) {
LOGGER.error("In order for list rendering delegation to work correctly you need to set up the teaserDelegate property on" +
log.error("In order for list rendering delegation to work correctly you need to set up the teaserDelegate property on" +
" the {} component; its value has to point to the resource type of a teaser component.", component.getPath());
} else {
ValueMap valueMap = brandImageResource.getValueMap();
@@ -184,14 +200,15 @@ public Resource getBrandImageResource() {
public static @Nullable Resource getPageIcon(@NotNull Page page) {
return page.getContentResource(NN_PAGE_ICON_IMAGE);
}

public static @Nullable Resource getPageBrandImage(@NotNull Page page) {
return page.getContentResource(NN_PAGE_BRANDIMAGE_IMAGE);
}

@PostConstruct
protected void init(){
protected void init() {

LOGGER.info("Teaser init");
log.info("Teaser init");

this.showPublishDate = this.resource.getValueMap().get(nz.co.manawabay.core.models.List.PN_SHOW_PUBLISH_DATE, Boolean.FALSE);
this.publishDateFormatString = this.resource.getValueMap().get(nz.co.manawabay.core.models.List.PN_PUBLISHED_DATE_FORMAT_STRING, nz.co.manawabay.core.models.List.PUBLISHED_DATE_FORMAT_DEFAULT);
@@ -217,6 +234,33 @@ protected void init(){
if (this.brandImageResource == null) {
this.brandImageResource = this.currentPage.getContentResource(NN_PAGE_BRANDIMAGE_IMAGE);
}

String videoFileReference = this.resource.getValueMap().get("videoFileReference", String.class);
if (StringUtils.isNotBlank(videoFileReference)) {
initVideoResource(videoFileReference);
}
}

private void initVideoResource(String videoFileReference) {
Resource downloadResource = resourceResolver.getResource(videoFileReference);

if (downloadResource != null) {
Asset downloadAsset = downloadResource.adaptTo(Asset.class);
String filename = downloadAsset.getName();

String format = downloadAsset.getMetadataValue(DamConstants.DC_FORMAT);
String extension = mimeTypeService.getExtension(format);

if (StringUtils.isEmpty(extension)) {
extension = FilenameUtils.getExtension(filename);
}

videoUrl = getDownloadUrl(downloadResource, extension);
}
}

private String getDownloadUrl(Resource resource, String extension) {
return resource.getPath() + ".coredownload." + extension;
}

@Override
@@ -254,7 +298,7 @@ public String getPretitle() {

@Override
public String getTitle() {
return ! this.hideTitle ? teaser.getTitle() : null;
return !this.hideTitle ? teaser.getTitle() : null;
}

@Override
Original file line number Diff line number Diff line change
@@ -359,6 +359,35 @@
jcr:title="Asset"
sling:resourceType="core/wcm/components/include/imagedelegate"
path="core/wcm/components/image/v3/image/cq:dialog/content/items/tabs/items/asset"/>
<video
jcr:primaryType="nt:unstructured"
jcr:title="Video"
sling:resourceType="granite/ui/components/coral/foundation/container"
margin="{Boolean}true">
<items jcr:primaryType="nt:unstructured">
<columns
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/fixedcolumns"
margin="{Boolean}true">
<items jcr:primaryType="nt:unstructured">
<column
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/container">
<items jcr:primaryType="nt:unstructured">
<file
jcr:primaryType="nt:unstructured"
sling:resourceType="cq/gui/components/authoring/dialog/fileupload"
allowUpload="false"
fieldLabel="Video"
fileReferenceParameter="./videoFileReference"
mimeTypes="[video/*]"
name="./videoFile"/>
</items>
</column>
</items>
</columns>
</items>
</video>
<styletab
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/include"
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div data-sly-use.teaser="com.adobe.cq.wcm.core.components.models.Teaser"
<div data-sly-use.teaser="nz.co.manawabay.core.models.Teaser"
data-sly-use.templates="core/wcm/components/commons/v1/templates.html"
data-sly-use.imageTemplate="image.html"
data-sly-use.pretitleTemplate="pretitle.html"
@@ -8,7 +8,8 @@
data-sly-use.iconTemplate="icon.html"
data-sly-use.brandImageTemplate="brandimage.html"
data-sly-use.publishDateTemplate="publishdate.html"
data-sly-test.hasContent="${teaser.imageResource || teaser.pretitle || teaser.title || teaser.description || teaser.actions.size > 0}"
data-sly-use.videoTemplate="video.html"
data-sly-test.hasContent="${teaser.videoUrl || teaser.imageResource || teaser.pretitle || teaser.title || teaser.description || teaser.actions.size > 0}"
id="${teaser.id}"
class="cmp-teaser${!wcmmode.disabled && teaser.imageResource ? ' cq-dd-image' : ''}"
data-cmp-data-layer="${teaser.data.json}">
@@ -24,6 +25,7 @@
<sly data-sly-call="${descriptionTemplate.description @ teaser=teaser}"></sly>
<sly data-sly-call="${actionsTemplate.actions @ teaser=teaser}"></sly>
<sly data-sly-call="${brandImageTemplate.brandimage @ teaser=teaser}"></sly>
<sly data-sly-test="${teaser.videoUrl}" data-sly-call="${videoTemplate.video @ teaser=teaser}"></sly>
</div>
<sly data-sly-test="${!teaser.hideImage}" data-sly-call="${imageTemplate.image @ teaser=teaser}"></sly>
</a>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<sly data-sly-template.video="${@ teaser}">
<video controls>
<source src="${teaser.videoUrl}" type="video/mp4">
</video>
</sly>