grip-provider.js (2570B)
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 fetchProperties, 8 } = require("resource://devtools/client/dom/content/actions/grips.js"); 9 const { 10 Property, 11 } = require("resource://devtools/client/dom/content/reducers/grips.js"); 12 13 /** 14 * This object provides data for the tree displayed in the tooltip 15 * content. 16 */ 17 class GripProvider { 18 constructor(grips, dispatch) { 19 this.grips = grips; 20 this.dispatch = dispatch; 21 } 22 /** 23 * Fetches properties from the backend. These properties might be 24 * displayed as child objects in e.g. a tree UI widget. 25 */ 26 getChildren(object) { 27 let grip = object; 28 if (object instanceof Property) { 29 grip = this.getValue(object); 30 } 31 32 if (!grip || !grip.actorID) { 33 return []; 34 } 35 36 const props = this.grips.get(grip.actorID); 37 if (!props) { 38 // Fetch missing data from the backend. Returning a promise 39 // from data provider causes the tree to show a spinner. 40 return this.dispatch(fetchProperties(grip)); 41 } 42 43 return props; 44 } 45 46 hasChildren(object) { 47 if (object instanceof Property) { 48 const value = this.getValue(object); 49 if (!value) { 50 return false; 51 } 52 const grip = value?.getGrip ? value.getGrip() : value; 53 54 let hasChildren = grip.ownPropertyLength > 0; 55 56 if (grip.preview) { 57 hasChildren = hasChildren || grip.preview.ownPropertiesLength > 0; 58 } 59 60 if (grip.preview) { 61 const preview = grip.preview; 62 const k = preview.kind; 63 const objectsWithProps = ["DOMNode", "ObjectWithURL"]; 64 hasChildren = hasChildren || objectsWithProps.includes(k); 65 hasChildren = hasChildren || (k == "ArrayLike" && !!preview.length); 66 } 67 68 return grip.type == "object" && hasChildren; 69 } 70 71 return null; 72 } 73 74 getValue(object) { 75 if (object instanceof Property) { 76 const value = object.value; 77 return typeof value.value != "undefined" 78 ? value.value 79 : value.getterValue; 80 } 81 82 return object; 83 } 84 85 getLabel(object) { 86 return object instanceof Property ? object.name : null; 87 } 88 89 getKey(object) { 90 return object instanceof Property ? object.key : null; 91 } 92 93 getType(object) { 94 const grip = object?.getGrip ? object.getGrip() : object; 95 return grip.class ? grip.class : ""; 96 } 97 } 98 99 // Exports from this module 100 exports.GripProvider = GripProvider;