UNPKG

@jsonforms/material-tree-renderer

Version:

Material-based tree renderer for JSON Forms

163 lines (159 loc) 8.18 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); 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. */ // tslint:disable:jsx-no-multiline-js // tslint:disable:jsx-no-lambda var keys_1 = __importDefault(require("lodash/keys")); var isEmpty_1 = __importDefault(require("lodash/isEmpty")); var find_1 = __importDefault(require("lodash/find")); var merge_1 = __importDefault(require("lodash/merge")); var DialogActions_1 = __importDefault(require("@material-ui/core/DialogActions")); var DialogTitle_1 = __importDefault(require("@material-ui/core/DialogTitle")); var react_redux_1 = require("react-redux"); var core_1 = require("@jsonforms/core"); var property_util_1 = require("../services/property.util"); var Button_1 = __importDefault(require("@material-ui/core/Button")); var DialogContent_1 = __importDefault(require("@material-ui/core/DialogContent")); var react_1 = __importDefault(require("react")); var Dialog_1 = __importDefault(require("@material-ui/core/Dialog")); var List_1 = __importDefault(require("@material-ui/core/List")); var ListItem_1 = __importDefault(require("@material-ui/core/ListItem")); var ListItemText_1 = __importDefault(require("@material-ui/core/ListItemText")); var core_2 = require("@material-ui/core"); var image_provider_util_1 = require("../helpers/image-provider.util"); var createData = function (defaultData, prop) { var foundData = find_1.default(defaultData, function (d) { return d.schemaPath === prop.schemaPath; }); // default behavior is to read default property from schemas var predefined = keys_1.default(prop.schema.properties).reduce(function (acc, key) { if (prop.schema.properties[key].default) { acc[key] = prop.schema.properties[key].default; } // FIXME generate id if identifying property // is set in editor to allow id refs return acc; }, {}); return merge_1.default(foundData ? foundData.data : {}, predefined); }; var createPropLabel = function (prop, labelProvider) { var label = prop.label; if (labelProvider) { label = labelProvider(prop.schema, prop.schemaPath); } return prop.property + " [" + label + "]"; }; var AddItemDialog = /** @class */ (function (_super) { __extends(AddItemDialog, _super); function AddItemDialog() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.onClick = function (prop) { var _a = _this.props, add = _a.add, closeDialog = _a.closeDialog, defaultData = _a.defaultData, path = _a.path, rootData = _a.rootData, setSelection = _a.setSelection; var newData = createData(defaultData, prop); var arrayPath = core_1.Paths.compose(path, prop.property); var array = core_1.Resolve.data(rootData, arrayPath); var selectionIndex = isEmpty_1.default(array) ? 0 : array.length; var selectionPath = core_1.Paths.compose(arrayPath, selectionIndex.toString()); add(path, prop, newData); setSelection(prop.schema, newData, selectionPath)(); closeDialog(); }; return _this; } AddItemDialog.prototype.render = function () { var _this = this; var _a = this.props, closeDialog = _a.closeDialog, dialogProps = _a.dialogProps, /** * Self contained schemas of the corresponding schema */ containerProperties = _a.containerProperties, imageProvider = _a.imageProvider, labelProvider = _a.labelProvider; return (react_1.default.createElement(Dialog_1.default, __assign({ id: 'dialog' }, dialogProps), react_1.default.createElement(DialogTitle_1.default, { id: 'dialog-title' }, "Select item to create"), react_1.default.createElement(DialogContent_1.default, null, react_1.default.createElement(List_1.default, null, containerProperties .map(function (prop, index) { var label = createPropLabel(prop, labelProvider); return (react_1.default.createElement(ListItem_1.default, { button: true, key: property_util_1.findPropertyLabel(prop) + "-button-" + index, onClick: function () { return _this.onClick(prop); } }, react_1.default.createElement(core_2.ListItemIcon, null, image_provider_util_1.wrapImageIfNecessary(imageProvider(prop.schema))), react_1.default.createElement(ListItemText_1.default, { primary: label }))); }))), react_1.default.createElement(DialogActions_1.default, null, react_1.default.createElement(Button_1.default, { onClick: closeDialog, variant: 'outlined', color: 'primary' }, "Close")))); }; return AddItemDialog; }(react_1.default.Component)); // TODO var mapStateToProps = function (state, ownProps) { var containerProperties = property_util_1.findContainerProperties(ownProps.schema, core_1.getSchema(state), false); return { rootData: core_1.getData(state), defaultData: core_1.getDefaultData(state), containerProperties: containerProperties, rootSchema: core_1.getSchema(state), path: ownProps.path, schema: ownProps.schema, closeDialog: ownProps.closeDialog, dialogProps: ownProps.dialogProps, setSelection: ownProps.setSelection, labelProvider: ownProps.labelProvider }; }; var mapDispatchToProps = function (dispatch) { return ({ add: function (path, prop, newData) { dispatch(core_1.update(core_1.Paths.compose(path, prop.property), function (array) { if (isEmpty_1.default(array)) { return [newData]; } array.push(newData); return array; })); } }); }; exports.default = react_redux_1.connect(mapStateToProps, mapDispatchToProps)(AddItemDialog); //# sourceMappingURL=AddItemDialog.js.map