@attivio/suit
Version:
Attivio SUIT, the Search UI Toolkit, is a library for creating search clients for searching the Attivio platform.
412 lines (334 loc) • 12.3 kB
JavaScript
'use strict';
exports.__esModule = true;
exports.default = undefined;
var _class, _temp;
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
var _FacetFilter = require('../api/FacetFilter');
var _FacetFilter2 = _interopRequireDefault(_FacetFilter);
var _QueryResponse = require('../api/QueryResponse');
var _QueryResponse2 = _interopRequireDefault(_QueryResponse);
var _SimpleQueryRequest = require('../api/SimpleQueryRequest');
var _SimpleQueryRequest2 = _interopRequireDefault(_SimpleQueryRequest);
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 dummy version of the Searcher component, used only for wrapping
* Searcher-dependent components in the style guide.
*
* This isn't included in the index.js for SUIT because
* YOU DO NOT WANT TO USE THIS COMPONENT IN YOUR APPLICATIONS!
*/
var DummySearcher = (_temp = _class = function (_React$Component) {
_inherits(DummySearcher, _React$Component);
function DummySearcher(props) {
_classCallCheck(this, DummySearcher);
var _this = _possibleConstructorReturn(this, _React$Component.call(this, props));
_this.state = _this.getDefaultState();
return _this;
}
DummySearcher.prototype.getChildContext = function getChildContext() {
return {
searcher: this
};
};
DummySearcher.prototype.getQueryRequest = function getQueryRequest() {
var qr = new _SimpleQueryRequest2.default();
qr.query = this.props.defaultQuery;
return qr;
};
DummySearcher.prototype.getDefaultState = function getDefaultState() {
return {
response: this.props.defaultQueryResponse,
error: this.props.defaultError,
haveSearched: false,
query: this.props.defaultQuery,
queryLanguage: this.props.defaultQueryLanguage,
sort: this.props.defaultSort,
relevancyModels: this.props.defaultRelevancyModels,
facetFilters: [],
geoFilters: [],
resultsPerPage: this.props.defaultResultsPerPage,
resultsOffset: 0,
debug: this.props.defaultDebug,
businessCenterProfile: this.props.defaultBusinessCenterProfile
};
};
/**
* Get the list of fields to use in the query request.
*/
DummySearcher.prototype.getFieldList = function getFieldList() {
return this.props.defaultFieldList;
};
/**
* Used to tell the search results component whether to override
* the format with the debug format.
*/
DummySearcher.prototype.updateDebug = function updateDebug(newDebug) {
if (this.state.debug !== newDebug) {
this.setState({
debug: newDebug
});
}
};
/**
* Perform a custom search given a query request. Calls the updateResults callback
* and doesn't affect the state of the DummySearcher itself.
*/
DummySearcher.prototype.doCustomSearch = function doCustomSearch(request, updateResults) {
updateResults(this.props.defaultQueryResponse, this.props.defaultError);
};
/**
* Completely reset the DummySearcher to its default state and call an
* optional callback when done.
*/
DummySearcher.prototype.reset = function reset() {
var callback = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {};
this.setState(this.getDefaultState(), function () {
callback();
});
};
/**
* Trigger a new search.
*
* If the search has been reset by one of the other
* methods, then
* <ul>
* <li>The "haveSearched" flag is reset to false until the search completes</li>
* <li>The offset is reset to 0 to show the first page of results</li>
* <li>Any facet filters that were applied are cleared.</li>
* <li>Any response or error from a previous search are cleared.</li>
* </ul>
*/
DummySearcher.prototype.doSearch = function doSearch() {
if (this.props.defaultQueryResponse) {
this.setState({
haveSearched: true,
response: this.props.defaultQueryResponse,
error: undefined
});
} else if (this.props.defaultError) {
this.setState({
haveSearched: true,
response: undefined,
error: this.props.defaultError
});
}
};
/**
* Set the query string to the passed-in value and trigger the
* query immediately, resetting parameters to the beginning.
*/
DummySearcher.prototype.performQueryImmediately = function performQueryImmediately() {
this.doSearch();
};
/**
* Set whether the simple or advanced query language should be
* used to perform the search.
* This causes subsequent searches to be reset.
*/
DummySearcher.prototype.updateQueryLanguage = function updateQueryLanguage(queryLanguage) {
if (queryLanguage !== this.state.queryLanguage) {
this.setState({
queryLanguage: queryLanguage
});
}
};
/**
* Update the query string to use for searching. Don't add this into the
* URL because we don't want the URL changing as the user types, only when
* the search button is clicked.
* This causes subsequent searches to be reset (see doSearch() for details).
*/
DummySearcher.prototype.updateQuery = function updateQuery(query) {
this.setState({
haveSearched: false,
query: query
});
};
/**
* Update the number of documents to show on a page.
* If there is a current search and the value has changed, the
* search will be repeated with the new value.
*/
DummySearcher.prototype.updateResultsPerPage = function updateResultsPerPage(newResultsPerPage) {
if (newResultsPerPage !== this.state.resultsPerPage) {
this.setState({
haveSearched: false,
response: undefined,
error: undefined,
resultsOffset: 0,
resultsPerPage: newResultsPerPage
});
}
};
/**
* Call to change the relevancy model in use by the DummySearcher.
* If there is a current search and the value has changed, the
* search will be repeated with the new value.
*/
DummySearcher.prototype.updateRelevancyModels = function updateRelevancyModels(newRelevancyModels) {
if (JSON.stringify(newRelevancyModels) !== JSON.stringify(this.state.relevancyModels)) {
this.setState({
haveSearched: false,
response: undefined,
error: undefined,
resultsOffset: 0,
relevancyModels: newRelevancyModels
});
}
};
/**
* Update the DummySearcher to use a new sort column. The value passed
* in should have the column name and sort direction, separated
* by a colon (direction is either ASC or DESC).
* If there is a current search and the value has changed, the
* search will be repeated with the new value.
* The search is reset to the first page when performed again.
*/
DummySearcher.prototype.updateSort = function updateSort(newSort) {
if (this.newSort !== this.state.sort) {
var _sort = this.state.sort;
if (_sort && _sort.length > 0) {
_sort[0] = newSort;
} else {
_sort = [newSort];
}
this.setState({
haveSearched: false,
response: undefined,
error: undefined,
resultsOffset: 0,
sort: _sort
});
}
};
/**
* Add a query filter (in AQL) to the query request.
*/
DummySearcher.prototype.addGeoFilter = function addGeoFilter(filter) {
this.addGeoFilters([filter]);
};
/**
* Add multiple query filters (in AQL) to the query request.
*/
DummySearcher.prototype.addGeoFilters = function addGeoFilters(filters) {
var geoFilters = this.state.geoFilters.slice();
geoFilters = geoFilters.concat(filters);
this.setState({
haveSearched: false,
response: undefined,
error: undefined,
resultsOffset: 0,
geoFilters: geoFilters
});
};
/**
* Remove a query filter by name (in AQL) from the query request.
*/
DummySearcher.prototype.removeGeoFilter = function removeGeoFilter(filter) {
var geoFilters = this.state.geoFilters.slice();
var index = geoFilters.indexOf(filter);
if (index !== -1) {
geoFilters.splice(index, 1);
}
this.setState({
haveSearched: false,
response: undefined,
error: undefined,
resultsOffset: 0,
geoFilters: geoFilters
});
};
/**
* Add a facet filter to the current search. Will repeat the search
* if it's already been performed. Note that if a filter for the
* same facet name already exists, it will be replaced with the
* new one.
*/
DummySearcher.prototype.addFacetFilter = function addFacetFilter(facetName, bucketLabel, filter) {
var newFF = new _FacetFilter2.default();
newFF.facetName = facetName;
newFF.bucketLabel = bucketLabel;
newFF.filter = filter;
var updatedFacetFilters = [];
var facetFilters = this.state.facetFilters;
facetFilters.forEach(function (facetFilter) {
if (facetFilter.facetName !== facetName) {
updatedFacetFilters.push(facetFilter);
}
});
updatedFacetFilters.push(newFF);
this.setState({
haveSearched: false,
response: undefined,
error: undefined,
resultsOffset: 0,
facetFilters: updatedFacetFilters
});
};
/**
* Remove the specified facet filter from the current
* search. If a search has already been performed, it
* will be repeated with the updated set of facet filters.
*/
DummySearcher.prototype.removeFacetFilter = function removeFacetFilter(removeFilter) {
var updatedFacetFilters = [];
var facetFilters = this.state.facetFilters;
facetFilters.forEach(function (facetFilter) {
if (facetFilter.facetName !== removeFilter.facetName) {
updatedFacetFilters.push(facetFilter);
}
});
this.setState({
haveSearched: false,
response: undefined,
error: undefined,
resultsOffset: 0,
facetFilters: updatedFacetFilters
});
};
/**
* Navigate to a new page of search results. (Should only actually be
* called if a search has been completed.) The search will be performed
* again with the new page's offset.
*/
DummySearcher.prototype.changePage = function changePage(newPage) {
var resultsPerPage = this.state.resultsPerPage;
var oldOffset = this.state.resultsOffset;
var newOffset = resultsPerPage * newPage;
if (newOffset !== oldOffset) {
this.setState({
resultsOffset: newOffset
});
}
};
DummySearcher.prototype.render = function render() {
// Nothing special to do here. The children will all look at our state to decide what to render
return _react2.default.createElement(
'div',
null,
this.props.children
);
};
return DummySearcher;
}(_react2.default.Component), _class.defaultProps = {
defaultRelevancyModels: ['default'],
defaultQueryLanguage: 'simple',
defaultDebug: false,
defaultResultsPerPage: 10,
defaultBusinessCenterProfile: null,
defaultSort: ['.score:DESC'],
defaultQuery: '*:*',
defaultQueryResponse: null,
defaultError: null,
defaultFieldList: []
}, _class.childContextTypes = {
searcher: _propTypes2.default.any
}, _class.displayName = 'DummySearcher', _temp);
exports.default = DummySearcher;
module.exports = exports['default'];