Skip to content

Commit

Permalink
Do not import breadcrumb. Style and structure article list.
Browse files Browse the repository at this point in the history
  • Loading branch information
codecodeio committed May 10, 2024
1 parent 4f18a8e commit 2581b9f
Show file tree
Hide file tree
Showing 3 changed files with 237 additions and 29 deletions.
90 changes: 79 additions & 11 deletions best-cigars-guide/blocks/article-list/article-list.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.cards > ul {
/* General styles for the list and list items */
.article-list > ul {
list-style: none;
margin: 0;
padding: 0;
Expand All @@ -7,25 +8,92 @@
grid-gap: 16px;
}

.cards > ul > li {
border: 1px solid var(--dark-color);
background-color: var(--background-color)
.article-list > ul > li {
background-color: #f9f9f9;
border: 1px solid #ccc;
position: relative;
display: flex;
flex-direction: column;
justify-content: space-between;
margin-bottom: 10px;
}

.cards .cards-card-body {
margin: 16px;
/* Styles for article body, title, and description */
.article-list-card-body {
margin: 10px;
padding: 10px;
display: flex;
flex-direction: column;
justify-content: flex-start;
text-align: center;
}

.cards .cards-card-image {
line-height: 0;
.article-title {
margin-top: 0;
padding-top: 0;
}

.cards .cards-card-body > *:first-child {
margin-top: 0;
/* Image and hover effects */
.article-list-card-image a {
display: block;
overflow: hidden;
}

.cards > ul > li img {
.article-list-card-image img {
width: 100%;
height: auto;
transition: transform 0.3s ease;
line-height: 0;
aspect-ratio: 4 / 3;
object-fit: cover;
}

.article-list-card-image a:hover img {
transform: scale(1.1);
}

/* Button styles */
.article-list .article-button-container .button {
font-size: 1.1rem;
background-color: #177abb;
color: white;
padding: 10px 40px;
text-align: center;
text-decoration: none;
border: none;
border-radius: 5px;
border-bottom: 1px solid #035890;
margin-top: auto;
transition: background-color 0.3s ease; /* Smooth transition for background color change */
}

/* Button hover effect */
.article-list .article-button-container .button:hover {
background-color: #035890;
}

/* Container styling for the button */
.article-button-container {
display: flex; /* Use flexbox for layout */
justify-content: center; /* Center horizontally */
align-items: center; /* Center vertically */
padding: 10px;
margin-top: auto; /* Pushes the container to the bottom if flex-direction is column in parent */
}

/* Styles for headings and paragraphs */
.article-list h3 {
color: #2c2c2c;
font-weight: 700;
line-height: 1.2em;
font-size: 1.3rem;
}

.article-list p {
font-family: montserrat, sans-serif;
font-weight: 300;
font-size: 1rem;
line-height: 1.4em;
color: #4a4a4a;
margin: 0 0 1.8em;
}
142 changes: 134 additions & 8 deletions best-cigars-guide/blocks/article-list/article-list.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,144 @@
import { createOptimizedPicture } from '../../scripts/aem.js';
import { createOptimizedPicture } from "../../scripts/aem.js";

Check failure on line 1 in best-cigars-guide/blocks/article-list/article-list.js

View workflow job for this annotation

GitHub Actions / build

Strings must use singlequote

export default function decorate(block) {
//Add nav

Check failure on line 4 in best-cigars-guide/blocks/article-list/article-list.js

View workflow job for this annotation

GitHub Actions / build

Expected exception block, space or tab after '//' in comment
//@todo sytle the nav

Check failure on line 5 in best-cigars-guide/blocks/article-list/article-list.js

View workflow job for this annotation

GitHub Actions / build

Expected exception block, space or tab after '//' in comment
const mainTag = document.querySelector(".article-list-container");

Check failure on line 6 in best-cigars-guide/blocks/article-list/article-list.js

View workflow job for this annotation

GitHub Actions / build

Strings must use singlequote
const navDiv = document.createElement("div");

Check failure on line 7 in best-cigars-guide/blocks/article-list/article-list.js

View workflow job for this annotation

GitHub Actions / build

Strings must use singlequote
navDiv.className = 'nav-row';
mainTag.prepend(navDiv);

//Add breadcrumbs

Check failure on line 11 in best-cigars-guide/blocks/article-list/article-list.js

View workflow job for this annotation

GitHub Actions / build

Expected exception block, space or tab after '//' in comment
//@todo style breadcrumbs

Check failure on line 12 in best-cigars-guide/blocks/article-list/article-list.js

View workflow job for this annotation

GitHub Actions / build

Expected exception block, space or tab after '//' in comment
const h1 = document.querySelector("h1");

Check failure on line 13 in best-cigars-guide/blocks/article-list/article-list.js

View workflow job for this annotation

GitHub Actions / build

Strings must use singlequote
const h1Text = h1.textContent.trim();

const breadcrumbDiv = document.createElement("div");

Check failure on line 16 in best-cigars-guide/blocks/article-list/article-list.js

View workflow job for this annotation

GitHub Actions / build

Strings must use singlequote
breadcrumbDiv.className = 'breadcrumb';
breadcrumbDiv.innerHTML += `
<p id="breadcrumbs">
<span><a href="https://www.famous-smoke.com/best-cigars-guide/">Home</a></span> » <span class="breadcrumb_last" aria-current="page">${h1Text}</span>
</p>
`;
navDiv.append(breadcrumbDiv);

//Add dropdown list (copied from existing site)

Check failure on line 25 in best-cigars-guide/blocks/article-list/article-list.js

View workflow job for this annotation

GitHub Actions / build

Expected exception block, space or tab after '//' in comment
//@todo build list dynamically and style it
const dropdownListDiv = document.createElement("div");
breadcrumbDiv.className = 'category-dropdown';
dropdownListDiv.innerHTML += `
<form action="https://www.famous-smoke.com/best-cigars-guide/" method="get" class="dropcats">
<select name="cat" id="cat" class="postform" onchange="return this.form.submit()">
<option value="-1">Select a Category</option>
<option class="level-0" value="2" selected="selected">Best Cigars by Country</option>
<option class="level-0" value="3">Best Cigars by Occasion</option>
<option class="level-0" value="4">Best Cigars by Food/Drink Pairing</option>
<option class="level-0" value="5">Best Cigars by Shape, Type &amp; Size</option>
<option class="level-0" value="6">Best Cigars by Year</option>
<option class="level-0" value="7">Best Cigar Accessories</option>
</select>
<noscript><input type="submit" value="View" /></noscript>
</form>
`;
navDiv.append(dropdownListDiv);

//Add Article list
/* change to ul, li */
const ul = document.createElement('ul');
const ul = document.createElement("ul");
//loop over each row of import file
[...block.children].forEach((row) => {
const li = document.createElement('li');
const li = document.createElement("li");
//For each row child, a new <li> is created.
while (row.firstElementChild) li.append(row.firstElementChild);

// Find the last div and extract the href from its link
const lastDiv = li.querySelector("div:last-child");
const link = lastDiv ? lastDiv.querySelector("a") : null;
const href = link ? link.href : "#"; // Default to '#' if no link is found

// The second div contains the title for links and buttons
const secondDiv = li.children[1]; // direct access to the second child assuming it's a div
const title = secondDiv ? secondDiv.textContent.trim() : ""; // Get the text content as title

//loop over each li
[...li.children].forEach((div) => {
if (div.children.length === 1 && div.querySelector('picture')) div.className = 'cards-card-image';
else div.className = 'cards-card-body';
//first div is for the article image
if (div.querySelector("picture")) {
div.className = "article-list-card-image";
const picture = div.querySelector("picture");
if (picture) {
const anchor = document.createElement("a");
anchor.href = href;
anchor.title = title;
picture.parentNode.insertBefore(anchor, picture);
anchor.appendChild(picture);
}
}
//second div is for the article body
else if (
!div.querySelector("picture") &&
li.querySelector(".article-list-card-body") === null
) {
div.className = "article-list-card-body";
}
//remaining divs are appended to prior article body div
else {
div.previousSibling.append(div.firstElementChild);
// Set class names for the first and second paragraphs within the article-list-card-body
const paragraphs = div.previousSibling.querySelectorAll("p");
// First paragraph as article-title
if (paragraphs.length > 0) {
//change article title to an h3
const h3 = document.createElement("h3");
h3.className = "article-title";
h3.textContent = paragraphs[0].textContent;
div.previousSibling.replaceChild(h3, paragraphs[0]);
}
// Second paragraph as article-description
if (paragraphs.length > 1) {
paragraphs[1].className = "article-description";
}
}

// Create and append a new div with a button
if (!li.querySelector("a.button")) {
const newDiv = document.createElement("div");
newDiv.className = 'article-button-container';
const newLink = document.createElement("a");
newLink.href = href;
newLink.className = "button";
newLink.textContent = "View List";
newLink.title = title;
newDiv.appendChild(newLink);
li.appendChild(newDiv);
}

ul.append(li);
});
ul.append(li);

// Remove the last div that contained the original link
const linkDiv = li.children[2];
if (linkDiv && linkDiv.parentNode) {
linkDiv.parentNode.removeChild(linkDiv);
}
});
ul.querySelectorAll('img').forEach((img) => img.closest('picture').replaceWith(createOptimizedPicture(img.src, img.alt, false, [{ width: '750' }])));
block.textContent = '';

//optimize images
ul.querySelectorAll("img").forEach((img) =>
img
.closest("picture")
.replaceWith(
createOptimizedPicture(img.src, img.alt, false, [{ width: "750" }])
)
);

// Remove any empty div tags
[...ul.querySelectorAll("div")].forEach((div) => {
if (div.children.length === 0 && div.textContent.trim() === "") {
div.parentNode.removeChild(div);
}
});

block.textContent = "";
block.append(ul);
}
34 changes: 24 additions & 10 deletions tools/importer/import-category.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ const createMetadataBlock = (main, document) => {
const meta = {};

// find the <title> element
const title = document.querySelector("title");
const title = document.querySelector('title');
if (title) {
meta.Title = title.innerHTML.replace(/[\n\t]/gm, "");
meta.Title = title.innerHTML.replace(/[\n\t]/gm, ``);
}

// find the <meta property="og:description"> element
Expand All @@ -14,8 +14,7 @@ const createMetadataBlock = (main, document) => {
}

// set the <meta property="og:image"> element
meta.image =
"/best-cigars-guide/icons/best-cigars-guide.png";
meta.image = 'https://www.famous-smoke.com/best-cigars-guide/icons/best-cigars-guide.png';

// helper to create the metadata block
const block = WebImporter.Blocks.getMetadataBlock(document, meta);
Expand All @@ -29,20 +28,23 @@ const createMetadataBlock = (main, document) => {

const createArticleListBlock = (main, document) => {
var categories = [["Article-list"]];
var categoriesList = document.querySelector(".categories-list");
var divElements = categoriesList.querySelectorAll("div");
let categoriesList = document.querySelector(".categories-list");
let divElements = categoriesList.querySelectorAll("div");

divElements.forEach(function (element) {

Check warning on line 34 in tools/importer/import-category.js

View workflow job for this annotation

GitHub Actions / build

Unexpected unnamed function
var title = element.querySelector("h3").textContent.trim();
var description = element.querySelector("p").textContent.trim();
var image = element.querySelector("img");
var imageSrc = image ? image.src : "";

var linkElement = element.querySelector("a.button");
linkElement.class = 'button';
var link = linkElement ? linkElement.href : "";

var card = [
[image, link],
[title, description, linkElement],
[image],
[title],
[description],
[formatUrl(link)]
];

categories.push(card);
Expand All @@ -58,9 +60,21 @@ const createArticleListBlock = (main, document) => {
return articleList;
};

const formatUrl = (originalUrl) => {
// Create a URL object based on the original URL
var url = new URL(originalUrl);

// Set the new domain (including protocol)
url.hostname = "www.famous-smoke.com";
url.protocol = "https:";
url.port = '';

return url;
}

const removeSectionsNotForImport = (main, document) => {
// remove any section from main not needed for import
WebImporter.DOMUtils.remove(main, [".category-dropdown"]);
WebImporter.DOMUtils.remove(main, [".category-dropdown",".breadcrumb"]);
};

export default {
Expand Down

0 comments on commit 2581b9f

Please sign in to comment.