generated from adobe/aem-boilerplate
-
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.
Merge pull request #181 from hlxsites/feature/dynamic-block
Feature/dynamic block
- Loading branch information
Showing
8 changed files
with
149 additions
and
39 deletions.
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
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,28 @@ | ||
.grey-article-outer { | ||
background-color: #ededed; | ||
padding: 80px 0; | ||
} | ||
|
||
.grey-article-outer > div { | ||
max-width: 980px; | ||
margin: 0 auto; | ||
} | ||
|
||
.recent-posts { | ||
text-align: left; | ||
} | ||
|
||
.recent-posts .post .image img { | ||
width: 100%; | ||
height: auto; | ||
} | ||
|
||
@media (max-width: 767px) { | ||
.grey-article-outer { | ||
padding: 60px 0; | ||
} | ||
|
||
.recent-posts .content .col { | ||
width: 100%; | ||
} | ||
} |
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,84 @@ | ||
import { | ||
div, a, img, p, h3, | ||
} from '../../scripts/dom-builder.js'; | ||
|
||
import { readBlockConfig } from '../../scripts/aem.js'; | ||
|
||
async function fetchPostData() { | ||
try { | ||
const response = await fetch('/query-index.json'); | ||
const jsonData = await response.json(); | ||
return jsonData.data; | ||
} catch (error) { | ||
return []; | ||
} | ||
} | ||
|
||
function truncateText(text, maxLength) { | ||
if (text.length <= maxLength) { | ||
return text; | ||
} | ||
return `${text.slice(0, maxLength)}...`; | ||
} | ||
|
||
function createRecentPosts(results) { | ||
const lists = div({ class: 'posts' }); | ||
results.forEach((post) => { | ||
const showcaseBanner = div({ class: 'post' }); | ||
const articleCardImage = a({ class: 'image', href: post.path }, img({ | ||
src: post.image, alt: post.title, width: '100%', height: 'auto', | ||
})); | ||
const articleCardBody = div({ class: 'text' }); | ||
const articleHeading = h3({ class: 'entry-title' }, a({ href: post.path }, post.title)); | ||
const articleDescription = p({ class: 'intro' }, truncateText(post.description, 180)); | ||
articleCardBody.appendChild(articleHeading); | ||
articleCardBody.appendChild(articleDescription); | ||
showcaseBanner.appendChild(articleCardImage); | ||
showcaseBanner.appendChild(articleCardBody); | ||
lists.append(showcaseBanner); | ||
}); | ||
return lists; | ||
} | ||
|
||
export default async function decorate(block) { | ||
const blockData = readBlockConfig(block); | ||
const postData = await fetchPostData(); | ||
let topic = ''; | ||
if (blockData.topic) { | ||
topic = blockData.topic; | ||
} | ||
const wrapper = div({ class: 'content flex cols2' }); | ||
const blogTitles = block.children[0].cloneNode(true); | ||
if (block.children[1]) { | ||
topic = block.children[1].children[1].innerText.trim(); | ||
} | ||
if (blogTitles.children[0]) { | ||
const title = blogTitles.children[0]; | ||
const blogsContainer = div({ class: 'col recent-posts' }); | ||
let sortedResults = []; | ||
const filteredResults = postData.filter((item) => item.path.includes('/news/') && (topic ? JSON.parse(item.tags).filter((tag) => tag.toLowerCase() === topic.toLowerCase()).length > 0 : true)); | ||
if (filteredResults.length) { | ||
sortedResults = filteredResults.sort((ar1, ar2) => ar2.date - ar1.date); | ||
} | ||
const postElement = createRecentPosts(sortedResults.slice(0, 3)); | ||
blogsContainer.appendChild(title); | ||
blogsContainer.appendChild(postElement); | ||
wrapper.appendChild(blogsContainer); | ||
} | ||
const newsTitles = block.children[0].cloneNode(true); | ||
if (newsTitles.children[1]) { | ||
const title = newsTitles.children[1]; | ||
const blogsContainer = div({ class: 'col recent-posts' }); | ||
let sortedResults = []; | ||
const filteredResults = postData.filter((item) => item.path.includes('/blog/') && (topic ? JSON.parse(item.tags).filter((tag) => tag.toLowerCase() === topic.toLowerCase()).length > 0 : true)); | ||
if (filteredResults.length) { | ||
sortedResults = filteredResults.sort((ar1, ar2) => ar2.date - ar1.date); | ||
} | ||
const postElement = createRecentPosts(sortedResults.slice(0, 3)); | ||
blogsContainer.appendChild(title); | ||
blogsContainer.appendChild(postElement); | ||
wrapper.appendChild(blogsContainer); | ||
} | ||
block.innerText = ''; | ||
block.appendChild(wrapper); | ||
} |
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
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
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
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
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