/* 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 sdk = require("../../../index"); var Invite = require("../../../Invite"); var createRoom = require("../../../createRoom"); var MatrixClientPeg = require("../../../MatrixClientPeg"); var rate_limited_func = require("../../../ratelimitedfunc"); var Modal = require('../../../Modal'); const TRUNCATE_QUERY_LIST = 40; module.exports = React.createClass({ displayName: "ChatInviteDialog", propTypes: { title: React.PropTypes.string, description: React.PropTypes.oneOfType([ React.PropTypes.element, React.PropTypes.string, ]), value: React.PropTypes.string, placeholder: React.PropTypes.string, button: React.PropTypes.string, focus: React.PropTypes.bool, onFinished: React.PropTypes.func.isRequired }, getDefaultProps: function() { return { title: "Start a chat", description: "Who would you like to communicate with?", value: "", placeholder: "User ID, Name or email", button: "Start Chat", focus: true }; }, getInitialState: function() { return { user: null, queryList: [], addressSelected: false, selected: 0, }; }, componentDidMount: function() { if (this.props.focus) { // Set the cursor at the end of the text input this.refs.textinput.value = this.props.value; } this._updateUserList(); }, onStartChat: function() { if (this.state.user) { this._startChat(this.state.user.userId); } else { this._startChat(this.refs.textinput.value); } }, 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 === 38) { // up arrow e.stopPropagation(); e.preventDefault(); if (this.state.selected > 0) { this.setState({ selected: this.state.selected - 1 }); } } else if (e.keyCode === 40) { // down arrow e.stopPropagation(); e.preventDefault(); if (this.state.selected < this._maxSelected(this.state.queryList)) { this.setState({ selected: this.state.selected + 1 }); } } else if (e.keyCode === 13) { // enter e.stopPropagation(); e.preventDefault(); if (this.state.queryList.length > 0) { this.setState({ user: this.state.queryList[this.state.selected], addressSelected: true, queryList: [], }); } } }, onQueryChanged: function(ev) { var query = ev.target.value; var queryList = []; // Only do search if there is something to search if (query.length > 0) { queryList = this._userList.filter((user) => { return this._matches(query, user); }); } // Make sure the selected item isn't outside the list bounds var selected = this.state.selected; var maxSelected = this._maxSelected(queryList); if (selected > maxSelected) { selected = maxSelected; } this.setState({ queryList: queryList, selected: selected, }); }, onDismissed: function() { this.setState({ user: null, addressSelected: false, selected: 0, queryList: [], }); }, createQueryListTiles: function() { var self = this; var AddressTile = sdk.getComponent("elements.AddressTile"); var maxSelected = this._maxSelected(this.state.queryList); var queryList = []; if (this.state.queryList.length > 0) { for (var i = 0; i <= maxSelected; i++) { queryList.push(