2016-01-14 17:27:06 +00:00
|
|
|
/*
|
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
var React = require('react');
|
|
|
|
var MatrixClientPeg = require("../../../MatrixClientPeg");
|
|
|
|
var Modal = require("../../../Modal");
|
2016-02-05 15:24:51 +00:00
|
|
|
var sdk = require("../../../index");
|
2017-05-23 14:16:31 +00:00
|
|
|
import _t from 'counterpart-riot';
|
2016-01-14 17:27:06 +00:00
|
|
|
var GeminiScrollbar = require('react-gemini-scrollbar');
|
|
|
|
|
|
|
|
// A list capable of displaying entities which conform to the SearchableEntity
|
|
|
|
// interface which is an object containing getJsx(): Jsx and matches(query: string): boolean
|
|
|
|
var SearchableEntityList = React.createClass({
|
|
|
|
displayName: 'SearchableEntityList',
|
|
|
|
|
|
|
|
propTypes: {
|
|
|
|
emptyQueryShowsAll: React.PropTypes.bool,
|
2016-01-19 14:51:26 +00:00
|
|
|
showInputBox: React.PropTypes.bool,
|
|
|
|
onQueryChanged: React.PropTypes.func, // fn(inputText)
|
2016-01-14 17:27:06 +00:00
|
|
|
onSubmit: React.PropTypes.func, // fn(inputText)
|
2016-02-05 15:24:51 +00:00
|
|
|
entities: React.PropTypes.array,
|
|
|
|
truncateAt: React.PropTypes.number
|
2016-01-14 17:27:06 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getDefaultProps: function() {
|
|
|
|
return {
|
2016-01-19 14:51:26 +00:00
|
|
|
showInputBox: true,
|
2016-01-14 17:27:06 +00:00
|
|
|
entities: [],
|
|
|
|
emptyQueryShowsAll: false,
|
2016-01-19 14:51:26 +00:00
|
|
|
onSubmit: function() {},
|
|
|
|
onQueryChanged: function(input) {}
|
2016-01-14 17:27:06 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
|
|
|
query: "",
|
2016-06-01 22:42:34 +00:00
|
|
|
focused: false,
|
2016-02-05 15:24:51 +00:00
|
|
|
truncateAt: this.props.truncateAt,
|
2016-01-22 17:20:52 +00:00
|
|
|
results: this.getSearchResults("", this.props.entities)
|
2016-01-14 17:27:06 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2016-01-22 17:20:52 +00:00
|
|
|
componentWillReceiveProps: function(newProps) {
|
|
|
|
// recalculate the search results in case we got new entities
|
|
|
|
this.setState({
|
|
|
|
results: this.getSearchResults(this.state.query, newProps.entities)
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2016-01-20 14:14:04 +00:00
|
|
|
componentWillUnmount: function() {
|
|
|
|
// pretend the query box was blanked out else filters could still be
|
|
|
|
// applied to other components which rely on onQueryChanged.
|
|
|
|
this.props.onQueryChanged("");
|
|
|
|
},
|
|
|
|
|
2016-01-19 14:51:26 +00:00
|
|
|
/**
|
|
|
|
* Public-facing method to set the input query text to the given input.
|
|
|
|
* @param {string} input
|
|
|
|
*/
|
|
|
|
setQuery: function(input) {
|
|
|
|
this.setState({
|
|
|
|
query: input,
|
2016-01-22 17:20:52 +00:00
|
|
|
results: this.getSearchResults(input, this.props.entities)
|
2016-01-19 14:51:26 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2016-01-14 17:27:06 +00:00
|
|
|
onQueryChanged: function(ev) {
|
|
|
|
var q = ev.target.value;
|
|
|
|
this.setState({
|
|
|
|
query: q,
|
2016-02-05 16:58:37 +00:00
|
|
|
// reset truncation if they back out the entire text
|
|
|
|
truncateAt: (q.length === 0 ? this.props.truncateAt : this.state.truncateAt),
|
2016-01-22 17:20:52 +00:00
|
|
|
results: this.getSearchResults(q, this.props.entities)
|
|
|
|
}, () => {
|
|
|
|
// invoke the callback AFTER we've flushed the new state. We need to
|
|
|
|
// do this because onQueryChanged can result in new props being passed
|
|
|
|
// to this component, which will then try to recalculate the search
|
|
|
|
// list. If we do this without flushing, we'll recalc with the last
|
|
|
|
// search term and not the current one!
|
|
|
|
this.props.onQueryChanged(q);
|
2016-01-14 17:27:06 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
onQuerySubmit: function(ev) {
|
|
|
|
ev.preventDefault();
|
|
|
|
this.props.onSubmit(this.state.query);
|
|
|
|
},
|
|
|
|
|
2016-01-22 17:20:52 +00:00
|
|
|
getSearchResults: function(query, entities) {
|
2016-01-14 17:27:06 +00:00
|
|
|
if (!query || query.length === 0) {
|
2016-09-14 17:01:07 +00:00
|
|
|
return this.props.emptyQueryShowsAll ? entities : [];
|
2016-01-14 17:27:06 +00:00
|
|
|
}
|
2016-01-22 17:20:52 +00:00
|
|
|
return entities.filter(function(e) {
|
2016-01-14 17:27:06 +00:00
|
|
|
return e.matches(query);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2016-02-05 15:24:51 +00:00
|
|
|
_showAll: function() {
|
|
|
|
this.setState({
|
|
|
|
truncateAt: -1
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_createOverflowEntity: function(overflowCount, totalCount) {
|
|
|
|
var EntityTile = sdk.getComponent("rooms.EntityTile");
|
|
|
|
var BaseAvatar = sdk.getComponent("avatars.BaseAvatar");
|
2017-05-23 14:16:31 +00:00
|
|
|
var text = (overflowCount > 1)
|
|
|
|
? _t("and %(overflowCount)s others...", { overflowCount: overflowCount })
|
|
|
|
: _t("and one other...");
|
2016-02-05 15:24:51 +00:00
|
|
|
return (
|
|
|
|
<EntityTile className="mx_EntityTile_ellipsis" avatarJsx={
|
|
|
|
<BaseAvatar url="img/ellipsis.svg" name="..." width={36} height={36} />
|
|
|
|
} name={text} presenceState="online" suppressOnHover={true}
|
|
|
|
onClick={this._showAll} />
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2016-01-14 17:27:06 +00:00
|
|
|
render: function() {
|
2016-01-19 14:51:26 +00:00
|
|
|
var inputBox;
|
2016-06-02 15:59:38 +00:00
|
|
|
|
2016-01-19 14:51:26 +00:00
|
|
|
if (this.props.showInputBox) {
|
|
|
|
inputBox = (
|
2016-01-22 17:32:43 +00:00
|
|
|
<form onSubmit={this.onQuerySubmit} autoComplete="off">
|
2016-01-21 00:16:10 +00:00
|
|
|
<input className="mx_SearchableEntityList_query" id="mx_SearchableEntityList_query" type="text"
|
2016-01-14 17:27:06 +00:00
|
|
|
onChange={this.onQueryChanged} value={this.state.query}
|
2017-01-20 14:22:27 +00:00
|
|
|
onFocus= {() => { this.setState({ focused: true }); }}
|
|
|
|
onBlur= {() => { this.setState({ focused: false }); }}
|
2017-05-23 14:16:31 +00:00
|
|
|
placeholder={ _t("Search") } />
|
2016-01-14 17:27:06 +00:00
|
|
|
</form>
|
2016-01-19 14:51:26 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-02-05 15:24:51 +00:00
|
|
|
var list;
|
2016-06-01 22:42:34 +00:00
|
|
|
if (this.state.results.length > 1 || this.state.focused) {
|
2016-02-15 07:52:57 +00:00
|
|
|
if (this.props.truncateAt) { // caller wants list truncated
|
|
|
|
var TruncatedList = sdk.getComponent("elements.TruncatedList");
|
|
|
|
list = (
|
|
|
|
<TruncatedList className="mx_SearchableEntityList_list"
|
|
|
|
truncateAt={this.state.truncateAt} // use state truncation as it may be expanded
|
|
|
|
createOverflowElement={this._createOverflowEntity}>
|
|
|
|
{this.state.results.map((entity) => {
|
|
|
|
return entity.getJsx();
|
|
|
|
})}
|
|
|
|
</TruncatedList>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
list = (
|
|
|
|
<div className="mx_SearchableEntityList_list">
|
|
|
|
{this.state.results.map((entity) => {
|
|
|
|
return entity.getJsx();
|
|
|
|
})}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2016-04-20 11:25:19 +00:00
|
|
|
list = (
|
|
|
|
<GeminiScrollbar autoshow={true}
|
|
|
|
className="mx_SearchableEntityList_listWrapper">
|
|
|
|
{ list }
|
|
|
|
</GeminiScrollbar>
|
|
|
|
);
|
2016-02-05 15:24:51 +00:00
|
|
|
}
|
|
|
|
|
2016-01-19 14:51:26 +00:00
|
|
|
return (
|
2016-06-01 22:42:34 +00:00
|
|
|
<div className={ "mx_SearchableEntityList " + (list ? "mx_SearchableEntityList_expanded" : "") }>
|
2016-02-15 07:52:57 +00:00
|
|
|
{ inputBox }
|
|
|
|
{ list }
|
2016-06-01 22:42:34 +00:00
|
|
|
{ list ? <div className="mx_SearchableEntityList_hrWrapper"><hr/></div> : '' }
|
2016-01-14 17:27:06 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = SearchableEntityList;
|