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