cspace-ui
Version:
CollectionSpace user interface for browsers
285 lines (283 loc) • 9.89 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 _get = _interopRequireDefault(require("lodash/get"));
var _immutable = _interopRequireDefault(require("immutable"));
var _ErrorPage = _interopRequireDefault(require("./ErrorPage"));
var _SearchForm = _interopRequireDefault(require("../search/SearchForm"));
var _TitleBar = _interopRequireDefault(require("../sections/TitleBar"));
var _configHelpers = require("../../helpers/configHelpers");
var _SearchPage = _interopRequireDefault(require("../../../styles/cspace-ui/SearchPage.css"));
var _PageBody = _interopRequireDefault(require("../../../styles/cspace-ui/PageBody.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; }
const SearchForm = (0, _reactIntl.injectIntl)(_SearchForm.default);
const messages = (0, _reactIntl.defineMessages)({
title: {
"id": "searchPage.title",
"defaultMessage": "Search"
}
});
const propTypes = {
// FIXME: Why is config both a prop and in context?
config: _propTypes.default.shape({
recordTypes: _propTypes.default.object
}),
recordTypeValue: _propTypes.default.string,
vocabularyValue: _propTypes.default.string,
keywordValue: _propTypes.default.string,
advancedSearchCondition: _propTypes.default.instanceOf(_immutable.default.Map),
history: _propTypes.default.shape({
push: _propTypes.default.func,
replace: _propTypes.default.func
}),
location: _propTypes.default.shape({
pathname: _propTypes.default.string
}),
match: _propTypes.default.shape({
params: _propTypes.default.object
}),
perms: _propTypes.default.instanceOf(_immutable.default.Map),
preferredAdvancedSearchBooleanOp: _propTypes.default.string,
getAuthorityVocabCsid: _propTypes.default.func,
buildRecordFieldOptionLists: _propTypes.default.func,
clearSearchPage: _propTypes.default.func,
deleteOptionList: _propTypes.default.func,
initiateSearch: _propTypes.default.func,
onAdvancedSearchConditionCommit: _propTypes.default.func,
onClearButtonClick: _propTypes.default.func,
onKeywordCommit: _propTypes.default.func,
onRecordTypeCommit: _propTypes.default.func,
onVocabularyCommit: _propTypes.default.func
};
const contextTypes = {
config: _propTypes.default.shape({
recordTypes: _propTypes.default.object
}).isRequired
};
class SearchPage extends _react.Component {
constructor() {
super();
this.handleRecordTypeCommit = this.handleRecordTypeCommit.bind(this);
this.handleSearch = this.handleSearch.bind(this);
this.handleVocabularyCommit = this.handleVocabularyCommit.bind(this);
this.handleTitleBarDocked = this.handleTitleBarDocked.bind(this);
this.state = {
headerDockPosition: null
};
}
componentDidMount() {
this.normalizePath();
}
componentDidUpdate(prevProps) {
let historyChanged = false;
const {
match
} = this.props;
const {
params
} = match;
const {
params: prevParams
} = prevProps.match;
if (params.recordType !== prevParams.recordType || params.vocabulary !== prevParams.vocabulary) {
historyChanged = this.normalizePath();
}
if (!historyChanged) {
// If the record type and/or vocab were changed via URL or by the selection of a default,
// commit the new values.
const searchDescriptor = this.getSearchDescriptor();
const recordType = searchDescriptor.get('recordType');
const vocabulary = searchDescriptor.get('vocabulary');
const {
recordTypeValue,
vocabularyValue,
onRecordTypeCommit,
onVocabularyCommit
} = this.props;
if (recordType !== recordTypeValue || vocabulary !== vocabularyValue) {
if (onRecordTypeCommit) {
onRecordTypeCommit(recordType);
}
if (onVocabularyCommit) {
const {
config
} = this.context;
const recordTypeConfig = (0, _get.default)(config, ['recordTypes', recordType]);
if ((0, _configHelpers.isAuthority)(recordTypeConfig)) {
onVocabularyCommit(vocabulary);
}
}
}
}
}
componentWillUnmount() {
const {
clearSearchPage
} = this.props;
if (clearSearchPage) {
clearSearchPage();
}
}
handleRecordTypeCommit(value) {
const {
history,
onRecordTypeCommit
} = this.props;
if (onRecordTypeCommit) {
onRecordTypeCommit(value);
}
history.replace({
pathname: `/search/${value}`
});
}
handleSearch() {
const {
config,
history,
initiateSearch
} = this.props;
if (initiateSearch) {
initiateSearch(config, history.push);
}
}
handleTitleBarDocked(height) {
this.setState({
headerDockPosition: height
});
}
handleVocabularyCommit(value) {
const {
history,
onVocabularyCommit
} = this.props;
if (onVocabularyCommit) {
onVocabularyCommit(value);
}
const searchDescriptor = this.getSearchDescriptor();
const recordType = searchDescriptor.get('recordType');
history.replace({
pathname: `/search/${recordType}/${value}`
});
}
getSearchDescriptor() {
const {
match
} = this.props;
const {
params
} = match;
const searchDescriptor = {};
['recordType', 'vocabulary'].forEach(param => {
const value = params[param];
if (typeof value !== 'undefined') {
searchDescriptor[param] = value;
}
});
return _immutable.default.fromJS(searchDescriptor);
}
normalizePath() {
const {
recordTypeValue,
vocabularyValue,
history,
location,
match
} = this.props;
const {
config
} = this.context;
if (history) {
let {
recordType,
vocabulary
} = match.params;
if (!recordType) {
recordType = recordTypeValue || (0, _configHelpers.getDefaultSearchRecordType)(config);
}
const recordTypeConfig = (0, _get.default)(config, ['recordTypes', recordType]);
if ((0, _configHelpers.isAuthority)(recordTypeConfig) && !vocabulary) {
vocabulary = vocabularyValue || (0, _configHelpers.getDefaultSearchVocabulary)(recordTypeConfig);
}
const vocabularyPath = vocabulary ? `/${vocabulary}` : '';
const normalizedPath = `/search/${recordType}${vocabularyPath}`;
if (normalizedPath !== location.pathname) {
history.replace({
pathname: normalizedPath
});
return true;
}
}
return false;
}
render() {
const {
advancedSearchCondition,
keywordValue,
perms,
preferredAdvancedSearchBooleanOp,
getAuthorityVocabCsid,
buildRecordFieldOptionLists,
deleteOptionList,
onAdvancedSearchConditionCommit,
onClearButtonClick,
onKeywordCommit
} = this.props;
const {
headerDockPosition
} = this.state;
const {
config
} = this.context;
const searchDescriptor = this.getSearchDescriptor();
const recordType = searchDescriptor.get('recordType');
const vocabulary = searchDescriptor.get('vocabulary');
const validation = (0, _configHelpers.validateLocation)(config, {
recordType,
vocabulary
});
if (validation.error) {
return /*#__PURE__*/_react.default.createElement(_ErrorPage.default, {
error: validation.error
});
}
const title = /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, messages.title);
return /*#__PURE__*/_react.default.createElement("div", {
className: _SearchPage.default.common
}, /*#__PURE__*/_react.default.createElement(_TitleBar.default, {
title: title,
updateDocumentTitle: true,
onDocked: this.handleTitleBarDocked
}), /*#__PURE__*/_react.default.createElement("div", {
className: _PageBody.default.common
}, /*#__PURE__*/_react.default.createElement(SearchForm, {
advancedSearchCondition: advancedSearchCondition,
config: config,
dockTop: headerDockPosition,
keywordValue: keywordValue,
recordTypeValue: recordType,
vocabularyValue: vocabulary,
perms: perms,
preferredAdvancedSearchBooleanOp: preferredAdvancedSearchBooleanOp,
showButtons: true,
getAuthorityVocabCsid: getAuthorityVocabCsid,
buildRecordFieldOptionLists: buildRecordFieldOptionLists,
deleteOptionList: deleteOptionList,
onAdvancedSearchConditionCommit: onAdvancedSearchConditionCommit,
onClearButtonClick: onClearButtonClick,
onKeywordCommit: onKeywordCommit,
onRecordTypeCommit: this.handleRecordTypeCommit,
onVocabularyCommit: this.handleVocabularyCommit,
onSearch: this.handleSearch
})));
}
}
exports.default = SearchPage;
SearchPage.propTypes = propTypes;
SearchPage.contextTypes = contextTypes;