cspace-ui
Version:
CollectionSpace user interface for browsers
266 lines (262 loc) • 9.31 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _reactIntl = require("react-intl");
var _immutable = _interopRequireDefault(require("immutable"));
var _get = _interopRequireDefault(require("lodash/get"));
var _RemoveConditionButton = _interopRequireDefault(require("../RemoveConditionButton"));
var _GroupInput = _interopRequireDefault(require("./GroupInput"));
var _GroupConditionInput = _interopRequireDefault(require("../../../../styles/cspace-ui/GroupConditionInput.css"));
var _searchOperators = require("../../../constants/searchOperators");
var _configHelpers = require("../../../helpers/configHelpers");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
const propTypes = {
condition: _propTypes.default.instanceOf(_immutable.default.Map),
config: _propTypes.default.shape({
recordTypes: _propTypes.default.object
}),
hasChildGroups: _propTypes.default.bool,
inline: _propTypes.default.bool,
name: _propTypes.default.string,
readOnly: _propTypes.default.bool,
recordType: _propTypes.default.string,
rootPath: _propTypes.default.string,
getSearchConditionInputComponent: _propTypes.default.func.isRequired,
buildRecordFieldOptionLists: _propTypes.default.func,
deleteOptionList: _propTypes.default.func,
onCommit: _propTypes.default.func,
onRemove: _propTypes.default.func
};
const messages = {
groupInput: (0, _reactIntl.defineMessages)({
pendingLabel: {
"id": "GroupConditionInput.groupInput.pendingLabel",
"defaultMessage": "In a single {groupInput} ..."
},
selectedLabel: {
"id": "GroupConditionInput.groupInput.selectedLabel",
"defaultMessage": "In a single {groupInput} group:"
},
compactLabel: {
"id": "GroupConditionInput.groupInput.compactLabel",
"defaultMessage": "In {groupInput}:"
}
})
};
class GroupConditionInput extends _react.Component {
constructor() {
super();
this.handleChildConditionCommit = this.handleChildConditionCommit.bind(this);
this.handleRef = this.handleRef.bind(this);
this.handleRemoveButtonClick = this.handleRemoveButtonClick.bind(this);
this.handleGroupCommit = this.handleGroupCommit.bind(this);
}
componentDidMount() {
const {
buildRecordFieldOptionLists,
condition,
config,
recordType
} = this.props;
const path = condition.get('path');
if (path === null) {
// The condition was just added, and the group needs to be selected. Focus it.
if (this.domNode) {
const input = this.domNode.querySelector('input[data-name="group"]');
if (input) {
input.focus();
}
}
} else if (buildRecordFieldOptionLists) {
buildRecordFieldOptionLists(config, recordType, path, true);
}
}
componentDidUpdate(prevProps) {
const {
condition,
buildRecordFieldOptionLists,
config,
recordType
} = this.props;
const {
condition: prevCondition
} = prevProps;
const path = condition.get('path');
const prevPath = prevCondition.get('path');
if (path !== prevPath) {
// The group changed. Load the field options for the group.
if (buildRecordFieldOptionLists) {
buildRecordFieldOptionLists(config, recordType, path, true);
}
}
}
componentWillUnmount() {
const {
deleteOptionList,
condition,
recordType
} = this.props;
const path = condition.get('path');
if (path && deleteOptionList) {
deleteOptionList((0, _configHelpers.getRecordFieldOptionListName)(recordType, path));
deleteOptionList((0, _configHelpers.getRecordGroupOptionListName)(recordType, path));
}
}
handleChildConditionCommit(childName, childCondition) {
const {
condition,
name,
onCommit
} = this.props;
if (onCommit) {
onCommit(name, condition.set('value', childCondition));
}
}
handleGroupCommit(path, groupPath) {
const {
condition,
name,
onCommit
} = this.props;
if (onCommit) {
// Reset the current value, since child conditions may not be valid for the new field.
onCommit(name, condition.set('path', groupPath).set('value', _immutable.default.fromJS({
op: _searchOperators.OP_AND,
value: [{
path: null
}]
})));
}
}
handleRef(ref) {
this.domNode = ref;
}
handleRemoveButtonClick() {
const {
name,
onRemove
} = this.props;
if (onRemove) {
onRemove(name);
}
}
renderRemoveButton() {
const {
readOnly
} = this.props;
if (readOnly) {
return null;
}
return /*#__PURE__*/_react.default.createElement(_RemoveConditionButton.default, {
onClick: this.handleRemoveButtonClick
});
}
renderGroupInput() {
const {
condition,
config,
inline,
recordType,
rootPath
} = this.props;
const pathSpec = condition.get('path');
if (!pathSpec) {
if (inline) {
return null;
}
return /*#__PURE__*/_react.default.createElement(_GroupInput.default, {
config: config,
inline: inline,
name: "group",
placeholder: "group",
recordType: recordType,
rootPath: rootPath,
onCommit: this.handleGroupCommit
});
}
const path = ['document', ...pathSpec.split('/')];
const fieldDescriptor = (0, _get.default)(config, ['recordTypes', recordType, 'fields', ...path]);
return /*#__PURE__*/_react.default.createElement(_GroupInput.default, {
config: config,
inline: inline,
readOnly: true,
recordType: recordType,
rootPath: rootPath,
value: pathSpec,
valueDescriptor: fieldDescriptor,
onCommit: this.handleGroupCommit
});
}
renderHeader() {
const {
condition,
inline
} = this.props;
if (inline) {
const groupInput = this.renderGroupInput();
return /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.groupInput.compactLabel, {
tagName: "div",
values: {
groupInput
}
}));
}
const groupInput = this.renderGroupInput();
const labelMessage = condition.get('path') === null ? messages.groupInput.pendingLabel : messages.groupInput.selectedLabel;
return /*#__PURE__*/_react.default.createElement("header", null, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, _extends({}, labelMessage, {
tagName: "div",
values: {
groupInput
}
})), this.renderRemoveButton());
}
renderChildConditions() {
const {
condition,
config,
hasChildGroups,
inline,
readOnly,
recordType,
getSearchConditionInputComponent
} = this.props;
const childCondition = condition.get('value');
if (!childCondition) {
return null;
}
const path = condition.get('path');
const SearchConditionInputComponent = getSearchConditionInputComponent(childCondition);
return /*#__PURE__*/_react.default.createElement(SearchConditionInputComponent, {
condition: childCondition,
config: config,
hasChildGroups: hasChildGroups,
inline: inline,
name: "groupConditions",
readOnly: readOnly,
recordType: recordType,
rootPath: path,
showRemoveButton: false,
getSearchConditionInputComponent: getSearchConditionInputComponent,
onCommit: this.handleChildConditionCommit
});
}
render() {
const {
inline
} = this.props;
const className = inline ? _GroupConditionInput.default.inline : _GroupConditionInput.default.normal;
return /*#__PURE__*/_react.default.createElement("div", {
className: className,
ref: this.handleRef
}, this.renderHeader(), inline ? ' ' : null, this.renderChildConditions());
}
}
exports.default = GroupConditionInput;
GroupConditionInput.propTypes = propTypes;