2016-09-05 14:03:16 +03: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");
|
2016-09-05 16:16:21 +03:00
|
|
|
var sdk = require("../../../index");
|
|
|
|
var Invite = require("../../../Invite");
|
|
|
|
var createRoom = require("../../../createRoom");
|
2016-09-05 19:28:08 +03:00
|
|
|
var MatrixClientPeg = require("../../../MatrixClientPeg");
|
|
|
|
var rate_limited_func = require("../../../ratelimitedfunc");
|
2016-09-05 16:16:21 +03:00
|
|
|
var Modal = require('../../../Modal');
|
2016-09-05 14:03:16 +03:00
|
|
|
|
2016-09-05 19:28:08 +03:00
|
|
|
const TRUNCATE_QUERY_LIST = 4;
|
|
|
|
|
2016-09-05 14:03:16 +03:00
|
|
|
module.exports = React.createClass({
|
2016-09-05 16:16:21 +03:00
|
|
|
displayName: "ChatInviteDialog",
|
2016-09-05 14:03:16 +03:00
|
|
|
propTypes: {
|
|
|
|
title: React.PropTypes.string,
|
|
|
|
description: React.PropTypes.oneOfType([
|
|
|
|
React.PropTypes.element,
|
|
|
|
React.PropTypes.string,
|
|
|
|
]),
|
|
|
|
value: React.PropTypes.string,
|
2016-09-05 16:29:21 +03:00
|
|
|
placeholder: React.PropTypes.string,
|
2016-09-05 14:03:16 +03:00
|
|
|
button: React.PropTypes.string,
|
|
|
|
focus: React.PropTypes.bool,
|
|
|
|
onFinished: React.PropTypes.func.isRequired
|
|
|
|
},
|
|
|
|
|
|
|
|
getDefaultProps: function() {
|
|
|
|
return {
|
2016-09-05 16:29:21 +03:00
|
|
|
title: "Start a chat",
|
|
|
|
description: "Who would you like to communicate with?",
|
2016-09-05 14:03:16 +03:00
|
|
|
value: "",
|
2016-09-05 16:29:21 +03:00
|
|
|
placeholder: "User ID, Name or email",
|
2016-09-05 14:03:16 +03:00
|
|
|
button: "Start Chat",
|
|
|
|
focus: true
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2016-09-05 19:28:08 +03:00
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
|
|
|
query: "",
|
|
|
|
queryList: [],
|
|
|
|
addressSelected: false,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2016-09-05 14:03:16 +03:00
|
|
|
componentDidMount: function() {
|
|
|
|
if (this.props.focus) {
|
|
|
|
// Set the cursor at the end of the text input
|
|
|
|
this.refs.textinput.value = this.props.value;
|
|
|
|
}
|
2016-09-05 19:28:08 +03:00
|
|
|
this._updateUserList();
|
2016-09-05 14:03:16 +03:00
|
|
|
},
|
|
|
|
|
2016-09-05 16:16:21 +03:00
|
|
|
onStartChat: function() {
|
|
|
|
this._startChat(this.refs.textinput.value);
|
|
|
|
},
|
|
|
|
|
2016-09-05 19:28:08 +03:00
|
|
|
onCancel: function() {
|
|
|
|
this.props.onFinished(false);
|
|
|
|
},
|
|
|
|
|
|
|
|
onKeyDown: function(e) {
|
|
|
|
if (e.keyCode === 27) { // escape
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
this.props.onFinished(false);
|
|
|
|
}
|
|
|
|
else if (e.keyCode === 13) { // enter
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
//this._startChat(this.refs.textinput.value);
|
|
|
|
this.setState({ addressSelected: true });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
onQueryChanged: function(ev) {
|
|
|
|
var query = ev.target.value;
|
2016-09-06 13:34:38 +03:00
|
|
|
var list;
|
|
|
|
// Use the already filtered list if it has been filtered
|
|
|
|
if (query.length > 1) {
|
|
|
|
list = this.state.queryList;
|
|
|
|
} else {
|
|
|
|
list = this._userList;
|
|
|
|
}
|
|
|
|
var queryList = list.filter((user) => {
|
2016-09-05 19:28:08 +03:00
|
|
|
return this._matches(query, user);
|
|
|
|
});
|
|
|
|
this.setState({ queryList: queryList });
|
|
|
|
},
|
|
|
|
|
|
|
|
onDismissed: function() {
|
|
|
|
this.setState({ addressSelected: false });
|
|
|
|
},
|
|
|
|
|
2016-09-05 16:16:21 +03:00
|
|
|
_startChat: function(addr) {
|
2016-09-05 16:29:21 +03:00
|
|
|
// Start the chat
|
2016-09-05 16:16:21 +03:00
|
|
|
createRoom().then(function(roomId) {
|
|
|
|
return Invite.inviteToRoom(roomId, addr);
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
var ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
|
|
|
|
Modal.createDialog(ErrorDialog, {
|
2016-09-05 16:29:21 +03:00
|
|
|
title: "Failure to invite user",
|
2016-09-05 16:16:21 +03:00
|
|
|
description: err.toString()
|
|
|
|
});
|
|
|
|
return null;
|
|
|
|
})
|
|
|
|
.done();
|
2016-09-05 16:29:21 +03:00
|
|
|
|
|
|
|
// Close - this will happen before the above, as that is async
|
2016-09-05 16:16:21 +03:00
|
|
|
this.props.onFinished(true, addr);
|
2016-09-05 14:03:16 +03:00
|
|
|
},
|
|
|
|
|
2016-09-05 19:28:08 +03:00
|
|
|
_updateUserList: new rate_limited_func(function() {
|
|
|
|
// Get all the users
|
|
|
|
this._userList = MatrixClientPeg.get().getUsers();
|
|
|
|
}, 500),
|
2016-09-05 14:03:16 +03:00
|
|
|
|
2016-09-05 19:28:08 +03:00
|
|
|
// This is the search algorithm for matching users
|
|
|
|
_matches: function(query, user) {
|
|
|
|
var name = user.displayName.toLowerCase();
|
|
|
|
var uid = user.userId.toLowerCase();
|
|
|
|
query = query.toLowerCase();
|
|
|
|
|
|
|
|
// direct prefix matches
|
|
|
|
if (name.indexOf(query) === 0 || uid.indexOf(query) === 0) {
|
|
|
|
return true;
|
2016-09-05 14:03:16 +03:00
|
|
|
}
|
2016-09-05 19:28:08 +03:00
|
|
|
|
|
|
|
// strip @ on uid and try matching again
|
|
|
|
if (uid.length > 1 && uid[0] === "@" && uid.substring(1).indexOf(query) === 0) {
|
|
|
|
return true;
|
2016-09-05 14:03:16 +03:00
|
|
|
}
|
2016-09-05 19:28:08 +03:00
|
|
|
|
|
|
|
// split spaces in name and try matching constituent parts
|
|
|
|
var parts = name.split(" ");
|
|
|
|
for (var i = 0; i < parts.length; i++) {
|
|
|
|
if (parts[i].indexOf(query) === 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2016-09-05 14:03:16 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
2016-09-05 16:16:21 +03:00
|
|
|
var TintableSvg = sdk.getComponent("elements.TintableSvg");
|
2016-09-05 19:28:08 +03:00
|
|
|
console.log("### D E B U G - queryList:");
|
|
|
|
console.log(this.state.queryList);
|
|
|
|
|
|
|
|
var query;
|
|
|
|
if (this.state.addressSelected) {
|
|
|
|
var AddressTile = sdk.getComponent("elements.AddressTile");
|
2016-09-05 19:34:20 +03:00
|
|
|
// NOTE: this.state.queryList[0] is just a place holder until the selection logic is completed
|
2016-09-05 19:28:08 +03:00
|
|
|
query = (
|
2016-09-05 19:34:20 +03:00
|
|
|
<AddressTile user={this.state.queryList[0]} canDismiss={true} onDismissed={this.onDismissed} />
|
2016-09-05 19:28:08 +03:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
query = (
|
|
|
|
<input type="text"
|
|
|
|
id="textinput"
|
|
|
|
ref="textinput"
|
|
|
|
className="mx_ChatInviteDialog_input"
|
|
|
|
onChange={this.onQueryChanged}
|
|
|
|
placeholder={this.props.placeholder}
|
|
|
|
defaultValue={this.props.value}
|
|
|
|
autoFocus={this.props.focus}
|
|
|
|
onKeyDown={this.onKeyDown}
|
|
|
|
autoComplete="off"
|
|
|
|
size="64"/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-09-05 14:03:16 +03:00
|
|
|
return (
|
|
|
|
<div className="mx_ChatInviteDialog">
|
|
|
|
<div className="mx_Dialog_title">
|
|
|
|
{this.props.title}
|
|
|
|
</div>
|
|
|
|
<div className="mx_ChatInviteDialog_cancel" onClick={this.onCancel} >
|
|
|
|
<TintableSvg src="img/icons-close-button.svg" width="35" height="35" />
|
|
|
|
</div>
|
|
|
|
<div className="mx_ChatInviteDialog_label">
|
2016-09-06 13:34:38 +03:00
|
|
|
<label htmlFor="textinput">{ this.props.description }</label>
|
2016-09-05 14:03:16 +03:00
|
|
|
</div>
|
|
|
|
<div className="mx_Dialog_content">
|
2016-09-06 13:34:38 +03:00
|
|
|
<div className="mx_ChatInviteDialog_inputContainer">{ query }</div>
|
2016-09-05 14:03:16 +03:00
|
|
|
</div>
|
|
|
|
<div className="mx_Dialog_buttons">
|
2016-09-05 16:16:21 +03:00
|
|
|
<button className="mx_Dialog_primary" onClick={this.onStartChat}>
|
2016-09-05 14:03:16 +03:00
|
|
|
{this.props.button}
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|