|
| 1 | +import { setBlockItemOptions, moveClassToTargetedChild } from '../../scripts/utils.js'; |
| 2 | +import { createOptimizedPicture } from '../../scripts/aem.js'; |
| 3 | +import { moveInstrumentation } from '../../scripts/scripts.js'; |
| 4 | +import { renderButton } from '../../components/button/button.js'; |
| 5 | + |
| 6 | +/* |
| 7 | + * Commerce-Teaser Block |
| 8 | + * |
| 9 | + * Authoring column order: |
| 10 | + * 0 — Product Title (text) |
| 11 | + * 1 — Price (text, supports currency formatting) |
| 12 | + * 2 — Description (rich-text / text) |
| 13 | + * 3 — Product Image (asset reference or <picture>) |
| 14 | + * 4 — Image Alt Text (text – optional) |
| 15 | + * 5 — Product Link (URL) |
| 16 | + * 6 — Button Label (text – optional, defaults to "Shop Now") |
| 17 | + * 7 — Link Target (select – optional, _blank opens new window) |
| 18 | + */ |
| 19 | + |
| 20 | +export default function decorate(block) { |
| 21 | + /* ---------------- Parse configuration ---------------- */ |
| 22 | + const blockItemsOptions = []; |
| 23 | + const blockItemMap = [ |
| 24 | + { name: 'title' }, |
| 25 | + { name: 'price' }, |
| 26 | + { name: 'description' }, |
| 27 | + { name: 'image' }, |
| 28 | + { name: 'imageAlt' }, |
| 29 | + { name: 'link' }, |
| 30 | + { name: 'label' }, |
| 31 | + { name: 'target' }, |
| 32 | + ]; |
| 33 | + |
| 34 | + setBlockItemOptions(block, blockItemMap, blockItemsOptions); |
| 35 | + const config = blockItemsOptions[0] || {}; |
| 36 | + |
| 37 | + /* ---------------- Build DOM ---------------- */ |
| 38 | + const wrapper = document.createElement('div'); |
| 39 | + wrapper.className = 'commerce-teaser'; |
| 40 | + |
| 41 | + /* Image */ |
| 42 | + const imageContainer = document.createElement('div'); |
| 43 | + imageContainer.className = 'commerce-teaser-image'; |
| 44 | + |
| 45 | + if (config.image) { |
| 46 | + let optimizedPicture; |
| 47 | + try { |
| 48 | + optimizedPicture = createOptimizedPicture( |
| 49 | + config.image, |
| 50 | + config.imageAlt || config.title || 'Product image', |
| 51 | + true, |
| 52 | + [{ width: '750' }], |
| 53 | + ); |
| 54 | + } catch (e) { |
| 55 | + // When config.image is already a <picture> |
| 56 | + optimizedPicture = config.image.cloneNode(true); |
| 57 | + } |
| 58 | + imageContainer.appendChild(optimizedPicture); |
| 59 | + } else { |
| 60 | + const placeholder = createOptimizedPicture( |
| 61 | + 'https://placehold.co/600x450?text=No+Image', |
| 62 | + config.imageAlt || 'Placeholder product image', |
| 63 | + true, |
| 64 | + [{ width: '750' }], |
| 65 | + ); |
| 66 | + imageContainer.appendChild(placeholder); |
| 67 | + } |
| 68 | + |
| 69 | + /* Content */ |
| 70 | + const contentContainer = document.createElement('div'); |
| 71 | + contentContainer.className = 'commerce-teaser-content'; |
| 72 | + |
| 73 | + if (config.title) { |
| 74 | + const titleEl = document.createElement('h3'); |
| 75 | + titleEl.className = 'commerce-teaser-title'; |
| 76 | + titleEl.textContent = config.title; |
| 77 | + contentContainer.appendChild(titleEl); |
| 78 | + } |
| 79 | + |
| 80 | + if (config.price) { |
| 81 | + const priceEl = document.createElement('p'); |
| 82 | + priceEl.className = 'commerce-teaser-price'; |
| 83 | + priceEl.textContent = config.price; |
| 84 | + priceEl.setAttribute('aria-label', `Price: ${config.price}`); |
| 85 | + contentContainer.appendChild(priceEl); |
| 86 | + } |
| 87 | + |
| 88 | + if (config.description) { |
| 89 | + const descEl = document.createElement('p'); |
| 90 | + descEl.className = 'commerce-teaser-description'; |
| 91 | + descEl.textContent = config.description; |
| 92 | + contentContainer.appendChild(descEl); |
| 93 | + } |
| 94 | + |
| 95 | + if (config.link) { |
| 96 | + const buttonContainer = document.createElement('div'); |
| 97 | + buttonContainer.className = 'commerce-teaser-button-container'; |
| 98 | + |
| 99 | + const anchor = document.createElement('a'); |
| 100 | + anchor.href = config.link; |
| 101 | + |
| 102 | + const button = renderButton({ |
| 103 | + linkButton: anchor, |
| 104 | + linkText: config.label || 'Shop Now', |
| 105 | + linkTitle: config.title || 'View Product', |
| 106 | + linkTarget: config.target || '_blank', |
| 107 | + linkType: '', |
| 108 | + linkStyle: '', |
| 109 | + }); |
| 110 | + |
| 111 | + buttonContainer.appendChild(button); |
| 112 | + moveClassToTargetedChild(block, button); |
| 113 | + contentContainer.appendChild(buttonContainer); |
| 114 | + } |
| 115 | + |
| 116 | + /* Assemble */ |
| 117 | + wrapper.appendChild(imageContainer); |
| 118 | + wrapper.appendChild(contentContainer); |
| 119 | + |
| 120 | + moveInstrumentation(block, wrapper); |
| 121 | + |
| 122 | + block.textContent = ''; |
| 123 | + block.appendChild(wrapper); |
| 124 | +} |
| 125 | + |
0 commit comments