@talend/react-components
Version:
Set of react components.
105 lines (103 loc) • 4.2 kB
JavaScript
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import { Component } from 'react';
import PropTypes from 'prop-types';
import Immutable from 'immutable';
export function addPathsToCollection(index, collection, paths, jsonpath) {
return collection.set(index, paths.push(jsonpath));
}
export function removePathsFromCollection(index, collection, paths, jsonpath) {
return collection.set(index, paths.filter(path => path !== jsonpath));
}
/**
* Collections expandedNodes / collapsedNodes are switched depending of the expand all value.
* @param {number} index
* @param {Immutable.Map} collection
* @param {boolean} expandAll
* @param {Immutable.List} paths
*/
export function updateCollection(index, collection, expandAll, paths, {
opened,
jsonpath
}) {
if (opened) {
return expandAll ? addPathsToCollection(index, collection, paths, jsonpath) : removePathsFromCollection(index, collection, paths, jsonpath);
}
return expandAll ? removePathsFromCollection(index, collection, paths, jsonpath) : addPathsToCollection(index, collection, paths, jsonpath);
}
/**
* Helps to manage opened and highlight state of the viewer.
*/
export default class TreeManager extends Component {
constructor(props) {
super();
_defineProperty(this, "onExpandAll", () => {
this.setState(oldState => ({
isAllExpanded: true,
collapsedNodes: oldState.collapsedNodes.clear()
}));
});
_defineProperty(this, "onCollapseAll", () => {
this.setState(oldState => ({
isAllExpanded: false,
expandedNodes: oldState.expandedNodes.clear()
}));
});
_defineProperty(this, "onToggle", (event, options, index) => {
/*
This is a special case for the union.
We have to make all union expanded by default,
so at the first click we don't want the union to be registered
in the expanded collection.
*/
if (options.firstClickUnion) {
return;
}
const isAllExpanded = this.state.isAllExpanded;
const collection = isAllExpanded ? this.state.collapsedNodes : this.state.expandedNodes;
if (isAllExpanded) {
this.setState({
collapsedNodes: updateCollection(index, collection, isAllExpanded, collection.get(index, Immutable.List()), options)
});
} else {
this.setState({
expandedNodes: updateCollection(index, collection, isAllExpanded, collection.get(index, Immutable.List()), options)
});
}
if (this.props.onToggle) {
this.props.onToggle(event, options, index);
}
// }
});
this.state = {
isAllExpanded: props.isAllExpanded || false,
collapsedNodes: props.collapsedNodes || Immutable.Map(),
expandedNodes: props.expandedNodes || Immutable.Map().set(0, Immutable.List(['$']))
};
}
render() {
const isAllExpanded = this.state.isAllExpanded;
const wrappedProps = {
onToggle: this.onToggle,
onCollapseAll: this.onCollapseAll,
onExpandAll: this.onExpandAll,
paths: isAllExpanded ? this.state.collapsedNodes.toJS() : this.state.expandedNodes.toJS(),
highlighted: this.props.highlighted,
isAllExpanded
};
return this.props.wrappedComponent({
...wrappedProps
});
}
}
_defineProperty(TreeManager, "displayName", 'Container(TreeManager)');
_defineProperty(TreeManager, "propTypes", {
highlighted: PropTypes.array,
onToggle: PropTypes.func,
wrappedComponent: PropTypes.func,
isAllExpanded: PropTypes.bool,
collapsedNodes: PropTypes.object,
expandedNodes: PropTypes.object
});
//# sourceMappingURL=TreeManager.container.js.map