Y-SLD/assets/playkit/plugins/sweetalert/lib/modules/handle-key.js

79 lines
2.2 KiB
JavaScript
Raw Normal View History

2024-03-01 11:23:55 +00:00
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
var _stopEventPropagation$fireClick = require('./handle-dom');
var _setFocusStyle = require('./handle-swal-dom');
var handleKeyDown = function handleKeyDown(event, params, modal) {
var e = event || window.event;
var keyCode = e.keyCode || e.which;
var $okButton = modal.querySelector('button.confirm');
var $cancelButton = modal.querySelector('button.cancel');
var $modalButtons = modal.querySelectorAll('button[tabindex]');
if ([9, 13, 32, 27].indexOf(keyCode) === -1) {
// Don't do work on keys we don't care about.
return;
}
var $targetElement = e.target || e.srcElement;
var btnIndex = -1; // Find the button - note, this is a nodelist, not an array.
for (var i = 0; i < $modalButtons.length; i++) {
if ($targetElement === $modalButtons[i]) {
btnIndex = i;
break;
}
}
if (keyCode === 9) {
// TAB
if (btnIndex === -1) {
// No button focused. Jump to the confirm button.
$targetElement = $okButton;
} else {
// Cycle to the next button
if (btnIndex === $modalButtons.length - 1) {
$targetElement = $modalButtons[0];
} else {
$targetElement = $modalButtons[btnIndex + 1];
}
}
_stopEventPropagation$fireClick.stopEventPropagation(e);
$targetElement.focus();
if (params.confirmButtonColor) {
_setFocusStyle.setFocusStyle($targetElement, params.confirmButtonColor);
}
} else {
if (keyCode === 13) {
if ($targetElement.tagName === 'INPUT') {
$targetElement = $okButton;
$okButton.focus();
}
if (btnIndex === -1) {
// ENTER/SPACE clicked outside of a button.
$targetElement = $okButton;
} else {
// Do nothing - let the browser handle it.
$targetElement = undefined;
}
} else if (keyCode === 27 && params.allowEscapeKey === true) {
$targetElement = $cancelButton;
_stopEventPropagation$fireClick.fireClick($targetElement, e);
} else {
// Fallback - let the browser handle it.
$targetElement = undefined;
}
}
};
exports['default'] = handleKeyDown;
module.exports = exports['default'];