UNPKG

qwc2-lts

Version:
188 lines (187 loc) 9.39 kB
var _KeyValCache; function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); } function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); } function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); } function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } 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); } /** * Copyright 2024 Sourcepole AG * All rights reserved. * * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. */ import React from 'react'; import PropTypes from 'prop-types'; import { v5 as uuidv5 } from 'uuid'; import LocaleUtils from '../utils/LocaleUtils'; var UUID_NS = '5ae5531d-8e21-4456-b45d-77e9840a5bb7'; export var KeyValCache = /*#__PURE__*/_createClass(function KeyValCache() { _classCallCheck(this, KeyValCache); }); _KeyValCache = KeyValCache; _defineProperty(KeyValCache, "store", {}); _defineProperty(KeyValCache, "requests", {}); _defineProperty(KeyValCache, "get", function (editIface, keyvalrel, filterExpr, callback) { var key = keyvalrel + uuidv5(JSON.stringify(filterExpr !== null && filterExpr !== void 0 ? filterExpr : null), UUID_NS); if (key in _KeyValCache.store) { callback(_KeyValCache.store[key]); } else if (key in _KeyValCache.requests) { _KeyValCache.requests[key].push(callback); } else { _KeyValCache.requests[key] = [callback]; editIface.getKeyValues(keyvalrel, function (result) { if (key in _KeyValCache.requests) { var dataSet = keyvalrel.split(":")[0]; if (result.keyvalues && result.keyvalues[dataSet]) { var values = result.keyvalues[dataSet].map(function (entry) { return { value: entry.key, label: entry.value }; }); _KeyValCache.store[key] = values; } else { _KeyValCache.store[key] = []; } _KeyValCache.requests[key].forEach(function (cb) { return cb(_KeyValCache.store[key]); }); delete _KeyValCache.requests[key]; } }, filterExpr ? [filterExpr] : null); } }); _defineProperty(KeyValCache, "getSync", function (keyvalrel, filterExpr) { var key = keyvalrel + uuidv5(JSON.stringify(filterExpr !== null && filterExpr !== void 0 ? filterExpr : null), UUID_NS); if (key in _KeyValCache.store) { return _KeyValCache.store[key]; } else { return []; } }); _defineProperty(KeyValCache, "clear", function () { _KeyValCache.store = {}; _KeyValCache.requests = {}; }); var EditComboField = /*#__PURE__*/function (_React$Component) { function EditComboField() { var _this; _classCallCheck(this, EditComboField); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _callSuper(this, EditComboField, [].concat(args)); _defineProperty(_this, "state", { showPlaceholder: true, values: [] }); _defineProperty(_this, "hasEmptyValue", function (values) { for (var i = 0; i < values.length; ++i) { if (typeof values[i] === 'string') { if (values[i] === '') { return true; } } else if (values[i].value === '') { return true; } } return false; }); return _this; } _inherits(EditComboField, _React$Component); return _createClass(EditComboField, [{ key: "componentDidMount", value: function componentDidMount() { var _this2 = this; if (this.props.values) { // eslint-disable-next-line this.setState({ values: this.props.values, showPlaceholder: !this.hasEmptyValue(this.props.values) }); } else if (this.props.keyvalrel) { var _this$props$filterExp; KeyValCache.get(this.props.editIface, this.props.keyvalrel, (_this$props$filterExp = this.props.filterExpr) !== null && _this$props$filterExp !== void 0 ? _this$props$filterExp : null, function (values) { // eslint-disable-next-line _this2.setState({ values: values, showPlaceholder: !_this2.hasEmptyValue(values) }); }); } } }, { key: "componentDidUpdate", value: function componentDidUpdate(prevProps) { var _this3 = this; if (this.props.keyvalrel && this.props.filterExpr !== prevProps.filterExpr) { var _this$props$filterExp2; KeyValCache.get(this.props.editIface, this.props.keyvalrel, (_this$props$filterExp2 = this.props.filterExpr) !== null && _this$props$filterExp2 !== void 0 ? _this$props$filterExp2 : null, function (values) { // eslint-disable-next-line _this3.setState({ values: values, showPlaceholder: !_this3.hasEmptyValue(values) }); }); } } }, { key: "render", value: function render() { var _this4 = this, _this$props$placehold; return /*#__PURE__*/React.createElement("select", { disabled: this.props.readOnly, name: this.props.name, onChange: function onChange(ev) { return _this4.props.updateField(_this4.props.fieldId, ev.target.selectedIndex === 0 && _this4.state.showPlaceholder ? null : ev.target.value); }, required: this.props.required, style: this.props.style, value: String(this.props.value) }, this.state.showPlaceholder ? /*#__PURE__*/React.createElement("option", { disabled: this.props.required, value: "" }, (_this$props$placehold = this.props.placeholder) !== null && _this$props$placehold !== void 0 ? _this$props$placehold : LocaleUtils.tr("editing.select")) : null, this.state.values.map(function (item, index) { var optValue = ""; var label = ""; if (typeof item === 'string') { optValue = label = item; } else { optValue = item.value; label = item.label; } return /*#__PURE__*/React.createElement("option", { key: _this4.props.fieldId + index, value: String(optValue) }, label); })); } }]); }(React.Component); _defineProperty(EditComboField, "propTypes", { editIface: PropTypes.object, fieldId: PropTypes.string, filterExpr: PropTypes.array, keyvalrel: PropTypes.string, name: PropTypes.string, placeholder: PropTypes.string, readOnly: PropTypes.bool, required: PropTypes.bool, style: PropTypes.object, updateField: PropTypes.func, value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), values: PropTypes.array }); export { EditComboField as default };