Source: jquery.focusable.js

  1. /**
  2. * @file Gets all focusable descendant elements for the first element in the set of matched elements
  3. * @author Ian McBurnie <ianmcburnie@hotmail.com>
  4. * @version 1.0.1
  5. * @requires jquery
  6. */
  7. (function($, window, document, undefined) { // eslint-disable-line no-unused-vars
  8. var pluginName = 'jquery-focusable'; // eslint-disable-line no-unused-vars
  9. var focusableElementsList = [
  10. 'a[href]',
  11. 'button:not([disabled])',
  12. 'area[href]',
  13. 'input:not([disabled])',
  14. 'select:not([disabled])',
  15. 'textarea:not([disabled])',
  16. 'iframe',
  17. 'object',
  18. 'embed',
  19. '*[tabindex]',
  20. '*[contenteditable]'
  21. ];
  22. var focusableElementsSelector = focusableElementsList.join();
  23. var defaults = {
  24. findNegativeTabindex: true,
  25. findPositiveTabindex: true
  26. };
  27. /**
  28. * jQuery collection plugin that implements aria-activedescendant keyboard navigation on given widgets
  29. *
  30. * @method "jQuery.fn.focusable"
  31. * @param {Object} options options
  32. * @param {boolean} options.findNegativeTabindex - will return elements with tabindex equal to -1 by default
  33. * @param {boolean} options.findPositiveTabindex - will return elements with tabindex greater than 0 by default
  34. * @return {jQuery} chainable jQuery class
  35. */
  36. $.fn.focusable = function focusable(options) {
  37. var opts = $.extend({}, defaults, options);
  38. return $(this).first().find(focusableElementsSelector)
  39. .filter(function() {
  40. // if findNegativeTabindex option is truthy, immediately return the element.
  41. // otherwise only return the element if tabindex is not negative
  42. return opts.findNegativeTabindex || ($(this).attr('tabindex') !== '-1');
  43. })
  44. .filter(function() {
  45. // if findPositiveTabindex option is truthy, immediately return the element.
  46. // otherwise only return the element if tabindex is not positive
  47. return opts.findPositiveTabindex || ($(this).attr('tabindex') > 0 === false);
  48. });
  49. };
  50. }(jQuery, window, document));
  51. /**
  52. * The jQuery plugin namespace.
  53. * @external "jQuery.fn"
  54. * @see {@link http://learn.jquery.com/plugins/|jQuery Plugins}
  55. */