@talend/react-components
Version:
Set of react components.
229 lines (221 loc) • 7.8 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.ModelViewer = void 0;
exports.defaultGetDisplayKey = defaultGetDisplayKey;
exports.defaultGetDisplayValue = defaultGetDisplayValue;
exports.defaultTransformChilds = defaultTransformChilds;
exports.filterNullUnion = filterNullUnion;
exports.getChilds = getChilds;
exports.getDqType = getDqType;
exports.getItemType = getItemType;
exports.getJSONPath = getJSONPath;
exports.transformUnions = transformUnions;
var _propTypes = _interopRequireDefault(require("prop-types"));
var _react = require("react");
var _reactI18next = require("react-i18next");
var _constants = _interopRequireDefault(require("../../constants"));
var _translate = _interopRequireDefault(require("../../translate"));
var _jsonPath = _interopRequireDefault(require("../jsonPath"));
var _Managers = require("../Managers");
var _Branch = _interopRequireDefault(require("./Branch"));
var _Leaf = _interopRequireDefault(require("./Leaf"));
var _ModelViewer2 = _interopRequireDefault(require("./ModelViewer.component"));
var _lodash = require("lodash");
var _jsxRuntime = require("react/jsx-runtime");
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); } /**
* Return the attribute to display from the item.
* @param {object} item
*/
function defaultGetDisplayValue(item) {
if (typeof item === 'string') {
return item;
}
return (0, _lodash.get)(item, 'doc', item.name);
}
/**
* Return the attribute displayed for type.
* @param {object} item
*/
function getDqType(item) {
return item.dqType || item.type;
}
/**
* Return the type.
* @param {object} item
*/
function defaultGetDisplayKey(item) {
if (typeof item.type === 'object') {
return `(${getDqType(item.type)})`;
}
if (typeof item.dqType === 'string' || typeof item.type === 'string') {
return `(${getDqType(item)})`;
}
return '';
}
/**
* Return an array of the next items to display.
* @param {object} item
*/
function getChilds(item) {
return (0, _lodash.get)(item, 'type.items.fields') || (0, _lodash.get)(item, 'type.fields') || (0, _lodash.get)(item, 'type.symbols') || (0, _lodash.get)(item, 'fields') || (0, _lodash.get)(item, 'type');
}
/**
* Filtering the union removing the null value.
* Return a new structure to represent the union.
* @param {object} union
*/
function filterNullUnion(union) {
const typeWithoutNull = union.type.filter(type => type.type !== 'null').map(item => {
if (!item.name) {
return {
...item,
name: union.name,
path: union.path
};
}
return item;
});
return {
name: union.name,
type: typeWithoutNull.length === 1 ? (0, _lodash.head)(typeWithoutNull) : typeWithoutNull,
optional: typeWithoutNull.length < union.type.length,
path: union.path,
'talend.component.label': union['talend.component.label']
};
}
/**
* Make operation on union in the childs.
* Return new array with the transformed union.
* @param {array} childs
*/
function transformUnions(childs) {
return childs.reduce((acc, child) => {
if (Array.isArray(child.type)) {
acc.push(filterNullUnion(child));
} else {
acc.push(child);
}
return acc;
}, []);
}
/**
* Return the array of childs, used in the branch.
* @param {object} item
*/
function defaultTransformChilds(item) {
const childs = getChilds(item);
return transformUnions(childs).map(field => ({
dataKey: field.name,
value: field
}));
}
/**
* Return the string representing the jsonpapth of the current element.
* @param {String} dataKey
* @param {object} parent
*/
function getJSONPath(dataKey, parent) {
if ((0, _lodash.get)(parent, 'value.value.type.type') === 'array') {
return (0, _jsonPath.default)([''], `${parent.jsonpath}['${dataKey}']`, 'array');
}
return (0, _jsonPath.default)([dataKey], parent.jsonpath);
}
/**
* Used in TreeNode, helps to determine if it's a branch or a leaf.
* Return the type of the childs.
* @param {object} item
*/
function getItemType(item) {
if (Array.isArray(getChilds(item))) {
return 'object';
}
return null;
}
class ModelViewer extends _react.Component {
constructor(..._args) {
super(..._args);
_defineProperty(this, "renderLeaf", args => /*#__PURE__*/(0, _jsxRuntime.jsx)(_Leaf.default, {
...args,
getDisplayKey: this.props.getDisplayKey,
getDisplayValue: this.props.getDisplayValue,
hasSemanticAwareness: this.props.hasSemanticAwareness,
menu: this.props.menuActions,
onSelect: this.props.onSelect,
t: this.props.t,
renderLeafOptions: this.props.renderLeafOptions
}));
_defineProperty(this, "renderBranch", args => /*#__PURE__*/(0, _jsxRuntime.jsx)(_Branch.default, {
...args,
getChilds: this.props.getChilds,
getDisplayKey: this.props.getDisplayKey,
getDisplayValue: this.props.getDisplayValue,
hasSemanticAwareness: this.props.hasSemanticAwareness,
isUnion: this.props.isUnion,
t: this.props.t
}));
_defineProperty(this, "renderComponent", args => {
const componentProps = {
...this.props,
branch: this.renderBranch,
getJSONPath: this.props.getJSONPath,
getItemType: this.props.getItemType,
leaf: this.renderLeaf,
t: this.props.t,
value: this.props.getChilds(this.props.sample.schema)
};
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_ModelViewer2.default, {
...args,
...componentProps
});
});
}
render() {
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Managers.TreeManager, {
componentId: this.props.componentId || 'ModelViewer',
jsonPathSelection: this.props.jsonPathSelection,
wrappedComponent: this.renderComponent,
sample: this.props.sample
});
}
}
exports.ModelViewer = ModelViewer;
_defineProperty(ModelViewer, "displayName", 'ModelViewerContainer');
_defineProperty(ModelViewer, "propTypes", {
componentId: _propTypes.default.string,
getChilds: _propTypes.default.func,
getDisplayKey: _propTypes.default.func,
getDisplayValue: _propTypes.default.func,
getDisplayClassName: _propTypes.default.func,
getJSONPath: _propTypes.default.func,
getItemType: _propTypes.default.func,
hasSemanticAwareness: _propTypes.default.bool,
jsonPathSelection: _propTypes.default.string,
menuActions: _propTypes.default.arrayOf({
label: _propTypes.default.string
}),
onSelect: _propTypes.default.func,
sample: _propTypes.default.shape({
schema: _propTypes.default.any,
data: _propTypes.default.any
}),
t: _propTypes.default.func,
renderLeafOptions: _propTypes.default.func,
isUnion: _propTypes.default.func
});
_defineProperty(ModelViewer, "defaultProps", {
getChilds: defaultTransformChilds,
getDisplayKey: defaultGetDisplayKey,
getDisplayValue: defaultGetDisplayValue,
getDisplayClassName: _lodash.noop,
getJSONPath,
getItemType,
hasSemanticAwareness: true,
t: (0, _translate.default)()
});
var _default = exports.default = (0, _reactI18next.withTranslation)(_constants.default)(ModelViewer);
//# sourceMappingURL=ModelViewer.container.js.map