Skip to content

Commit

Permalink
add video to teaser component
Browse files Browse the repository at this point in the history
  • Loading branch information
QuangTranXuan committed Nov 19, 2023
1 parent dbd2523 commit 8c68e21
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 21 deletions.
86 changes: 67 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,45 @@
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.commons.link.LinkManager;
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";

Expand All @@ -56,9 +57,19 @@ 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;

@Self
private LinkManager linkManager;

@OSGiService
private MimeTypeService mimeTypeService;

private Resource iconResource;
private Resource brandImageResource;
@ValueMapValue
Expand All @@ -79,6 +90,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);
Expand All @@ -89,18 +101,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;
}
Expand All @@ -109,6 +126,9 @@ public String getPublishDateFormatString() {
return publishDateFormatString;
}

@Getter
private String videoUrl;

@JsonIgnore
public Resource getIconImage(@NotNull Page page) {
if (this.iconResource == null) {
Expand All @@ -131,7 +151,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();
Expand All @@ -158,7 +178,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();
Expand All @@ -184,14 +204,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);
Expand All @@ -217,6 +238,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
Expand Down Expand Up @@ -254,7 +302,7 @@ public String getPretitle() {

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
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"
Expand All @@ -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}">
Expand All @@ -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>
Expand Down
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>

0 comments on commit 8c68e21

Please sign in to comment.