ObjectTreeView.js (1764B)
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 5 "use strict"; 6 7 const { 8 createFactory, 9 PureComponent, 10 } = require("resource://devtools/client/shared/vendor/react.mjs"); 11 const PropTypes = require("resource://devtools/client/shared/vendor/react-prop-types.mjs"); 12 13 const { REPS, MODE } = ChromeUtils.importESModule( 14 "resource://devtools/client/shared/components/reps/index.mjs" 15 ); 16 const { Rep } = REPS; 17 const TreeViewClass = ChromeUtils.importESModule( 18 "resource://devtools/client/shared/components/tree/TreeView.mjs" 19 ).default; 20 const TreeView = createFactory(TreeViewClass); 21 22 /** 23 * The ExpressionResultView React Component is used in the ExtensionSidebar component to 24 * provide a UI viewMode which shows a tree view of the passed JavaScript object. 25 */ 26 class ExpressionResultView extends PureComponent { 27 static get propTypes() { 28 return { 29 object: PropTypes.object.isRequired, 30 }; 31 } 32 33 render() { 34 const { object } = this.props; 35 36 const columns = [ 37 { 38 id: "value", 39 }, 40 ]; 41 42 // Render the node value (omitted on the root element if it has children). 43 const renderValue = props => { 44 if (props.member.level === 0 && props.member.hasChildren) { 45 return undefined; 46 } 47 48 return Rep( 49 Object.assign({}, props, { 50 cropLimit: 50, 51 }) 52 ); 53 }; 54 55 return TreeView({ 56 object, 57 mode: MODE.SHORT, 58 columns, 59 renderValue, 60 expandedNodes: TreeViewClass.getExpandedNodes(object, { 61 maxLevel: 1, 62 maxNodes: 1, 63 }), 64 }); 65 } 66 } 67 68 module.exports = ExpressionResultView;