handsontable
Version:
Handsontable is a JavaScript Data Grid available for React, Angular and Vue.
106 lines (103 loc) • 3.36 kB
JavaScript
;
exports.__esModule = true;
exports.confirmTemplate = confirmTemplate;
require("core-js/modules/es.error.cause.js");
require("core-js/modules/es.array.push.js");
require("core-js/modules/esnext.iterator.constructor.js");
require("core-js/modules/esnext.iterator.map.js");
var _constants = require("../constants");
var _string = require("../../../helpers/string");
var _templateLiteralTag = require("../../../helpers/templateLiteralTag");
/**
* The `confirmTemplate` function returns a HTML string with the confirm template.
*
* @param {object} vars The variables to use for the template.
* @param {string} vars.id The ID of the confirm.
* @param {string} vars.title The title of the confirm.
* @param {string} vars.description The description of the confirm.
* @param {object[]} vars.buttons The buttons to display in the confirm.
* - `text`: The text of the button.
* - `type`: The type of the button ('primary' | 'secondary').
* - `callback`: The callback to trigger when the button is clicked.
* @returns {string} HTML string with the confirm template.
*/
function confirmTemplate(_ref) {
let {
id = '',
title = '',
description = '',
buttons = []
} = _ref;
/**
* Returns the HTML string for the template.
*
* @returns {string}
*/
function template() {
return `
<div tabindex="-1" data-ref="contentElement" class="${_constants.DIALOG_CLASS_NAME}__content-wrapper-inner">
<div class="${_constants.DIALOG_CLASS_NAME}__content">
<h2
id="${id}-dialog-confirm-title"
class="${_constants.DIALOG_CLASS_NAME}__title">${(0, _string.stripTags)(title)}</h2>
<p
id="${id}-dialog-confirm-description"
class="${_constants.DIALOG_CLASS_NAME}__description">${(0, _string.stripTags)(description)}</p>
</div>
${buttons.length > 0 ? `
<div data-ref="buttonsContainer" class="${_constants.DIALOG_CLASS_NAME}__buttons">
${buttons.map(button => `
<button class="ht-button ht-button--${button.type}">${(0, _string.stripTags)(button.text)}</button>
`).join('')}
</div>
` : ''}
</div>
`;
}
let fragment = null;
const refs = {};
/**
* Compiles the template.
*
* @returns {object} The compiled template.
*/
function compile() {
const elements = (0, _templateLiteralTag.html)`${template()}`;
Object.assign(refs, elements.refs);
fragment = elements.fragment;
return elements;
}
/**
* Gets the focusable elements of the template.
*
* @returns {HTMLElement[]} The focusable elements.
*/
function focusableElements() {
if (fragment === null) {
throw new Error('Compile the template first.');
}
const {
contentElement,
buttonsContainer
} = refs;
const elements = [];
if (buttonsContainer) {
elements.push(...Array.from(buttonsContainer.children));
} else {
elements.push(contentElement);
}
return elements;
}
return {
TEMPLATE_NAME: 'confirm',
dialogA11YOptions() {
return {
role: 'alertdialog',
ariaLabelledby: `${id}-dialog-confirm-title`,
ariaDescribedby: description ? `${id}-dialog-confirm-description` : undefined
};
},
compile,
focusableElements
};
}