focusable.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*!
  2. * jQuery UI Focusable 1.14.1
  3. * https://jqueryui.com
  4. *
  5. * Copyright OpenJS Foundation and other contributors
  6. * Released under the MIT license.
  7. * https://jquery.org/license
  8. */
  9. //>>label: :focusable Selector
  10. //>>group: Core
  11. //>>description: Selects elements which can be focused.
  12. //>>docs: https://api.jqueryui.com/focusable-selector/
  13. ( function( factory ) {
  14. "use strict";
  15. if ( typeof define === "function" && define.amd ) {
  16. // AMD. Register as an anonymous module.
  17. define( [ "jquery", "./version" ], factory );
  18. } else {
  19. // Browser globals
  20. factory( jQuery );
  21. }
  22. } )( function( $ ) {
  23. "use strict";
  24. // Selectors
  25. $.ui.focusable = function( element, hasTabindex ) {
  26. var map, mapName, img, focusableIfVisible, fieldset,
  27. nodeName = element.nodeName.toLowerCase();
  28. if ( "area" === nodeName ) {
  29. map = element.parentNode;
  30. mapName = map.name;
  31. if ( !element.href || !mapName || map.nodeName.toLowerCase() !== "map" ) {
  32. return false;
  33. }
  34. img = $( "img[usemap='#" + mapName + "']" );
  35. return img.length > 0 && img.is( ":visible" );
  36. }
  37. if ( /^(input|select|textarea|button|object)$/.test( nodeName ) ) {
  38. focusableIfVisible = !element.disabled;
  39. if ( focusableIfVisible ) {
  40. // Form controls within a disabled fieldset are disabled.
  41. // However, controls within the fieldset's legend do not get disabled.
  42. // Since controls generally aren't placed inside legends, we skip
  43. // this portion of the check.
  44. fieldset = $( element ).closest( "fieldset" )[ 0 ];
  45. if ( fieldset ) {
  46. focusableIfVisible = !fieldset.disabled;
  47. }
  48. }
  49. } else if ( "a" === nodeName ) {
  50. focusableIfVisible = element.href || hasTabindex;
  51. } else {
  52. focusableIfVisible = hasTabindex;
  53. }
  54. return focusableIfVisible && $( element ).is( ":visible" ) &&
  55. $( element ).css( "visibility" ) === "visible";
  56. };
  57. $.extend( $.expr.pseudos, {
  58. focusable: function( element ) {
  59. return $.ui.focusable( element, $.attr( element, "tabindex" ) != null );
  60. }
  61. } );
  62. return $.ui.focusable;
  63. } );