@talend/react-components
Version:
Set of react components.
116 lines (113 loc) • 4.7 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.addPathsToCollection = addPathsToCollection;
exports.default = void 0;
exports.removePathsFromCollection = removePathsFromCollection;
exports.updateCollection = updateCollection;
var _react = require("react");
var _propTypes = _interopRequireDefault(require("prop-types"));
var _immutable = _interopRequireDefault(require("immutable"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
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); }
function addPathsToCollection(index, collection, paths, jsonpath) {
return collection.set(index, paths.push(jsonpath));
}
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
*/
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.
*/
class TreeManager extends _react.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.default.List()), options)
});
} else {
this.setState({
expandedNodes: updateCollection(index, collection, isAllExpanded, collection.get(index, _immutable.default.List()), options)
});
}
if (this.props.onToggle) {
this.props.onToggle(event, options, index);
}
// }
});
this.state = {
isAllExpanded: props.isAllExpanded || false,
collapsedNodes: props.collapsedNodes || _immutable.default.Map(),
expandedNodes: props.expandedNodes || _immutable.default.Map().set(0, _immutable.default.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
});
}
}
exports.default = TreeManager;
_defineProperty(TreeManager, "displayName", 'Container(TreeManager)');
_defineProperty(TreeManager, "propTypes", {
highlighted: _propTypes.default.array,
onToggle: _propTypes.default.func,
wrappedComponent: _propTypes.default.func,
isAllExpanded: _propTypes.default.bool,
collapsedNodes: _propTypes.default.object,
expandedNodes: _propTypes.default.object
});
//# sourceMappingURL=TreeManager.container.js.map