Upgrade to ExtJS 3.0.0 - Released 07/06/2009
[extjs.git] / examples / tree / ARIA.js
diff --git a/examples/tree/ARIA.js b/examples/tree/ARIA.js
new file mode 100644 (file)
index 0000000..8aee9dc
--- /dev/null
@@ -0,0 +1,110 @@
+/*!
+ * Ext JS Library 3.0.0
+ * Copyright(c) 2006-2009 Ext JS, LLC
+ * licensing@extjs.com
+ * http://www.extjs.com/license
+ */
+(function() {\r
+\r
+Ext.a11y.ARIA = Ext.apply(new Ext.util.Observable(), function() {\r
+    return {\r
+        setRole : function(el, role) {\r
+            el = Ext.getDom(el);\r
+            if(el) {\r
+                el.setAttribute('role', role.toString());\r
+            }\r
+        },\r
+        \r
+        setProperty : function(el, key, value) {\r
+            el = Ext.getDom(el);\r
+            if(el) {\r
+                el.setAttribute(key.toString(), value.toString());\r
+            }                \r
+        }\r
+    }\r
+}());\r
+\r
+var ARIA = Ext.a11y.ARIA;\r
+\r
+Ext.override(Ext.tree.TreeNode, {\r
+    render : function(bulkRender){\r
+        this.ui.render(bulkRender);\r
+        if(!this.rendered){\r
+            // make sure it is registered\r
+            this.getOwnerTree().registerNode(this);\r
+            this.rendered = true;\r
+            this.fireEvent('noderender', this);\r
+            if(this.expanded){\r
+                this.expanded = false;\r
+                this.expand(false, false);\r
+            }\r
+        }\r
+    }\r
+});\r
+\r
+Ext.override(Ext.tree.TreePanel, {\r
+    initARIA : function() {\r
+        Ext.tree.TreePanel.superclass.initARIA.call(this);\r
+        this.getSelectionModel().on('selectionchange', this.onNodeSelect, this);\r
+        this.ariaTreeEl = this.body.down('.x-tree-root-ct');\r
+        this.on('collapsenode', this.onNodeCollapse, this);\r
+        this.on('expandnode', this.onNodeExpand, this);\r
+    },\r
+    \r
+    // private\r
+    registerNode : function(node){\r
+        if(this.nodeHash[node.id] === undefined) {\r
+            node.on('noderender', this.onNodeRender, this);\r
+        }\r
+        this.nodeHash[node.id] = node;\r
+    },\r
+\r
+    // private\r
+    unregisterNode : function(node){\r
+        node.un('noderender', this.onNodeRender, this);\r
+        delete this.nodeHash[node.id];\r
+    },\r
+    \r
+    onNodeRender : function(node) {\r
+        var a = node.ui.anchor,\r
+            level = this.rootVisible ? 1 : 0,\r
+            pnode = node;\r
+                                \r
+        if(node.isRoot) {\r
+            ARIA.setRole(this.ariaTreeEl, 'tree');\r
+            ARIA.setProperty(this.ariaTreeEl, 'aria-labelledby', Ext.id(node.ui.textNode));\r
+            ARIA.setProperty(this.ariaTreeEl, 'aria-activedescendant', 'false');\r
+            if(!this.rootVisible) {\r
+                return;\r
+            }\r
+        }\r
+        ARIA.setRole(node.ui.wrap, 'treeitem');\r
+        ARIA.setProperty(node.ui.wrap, 'aria-labelledby', Ext.id(node.ui.textNode));            \r
+        ARIA.setProperty(node.ui.wrap, 'aria-expanded', 'false');\r
+        ARIA.setProperty(node.ui.wrap, 'aria-selected', 'false');\r
+        while (pnode.parentNode) {\r
+            level++;\r
+            pnode = pnode.parentNode;\r
+        }\r
+        ARIA.setProperty(node.ui.wrap, 'aria-level', level);   \r
+        if(!node.isLeaf()) {\r
+            ARIA.setRole(node.ui.ctNode, 'group');\r
+            ARIA.setProperty(node.ui.wrap, 'aria-expanded', node.isExpanded());\r
+        }\r
+    },\r
+    \r
+    onNodeSelect : function(sm, node) {\r
+        ARIA.setProperty(this.ariaTreeEl, 'aria-activedescendant', Ext.id(node.ui.wrap));\r
+        ARIA.setProperty(node.ui.wrap, 'aria-selected', 'true');\r
+    },\r
+    \r
+    onNodeCollapse : function(node) {\r
+        ARIA.setProperty(node.ui.wrap, 'aria-expanded', 'false');\r
+    },\r
+    \r
+    onNodeExpand : function(node) {\r
+        ARIA.setProperty(node.ui.wrap, 'aria-expanded', 'true');\r
+    }\r
+});\r
+     \r
+})();
\ No newline at end of file