Backed out 2 changesets (bug 1943998) for causing wd failures @ phases.py CLOSED...
[gecko.git] / devtools / client / accessibility / provider.js
blobf0fc05c628bb8a2787e91c7de2e169715694f1af
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 "use strict";
6 const {
7 fetchChildren,
8 } = require("resource://devtools/client/accessibility/actions/accessibles.js");
10 /**
11 * Data provider that is responsible for mapping of an accessibles cache to the
12 * data format that is supported by the TreeView component.
13 * @param {Map} accessibles accessibles object cache
14 * @param {Function} dispatch react dispatch function that triggers a redux
15 * action.
18 class Provider {
19 constructor(accessibles, filtered, dispatch) {
20 this.accessibles = accessibles;
21 this.filtered = filtered;
22 this.dispatch = dispatch;
25 /**
26 * Get accessible's cached children if available, if not fetch them from
27 * backend.
28 * @param {Object} accessible accessible object whose children to get.
29 * @returns {Array} arraof of accessible children.
31 getChildren(accessible) {
32 if (!accessible || !accessible.actorID || accessible.childCount === 0) {
33 return [];
36 const obj = this.accessibles.get(accessible.actorID);
37 if (!obj || !obj.children) {
38 return this.dispatch(fetchChildren(accessible));
41 return obj.children;
44 /**
45 * Return a flag indicating if an accessible object has any children.
46 * @param {Object} accessible accessible object whose children to get.
47 * @returns {Boolean} idicator of whether accessible object has children.
49 hasChildren(accessible) {
50 return accessible.childCount > 0;
53 /**
54 * Get a value for an accessible object. Used to render the second (value)
55 * column of the accessible tree. Corresponds to an accesible object name, if
56 * available.
57 * @param {Object} accessible accessible object
58 * @returns {String} accessible object value.
60 getValue(accessible) {
61 return accessible.name || "";
64 /**
65 * Get a label for an accessible object. Used to render the first column of
66 * the accessible tree. Corresponds to an accessible object role.
67 * @param {Object} accessible accessible object
68 * @returns {String} accessible object label.
70 getLabel(accessible) {
71 return accessible.role;
74 /**
75 * Get a unique key for an accessible object. Corresponds to an accessible
76 * front's actorID.
77 * @param {Object} accessible accessible object
78 * @returns {String} a key for an accessible object.
80 getKey(accessible) {
81 return accessible.actorID;
84 /**
85 * Get a type of an accesible object. Corresponds to the type of an accessible
86 * front.
87 * @param {Object} accessible accessible object
88 * @returns {String} accessible object type
90 getType(accessible) {
91 return accessible.typeName;
94 /**
95 * Get the depth of the accesible object in the accessibility tree. When the
96 * tree is filtered it is flattened and the level is set to 0. Otherwise use
97 * internal TreeView level.
99 * @param {Object} accessible
100 * accessible object
101 * @param {Number} defaultLevel
102 * default level provided by the TreeView component.
104 * @returns {null|Number}
105 * depth level of the accessible object.
107 getLevel(accessible, defaultLevel) {
108 return this.filtered ? 0 : defaultLevel;
112 exports.Provider = Provider;