123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- "use strict";
-
- exports.__esModule = true;
- exports.default = void 0;
-
- var _class = _interopRequireDefault(require("dom-helpers/class"));
-
- var _style = _interopRequireDefault(require("dom-helpers/style"));
-
- var _scrollbarSize = _interopRequireDefault(require("dom-helpers/util/scrollbarSize"));
-
- var _isOverflowing = _interopRequireDefault(require("./utils/isOverflowing"));
-
- var _manageAriaHidden = require("./utils/manageAriaHidden");
-
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
-
- function findIndexOf(arr, cb) {
- var idx = -1;
- arr.some(function (d, i) {
- if (cb(d, i)) {
- idx = i;
- return true;
- }
- });
- return idx;
- }
- /**
- * Proper state managment for containers and the modals in those containers.
- *
- * @internal Used by the Modal to ensure proper styling of containers.
- */
-
-
- var ModalManager =
- /*#__PURE__*/
- function () {
- function ModalManager(_temp) {
- var _ref = _temp === void 0 ? {} : _temp,
- _ref$hideSiblingNodes = _ref.hideSiblingNodes,
- hideSiblingNodes = _ref$hideSiblingNodes === void 0 ? true : _ref$hideSiblingNodes,
- _ref$handleContainerO = _ref.handleContainerOverflow,
- handleContainerOverflow = _ref$handleContainerO === void 0 ? true : _ref$handleContainerO;
-
- this.hideSiblingNodes = hideSiblingNodes;
- this.handleContainerOverflow = handleContainerOverflow;
- this.modals = [];
- this.containers = [];
- this.data = [];
- this.scrollbarSize = (0, _scrollbarSize.default)();
- }
-
- var _proto = ModalManager.prototype;
-
- _proto.isContainerOverflowing = function isContainerOverflowing(modal) {
- var data = this.data[this.containerIndexFromModal(modal)];
- return data && data.overflowing;
- };
-
- _proto.containerIndexFromModal = function containerIndexFromModal(modal) {
- return findIndexOf(this.data, function (d) {
- return d.modals.indexOf(modal) !== -1;
- });
- };
-
- _proto.setContainerStyle = function setContainerStyle(containerState, container) {
- var style = {
- overflow: 'hidden' // we are only interested in the actual `style` here
- // becasue we will override it
-
- };
- containerState.style = {
- overflow: container.style.overflow,
- paddingRight: container.style.paddingRight
- };
-
- if (containerState.overflowing) {
- // use computed style, here to get the real padding
- // to add our scrollbar width
- style.paddingRight = parseInt((0, _style.default)(container, 'paddingRight') || 0, 10) + this.scrollbarSize + "px";
- }
-
- (0, _style.default)(container, style);
- };
-
- _proto.removeContainerStyle = function removeContainerStyle(containerState, container) {
- var style = containerState.style;
- Object.keys(style).forEach(function (key) {
- container.style[key] = style[key];
- });
- };
-
- _proto.add = function add(modal, container, className) {
- var modalIdx = this.modals.indexOf(modal);
- var containerIdx = this.containers.indexOf(container);
-
- if (modalIdx !== -1) {
- return modalIdx;
- }
-
- modalIdx = this.modals.length;
- this.modals.push(modal);
-
- if (this.hideSiblingNodes) {
- (0, _manageAriaHidden.hideSiblings)(container, modal);
- }
-
- if (containerIdx !== -1) {
- this.data[containerIdx].modals.push(modal);
- return modalIdx;
- }
-
- var data = {
- modals: [modal],
- //right now only the first modal of a container will have its classes applied
- classes: className ? className.split(/\s+/) : [],
- overflowing: (0, _isOverflowing.default)(container)
- };
-
- if (this.handleContainerOverflow) {
- this.setContainerStyle(data, container);
- }
-
- data.classes.forEach(_class.default.addClass.bind(null, container));
- this.containers.push(container);
- this.data.push(data);
- return modalIdx;
- };
-
- _proto.remove = function remove(modal) {
- var modalIdx = this.modals.indexOf(modal);
-
- if (modalIdx === -1) {
- return;
- }
-
- var containerIdx = this.containerIndexFromModal(modal);
- var data = this.data[containerIdx];
- var container = this.containers[containerIdx];
- data.modals.splice(data.modals.indexOf(modal), 1);
- this.modals.splice(modalIdx, 1); // if that was the last modal in a container,
- // clean up the container
-
- if (data.modals.length === 0) {
- data.classes.forEach(_class.default.removeClass.bind(null, container));
-
- if (this.handleContainerOverflow) {
- this.removeContainerStyle(data, container);
- }
-
- if (this.hideSiblingNodes) {
- (0, _manageAriaHidden.showSiblings)(container, modal);
- }
-
- this.containers.splice(containerIdx, 1);
- this.data.splice(containerIdx, 1);
- } else if (this.hideSiblingNodes) {
- //otherwise make sure the next top modal is visible to a SR
- var _data$modals = data.modals[data.modals.length - 1],
- backdrop = _data$modals.backdrop,
- dialog = _data$modals.dialog;
- (0, _manageAriaHidden.ariaHidden)(false, dialog);
- (0, _manageAriaHidden.ariaHidden)(false, backdrop);
- }
- };
-
- _proto.isTopModal = function isTopModal(modal) {
- return !!this.modals.length && this.modals[this.modals.length - 1] === modal;
- };
-
- return ModalManager;
- }();
-
- var _default = ModalManager;
- exports.default = _default;
- module.exports = exports.default;
|