wai.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. define("dijit/_base/wai", [
  2. "dojo/dom-attr", // domAttr.attr
  3. "dojo/_base/lang", // lang.mixin
  4. "..", // export symbols to dijit
  5. "../hccss" // not using this module directly, but loading it sets CSS flag on <html>
  6. ], function(domAttr, lang, dijit){
  7. // module:
  8. // dijit/_base/wai
  9. // summary:
  10. // Deprecated methods for setting/getting wai roles and states.
  11. // New code should call setAttribute()/getAttribute() directly.
  12. //
  13. // Also loads hccss to apply dijit_a11y class to root node if machine is in high-contrast mode.
  14. lang.mixin(dijit, {
  15. hasWaiRole: function(/*Element*/ elem, /*String?*/ role){
  16. // summary:
  17. // Determines if an element has a particular role.
  18. // returns:
  19. // True if elem has the specific role attribute and false if not.
  20. // For backwards compatibility if role parameter not provided,
  21. // returns true if has a role
  22. var waiRole = this.getWaiRole(elem);
  23. return role ? (waiRole.indexOf(role) > -1) : (waiRole.length > 0);
  24. },
  25. getWaiRole: function(/*Element*/ elem){
  26. // summary:
  27. // Gets the role for an element (which should be a wai role).
  28. // returns:
  29. // The role of elem or an empty string if elem
  30. // does not have a role.
  31. return lang.trim((domAttr.get(elem, "role") || "").replace("wairole:",""));
  32. },
  33. setWaiRole: function(/*Element*/ elem, /*String*/ role){
  34. // summary:
  35. // Sets the role on an element.
  36. // description:
  37. // Replace existing role attribute with new role.
  38. domAttr.set(elem, "role", role);
  39. },
  40. removeWaiRole: function(/*Element*/ elem, /*String*/ role){
  41. // summary:
  42. // Removes the specified role from an element.
  43. // Removes role attribute if no specific role provided (for backwards compat.)
  44. var roleValue = domAttr.get(elem, "role");
  45. if(!roleValue){ return; }
  46. if(role){
  47. var t = lang.trim((" " + roleValue + " ").replace(" " + role + " ", " "));
  48. domAttr.set(elem, "role", t);
  49. }else{
  50. elem.removeAttribute("role");
  51. }
  52. },
  53. hasWaiState: function(/*Element*/ elem, /*String*/ state){
  54. // summary:
  55. // Determines if an element has a given state.
  56. // description:
  57. // Checks for an attribute called "aria-"+state.
  58. // returns:
  59. // true if elem has a value for the given state and
  60. // false if it does not.
  61. return elem.hasAttribute ? elem.hasAttribute("aria-"+state) : !!elem.getAttribute("aria-"+state);
  62. },
  63. getWaiState: function(/*Element*/ elem, /*String*/ state){
  64. // summary:
  65. // Gets the value of a state on an element.
  66. // description:
  67. // Checks for an attribute called "aria-"+state.
  68. // returns:
  69. // The value of the requested state on elem
  70. // or an empty string if elem has no value for state.
  71. return elem.getAttribute("aria-"+state) || "";
  72. },
  73. setWaiState: function(/*Element*/ elem, /*String*/ state, /*String*/ value){
  74. // summary:
  75. // Sets a state on an element.
  76. // description:
  77. // Sets an attribute called "aria-"+state.
  78. elem.setAttribute("aria-"+state, value);
  79. },
  80. removeWaiState: function(/*Element*/ elem, /*String*/ state){
  81. // summary:
  82. // Removes a state from an element.
  83. // description:
  84. // Sets an attribute called "aria-"+state.
  85. elem.removeAttribute("aria-"+state);
  86. }
  87. });
  88. return dijit;
  89. });