wai.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. if (!dojo._hasResource["dijit._base.wai"]) { // _hasResource checks added by
  2. // build. Do not use
  3. // _hasResource directly in your
  4. // code.
  5. dojo._hasResource["dijit._base.wai"] = true;
  6. dojo.provide("dijit._base.wai");
  7. dijit.wai = {
  8. onload : function() {
  9. // summary:
  10. // Function that detects if we are in high-contrast mode or not,
  11. // and sets up a timer to periodically confirm the value.
  12. // figure out the background-image style property
  13. // and apply that to the image.src property.
  14. // description:
  15. // This must be a named function and not an anonymous
  16. // function, so that the widget parsing code can make sure it
  17. // registers its onload function after this function.
  18. // DO NOT USE "this" within this function.
  19. // create div for testing if high contrast mode is on or images are
  20. // turned off
  21. var div = document.createElement("div");
  22. div.id = "a11yTestNode";
  23. div.style.cssText = 'border: 1px solid;'
  24. + 'border-color:red green;' + 'position: absolute;'
  25. + 'height: 5px;' + 'top: -999px;'
  26. + 'background-image: url("'
  27. + dojo.moduleUrl("dijit", "form/templates/blank.gif")
  28. + '");';
  29. dojo.body().appendChild(div);
  30. // test it
  31. function check() {
  32. var cs = dojo.getComputedStyle(div);
  33. if (cs) {
  34. var bkImg = cs.backgroundImage;
  35. var needsA11y = (cs.borderTopColor == cs.borderRightColor)
  36. || (bkImg != null && (bkImg == "none" || bkImg == "url(invalid-url:)"));
  37. dojo[needsA11y ? "addClass" : "removeClass"](dojo.body(),
  38. "dijit_a11y");
  39. }
  40. }
  41. check();
  42. if (dojo.isIE) {
  43. setInterval(check, 4000);
  44. }
  45. }
  46. };
  47. // Test if computer is in high contrast mode.
  48. // Make sure the a11y test runs first, before widgets are instantiated.
  49. if (dojo.isIE || dojo.isMoz) { // NOTE: checking in Safari messes things up
  50. dojo._loaders.unshift(dijit.wai.onload);
  51. }
  52. dojo.mixin(dijit, {
  53. hasWaiRole : function(/* Element */elem) {
  54. // Summary: Return true if elem has a role attribute and false if
  55. // not.
  56. if (elem.hasAttribute) {
  57. return elem.hasAttribute("role");
  58. } else {
  59. return elem.getAttribute("role") ? true : false;
  60. }
  61. },
  62. getWaiRole : function(/* Element */elem) {
  63. // Summary: Return the role of elem or an empty string if
  64. // elem does not have a role.
  65. var value = elem.getAttribute("role");
  66. if (value) {
  67. var prefixEnd = value.indexOf(":");
  68. return prefixEnd == -1 ? value : value.substring(prefixEnd + 1);
  69. } else {
  70. return "";
  71. }
  72. },
  73. setWaiRole : function(/* Element */elem, /* String */role) {
  74. // Summary: Set the role on elem. On Firefox 2 and below, "wairole:"
  75. // is
  76. // prepended to the provided role value.
  77. if (dojo.isFF && dojo.isFF < 3) {
  78. elem.setAttribute("role", "wairole:" + role);
  79. } else {
  80. elem.setAttribute("role", role);
  81. }
  82. },
  83. removeWaiRole : function(/* Element */elem) {
  84. // Summary: Removes the role attribute from elem.
  85. elem.removeAttribute("role");
  86. },
  87. hasWaiState : function(/* Element */elem, /* String */state) {
  88. // Summary: Return true if elem has a value for the given state and
  89. // false if it does not.
  90. // On Firefox 2 and below, we check for an attribute in namespace
  91. // "http://www.w3.org/2005/07/aaa" with a name of the given state.
  92. // On all other browsers, we check for an attribute called
  93. // "aria-"+state.
  94. if (dojo.isFF && dojo.isFF < 3) {
  95. return elem.hasAttributeNS("http://www.w3.org/2005/07/aaa",
  96. state);
  97. } else {
  98. if (elem.hasAttribute) {
  99. return elem.hasAttribute("aria-" + state);
  100. } else {
  101. return elem.getAttribute("aria-" + state) ? true : false;
  102. }
  103. }
  104. },
  105. getWaiState : function(/* Element */elem, /* String */state) {
  106. // Summary: Return the value of the requested state on elem
  107. // or an empty string if elem has no value for state.
  108. // On Firefox 2 and below, we check for an attribute in namespace
  109. // "http://www.w3.org/2005/07/aaa" with a name of the given state.
  110. // On all other browsers, we check for an attribute called
  111. // "aria-"+state.
  112. if (dojo.isFF && dojo.isFF < 3) {
  113. return elem.getAttributeNS("http://www.w3.org/2005/07/aaa",
  114. state);
  115. } else {
  116. var value = elem.getAttribute("aria-" + state);
  117. return value ? value : "";
  118. }
  119. },
  120. setWaiState : function(/* Element */elem, /* String */state, /* String */
  121. value) {
  122. // Summary: Set state on elem to value.
  123. // On Firefox 2 and below, we set an attribute in namespace
  124. // "http://www.w3.org/2005/07/aaa" with a name of the given state.
  125. // On all other browsers, we set an attribute called
  126. // "aria-"+state.
  127. if (dojo.isFF && dojo.isFF < 3) {
  128. elem.setAttributeNS("http://www.w3.org/2005/07/aaa", "aaa:"
  129. + state, value);
  130. } else {
  131. elem.setAttribute("aria-" + state, value);
  132. }
  133. },
  134. removeWaiState : function(/* Element */elem, /* String */state) {
  135. // Summary: Removes the given state from elem.
  136. // On Firefox 2 and below, we remove the attribute in namespace
  137. // "http://www.w3.org/2005/07/aaa" with a name of the given state.
  138. // On all other browsers, we remove the attribute called
  139. // "aria-"+state.
  140. if (dojo.isFF && dojo.isFF < 3) {
  141. elem.removeAttributeNS("http://www.w3.org/2005/07/aaa", state);
  142. } else {
  143. elem.removeAttribute("aria-" + state);
  144. }
  145. }
  146. });
  147. }