UNPKG

hbp-quickfire

Version:

A library of useful user-interface components built with React on top of React Bootstrap and MobX

165 lines (132 loc) 32 kB
var _createClass = function () {function defineProperties(target, props) {for (var i = 0; i < props.length; i++) {var descriptor = props[i];descriptor.enumerable = descriptor.enumerable || false;descriptor.configurable = true;if ("value" in descriptor) descriptor.writable = true;Object.defineProperty(target, descriptor.key, descriptor);}}return function (Constructor, protoProps, staticProps) {if (protoProps) defineProperties(Constructor.prototype, protoProps);if (staticProps) defineProperties(Constructor, staticProps);return Constructor;};}();var _get = function get(object, property, receiver) {if (object === null) object = Function.prototype;var desc = Object.getOwnPropertyDescriptor(object, property);if (desc === undefined) {var parent = Object.getPrototypeOf(object);if (parent === null) {return undefined;} else {return get(parent, property, receiver);}} else if ("value" in desc) {return desc.value;} else {var getter = desc.get;if (getter === undefined) {return undefined;}return getter.call(receiver);}};var _desc, _value, _class, _descriptor, _descriptor2, _descriptor3, _descriptor4, _descriptor5, _descriptor6, _descriptor7, _descriptor8, _descriptor9, _descriptor10, _descriptor11, _descriptor12;function _initDefineProp(target, property, descriptor, context) {if (!descriptor) return;Object.defineProperty(target, property, { enumerable: descriptor.enumerable, configurable: descriptor.configurable, writable: descriptor.writable, value: descriptor.initializer ? descriptor.initializer.call(context) : void 0 });}function _classCallCheck(instance, Constructor) {if (!(instance instanceof Constructor)) {throw new TypeError("Cannot call a class as a function");}}function _possibleConstructorReturn(self, call) {if (!self) {throw new ReferenceError("this hasn't been initialised - super() hasn't been called");}return call && (typeof call === "object" || typeof call === "function") ? call : self;}function _inherits(subClass, superClass) {if (typeof superClass !== "function" && superClass !== null) {throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);}subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } });if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;}function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) {var desc = {};Object['ke' + 'ys'](descriptor).forEach(function (key) {desc[key] = descriptor[key];});desc.enumerable = !!desc.enumerable;desc.configurable = !!desc.configurable;if ('value' in desc || desc.initializer) {desc.writable = true;}desc = decorators.slice().reverse().reduce(function (desc, decorator) {return decorator(target, property, desc) || desc;}, desc);if (context && desc.initializer !== void 0) {desc.value = desc.initializer ? desc.initializer.call(context) : void 0;desc.initializer = undefined;}if (desc.initializer === void 0) {Object['define' + 'Property'](target, property, desc);desc = null;}return desc;}function _initializerWarningHelper(descriptor, context) {throw new Error('Decorating class property failed. Please ensure that transform-class-properties is enabled.');} /* * Copyright (c) Human Brain Project * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ import { observable, action, toJS } from "mobx";import union from "lodash/union";import remove from "lodash/remove";import isArray from "lodash/isArray";import isEqual from "lodash/isEqual"; import DefaultField from "./DefaultField"; /** * @memberof FormFields.DataSheetField * @name Options * @param {string} label "" - The field label * @param {string} labelTooltip "" - The field label tooltip message * @param {string} labelTooltipPlacement "top" - The field label tooltip message position * @param {string} type "DataSheet" * @param {array} value [] - The current value of the field * @param {array} defaultValue [] - The defaultValue of the field * @param {array} headers [] - The headers of the datasheet, must be an array of objects dscribing at least a "label" and a "key" property * @param {string} path "" - Field path * @param {number} min 0 - Minimum rows that the field can have * @param {number} max Infinity - Maximum rows that the field can have * @param {boolean} rowControlRemove true - Flag option for specifying if a row delete button should be displayed * @param {boolean} rowControlMove true - Flag option for specifying if row move buttons should be displayed * @param {boolean} rowControlDuplicate true - Flag option for specifying if a row duplicate button should be displayed * @param {boolean} rowControlAdd true - Flag option for specifying if row add buttons should be displayed * @param {boolean} clipContent false - Whether cells content should wrap or clip the text content * @param {boolean} emptyToNull false - Flag that determines if empty values are transformed to null in the value function of the formStore * @param {boolean} disabled false - Is the field disabled or not, a disabled field won't be editable or processed by FormStore.getValues() * @param {boolean} readOnly false - Is the field readOnly or not, a readOnly field won't be editable but will be processed by FormStore.getValues() * @param {boolean} readMode false - If true, displays the field as label and value without the actual form input * @param {string} buttonLabel "Add a row" - The label used for the button to add a row * @name HeaderOptions * @param {string} key "" - The column key that will be used in the values row for input and output * @param {string} label "" - The column label * @param {boolean} show undefined - If false, the column will not be displayed at all * @param {boolean} readOnly undefined - If true, the column will be displayed as read only cells * @param {string} defaultValue "" - The default value the column will take when creating a new row * @param {string} duplicatedValue "" - The default value the column will take when duplicating an existing row * @param {string} width undefined - The column width (e.g. "50px" or "25%") */var DataSheetField = (_class = function (_DefaultField) {_inherits(DataSheetField, _DefaultField);_createClass(DataSheetField, null, [{ key: "properties", get: function get() { return union(_get(DataSheetField.__proto__ || Object.getPrototypeOf(DataSheetField), "properties", this), ["value", "defaultValue", "headers", "min", "max", "rowControlRemove", "rowControlMove", "rowControlDuplicate", "rowControlAdd", "clipContent", "returnEmptyRows", "buttonLabel"]); } }]); function DataSheetField(fieldData, store, path) {_classCallCheck(this, DataSheetField);var _this = _possibleConstructorReturn(this, (DataSheetField.__proto__ || Object.getPrototypeOf(DataSheetField)).call(this, fieldData, store, path));_initDefineProp(_this, "value", _descriptor, _this);_initDefineProp(_this, "defaultValue", _descriptor2, _this);_initDefineProp(_this, "headers", _descriptor3, _this);_initDefineProp(_this, "min", _descriptor4, _this);_initDefineProp(_this, "max", _descriptor5, _this);_initDefineProp(_this, "rowControlRemove", _descriptor6, _this);_initDefineProp(_this, "rowControlMove", _descriptor7, _this);_initDefineProp(_this, "rowControlDuplicate", _descriptor8, _this);_initDefineProp(_this, "rowControlAdd", _descriptor9, _this);_initDefineProp(_this, "clipContent", _descriptor10, _this);_initDefineProp(_this, "returnEmptyRows", _descriptor11, _this);_initDefineProp(_this, "buttonLabel", _descriptor12, _this);_this.__emptyValue = function () {return [];}; _this.injectValue(_this.value);return _this; }_createClass(DataSheetField, [{ key: "getValue", value: function getValue() {var applyMapping = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true; var result = toJS(this.value); var newRow = {}; this.headers.forEach(function (header) { newRow[header.key] = header.defaultValue !== undefined ? header.defaultValue : ""; }); if (!this.returnEmptyRows) { result = result.filter(function (row) {return !isEqual(row, newRow);}); } return applyMapping ? this.mapReturnValue(result) : result; } }, { key: "injectValue", value: function injectValue( value) {var _this2 = this; if (value !== undefined) { this.registerProvidedValue(value, true); } this.value = this.__emptyValue(); var providedValue = this.getProvidedValue(); providedValue.forEach(function (value) { if (!value || _this2.value.length >= _this2.max) { return; } var newRow = {}; _this2.headers.forEach(function (header) { if (value[header.key] !== undefined) { newRow[header.key] = value[header.key]; } else { newRow[header.key] = header.defaultValue !== undefined ? header.defaultValue : ""; } }); _this2.value.push(newRow); }); while (this.value.length < this.min) { this.addRow(); } } }, { key: "applyChanges", value: function applyChanges( changes, outOfScopeChanges) {var _this3 = this; changes.forEach(function (change) { change.cell.row[change.cell.key] = change.value; }); if (isArray(outOfScopeChanges)) { outOfScopeChanges.forEach(function (change) { if (change.row > _this3.value.length - 1 && _this3.value.length < _this3.max) { _this3.addRow(); } var colValue = _this3.headers.filter(function (header) {return header.show !== false;})[change.col]; if (colValue !== undefined && colValue.key !== undefined && colValue.readOnly !== true && change.row < _this3.value.length) { _this3.value[change.row][colValue.key] = change.value; } }); } } }, { key: "addRow", value: function addRow( index) { var newRow = {}; this.headers.forEach(function (header) { newRow[header.key] = header.defaultValue !== undefined ? header.defaultValue : ""; }); if (index === undefined) { this.value.push(newRow); } else { this.value.splice(index, 0, newRow); } } }, { key: "removeRow", value: function removeRow( row) { remove(this.value, function (value) {return value === row;}); } }, { key: "moveRow", value: function moveRow( index, newIndex) { if (newIndex < index) { var removedRow = this.value.splice(index, 1); this.value.splice(newIndex, 0, removedRow[0]); } else if (newIndex > index) { this.value.splice(newIndex + 1, 0, this.value[index]); this.value.splice(index, 1); } } }, { key: "duplicateRow", value: function duplicateRow( index) { var newRow = toJS(this.value[index]); this.headers.forEach(function (header) { if (header.duplicatedValue !== undefined) { newRow[header.key] = header.duplicatedValue; } }); this.value.splice(index + 1, 0, newRow); } }]);return DataSheetField;}(DefaultField), (_descriptor = _applyDecoratedDescriptor(_class.prototype, "value", [observable], { enumerable: true, initializer: function initializer() {return [];} }), _descriptor2 = _applyDecoratedDescriptor(_class.prototype, "defaultValue", [observable], { enumerable: true, initializer: function initializer() {return [];} }), _descriptor3 = _applyDecoratedDescriptor(_class.prototype, "headers", [observable], { enumerable: true, initializer: function initializer() {return [];} }), _descriptor4 = _applyDecoratedDescriptor(_class.prototype, "min", [observable], { enumerable: true, initializer: function initializer() {return 0;} }), _descriptor5 = _applyDecoratedDescriptor(_class.prototype, "max", [observable], { enumerable: true, initializer: function initializer() {return Infinity;} }), _descriptor6 = _applyDecoratedDescriptor(_class.prototype, "rowControlRemove", [observable], { enumerable: true, initializer: function initializer() {return true;} }), _descriptor7 = _applyDecoratedDescriptor(_class.prototype, "rowControlMove", [observable], { enumerable: true, initializer: function initializer() {return true;} }), _descriptor8 = _applyDecoratedDescriptor(_class.prototype, "rowControlDuplicate", [observable], { enumerable: true, initializer: function initializer() {return true;} }), _descriptor9 = _applyDecoratedDescriptor(_class.prototype, "rowControlAdd", [observable], { enumerable: true, initializer: function initializer() {return true;} }), _descriptor10 = _applyDecoratedDescriptor(_class.prototype, "clipContent", [observable], { enumerable: true, initializer: function initializer() {return false;} }), _descriptor11 = _applyDecoratedDescriptor(_class.prototype, "returnEmptyRows", [observable], { enumerable: true, initializer: function initializer() {return false;} }), _descriptor12 = _applyDecoratedDescriptor(_class.prototype, "buttonLabel", [observable], { enumerable: true, initializer: function initializer() {return "Add a row";} }), _applyDecoratedDescriptor(_class.prototype, "injectValue", [action], Object.getOwnPropertyDescriptor(_class.prototype, "injectValue"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "applyChanges", [action], Object.getOwnPropertyDescriptor(_class.prototype, "applyChanges"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "addRow", [action], Object.getOwnPropertyDescriptor(_class.prototype, "addRow"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "removeRow", [action], Object.getOwnPropertyDescriptor(_class.prototype, "removeRow"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "moveRow", [action], Object.getOwnPropertyDescriptor(_class.prototype, "moveRow"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "duplicateRow", [action], Object.getOwnPropertyDescriptor(_class.prototype, "duplicateRow"), _class.prototype)), _class);export { DataSheetField as default };