provider.js (3502B)
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"; 5 6 const { 7 fetchChildren, 8 } = require("resource://devtools/client/accessibility/actions/accessibles.js"); 9 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 * 14 * @param {Map} accessibles accessibles object cache 15 * @param {Function} dispatch react dispatch function that triggers a redux 16 * action. 17 */ 18 19 class Provider { 20 constructor(accessibles, filtered, dispatch) { 21 this.accessibles = accessibles; 22 this.filtered = filtered; 23 this.dispatch = dispatch; 24 } 25 26 /** 27 * Get accessible's cached children if available, if not fetch them from 28 * backend. 29 * 30 * @param {object} accessible accessible object whose children to get. 31 * @returns {Array} arraof of accessible children. 32 */ 33 getChildren(accessible) { 34 if (!accessible || !accessible.actorID || accessible.childCount === 0) { 35 return []; 36 } 37 38 const obj = this.accessibles.get(accessible.actorID); 39 if (!obj || !obj.children) { 40 return this.dispatch(fetchChildren(accessible)); 41 } 42 43 return obj.children; 44 } 45 46 /** 47 * Return a flag indicating if an accessible object has any children. 48 * 49 * @param {object} accessible accessible object whose children to get. 50 * @returns {boolean} idicator of whether accessible object has children. 51 */ 52 hasChildren(accessible) { 53 return accessible.childCount > 0; 54 } 55 56 /** 57 * Get a value for an accessible object. Used to render the second (value) 58 * column of the accessible tree. Corresponds to an accesible object name, if 59 * available. 60 * 61 * @param {object} accessible accessible object 62 * @returns {string} accessible object value. 63 */ 64 getValue(accessible) { 65 return accessible.name || ""; 66 } 67 68 /** 69 * Get a label for an accessible object. Used to render the first column of 70 * the accessible tree. Corresponds to an accessible object role. 71 * 72 * @param {object} accessible accessible object 73 * @returns {string} accessible object label. 74 */ 75 getLabel(accessible) { 76 return accessible.role; 77 } 78 79 /** 80 * Get a unique key for an accessible object. Corresponds to an accessible 81 * front's actorID. 82 * 83 * @param {object} accessible accessible object 84 * @returns {string} a key for an accessible object. 85 */ 86 getKey(accessible) { 87 return accessible.actorID; 88 } 89 90 /** 91 * Get a type of an accesible object. Corresponds to the type of an accessible 92 * front. 93 * 94 * @param {object} accessible accessible object 95 * @returns {string} accessible object type 96 */ 97 getType(accessible) { 98 return accessible.typeName; 99 } 100 101 /** 102 * Get the depth of the accesible object in the accessibility tree. When the 103 * tree is filtered it is flattened and the level is set to 0. Otherwise use 104 * internal TreeView level. 105 * 106 * @param {object} accessible 107 * accessible object 108 * @param {number} defaultLevel 109 * default level provided by the TreeView component. 110 * 111 * @returns {null | number} 112 * depth level of the accessible object. 113 */ 114 getLevel(accessible, defaultLevel) { 115 return this.filtered ? 0 : defaultLevel; 116 } 117 } 118 119 exports.Provider = Provider;