100 lines
3.7 KiB
JavaScript
100 lines
3.7 KiB
JavaScript
function getApiSourceById(sourceId) {
|
|
var sources = Array.isArray(initialData && initialData.apiSources) ? initialData.apiSources : [];
|
|
var normalizedId = Number(sourceId || 0);
|
|
return sources.find(function (source) {
|
|
return Number(source.id) === normalizedId;
|
|
}) || null;
|
|
}
|
|
|
|
function getApiSourceItems(sourceId) {
|
|
var source = getApiSourceById(sourceId);
|
|
var responseJson = source && source.responseJson && typeof source.responseJson === 'object' ? source.responseJson : null;
|
|
if (Array.isArray(responseJson)) {
|
|
return responseJson;
|
|
}
|
|
if (responseJson && Array.isArray(responseJson.items)) {
|
|
return responseJson.items;
|
|
}
|
|
if (responseJson && Array.isArray(responseJson.results)) {
|
|
return responseJson.results;
|
|
}
|
|
if (responseJson && Array.isArray(responseJson.data)) {
|
|
return responseJson.data;
|
|
}
|
|
return responseJson ? [responseJson] : [];
|
|
}
|
|
|
|
function getApiItem(sourceId, itemNumber) {
|
|
var items = getApiSourceItems(sourceId);
|
|
var parsedItemNumber = Math.max(1, Number(itemNumber || 1));
|
|
var index = Number.isFinite(parsedItemNumber) && parsedItemNumber > 0 ? parsedItemNumber - 1 : 0;
|
|
return items[index] || null;
|
|
}
|
|
|
|
function resolveApiPath(value, path) {
|
|
var current = value;
|
|
if (!path) {
|
|
return current;
|
|
}
|
|
|
|
String(path).split('.').forEach(function (segment) {
|
|
if (current === undefined || current === null) {
|
|
current = '';
|
|
return;
|
|
}
|
|
current = current[segment];
|
|
});
|
|
|
|
return current === undefined || current === null ? '' : current;
|
|
}
|
|
|
|
function substituteApiVariables(html, item) {
|
|
var source = String(html || '');
|
|
return source.replace(/\{\{\s*([a-zA-Z0-9_]+)(?:\.([a-zA-Z0-9_.]+))?\s*\}\}/g, function (_match, tokenName, tokenPath) {
|
|
if (!item || typeof item !== 'object') {
|
|
return '';
|
|
}
|
|
var key = tokenName === 'item' && tokenPath ? tokenPath : tokenName;
|
|
return escapeHtml(resolveApiPath(item, key || ''));
|
|
});
|
|
}
|
|
|
|
function getApiPreviewFallback(item) {
|
|
if (!item || typeof item !== 'object') {
|
|
return '';
|
|
}
|
|
|
|
var title = String(item.title || item.name || '').trim();
|
|
var description = String(item.description || item.summary || item.text || '').trim();
|
|
var summary = [];
|
|
if (title) {
|
|
summary.push('<h3>' + escapeHtml(title) + '</h3>');
|
|
}
|
|
if (description) {
|
|
summary.push('<div>' + sanitizeRichText(description) + '</div>');
|
|
}
|
|
if (!summary.length) {
|
|
return '';
|
|
}
|
|
return summary.join('');
|
|
}
|
|
|
|
function renderApiRegion(region, regionContent) {
|
|
var content = regionContent && regionContent.value !== undefined ? regionContent.value : '';
|
|
var sourceId = regionContent && regionContent.source_id !== undefined ? regionContent.source_id : null;
|
|
var itemNumber = regionContent && regionContent.item_number !== undefined ? regionContent.item_number : 1;
|
|
var fontFamily = sanitizeFontFamily(regionContent.font_family || region.fontFamily);
|
|
var fontSize = sanitizeFontSize(regionContent.font_size || region.fontSize);
|
|
var fontColor = sanitizeTextColor(regionContent.font_color || region.fontColor);
|
|
var item = getApiItem(sourceId, itemNumber);
|
|
var body = item ? substituteApiVariables(content, item) : '';
|
|
if (!body && item) {
|
|
body = getApiPreviewFallback(item);
|
|
}
|
|
var contentStyle = 'width:' + region.pixelWidth + 'px;height:' + region.pixelHeight + 'px;transform:scale(' + region.canvasScale + ');transform-origin:top left;' + (fontFamily ? 'font-family:' + escapeHtml(fontFamily) + ';' : '') + 'font-size:' + fontSize + 'px;color:' + escapeHtml(fontColor) + ';';
|
|
if (!body) {
|
|
return '';
|
|
}
|
|
var renderedBody = renderEditorJsContent(body);
|
|
return renderedBody ? '<div class="template-region api" style="' + region.baseStyle + '"><div class="template-region-text-scale" style="' + contentStyle + '">' + renderedBody + '</div></div>' : '';
|
|
} |