UNPKG

@jsonforms/material-tree-renderer

Version:

Material-based tree renderer for JSON Forms

208 lines (204 loc) 9.61 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); /* The MIT License Copyright (c) 2017-2019 EclipseSource Munich https://github.com/eclipsesource/jsonforms Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ var set_1 = __importDefault(require("lodash/set")); var get_1 = __importDefault(require("lodash/get")); var isEmpty_1 = __importDefault(require("lodash/isEmpty")); var find_1 = __importDefault(require("lodash/find")); var reduce_1 = __importDefault(require("lodash/reduce")); var pickBy_1 = __importDefault(require("lodash/pickBy")); var omitBy_1 = __importDefault(require("lodash/omitBy")); var startsWith_1 = __importDefault(require("lodash/startsWith")); var values_1 = __importDefault(require("lodash/values")); var each_1 = __importDefault(require("lodash/each")); var core_1 = require("@jsonforms/core"); var isObject = function (schema) { return schema.properties !== undefined; }; var isArray = function (schema) { return schema.items !== undefined; }; /** * Returns the label of a property based on the best-effort solution * @param property The property contains a schema, label and a property as key * @returns {string} */ exports.findPropertyLabel = function (property) { return find_1.default([property.schema.title, property.label, property.property], function (n) { return !isEmpty_1.default(n); }); }; /** * Finding required definitions from parentSchema by using schema refs * * @param parentSchema The root schema * @param allRefs All the refs of root schema * @param schemaRefs The current schema refs * @param extractedReferences Contains the definition attributes for the current schema * @returns SchemaRefs all the required reference paths for subschema */ var findReferences = function (parentSchema, allRefs, schemaRefs, extractedReferences) { return reduce_1.default(schemaRefs, function (prev, schemaRefValue) { var refs = pickBy_1.default(prev, function (_value, key) { return startsWith_1.default(key, schemaRefValue.uri); }); if (extractedReferences[schemaRefValue.uri]) { refs = undefined; } if (!extractedReferences[schemaRefValue.uri]) { extractedReferences[schemaRefValue.uri] = schemaRefValue.uri; prev = omitBy_1.default(prev, function (value) { return value.uri === schemaRefValue.uri; }); } if (refs !== undefined) { findReferences(parentSchema, prev, refs, extractedReferences); } return prev; }, allRefs); }; /** * Calculate references for a given schema and copy definitions into the schema * * @param parentSchema root schema which is used to find all the schema refs * @param schema current subschema without resolved references * @returns JsonSchema current subschema with resolved references */ exports.makeSchemaSelfContained = function (parentSchema, schema) { var schemaRefs = core_1.findRefs(schema); var allRefs = core_1.findRefs(parentSchema); var extractedReferences; findReferences(parentSchema, allRefs, schemaRefs, (extractedReferences = {})); var refList = values_1.default(extractedReferences); if (!isEmpty_1.default(refList)) { each_1.default(refList, function (ref) { var _a, _b, _c; var propertyKey = ref.substring(ref.indexOf('/') + 1, ref.lastIndexOf('/')); var property = ref.substring(ref.lastIndexOf('/') + 1); if (parentSchema[propertyKey] !== undefined && parentSchema[propertyKey][property] !== undefined) { if (get_1.default(schema, propertyKey)) { set_1.default(schema, propertyKey, __assign({}, get_1.default(schema, propertyKey), (_a = {}, _a[property] = get_1.default(parentSchema, [propertyKey, property]), _a))); } else { schema = __assign({}, schema, (_b = {}, _b[propertyKey] = (_c = {}, _c[property] = get_1.default(parentSchema, [propertyKey, property]), _c), _b)); } } }); } return schema; }; /** * Create a self contained schema. * @param parentSchema The schema to use for resolving * @param refPath The path to resolve * @return a JsonSchema that is self-contained */ var resolveAndMakeSchemaSelfContained = function (parentSchema, refPath) { var schema = core_1.resolveSchema(parentSchema, refPath); return __assign({}, schema, exports.makeSchemaSelfContained(parentSchema, schema)); }; /** * Returns a flattened list of container properties. * A property is being considered a container property if it is an array of objects, * meaning that all its children are non-primitive. * * @param {string} property The schema key from which this property was created. * @param {string} label The name of the schema the property describes. * @param {JsonSchema} schema The schema is the JsonSchema this property describes. * @param {JsonSchema} rootSchema The parent schema * @param {boolean} isInContainer To indicate whether the schema is in a container or not * Properties that are described in array are considered as * being in a container. * @returns {@link Property[]} An array of properties where each property describes * a self-contained schema for the corresponding schema */ var findContainerProps = function (property, label, schemaPath, schema, rootSchema, isInContainer, visited, recurse) { if (schema.$ref !== undefined) { if (visited.indexOf(schema.$ref) !== -1) { return []; } return findContainerProps(property, schema.$ref === '#' ? undefined : schema.$ref.substring(schema.$ref.lastIndexOf('/') + 1), schemaPath, resolveAndMakeSchemaSelfContained(rootSchema, schema.$ref), rootSchema, isInContainer, visited.slice().concat(schema.$ref), recurse); } else if (isObject(schema)) { var props = []; if (isInContainer) { var prop = { property: property, label: label, schema: schema, schemaPath: schemaPath }; props.push(prop); if (!recurse) { return props; } } return Object.keys(schema.properties).reduce(function (prev, currentProp) { return prev.concat(findContainerProps(currentProp, currentProp, (schemaPath.length > 0 ? schemaPath + '.' : '') + "properties." + currentProp, schema.properties[currentProp], rootSchema, false, visited, recurse)); }, props); } else if (isArray(schema) && !Array.isArray(schema.items)) { return findContainerProps(property, label, schemaPath + ".items", schema.items, rootSchema, true, visited, recurse); } else if (schema.anyOf !== undefined) { // TODO: oneOf? var init = []; return reduce_1.default(schema.anyOf, function (prev, anyOfSubSchema, index) { return prev.concat(findContainerProps(property, label, schemaPath + ".anyOf." + index, anyOfSubSchema, rootSchema, isInContainer, visited, recurse)); }, init); } return []; }; /** * Retrieves an array of properties based on the provided schema. * * @param schema the schema to check for container properties * @param rootSchema the root schema * @param recurse whether to recurse in case properties have already been found. * Set to true for finding all valid, i.e. nested container properties * @return The array of {@link Property} or empty if no properties are available * @see Property */ exports.findContainerProperties = function (schema, rootSchema, recurse) { return findContainerProps('root', 'root', '', schema, rootSchema, false, [], recurse); }; //# sourceMappingURL=property.util.js.map