@attivio/suit
Version:
Attivio SUIT, the Search UI Toolkit, is a library for creating search clients for searching the Attivio platform.
160 lines (123 loc) • 6.28 kB
JavaScript
;
exports.__esModule = true;
exports.default = undefined;
var _extends = Object.assign || 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; };
var _class, _temp;
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
var _FieldNames = require('../api/FieldNames');
var _FieldNames2 = _interopRequireDefault(_FieldNames);
var _DebugSearchResult = require('./DebugSearchResult');
var _DebugSearchResult2 = _interopRequireDefault(_DebugSearchResult);
var _ListSearchResult = require('./ListSearchResult');
var _ListSearchResult2 = _interopRequireDefault(_ListSearchResult);
var _SimpleSearchResult = require('./SimpleSearchResult');
var _SimpleSearchResult2 = _interopRequireDefault(_SimpleSearchResult);
var _SearchDocument = require('../api/SearchDocument');
var _SearchDocument2 = _interopRequireDefault(_SearchDocument);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
/**
* A container for showing a list of documents from the search results.
* This comes from the parent Searcher component.
*/
/**
* This is the definition of a search result renderer. It is passed the document
* for the result, the document's position in the search results, and a base URI
* that can be used when linking, etc. It should return either a React component
* that will be rendered for the document (the component returned should have its
* key set to the key parameter) or null if the document shouldn't be
* rendered by this function. If no SearchResultRenderer functions handle the
* rendering, then the document will be rendered by the 'list' renderer.
*/
var SearchResults = (_temp = _class = function (_React$Component) {
_inherits(SearchResults, _React$Component);
function SearchResults() {
_classCallCheck(this, SearchResults);
return _possibleConstructorReturn(this, _React$Component.apply(this, arguments));
}
SearchResults.prototype.renderResults = function renderResults() {
var _props = this.props,
baseUri = _props.baseUri,
format = _props.format,
hide360Link = _props.hide360Link;
var _context$searcher = this.context.searcher,
searcher = _context$searcher === undefined ? null : _context$searcher;
var response = searcher && searcher.state ? searcher.state.response : null;
var offset = searcher && searcher.state ? searcher.state.resultsOffset : null;
var formatRenderers = [];
if (searcher && searcher.state && searcher.state.debug) {
// If the searcher is overriding with the debug flag...
formatRenderers = [_DebugSearchResult2.default.renderer];
} else if (typeof format === 'function') {
formatRenderers = [format];
} else if (Array.isArray(format)) {
formatRenderers = format;
} else if (format === 'list') {
// 'list' -> ListSearchResult
formatRenderers = [_ListSearchResult2.default.renderer];
} else if (format === 'simple') {
// 'simple' -> SimpleSearchResult
formatRenderers = [_SimpleSearchResult2.default.renderer];
} else if (format === 'debug') {
// 'debug' -> DebugSearchResult
formatRenderers = [_DebugSearchResult2.default.renderer];
}
if (response && response.documents && response.documents.length > 0) {
var documents = response.documents;
var results = [];
documents.forEach(function (document, index) {
var key = document.getFirstValue(_FieldNames2.default.ID);
var position = offset + index + 1;
var renderedDocument = null;
// Look through the format array and see if one of the functions
// will render a result.
formatRenderers.forEach(function (formatRenderer) {
if (!renderedDocument) {
var possibleResult = formatRenderer(document, position, baseUri, key, hide360Link);
if (possibleResult) {
renderedDocument = possibleResult;
}
}
});
if (!renderedDocument) {
// Default to the List renderer if nothing else did anything... It will always produce a result.
renderedDocument = _ListSearchResult2.default.renderer(document, position, baseUri, key, hide360Link);
}
results.push(renderedDocument);
});
return results;
}
return null;
};
SearchResults.prototype.render = function render() {
var _props$style = this.props.style,
baseStyle = _props$style === undefined ? {} : _props$style;
var style = _extends({}, baseStyle, {
listStyle: 'none',
paddingLeft: 0
});
return _react2.default.createElement(
'ul',
{ style: style },
this.renderResults()
);
};
return SearchResults;
}(_react2.default.Component), _class.defaultProps = {
baseUri: '',
format: 'list',
showScores: false,
showTags: true,
showRatings: true,
hide360Link: false,
style: {}
}, _class.contextTypes = {
searcher: _propTypes2.default.any
}, _class.displayName = 'SearchResults', _temp);
exports.default = SearchResults;
module.exports = exports['default'];