@attivio/suit
Version:
Attivio SUIT, the Search UI Toolkit, is a library for creating search clients for searching the Attivio platform.
216 lines (189 loc) • 7.65 kB
JavaScript
var _class, _temp2;
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 Facet from './Facet';
import SearchFacet from '../api/SearchFacet';
import FacetFilter from '../api/FacetFilter';
import ObjectUtils from '../util/ObjectUtils';
/**
* A container for showing facet results from a search.
* It must be contained within a Searcher component and
* will obtain the list of facets from there. Via properties,
* you can specify how to display specific facets. Any facet
* not coveed by one of these property's lists will be displayed
* in a standard "More…" list.
*/
var FacetInsights = (_temp2 = _class = function (_React$Component) {
_inherits(FacetInsights, _React$Component);
function FacetInsights() {
var _temp, _this, _ret;
_classCallCheck(this, FacetInsights);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, _React$Component.call.apply(_React$Component, [this].concat(args))), _this), _this.filterFacetBuckets = function (facet, type, facetFiltersMap) {
if (type === 'tagcloud' || type === 'timeseries') {
return facet;
}
var filteredBuckets = facet.buckets.reduce(function (accumBuckets, currentBucket) {
return !facetFiltersMap.get(currentBucket.filter) ? [].concat(accumBuckets, [currentBucket]) : accumBuckets;
}, []);
return new SearchFacet(facet.name, facet.field, facet.label, facet.count, filteredBuckets, facet.statistics);
}, _temp), _possibleConstructorReturn(_this, _ret);
}
FacetInsights.matchesFacetList = function matchesFacetList(field, facetList) {
if (facetList) {
if (typeof facetList === 'string') {
return facetList === field;
}
return facetList.includes(field);
}
return false;
};
FacetInsights.prototype.componentDidMount = function componentDidMount() {
// If the user hasn't yet done a search, do a search for
// "*:*" so there will be something to display here. If the
// user has already searched, show things based on the current
// search results.
var searcher = this.context.searcher;
if (searcher && !searcher.state.haveSearched) {
searcher.doSearch();
}
};
FacetInsights.prototype.getFacetDisplayType = function getFacetDisplayType(field) {
if (FacetInsights.matchesFacetList(field, this.props.pieChartFacets)) {
return 'piechart';
}
if (FacetInsights.matchesFacetList(field, this.props.barChartFacets)) {
return 'barchart';
}
if (FacetInsights.matchesFacetList(field, this.props.columnChartFacets)) {
return 'columnchart';
}
if (FacetInsights.matchesFacetList(field, this.props.barListFacets)) {
return 'barlist';
}
if (FacetInsights.matchesFacetList(field, this.props.tagCloudFacets)) {
return 'tagcloud';
}
if (FacetInsights.matchesFacetList(field, this.props.timeSeriesFacets)) {
return 'timeseries';
}
if (FacetInsights.matchesFacetList(field, this.props.sentimentFacets)) {
return 'sentiment';
}
if (FacetInsights.matchesFacetList(field, this.props.geoMapFacets)) {
return 'geomap';
}
return 'list';
};
/**
* Filter Buckets in Facet that are already applied to the search filters.
* Buckets of type tagcloud and timeseries do not need to be filtered.
*/
FacetInsights.prototype.facetForField = function facetForField(fieldName, facetMap, facetFiltersMap) {
var result = void 0;
var facet = facetMap.get(fieldName);
if (facet) {
var displayType = this.getFacetDisplayType(fieldName);
var filteredFacet = this.filterFacetBuckets(facet, displayType, facetFiltersMap);
result = React.createElement(Facet, {
facet: filteredFacet,
key: fieldName,
maxBuckets: this.props.maxFacetBuckets,
type: displayType,
bordered: true,
entityColors: this.props.entityColors
});
} else {
result = null;
}
return result;
};
FacetInsights.prototype.render = function render() {
var _this2 = this;
var searcher = this.context.searcher;
var facets = searcher.state.response ? searcher.state.response.facets.slice() : [];
var facetMap = new Map();
facets.forEach(function (facet) {
facetMap.set(facet.field, facet);
});
facets.sort(function (f1, f2) {
var label1 = f1.findLabel();
var label2 = f2.findLabel();
return label1.localeCompare(label2);
});
var facetOrder = facets.map(function (facet) {
return facet.field;
});
// Remove special "first" facets...
ObjectUtils.removeItem(facetOrder, 'table');
ObjectUtils.removeItem(facetOrder, 'keyphrases');
ObjectUtils.removeItem(facetOrder, 'date');
var facetFilters = searcher.state.facetFilters;
var facetFiltersMap = new Map();
facetFilters.forEach(function (facetFilter) {
facetFiltersMap.set(facetFilter.filter, facetFilter);
});
var additionalFacets = facetOrder.map(function (facetField) {
var facet = facetMap.get(facetField);
if (facet) {
return React.createElement(
'div',
{ key: facetField, style: { flex: 1 } },
_this2.facetForField(facetField, facetMap, facetFiltersMap)
);
}
return null;
});
return React.createElement(
'div',
{ className: 'facet-insights' },
React.createElement(
'div',
{
style: {
display: 'grid',
gridTemplateColumns: 'repeat(5, 1fr)',
gridGap: '20px',
gridAutoRows: 'minmax(200px, auto)'
}
},
React.createElement(
'div',
{ style: { gridRow: 'span 2' } },
this.facetForField('table', facetMap, facetFiltersMap)
),
React.createElement(
'div',
null,
this.facetForField('keyphrases', facetMap, facetFiltersMap)
),
React.createElement(
'div',
{ style: { gridColumn: 'span 3' } },
this.facetForField('date', facetMap, facetFiltersMap)
),
additionalFacets
)
);
};
return FacetInsights;
}(React.Component), _class.defaultProps = {
pieChartFacets: null,
barChartFacets: null,
columnChartFacets: null,
barListFacets: null,
tagCloudFacets: null,
timeSeriesFacets: null,
sentimentFacets: null,
geoMapFacets: null,
maxFacetBuckets: 15,
entityColors: new Map()
}, _class.contextTypes = {
searcher: PropTypes.any
}, _class.displayName = 'FacetInsights', _temp2);
export { FacetInsights as default };