@sitecore-jss/sitecore-jss
Version:
This module is provided as a part of Sitecore JavaScript Rendering SDK. It contains the core JSS APIs (layout service) and utilities.
139 lines (138 loc) • 6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DefaultEditFrameButtons = exports.DefaultEditFrameButton = exports.DefaultEditFrameButtonIds = void 0;
exports.mapButtonToCommand = mapButtonToCommand;
exports.commandBuilder = commandBuilder;
exports.DefaultEditFrameButtonIds = {
edit: '{70C4EED5-D4CD-4D7D-9763-80C42504F5E7}',
};
exports.DefaultEditFrameButton = {
insert: {
header: 'Insert New',
icon: '/~/icon/Office/16x16/insert_from_template.png',
click: 'webedit:new',
tooltip: 'Insert a new item',
},
editRelatedItem: {
header: 'Edit the related item',
icon: '/~/icon/Office/16x16/cubes.png',
click: 'webedit:open', // Command in Sitecore, 'chrome:common:edititem({command:"webedit:open"})',
tooltip: 'Edit the related item in the Content Editor.',
},
edit: {
header: 'Edit Item',
icon: '/~/icon/people/16x16/cubes_blue.png',
fields: ['Title', 'Text'],
tooltip: 'Edit the item fields.',
},
};
exports.DefaultEditFrameButtons = [
exports.DefaultEditFrameButton.editRelatedItem,
exports.DefaultEditFrameButton.insert,
exports.DefaultEditFrameButton.edit,
];
/**
* @param {WebEditButton | FieldEditButton} button the button to determine the type of
*/
function isWebEditButton(button) {
return button.click !== undefined;
}
/**
* Map the edit button types to chrome data
* @param {EditButtonTypes } button the edit button to build a ChromeCommand for
* @param {string} itemId the ID of the item the EditFrame is associated with
* @param {Record<string, string | number | boolean | undefined | null>} frameParameters additional parameters passed to the EditFrame
*/
function mapButtonToCommand(button, itemId, frameParameters) {
if (button === '|' || button.isDivider) {
return {
click: 'chrome:dummy',
header: 'Separator',
icon: '',
isDivider: true,
tooltip: null,
type: 'separator',
};
}
else if (isWebEditButton(button)) {
return commandBuilder(button, itemId, frameParameters);
}
else {
const fieldsString = button.fields.join('|');
const editButton = Object.assign({ click: `webedit:fieldeditor(command=${exports.DefaultEditFrameButtonIds.edit},fields=${fieldsString})` }, button);
return commandBuilder(editButton, itemId, frameParameters);
}
}
/**
* Build a ChromeCommand from a web edit button. Merging the parameters from the button, frame and id
* @param {WebEditButton } button the web edit button to build a ChromeCommand for
* @param {string} itemId the ID of the item the EditFrame is associated with
* @param {Record<string, string>} frameParameters additional parameters passed to the EditFrame
*/
function commandBuilder(button, itemId, frameParameters) {
if (!button.click) {
return Object.assign({ isDivider: false, type: button.type || null, header: button.header || '', icon: button.icon || '', tooltip: button.tooltip || '' }, button);
}
else if (button.click.startsWith('javascript:') || button.click.startsWith('chrome:')) {
return Object.assign({ isDivider: false, type: button.type || null, header: button.header || '', icon: button.icon || '', tooltip: button.tooltip || '' }, button);
}
else {
if (!itemId) {
return Object.assign({ isDivider: false, type: button.type || null, header: button.header || '', icon: button.icon || '', tooltip: button.tooltip || '' }, button);
}
else {
let message = button.click;
let parameters = {};
// Extract any parameters already in the command
const length = button.click.indexOf('(');
if (length >= 0) {
const end = button.click.indexOf(')');
if (end < 0) {
throw new Error('Message with arguments must end with ")".');
}
parameters = button.click
.substring(length + 1, end)
.split(',')
.map((_) => _.trim())
.reduce((previous, current) => {
const parts = current.split('=');
if (parts.length < 2) {
previous[parts[0]] = '';
}
else {
previous[parts[0]] = parts[1];
}
return previous;
}, {});
message = button.click.substring(0, length);
}
parameters.id = itemId;
if (button.parameters) {
Object.keys(button.parameters).forEach((_) => {
var _a;
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
parameters[_] = ((_a = button.parameters[_]) === null || _a === void 0 ? void 0 : _a.toString()) || '';
});
}
if (frameParameters) {
Object.keys(frameParameters).forEach((_) => {
var _a;
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
parameters[_] = ((_a = frameParameters[_]) === null || _a === void 0 ? void 0 : _a.toString()) || '';
});
}
const parameterString = Object.keys(parameters)
.map((_) => `${_}=${parameters[_]}`)
.join(', ');
const click = `${message}(${parameterString})`;
return {
isDivider: false,
click: `javascript:Sitecore.PageModes.PageEditor.postRequest('${click}',null,false)`,
header: button.header || '',
icon: button.icon || '',
tooltip: button.tooltip || '',
type: button.type || null,
};
}
}
}