UNPKG

react-json-schema-editor-antd

Version:

Json Schema Editor

57 lines 3.94 kB
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } import { Flex, Tooltip } from 'antd'; import i18next from 'i18next'; import { isArray, isUndefined } from 'lodash'; import React from 'react'; export var listToTree = function listToTree(data) { var treeData = {}; var rootData = []; if (!Array.isArray(data)) { return; } data.forEach(function (item) { var _item$schema, _item$schema2; var newTreeItem = { key: item === null || item === void 0 ? void 0 : item.model_id, title: (item === null || item === void 0 ? void 0 : item.display_name) || (item === null || item === void 0 ? void 0 : item.name) || '', model_type: item.model_type }; if ((item === null || item === void 0 || (_item$schema = item.schema) === null || _item$schema === void 0 ? void 0 : _item$schema.type) !== 'object' && !((item === null || item === void 0 || (_item$schema2 = item.schema) === null || _item$schema2 === void 0 ? void 0 : _item$schema2.type) || []).includes('object') && newTreeItem.model_type === 'model') { newTreeItem.disabled = true; newTreeItem.disableCheckbox = true; newTreeItem.title = /*#__PURE__*/React.createElement(Tooltip, { title: i18next.t('cannot_reference_not_object') || '不能引用非 object 类型的数据模型' }, /*#__PURE__*/React.createElement(Flex, { style: { width: '100%' } }, (item === null || item === void 0 ? void 0 : item.display_name) || (item === null || item === void 0 ? void 0 : item.name) || '')); } treeData[item.model_id] = newTreeItem; }); var _iterator = _createForOfIteratorHelper(data), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var _parent$children; var item = _step.value; var nodeItem = treeData[item.model_id]; var parent = treeData[item === null || item === void 0 ? void 0 : item.parent_id]; if (isUndefined(parent)) { rootData.push(nodeItem); continue; } if (!isArray(parent === null || parent === void 0 ? void 0 : parent.children)) { parent.children = []; } parent === null || parent === void 0 || (_parent$children = parent.children) === null || _parent$children === void 0 || _parent$children.push(nodeItem); } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } return rootData; };