UNPKG

@mucfe/matui

Version:

基于Vue和ElementUi的PC组件库

110 lines (87 loc) 3 kB
"use strict"; exports.__esModule = true; exports.default = void 0; var _ariaUtils = _interopRequireDefault(require("./aria-utils")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * @constructor * @desc Dialog object providing modal focus management. * * Assumptions: The element serving as the dialog container is present in the * DOM and hidden. The dialog container has role='dialog'. * * @param dialogId * The ID of the element serving as the dialog container. * @param focusAfterClosed * Either the DOM node or the ID of the DOM node to focus when the * dialog closes. * @param focusFirst * Optional parameter containing either the DOM node or the ID of the * DOM node to focus when the dialog opens. If not specified, the * first focusable element in the dialog will receive focus. */ // eslint-disable-next-line no-use-before-define var aria = aria || {}; var tabEvent; aria.Dialog = function (dialog, focusAfterClosed, focusFirst) { var _this = this; this.dialogNode = dialog; if (this.dialogNode === null || this.dialogNode.getAttribute('role') !== 'dialog') { throw new Error('Dialog() requires a DOM element with ARIA role of dialog.'); } if (typeof focusAfterClosed === 'string') { this.focusAfterClosed = document.getElementById(focusAfterClosed); } else if (typeof focusAfterClosed === 'object') { this.focusAfterClosed = focusAfterClosed; } else { this.focusAfterClosed = null; } if (typeof focusFirst === 'string') { this.focusFirst = document.getElementById(focusFirst); } else if (typeof focusFirst === 'object') { this.focusFirst = focusFirst; } else { this.focusFirst = null; } if (this.focusFirst) { this.focusFirst.focus(); } else { _ariaUtils.default.focusFirstDescendant(this.dialogNode); } this.lastFocus = document.activeElement; tabEvent = function tabEvent(e) { _this.trapFocus(e); }; this.addListeners(); }; aria.Dialog.prototype.addListeners = function () { document.addEventListener('focus', tabEvent, true); }; aria.Dialog.prototype.removeListeners = function () { document.removeEventListener('focus', tabEvent, true); }; aria.Dialog.prototype.closeDialog = function () { var _this2 = this; this.removeListeners(); if (this.focusAfterClosed) { setTimeout(function () { _this2.focusAfterClosed.focus(); }); } }; aria.Dialog.prototype.trapFocus = function (event) { if (_ariaUtils.default.IgnoreUtilFocusChanges) { return; } if (this.dialogNode.contains(event.target)) { this.lastFocus = event.target; } else { _ariaUtils.default.focusFirstDescendant(this.dialogNode); if (this.lastFocus === document.activeElement) { _ariaUtils.default.focusLastDescendant(this.dialogNode); } this.lastFocus = document.activeElement; } }; var _default = aria.Dialog; exports.default = _default;