Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 2 additions & 16 deletions blocks/carousel/carousel.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// eslint-disable-next-line import/no-cycle
import { buildVideoAutoBlocks, getLocale } from '../../scripts/scripts.js';
import { buildVideoAutoBlocks, loadCopy } from '../../scripts/scripts.js';
import { decorateBlock, loadBlock } from '../../scripts/aem.js';

const CAROUSEL_VARIANTS = ['slides', 'promo'];
Expand All @@ -11,20 +11,6 @@ function resolveVariant(block) {
return 'promo';
}

async function loadCopy(lang) {
const scriptPath = new URL(import.meta.url).pathname;
const jsonPath = scriptPath.replace(/\.js$/, '.json');
const url = `${window.hlx?.codeBasePath || ''}${jsonPath}`;
try {
const resp = await fetch(url);
if (!resp.ok) return {};
const data = await resp.json();
return data[lang] || data.en || {};
} catch {
return {};
}
}

function updateNavigation(block, slideIndex) {
const slides = block.querySelectorAll('.carousel-slide');
const prev = block.querySelector('.slide-prev');
Expand Down Expand Up @@ -318,7 +304,7 @@ export default async function decorate(block) {
const isSingleSlide = rows.length < 2;
const isSlides = variant === 'slides';

const copy = await loadCopy(getLocale());
const copy = await loadCopy(import.meta.url);
block.dataset.slideOf = copy.of || 'of';

block.setAttribute('role', 'region');
Expand Down
4 changes: 2 additions & 2 deletions scripts/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ export function getLocale() {
}

/**
* Fetches localized UI strings from a block's companion JSON file.
* @param {string} scriptUrl - The block module's `import.meta.url`
* Fetches localized UI strings from a folders's companion JSON file.
* @param {string} scriptUrl - The module's `import.meta.url`
* @returns {Promise<Object>}
*/
export async function loadCopy(scriptUrl) {
Expand Down
18 changes: 2 additions & 16 deletions widgets/plp/plp.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
createOptimizedPicture, decorateBlock, loadBlock,
} from '../../scripts/aem.js';
import { normalizeIndexImageUrl, getLocale } from '../../scripts/scripts.js';
import { normalizeIndexImageUrl, getLocale, loadCopy } from '../../scripts/scripts.js';

const FACETS = [
{ key: 'region', copyKey: 'region' },
Expand All @@ -12,20 +12,6 @@ const FACETS = [
{ key: 'traction-system', copyKey: 'tractionSystem' },
];

async function loadWidgetCopy(lang) {
const scriptPath = new URL(import.meta.url).pathname;
const jsonPath = scriptPath.replace(/\.js$/, '.json');
const url = `${window.hlx?.codeBasePath || ''}${jsonPath}`;
try {
const resp = await fetch(url);
if (!resp.ok) return {};
const data = await resp.json();
return data[lang] || data.en || {};
} catch {
return {};
}
}

function isDirectChild(itemPath, parentPath) {
const normalized = parentPath.endsWith('/') ? parentPath.slice(0, -1) : parentPath;
if (!itemPath.startsWith(`${normalized}/`)) return false;
Expand Down Expand Up @@ -151,7 +137,7 @@ async function renderCards(container, items) {

export default async function decorate(widget) {
const lang = getLocale();
const [allItems, copy] = await Promise.all([loadIndex(lang), loadWidgetCopy(lang)]);
const [allItems, copy] = await Promise.all([loadIndex(lang), loadCopy(import.meta.url)]);
const parentPath = window.location.pathname;
const children = allItems
.filter((item) => isDirectChild(item.path || '', parentPath))
Expand Down
28 changes: 4 additions & 24 deletions widgets/press-releases/press-releases.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,6 @@
import { getLocale } from '../../scripts/scripts.js';
import { getLocale, loadCopy } from '../../scripts/scripts.js';

/**
* Load widget config from the widget's local JSON (same name as the script).
* @param {string} lang - Language key (e.g. en)
* @returns {Promise<{ copy: Object, defaultPageSize: number }>}
*/
async function loadWidgetConfig(lang) {
const scriptPath = new URL(import.meta.url).pathname;
const jsonPath = scriptPath.replace(/\.js$/, '.json');
const url = `${window.hlx?.codeBasePath || ''}${jsonPath}`;
try {
const resp = await fetch(url);
if (!resp.ok) return { copy: {}, defaultPageSize: 12 };
const data = await resp.json();
const key = data[lang] ? lang : 'en';
const defaultPageSize = parseInt(data.defaultPageSize, 10) || 12;
return { copy: data[key] || {}, defaultPageSize };
} catch (_) {
return { copy: {}, defaultPageSize: 12 };
}
}
const DEFAULT_PAGE_SIZE = 12;

/**
* Normalize a press release row from the query index.
Expand Down Expand Up @@ -225,11 +206,10 @@ export default async function decorate(widget) {
if (widget.dataset.pressReleasesInitialized === 'true') return;
widget.dataset.pressReleasesInitialized = 'true';

const lang = getLocale();
const { copy, defaultPageSize } = await loadWidgetConfig(lang);
const copy = await loadCopy(import.meta.url);

hydrateCopy(widget, copy);

const pageSize = parseInt(widget.dataset.pageSize, 10) || defaultPageSize;
const pageSize = parseInt(widget.dataset.pageSize, 10) || DEFAULT_PAGE_SIZE;
buildPressReleasesListing(widget, copy, pageSize);
}
1 change: 0 additions & 1 deletion widgets/press-releases/press-releases.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"defaultPageSize": 12,
"en": {
"learnMore": "Learn More >",
"loadMore": "Load More",
Expand Down
28 changes: 3 additions & 25 deletions widgets/search/search.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,5 @@
import { createOptimizedPicture, loadCSS } from '../../scripts/aem.js';
import { normalizeIndexImageUrl, getLocale } from '../../scripts/scripts.js';

/**
* Load widget copy from the widget's local JSON (same name as the script).
* @param {string} lang - Language key (e.g. en)
* @returns {Promise<Object>} Copy for that language (flat key-value)
*/
async function loadWidgetCopy(lang) {
const scriptPath = new URL(import.meta.url).pathname;
const jsonPath = scriptPath.replace(/\.js$/, '.json');
const url = `${window.hlx?.codeBasePath || ''}${jsonPath}`;
try {
const resp = await fetch(url);
if (!resp.ok) return {};
const data = await resp.json();
const key = data[lang] ? lang : 'en';
return data[key] || {};
} catch (_) {
return {};
}
}
import { normalizeIndexImageUrl, getLocale, loadCopy } from '../../scripts/scripts.js';

/**
* Normalize a single item from the query index.
Expand Down Expand Up @@ -687,8 +667,7 @@ export async function attachSearchSuggestions(input, opts = {}) {

await loadCSS(`${window.hlx?.codeBasePath || ''}/widgets/search/suggestions.css`);

const lang = (document.documentElement.lang || 'en').split('-')[0];
const copy = await loadWidgetCopy(lang);
const copy = await loadCopy(import.meta.url);

const overlay = document.createElement('div');
overlay.id = 'search-suggestions';
Expand Down Expand Up @@ -851,8 +830,7 @@ export async function initHeaderSearch(input, opts = {}) {
* @param {HTMLElement} widget - Widget container element
*/
export default async function decorate(widget) {
const lang = (document.documentElement.lang || 'en').split('-')[0];
const copy = await loadWidgetCopy(lang);
const copy = await loadCopy(import.meta.url);

hydrateCopy(widget, copy);
searchResultsContainer = widget;
Expand Down