Skip to content

Commit

Permalink
Merge pull request #35 from maximkou/patch-9
Browse files Browse the repository at this point in the history
Fix: When pressing tab focus should not go out of modal
  • Loading branch information
samdark committed Mar 5, 2014
2 parents 14efbd1 + b87e483 commit c552150
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions jquery.the-modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,40 @@
"use strict";
/*jshint smarttabs:true*/

// :focusable expression, needed for tabindexes in modal
$.extend($.expr[':'],{
focusable: function(element){
var map, mapName, img,
nodeName = element.nodeName.toLowerCase(),
isTabIndexNotNaN = !isNaN($.attr(element,'tabindex'));
if ('area' === nodeName) {
map = element.parentNode;
mapName = map.name;
if (!element.href || !mapName || map.nodeName.toLowerCase() !== 'map') {
return false;
}
img = $('img[usemap=#' + mapName + ']')[0];
return !!img && visible(img);
}

var result = isTabIndexNotNaN;
if (/input|select|textarea|button|object/.test(nodeName)) {
result = !element.disabled;
} else if ('a' === nodeName) {
result = element.href || isTabIndexNotNaN;
}

return result && visible(element);

function visible(element) {
return $.expr.filters.visible(element) &&
!$(element).parents().addBack().filter(function() {
return $.css(this,'visibility') === 'hidden';
}).length;
}
}
});

var pluginNamespace = 'the-modal',
// global defaults
defaults = {
Expand Down Expand Up @@ -86,6 +120,14 @@
return true;
});

els.bind('keydown',function(e){
var modalFocusableElements = $(':focusable',$(this));
if(modalFocusableElements.filter(':last').is(':focus') && (e.which || e.keyCode) == 9){
e.preventDefault();
modalFocusableElements.filter(':first').focus();
}
});

return {
open: function(options) {
var el = els.get(0);
Expand Down

0 comments on commit c552150

Please sign in to comment.