// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved. // See License.txt for license information. import * as client from '../utils/client.jsx'; export default class CommandList extends React.Component { constructor(props) { super(props); this.handleClick = this.handleClick.bind(this); this.addFirstCommand = this.addFirstCommand.bind(this); this.isEmpty = this.isEmpty.bind(this); this.getSuggestedCommands = this.getSuggestedCommands.bind(this); this.state = { suggestions: [], cmd: '' }; } handleClick(i) { this.props.addCommand(this.state.suggestions[i].suggestion); this.setState({suggestions: [], cmd: ''}); } addFirstCommand() { if (this.state.suggestions.length === 0) { return; } this.handleClick(0); } isEmpty() { return this.state.suggestions.length === 0; } getSuggestedCommands(cmd) { if (!cmd || cmd.charAt(0) !== '/') { this.setState({suggestions: [], cmd: ''}); return; } client.executeCommand( this.props.channelId, cmd, true, function success(data) { if (data.suggestions.length === 1 && data.suggestions[0].suggestion === cmd) { data.suggestions = []; } this.setState({suggestions: data.suggestions, cmd: cmd}); }.bind(this), function fail() { } ); } render() { if (this.state.suggestions.length === 0) { return (
); } var suggestions = []; for (var i = 0; i < this.state.suggestions.length; i++) { if (this.state.suggestions[i].suggestion !== this.state.cmd) { suggestions.push(
{this.state.suggestions[i].suggestion}
{this.state.suggestions[i].description}
); } } return (
{suggestions}
); } } CommandList.defaultProps = { channelId: null }; CommandList.propTypes = { addCommand: React.PropTypes.func, channelId: React.PropTypes.string };