cspace-ui
Version:
CollectionSpace user interface for browsers
343 lines (339 loc) • 12.5 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 _cspaceInput = require("cspace-input");
var _OptionPickerInput = _interopRequireDefault(require("../../record/OptionPickerInput"));
var _RemoveConditionButton = _interopRequireDefault(require("../RemoveConditionButton"));
var _searchOperators = require("../../../constants/searchOperators");
var _BooleanConditionInput = _interopRequireDefault(require("../../../../styles/cspace-ui/BooleanConditionInput.css"));
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,
showInlineParens: _propTypes.default.bool,
showRemoveButton: _propTypes.default.bool,
getSearchConditionInputComponent: _propTypes.default.func.isRequired,
onCommit: _propTypes.default.func,
onRemove: _propTypes.default.func
};
const defaultProps = {
showInlineParens: true,
showRemoveButton: true
};
const {
MiniButton
} = _cspaceInput.baseComponents;
const messages = {
[_searchOperators.OP_AND]: (0, _reactIntl.defineMessages)({
label: {
"id": "booleanConditionInput.and.label",
"defaultMessage": "and"
},
opSelectorLabel: {
"id": "booleanConditionInput.and.opSelectorLabel",
"defaultMessage": "All"
}
}),
[_searchOperators.OP_OR]: (0, _reactIntl.defineMessages)({
label: {
"id": "booleanConditionInput.or.label",
"defaultMessage": "or"
},
opSelectorLabel: {
"id": "booleanConditionInput.or.opSelectorLabel",
"defaultMessage": "Any"
}
}),
opSelector: (0, _reactIntl.defineMessages)({
label: {
"id": "booleanConditionInput.opSelector.label",
"defaultMessage": "{opSelectorInput} of the following conditions {operator, select, and {must} or {may}} be satisfied:"
}
}),
addBoolean: (0, _reactIntl.defineMessages)({
label: {
"id": "booleanConditionInput.addBoolean.label",
"defaultMessage": "+ Any/All"
}
}),
addField: (0, _reactIntl.defineMessages)({
label: {
"id": "booleanConditionInput.addField.label",
"defaultMessage": "+ Field"
}
}),
addGroup: (0, _reactIntl.defineMessages)({
label: {
"id": "booleanConditionInput.addGroup.label",
"defaultMessage": "+ Group"
}
})
};
class BooleanConditionInput extends _react.Component {
constructor() {
super();
this.handleAddBooleanButtonClick = this.handleAddBooleanButtonClick.bind(this);
this.handleAddFieldButtonClick = this.handleAddFieldButtonClick.bind(this);
this.handleAddGroupButtonClick = this.handleAddGroupButtonClick.bind(this);
this.handleChildConditionCommit = this.handleChildConditionCommit.bind(this);
this.handleChildConditionRemove = this.handleChildConditionRemove.bind(this);
this.handleOpSelectorCommit = this.handleOpSelectorCommit.bind(this);
this.handleRef = this.handleRef.bind(this);
this.handleRemoveButtonClick = this.handleRemoveButtonClick.bind(this);
}
componentDidMount() {
const {
condition
} = this.props;
const value = condition.get('value');
if (value === null) {
// The condition was just added, and the operator needs to be selected. Focus it.
if (this.domNode) {
const input = this.domNode.querySelector('input[data-name="booleanSearchOp"]');
if (input) {
input.focus();
}
}
}
}
handleAddBooleanButtonClick() {
const {
condition,
name,
onCommit
} = this.props;
if (onCommit) {
const op = condition.get('op');
const value = condition.get('value') || _immutable.default.List();
// For the new nested boolean, set the operator to the opposite of the current operator,
// since this is likely what the user wants.
const nestedOp = op === _searchOperators.OP_AND ? _searchOperators.OP_OR : _searchOperators.OP_AND;
const newCondition = _immutable.default.Map({
op: nestedOp,
path: null,
value: null
});
onCommit(name, condition.set('value', value.push(newCondition)));
}
}
handleAddFieldButtonClick() {
const {
condition,
name,
onCommit
} = this.props;
if (onCommit) {
const value = condition.get('value') || _immutable.default.List();
onCommit(name, condition.set('value', value.push(_immutable.default.Map({
path: null
}))));
}
}
handleAddGroupButtonClick() {
const {
condition,
name,
onCommit
} = this.props;
if (onCommit) {
const value = condition.get('value') || _immutable.default.List();
onCommit(name, condition.set('value', value.push(_immutable.default.Map({
op: _searchOperators.OP_GROUP,
path: null
}))));
}
}
handleOpSelectorCommit(path, value) {
const {
condition,
name,
onCommit
} = this.props;
if (onCommit) {
onCommit(name, condition.set('op', value));
}
}
handleChildConditionCommit(childName, childCondition) {
const {
condition,
name,
onCommit
} = this.props;
if (onCommit) {
const index = parseInt(childName, 10);
onCommit(name, condition.setIn(['value', index], childCondition));
}
}
handleChildConditionRemove(childName) {
const {
condition,
name,
onCommit
} = this.props;
if (onCommit) {
const index = parseInt(childName, 10);
onCommit(name, condition.deleteIn(['value', index]));
}
}
handleRef(ref) {
this.domNode = ref;
}
handleRemoveButtonClick() {
const {
name,
onRemove
} = this.props;
if (onRemove) {
onRemove(name);
}
}
renderRemoveButton() {
const {
readOnly,
showRemoveButton
} = this.props;
if (readOnly || !showRemoveButton) {
return null;
}
return /*#__PURE__*/_react.default.createElement(_RemoveConditionButton.default, {
onClick: this.handleRemoveButtonClick
});
}
renderHeader() {
const {
condition,
readOnly
} = this.props;
if (readOnly) {
return null;
}
const operator = condition.get('op');
const opSelectorInput = /*#__PURE__*/_react.default.createElement(_OptionPickerInput.default, {
blankable: false,
name: "booleanSearchOp",
options: [{
value: _searchOperators.OP_OR,
message: messages[_searchOperators.OP_OR].opSelectorLabel
}, {
value: _searchOperators.OP_AND,
message: messages[_searchOperators.OP_AND].opSelectorLabel
}],
value: operator,
onCommit: this.handleOpSelectorCommit
});
return /*#__PURE__*/_react.default.createElement("header", null, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.opSelector.label, {
tagName: "div",
values: {
opSelectorInput,
operator
}
})), this.renderRemoveButton());
}
renderChildConditions() {
const {
condition,
config,
hasChildGroups,
inline,
readOnly,
recordType,
rootPath,
getSearchConditionInputComponent
} = this.props;
const operator = condition.get('op');
const childConditions = condition.get('value');
if (!childConditions || childConditions.size === 0) {
return null;
}
const inputs = childConditions.map((childCondition, index) => {
const SearchConditionInputComponent = getSearchConditionInputComponent(childCondition);
const operatorLabel = index > 0 ? /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, messages[operator].label) : /*#__PURE__*/_react.default.createElement("span", null);
return (
/*#__PURE__*/
// eslint-disable-next-line react/no-array-index-key
_react.default.createElement("li", {
key: index
}, operatorLabel, inline ? ' ' : null, /*#__PURE__*/_react.default.createElement(SearchConditionInputComponent, {
condition: childCondition,
config: config,
hasChildGroups: hasChildGroups,
index: index,
inline: inline,
name: `${index}`,
readOnly: readOnly,
recordType: recordType,
rootPath: rootPath,
getSearchConditionInputComponent: getSearchConditionInputComponent,
onCommit: this.handleChildConditionCommit,
onRemove: this.handleChildConditionRemove
}), inline ? ' ' : null)
);
});
return /*#__PURE__*/_react.default.createElement("ul", null, inputs);
}
renderFooter() {
const {
condition,
hasChildGroups,
readOnly
} = this.props;
if (readOnly) {
return null;
}
const operator = condition.get('op');
let addGroupButton;
if (hasChildGroups) {
addGroupButton = /*#__PURE__*/_react.default.createElement(MiniButton, {
autoWidth: true,
name: "addGroup",
onClick: this.handleAddGroupButtonClick
}, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, messages.addGroup.label));
}
return /*#__PURE__*/_react.default.createElement("footer", null, /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, messages[operator].label)), /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement(MiniButton, {
autoWidth: true,
name: "addField",
onClick: this.handleAddFieldButtonClick
}, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, messages.addField.label)), addGroupButton, /*#__PURE__*/_react.default.createElement(MiniButton, {
autoWidth: true,
name: "addBoolean",
onClick: this.handleAddBooleanButtonClick
}, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, messages.addBoolean.label))));
}
render() {
const {
inline,
showInlineParens
} = this.props;
let openParen;
let closeParen;
if (inline && showInlineParens) {
openParen = /*#__PURE__*/_react.default.createElement("div", null, "(");
closeParen = /*#__PURE__*/_react.default.createElement("div", null, ")");
}
const className = inline ? _BooleanConditionInput.default.inline : _BooleanConditionInput.default.normal;
return /*#__PURE__*/_react.default.createElement("div", {
className: className,
ref: this.handleRef
}, this.renderHeader(), openParen, this.renderChildConditions(), closeParen, this.renderFooter());
}
}
exports.default = BooleanConditionInput;
BooleanConditionInput.propTypes = propTypes;
BooleanConditionInput.defaultProps = defaultProps;