Fixed #77 - Modal dialog not blocking background
parent
50713d50b0
commit
98f9ab2579
|
@ -50,6 +50,10 @@ export default {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
mask: null,
|
mask: null,
|
||||||
|
documentKeydownListener: null,
|
||||||
|
destroyed() {
|
||||||
|
//this.unbindDocumentKeydownListener();
|
||||||
|
},
|
||||||
methods: {
|
methods: {
|
||||||
close() {
|
close() {
|
||||||
this.$emit('update:visible', false);
|
this.$emit('update:visible', false);
|
||||||
|
@ -85,6 +89,7 @@ export default {
|
||||||
DomHandler.addMultipleClasses(this.mask, 'p-component-overlay p-dialog-mask p-fadein');
|
DomHandler.addMultipleClasses(this.mask, 'p-component-overlay p-dialog-mask p-fadein');
|
||||||
document.body.appendChild(this.mask);
|
document.body.appendChild(this.mask);
|
||||||
DomHandler.addClass(document.body, 'p-overflow-hidden');
|
DomHandler.addClass(document.body, 'p-overflow-hidden');
|
||||||
|
this.bindDocumentKeydownListener();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
disableModality() {
|
disableModality() {
|
||||||
|
@ -92,6 +97,48 @@ export default {
|
||||||
document.body.removeChild(this.mask);
|
document.body.removeChild(this.mask);
|
||||||
DomHandler.removeClass(document.body, 'p-overflow-hidden');
|
DomHandler.removeClass(document.body, 'p-overflow-hidden');
|
||||||
this.mask = null;
|
this.mask = null;
|
||||||
|
this.unbindDocumentKeydownListener();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
onKeyDown(event) {
|
||||||
|
if (event.which === 9) {
|
||||||
|
event.preventDefault();
|
||||||
|
|
||||||
|
let focusableElements = DomHandler.getFocusableElements(this.$refs.container);
|
||||||
|
|
||||||
|
if (focusableElements && focusableElements.length > 0) {
|
||||||
|
if (!document.activeElement) {
|
||||||
|
focusableElements[0].focus();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
let focusedIndex = focusableElements.indexOf(document.activeElement);
|
||||||
|
|
||||||
|
if (event.shiftKey) {
|
||||||
|
if (focusedIndex == -1 || focusedIndex === 0)
|
||||||
|
focusableElements[focusableElements.length - 1].focus();
|
||||||
|
else
|
||||||
|
focusableElements[focusedIndex - 1].focus();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (focusedIndex == -1 || focusedIndex === (focusableElements.length - 1))
|
||||||
|
focusableElements[0].focus();
|
||||||
|
else
|
||||||
|
focusableElements[focusedIndex + 1].focus();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
bindDocumentKeydownListener() {
|
||||||
|
if (!this.documentKeydownListener) {
|
||||||
|
this.documentKeydownListener = this.onKeyDown.bind(this);
|
||||||
|
window.document.addEventListener('keydown', this.documentKeydownListener);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
unbindDocumentKeydownListener() {
|
||||||
|
if (this.documentKeydownListener) {
|
||||||
|
window.document.removeEventListener('keydown', this.documentKeydownListener);
|
||||||
|
this.documentKeydownListener = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
@ -384,4 +384,21 @@ export default class DomHandler {
|
||||||
static invokeElementMethod(element, methodName, args) {
|
static invokeElementMethod(element, methodName, args) {
|
||||||
(element)[methodName].apply(element, args);
|
(element)[methodName].apply(element, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static getFocusableElements(element) {
|
||||||
|
let focusableElements = DomHandler.find(element, `button:not([tabindex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden]),
|
||||||
|
[href][clientHeight][clientWidth]:not([tabindex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden]),
|
||||||
|
input:not([tabindex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden]), select:not([tabindex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden]),
|
||||||
|
textarea:not([tabindex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden]), [tabIndex]:not([tabIndex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden]),
|
||||||
|
[contenteditable]:not([tabIndex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden])`
|
||||||
|
);
|
||||||
|
|
||||||
|
let visibleFocusableElements = [];
|
||||||
|
for (let focusableElement of focusableElements) {
|
||||||
|
if (getComputedStyle(focusableElement).display != "none" && getComputedStyle(focusableElement).visibility != "hidden")
|
||||||
|
visibleFocusableElements.push(focusableElement);
|
||||||
|
}
|
||||||
|
|
||||||
|
return visibleFocusableElements;
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue