UNPKG

cspace-ui

Version:
284 lines (238 loc) 8.44 kB
"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 _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } } const SearchForm = (0, _reactIntl.injectIntl)(_SearchForm.default); const messages = (0, _reactIntl.defineMessages)({ title: { "id": "searchPage.title", "defaultMessage": "Search" } }); const propTypes = { recordTypeValue: _propTypes.default.string, vocabularyValue: _propTypes.default.string, keywordValue: _propTypes.default.string, advancedSearchCondition: _propTypes.default.object, history: _propTypes.default.object, location: _propTypes.default.object, match: _propTypes.default.object, perms: _propTypes.default.instanceOf(_immutable.default.Map), preferredAdvancedSearchBooleanOp: _propTypes.default.string, getAuthorityVocabCsid: _propTypes.default.func, onAdvancedSearchConditionCommit: _propTypes.default.func, onClearButtonClick: _propTypes.default.func, onKeywordCommit: _propTypes.default.func, onRecordTypeCommit: _propTypes.default.func, onVocabularyCommit: _propTypes.default.func, onSearch: _propTypes.default.func }; const contextTypes = { config: _propTypes.default.object.isRequired }; class SearchPage extends _react.Component { constructor() { super(); this.handleRecordTypeCommit = this.handleRecordTypeCommit.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 { params } = this.props.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); } } } } } getSearchDescriptor() { const { params } = this.props.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 ? "/".concat(vocabulary) : ''; const normalizedPath = "/search/".concat(recordType).concat(vocabularyPath); if (normalizedPath !== location.pathname) { history.replace({ pathname: normalizedPath }); return true; } } return false; } handleRecordTypeCommit(value) { const { history, onRecordTypeCommit } = this.props; if (onRecordTypeCommit) { onRecordTypeCommit(value); } history.replace({ pathname: "/search/".concat(value) }); } 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/".concat(recordType, "/").concat(value) }); } render() { const { advancedSearchCondition, keywordValue, perms, preferredAdvancedSearchBooleanOp, getAuthorityVocabCsid, onAdvancedSearchConditionCommit, onClearButtonClick, onKeywordCommit, onSearch } = 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 _react.default.createElement(_ErrorPage.default, { error: validation.error }); } const title = _react.default.createElement(_reactIntl.FormattedMessage, messages.title); return _react.default.createElement("div", { className: _SearchPage.default.common }, _react.default.createElement(_TitleBar.default, { title: title, updateDocumentTitle: true, onDocked: this.handleTitleBarDocked }), _react.default.createElement("div", { className: _PageBody.default.common }, _react.default.createElement(SearchForm, { advancedSearchCondition: advancedSearchCondition, config: config, dockTop: headerDockPosition, keywordValue: keywordValue, recordTypeValue: recordType, vocabularyValue: vocabulary, perms: perms, preferredAdvancedSearchBooleanOp: preferredAdvancedSearchBooleanOp, showButtons: true, getAuthorityVocabCsid: getAuthorityVocabCsid, onAdvancedSearchConditionCommit: onAdvancedSearchConditionCommit, onClearButtonClick: onClearButtonClick, onKeywordCommit: onKeywordCommit, onRecordTypeCommit: this.handleRecordTypeCommit, onVocabularyCommit: this.handleVocabularyCommit, onSearch: onSearch }))); } } exports.default = SearchPage; SearchPage.propTypes = propTypes; SearchPage.contextTypes = contextTypes;