UNPKG

@attivio/suit

Version:

Attivio SUIT, the Search UI Toolkit, is a library for creating search clients for searching the Attivio platform.

274 lines (241 loc) 9.57 kB
var _class, _temp; 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; } import React from 'react'; import PropTypes from 'prop-types'; import Row from 'react-bootstrap/lib/Row'; import Col from 'react-bootstrap/lib/Col'; import SearchDocument from '../api/SearchDocument'; import FieldNames from '../api/FieldNames'; import Configurable from './Configurable'; import Configuration from './Configuration'; import DocumentEntityList from './DocumentEntityList'; import DocumentThumbnail from './DocumentThumbnail'; import DocumentType from './DocumentType'; import RelevancyScore from './RelevancyScore'; import SearchResultBody from './SearchResultBody'; import SearchResultTags from './SearchResultTags'; import SearchResultTitle from './SearchResultTitle'; import Signals from '../api/Signals'; import StarRating from './StarRating'; import TabPanel, { TabInfo } from './TabPanel'; /** * An individual List-type search result. */ var InnerListSearchResult = (_temp = _class = function (_React$Component) { _inherits(InnerListSearchResult, _React$Component); /** * Renders a <ListSearchResult> component for the document. */ InnerListSearchResult.renderer = function renderer(doc, position, baseUri, key) { var hide360Link = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false; return React.createElement(ListSearchResult, { document: doc, position: position, baseUri: baseUri, key: key, hide360Link: hide360Link }); }; // eslint-disable-line max-len InnerListSearchResult.getFirstDocumentType = function getFirstDocumentType(list) { var result = ''; if (list && list.length > 0) { result = list[0].getFirstValue('table'); } return result; }; InnerListSearchResult.valueToDisplay = function valueToDisplay(value) { if (typeof value === 'string') { return value; } var json = JSON.stringify(value, null, 2); if (json.startsWith('{')) { return React.createElement( 'pre', null, json ); } return React.createElement( 'span', null, json ); }; function InnerListSearchResult(props) { _classCallCheck(this, InnerListSearchResult); var _this = _possibleConstructorReturn(this, _React$Component.call(this, props)); _this.tabChanged = function (newTab) { _this.setState({ currentTab: newTab }); }; _this.rateDocument = function (doc, rating) { if (doc.signal) { new Signals(_this.props.baseUri).addSignal(doc, 'like', rating); } }; _this.state = { currentTab: InnerListSearchResult.getFirstDocumentType(props.document.children) }; return _this; } // FIXME: Avoid expensive operations in render method. InnerListSearchResult.prototype.renderNestedDocs = function renderNestedDocs() { var _props = this.props, doc = _props.document, baseUri = _props.baseUri; if (doc.children && doc.children.length > 0) { var childMap = new Map(); doc.children.forEach(function (child) { var childTable = child.getFirstValue('table'); var tableDocs = childMap.get(childTable); if (tableDocs) { tableDocs.push(child); } else { var newTableDocs = [child]; childMap.set(childTable, newTableDocs); } }); var tabInfos = []; childMap.forEach(function (tableDocs, tabTable) { var label = tableDocs.length === 1 ? '1 ' + tabTable : tableDocs.length + ' ' + tabTable; var docResults = tableDocs.map(function (tableDoc, index) { var childPosition = index + 1; return React.createElement(ListSearchResult, { document: tableDoc, key: tableDoc.getFirstValue('.id'), position: childPosition, baseUri: baseUri }); }); var docResultsList = React.createElement( 'div', { className: 'attivio-nested-search-results' }, docResults ); tabInfos.push(new TabInfo(label, tabTable, docResultsList)); }); var tabLabel = doc.children.length === 1 ? 'One Child Record:' : doc.children.length + ' Child Records:'; return React.createElement(TabPanel, { tabInfos: tabInfos, activeTabId: this.state.currentTab, tabChanged: this.tabChanged, tabLabel: tabLabel, nested: true }); } return null; }; InnerListSearchResult.prototype.render = function render() { var _this2 = this; var _props2 = this.props, baseUri = _props2.baseUri, doc = _props2.document, entityFields = _props2.entityFields, position = _props2.position, showRatings = _props2.showRatings, showScores = _props2.showScores, showTags = _props2.showTags, hide360Link = _props2.hide360Link; var _context$configuratio = this.context.configuration, configuration = _context$configuratio === undefined ? null : _context$configuratio; var docId = doc.getFirstValue('.id'); var table = doc.getFirstValue('table'); var thumbnailUri = doc.getFirstValue('thumbnailImageUri'); var previewUri = doc.getAllValues('previewImageUri'); var scoreString = doc.getFirstValue(FieldNames.SCORE); var score = scoreString ? parseFloat(scoreString) : 0; var scoreDescription = doc.getFirstValue(FieldNames.SCORE_EXPLAIN); var text = doc.getFirstValue('teaser'); var moreLikeThisQuery = doc.getFirstValue('morelikethisquery'); var docTags = doc.getAllValues('tags'); var userDefinedEntityFieldsExist = configuration && configuration.state && configuration.state.ALL && configuration.state.ALL.entityFields; var userEntityFields = userDefinedEntityFieldsExist ? configuration.state.ALL.entityFields : entityFields; return React.createElement( 'div', { className: ' attivio-search-result' }, React.createElement( 'div', { className: 'attivio-search-result-col' }, React.createElement(DocumentType, { docType: table, position: position }), React.createElement(DocumentThumbnail, { uri: thumbnailUri, previewUris: previewUri, previewTitle: doc.getFirstValue(FieldNames.TITLE) }), React.createElement( 'dl', { className: 'attivio-labeldata-stacked attivio-labeldata-stacked-search-results' }, showRatings && React.createElement( 'div', null, React.createElement( 'dt', null, 'Rating' ), React.createElement( 'dd', null, React.createElement(StarRating, { onRated: function onRated(rating) { _this2.rateDocument(doc, rating); } }) ) ), showScores && React.createElement( 'dt', null, 'Relevancy Score' ), showScores && React.createElement( 'dd', null, React.createElement(RelevancyScore, { score: score, explanation: scoreDescription, id: docId }) ) ) ), React.createElement( 'div', { className: 'attivio-search-result-content' }, React.createElement(SearchResultTitle, { doc: doc, baseUri: baseUri }), React.createElement( Row, null, React.createElement( Col, { xs: 7, sm: 7 }, React.createElement(SearchResultBody, { body: text }), showTags && React.createElement(SearchResultTags, { tags: docTags, moreLikeThisQuery: moreLikeThisQuery, docId: docId, hide360Link: hide360Link }) ), React.createElement( Col, { xs: 5, sm: 5 }, React.createElement(DocumentEntityList, { doc: doc, entityFields: userEntityFields }) ) ) ), this.renderNestedDocs() ); }; return InnerListSearchResult; }(React.Component), _class.defaultProps = { baseUri: '', format: 'list', showScores: false, entityFields: new Map([['people', 'People'], ['location', 'Locations'], ['company', 'Companies']]), showTags: true, showRatings: true, hide360Link: false }, _class.displayName = 'ListSearchResult', _class.contextTypes = { configuration: PropTypes.instanceOf(Configuration) }, _temp); var ListSearchResult = Configurable(InnerListSearchResult); ListSearchResult.renderer = InnerListSearchResult.renderer; export default ListSearchResult;