2016-02-10 00:38:20 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
"use strict";
|
|
|
|
|
2016-03-21 15:29:17 +03:00
|
|
|
// Make this available to both AMD and CJS environments
|
2018-03-12 21:24:38 +03:00
|
|
|
define(function(require, exports, module) {
|
2019-04-04 17:12:37 +03:00
|
|
|
const {
|
|
|
|
Component,
|
|
|
|
createFactory,
|
|
|
|
createRef,
|
|
|
|
} = require("devtools/client/shared/vendor/react");
|
2017-11-03 16:34:41 +03:00
|
|
|
const PropTypes = require("devtools/client/shared/vendor/react-prop-types");
|
|
|
|
const dom = require("devtools/client/shared/vendor/react-dom-factories");
|
2017-10-27 17:33:10 +03:00
|
|
|
const { findDOMNode } = require("devtools/client/shared/vendor/react-dom");
|
2017-11-03 16:34:41 +03:00
|
|
|
const { tr } = dom;
|
2016-03-21 15:29:17 +03:00
|
|
|
|
|
|
|
// Tree
|
2019-12-27 15:01:24 +03:00
|
|
|
const TreeCell = createFactory(
|
|
|
|
require("devtools/client/shared/components/tree/TreeCell")
|
|
|
|
);
|
|
|
|
const LabelCell = createFactory(
|
|
|
|
require("devtools/client/shared/components/tree/LabelCell")
|
|
|
|
);
|
2016-03-21 15:29:17 +03:00
|
|
|
|
2019-07-15 14:01:04 +03:00
|
|
|
const {
|
|
|
|
wrapMoveFocus,
|
|
|
|
getFocusableElements,
|
|
|
|
} = require("devtools/client/shared/focus");
|
2019-04-04 17:12:37 +03:00
|
|
|
|
|
|
|
const UPDATE_ON_PROPS = [
|
|
|
|
"name",
|
|
|
|
"open",
|
|
|
|
"value",
|
|
|
|
"loading",
|
2019-05-14 15:31:13 +03:00
|
|
|
"level",
|
2019-04-04 17:12:37 +03:00
|
|
|
"selected",
|
|
|
|
"active",
|
|
|
|
"hasChildren",
|
|
|
|
];
|
2017-03-09 18:20:07 +03:00
|
|
|
|
2016-02-10 00:38:20 +03:00
|
|
|
/**
|
2016-03-21 15:29:17 +03:00
|
|
|
* This template represents a node in TreeView component. It's rendered
|
|
|
|
* using <tr> element (the entire tree is one big <table>).
|
2016-02-10 00:38:20 +03:00
|
|
|
*/
|
2017-10-27 17:33:10 +03:00
|
|
|
class TreeRow extends Component {
|
2016-03-21 15:29:17 +03:00
|
|
|
// See TreeView component for more details about the props and
|
|
|
|
// the 'member' object.
|
2017-10-27 17:33:10 +03:00
|
|
|
static get propTypes() {
|
|
|
|
return {
|
|
|
|
member: PropTypes.shape({
|
2019-08-09 09:08:01 +03:00
|
|
|
object: PropTypes.object,
|
|
|
|
name: PropTypes.string,
|
2017-10-27 17:33:10 +03:00
|
|
|
type: PropTypes.string.isRequired,
|
|
|
|
rowClass: PropTypes.string.isRequired,
|
|
|
|
level: PropTypes.number.isRequired,
|
|
|
|
hasChildren: PropTypes.bool,
|
|
|
|
value: PropTypes.any,
|
|
|
|
open: PropTypes.bool.isRequired,
|
|
|
|
path: PropTypes.string.isRequired,
|
|
|
|
hidden: PropTypes.bool,
|
|
|
|
selected: PropTypes.bool,
|
2019-04-04 17:12:37 +03:00
|
|
|
active: PropTypes.bool,
|
2019-07-17 13:35:44 +03:00
|
|
|
loading: PropTypes.bool,
|
2017-10-27 17:33:10 +03:00
|
|
|
}),
|
|
|
|
decorator: PropTypes.object,
|
2019-08-16 12:04:11 +03:00
|
|
|
renderCell: PropTypes.func,
|
|
|
|
renderLabelCell: PropTypes.func,
|
2017-10-27 17:33:10 +03:00
|
|
|
columns: PropTypes.array.isRequired,
|
|
|
|
id: PropTypes.string.isRequired,
|
|
|
|
provider: PropTypes.object.isRequired,
|
|
|
|
onClick: PropTypes.func.isRequired,
|
2018-11-30 17:54:59 +03:00
|
|
|
onContextMenu: PropTypes.func,
|
2017-10-27 17:33:10 +03:00
|
|
|
onMouseOver: PropTypes.func,
|
|
|
|
onMouseOut: PropTypes.func,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2019-04-04 17:12:37 +03:00
|
|
|
|
|
|
|
this.treeRowRef = createRef();
|
|
|
|
|
2017-10-27 17:33:10 +03:00
|
|
|
this.getRowClass = this.getRowClass.bind(this);
|
2019-04-04 17:12:37 +03:00
|
|
|
this._onKeyDown = this._onKeyDown.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this._setTabbableState();
|
|
|
|
|
|
|
|
// Child components might add/remove new focusable elements, watch for the
|
|
|
|
// additions/removals of descendant nodes and update focusable state.
|
|
|
|
const win = this.treeRowRef.current.ownerDocument.defaultView;
|
|
|
|
const { MutationObserver } = win;
|
|
|
|
this.observer = new MutationObserver(() => {
|
|
|
|
this._setTabbableState();
|
|
|
|
});
|
|
|
|
this.observer.observe(this.treeRowRef.current, {
|
|
|
|
childList: true,
|
|
|
|
subtree: true,
|
|
|
|
});
|
2017-10-27 17:33:10 +03:00
|
|
|
}
|
2016-03-21 15:29:17 +03:00
|
|
|
|
2016-05-18 00:20:14 +03:00
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
// I don't like accessing the underlying DOM elements directly,
|
|
|
|
// but this optimization makes the filtering so damn fast!
|
|
|
|
// The row doesn't have to be re-rendered, all we really need
|
|
|
|
// to do is toggling a class name.
|
|
|
|
// The important part is that DOM elements don't need to be
|
|
|
|
// re-created when they should appear again.
|
|
|
|
if (nextProps.member.hidden != this.props.member.hidden) {
|
2018-06-01 13:36:09 +03:00
|
|
|
const row = findDOMNode(this);
|
2016-05-18 00:20:14 +03:00
|
|
|
row.classList.toggle("hidden");
|
|
|
|
}
|
2017-10-27 17:33:10 +03:00
|
|
|
}
|
2016-03-21 15:29:17 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Optimize row rendering. If props are the same do not render.
|
|
|
|
* This makes the rendering a lot faster!
|
|
|
|
*/
|
2017-10-27 17:33:10 +03:00
|
|
|
shouldComponentUpdate(nextProps) {
|
2019-04-04 17:12:37 +03:00
|
|
|
for (const prop of UPDATE_ON_PROPS) {
|
|
|
|
if (nextProps.member[prop] != this.props.member[prop]) {
|
2016-03-21 15:29:17 +03:00
|
|
|
return true;
|
|
|
|
}
|
2016-02-10 00:38:20 +03:00
|
|
|
}
|
|
|
|
|
2016-03-21 15:29:17 +03:00
|
|
|
return false;
|
2017-10-27 17:33:10 +03:00
|
|
|
}
|
2016-03-21 15:29:17 +03:00
|
|
|
|
2019-04-04 17:12:37 +03:00
|
|
|
componentWillUnmount() {
|
|
|
|
this.observer.disconnect();
|
|
|
|
this.observer = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes sure that none of the focusable elements inside the row container
|
|
|
|
* are tabbable if the row is not active. If the row is active and focus
|
|
|
|
* is outside its container, focus on the first focusable element inside.
|
|
|
|
*/
|
|
|
|
_setTabbableState() {
|
2019-07-15 14:01:04 +03:00
|
|
|
const elms = getFocusableElements(this.treeRowRef.current);
|
2019-04-04 17:12:37 +03:00
|
|
|
if (elms.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { active } = this.props.member;
|
|
|
|
if (!active) {
|
|
|
|
elms.forEach(elm => elm.setAttribute("tabindex", "-1"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!elms.includes(document.activeElement)) {
|
|
|
|
elms[0].focus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_onKeyDown(e) {
|
|
|
|
const { target, key, shiftKey } = e;
|
|
|
|
|
|
|
|
if (key !== "Tab") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-15 14:01:04 +03:00
|
|
|
const focusMoved = !!wrapMoveFocus(
|
|
|
|
getFocusableElements(this.treeRowRef.current),
|
|
|
|
target,
|
|
|
|
shiftKey
|
|
|
|
);
|
2019-04-04 17:12:37 +03:00
|
|
|
if (focusMoved) {
|
|
|
|
// Focus was moved to the begining/end of the list, so we need to
|
|
|
|
// prevent the default focus change that would happen here.
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
e.stopPropagation();
|
|
|
|
}
|
|
|
|
|
2017-10-27 17:33:10 +03:00
|
|
|
getRowClass(object) {
|
2018-06-01 13:36:09 +03:00
|
|
|
const decorator = this.props.decorator;
|
2016-03-21 15:29:17 +03:00
|
|
|
if (!decorator || !decorator.getRowClass) {
|
|
|
|
return [];
|
|
|
|
}
|
2016-02-10 00:38:20 +03:00
|
|
|
|
2016-03-21 15:29:17 +03:00
|
|
|
// Decorator can return a simple string or array of strings.
|
|
|
|
let classNames = decorator.getRowClass(object);
|
|
|
|
if (!classNames) {
|
|
|
|
return [];
|
|
|
|
}
|
2016-02-10 00:38:20 +03:00
|
|
|
|
2016-03-21 15:29:17 +03:00
|
|
|
if (typeof classNames == "string") {
|
|
|
|
classNames = [classNames];
|
|
|
|
}
|
2016-02-10 00:38:20 +03:00
|
|
|
|
2016-03-21 15:29:17 +03:00
|
|
|
return classNames;
|
2017-10-27 17:33:10 +03:00
|
|
|
}
|
2016-02-10 00:38:20 +03:00
|
|
|
|
2017-10-27 17:33:10 +03:00
|
|
|
render() {
|
2018-06-01 13:36:09 +03:00
|
|
|
const member = this.props.member;
|
|
|
|
const decorator = this.props.decorator;
|
2018-11-30 17:54:59 +03:00
|
|
|
|
2018-06-01 13:36:09 +03:00
|
|
|
const props = {
|
2017-03-09 18:20:07 +03:00
|
|
|
id: this.props.id,
|
2019-04-04 17:12:37 +03:00
|
|
|
ref: this.treeRowRef,
|
2017-03-09 18:20:07 +03:00
|
|
|
role: "treeitem",
|
2019-05-14 15:31:13 +03:00
|
|
|
"aria-level": member.level + 1,
|
2017-03-09 18:20:07 +03:00
|
|
|
"aria-selected": !!member.selected,
|
|
|
|
onClick: this.props.onClick,
|
2018-11-30 17:54:59 +03:00
|
|
|
onContextMenu: this.props.onContextMenu,
|
2019-04-04 17:12:37 +03:00
|
|
|
onKeyDownCapture: member.active ? this._onKeyDown : undefined,
|
2017-03-09 18:20:07 +03:00
|
|
|
onMouseOver: this.props.onMouseOver,
|
|
|
|
onMouseOut: this.props.onMouseOut,
|
|
|
|
};
|
2016-02-10 00:38:20 +03:00
|
|
|
|
2016-03-21 15:29:17 +03:00
|
|
|
// Compute class name list for the <tr> element.
|
2018-06-01 13:36:09 +03:00
|
|
|
const classNames = this.getRowClass(member.object) || [];
|
2016-03-21 15:29:17 +03:00
|
|
|
classNames.push("treeRow");
|
|
|
|
classNames.push(member.type + "Row");
|
2016-02-10 00:38:20 +03:00
|
|
|
|
2016-03-21 15:29:17 +03:00
|
|
|
if (member.hasChildren) {
|
|
|
|
classNames.push("hasChildren");
|
2020-03-29 12:32:58 +03:00
|
|
|
|
|
|
|
// There are 2 situations where hasChildren is true:
|
|
|
|
// 1. it is an object with children. Only set aria-expanded in this situation
|
|
|
|
// 2. It is a long string (> 50 chars) that can be expanded to fully display it
|
|
|
|
if (member.type !== "string") {
|
|
|
|
props["aria-expanded"] = member.open;
|
|
|
|
}
|
2016-03-21 15:29:17 +03:00
|
|
|
}
|
2016-02-10 00:38:20 +03:00
|
|
|
|
2016-03-21 15:29:17 +03:00
|
|
|
if (member.open) {
|
|
|
|
classNames.push("opened");
|
|
|
|
}
|
2016-02-10 00:38:20 +03:00
|
|
|
|
2016-03-21 15:29:17 +03:00
|
|
|
if (member.loading) {
|
|
|
|
classNames.push("loading");
|
|
|
|
}
|
2016-02-10 00:38:20 +03:00
|
|
|
|
2017-03-09 18:20:07 +03:00
|
|
|
if (member.selected) {
|
|
|
|
classNames.push("selected");
|
|
|
|
}
|
|
|
|
|
2016-03-21 15:29:17 +03:00
|
|
|
if (member.hidden) {
|
|
|
|
classNames.push("hidden");
|
|
|
|
}
|
2016-02-10 00:38:20 +03:00
|
|
|
|
2017-03-09 18:20:07 +03:00
|
|
|
props.className = classNames.join(" ");
|
|
|
|
|
2016-03-21 15:29:17 +03:00
|
|
|
// The label column (with toggle buttons) is usually
|
|
|
|
// the first one, but there might be cases (like in
|
|
|
|
// the Memory panel) where the toggling is done
|
|
|
|
// in the last column.
|
2018-06-01 13:36:09 +03:00
|
|
|
const cells = [];
|
2016-03-21 15:29:17 +03:00
|
|
|
|
|
|
|
// Get components for rendering cells.
|
|
|
|
let renderCell = this.props.renderCell || RenderCell;
|
|
|
|
let renderLabelCell = this.props.renderLabelCell || RenderLabelCell;
|
2020-03-20 00:13:38 +03:00
|
|
|
if (decorator?.renderLabelCell) {
|
2016-03-21 15:29:17 +03:00
|
|
|
renderLabelCell =
|
|
|
|
decorator.renderLabelCell(member.object) || renderLabelCell;
|
2016-02-10 00:38:20 +03:00
|
|
|
}
|
|
|
|
|
2016-03-21 15:29:17 +03:00
|
|
|
// Render a cell for every column.
|
|
|
|
this.props.columns.forEach(col => {
|
2018-06-01 13:36:09 +03:00
|
|
|
const cellProps = Object.assign({}, this.props, {
|
2016-03-21 15:29:17 +03:00
|
|
|
key: col.id,
|
|
|
|
id: col.id,
|
|
|
|
value: this.props.provider.getValue(member.object, col.id),
|
|
|
|
});
|
|
|
|
|
2020-03-20 00:13:38 +03:00
|
|
|
if (decorator?.renderCell) {
|
2016-03-21 15:29:17 +03:00
|
|
|
renderCell = decorator.renderCell(member.object, col.id);
|
|
|
|
}
|
|
|
|
|
2018-06-01 13:36:09 +03:00
|
|
|
const render = col.id == "default" ? renderLabelCell : renderCell;
|
2016-03-21 15:29:17 +03:00
|
|
|
|
|
|
|
// Some cells don't have to be rendered. This happens when some
|
|
|
|
// other cells span more columns. Note that the label cells contains
|
|
|
|
// toggle buttons and should be usually there unless we are rendering
|
|
|
|
// a simple non-expandable table.
|
|
|
|
if (render) {
|
2017-03-09 18:20:07 +03:00
|
|
|
cells.push(render(cellProps));
|
2016-03-21 15:29:17 +03:00
|
|
|
}
|
|
|
|
});
|
2016-02-10 00:38:20 +03:00
|
|
|
|
2016-03-21 15:29:17 +03:00
|
|
|
// Render tree row
|
2017-03-09 18:20:07 +03:00
|
|
|
return tr(props, cells);
|
2016-03-21 15:29:17 +03:00
|
|
|
}
|
2017-10-27 17:33:10 +03:00
|
|
|
}
|
2016-02-10 00:38:20 +03:00
|
|
|
|
2016-03-21 15:29:17 +03:00
|
|
|
// Helpers
|
2016-02-10 00:38:20 +03:00
|
|
|
|
2018-06-01 13:36:09 +03:00
|
|
|
const RenderCell = props => {
|
2016-03-21 15:29:17 +03:00
|
|
|
return TreeCell(props);
|
|
|
|
};
|
2016-02-10 00:38:20 +03:00
|
|
|
|
2018-06-01 13:36:09 +03:00
|
|
|
const RenderLabelCell = props => {
|
2016-03-21 15:29:17 +03:00
|
|
|
return LabelCell(props);
|
|
|
|
};
|
2016-02-10 00:38:20 +03:00
|
|
|
|
2016-03-21 15:29:17 +03:00
|
|
|
// Exports from this module
|
|
|
|
module.exports = TreeRow;
|
|
|
|
});
|