Files
mattermost/web/react/components/command_list.jsx

100 lines
2.7 KiB
React
Raw Normal View History

// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
2015-06-14 23:53:32 -08:00
// See License.txt for license information.
var client = require('../utils/client.jsx');
2015-09-01 17:06:31 -07:00
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;
}
2015-06-14 23:53:32 -08:00
this.handleClick(0);
2015-09-01 17:06:31 -07:00
}
2015-06-14 23:53:32 -08:00
2015-09-01 17:06:31 -07:00
isEmpty() {
return this.state.suggestions.length === 0;
}
getSuggestedCommands(cmd) {
if (!cmd || cmd.charAt(0) !== '/') {
this.setState({suggestions: [ ], cmd: ''});
2015-06-14 23:53:32 -08:00
return;
}
client.executeCommand(
this.props.channelId,
cmd,
true,
2015-09-01 17:06:31 -07:00
function success(data) {
2015-07-08 12:51:43 -07:00
if (data.suggestions.length === 1 && data.suggestions[0].suggestion === cmd) {
data.suggestions = [];
}
2015-09-01 17:06:31 -07:00
this.setState({suggestions: data.suggestions, cmd: cmd});
2015-06-14 23:53:32 -08:00
}.bind(this),
2015-09-01 17:06:31 -07:00
function fail() {
}
2015-06-14 23:53:32 -08:00
);
2015-09-01 17:06:31 -07:00
}
render() {
if (this.state.suggestions.length === 0) {
return (<div/>);
}
2015-06-14 23:53:32 -08:00
2015-07-08 12:51:43 -07:00
var suggestions = [];
2015-06-14 23:53:32 -08:00
for (var i = 0; i < this.state.suggestions.length; i++) {
2015-09-01 17:06:31 -07:00
if (this.state.suggestions[i].suggestion !== this.state.cmd) {
2015-06-14 23:53:32 -08:00
suggestions.push(
2015-09-01 17:06:31 -07:00
<div
key={i}
className='command-name'
onClick={this.handleClick.bind(this, i)}
>
<div className='command__title'><strong>{this.state.suggestions[i].suggestion}</strong></div>
<div className='command__desc'>{this.state.suggestions[i].description}</div>
2015-06-14 23:53:32 -08:00
</div>
);
}
}
return (
2015-09-01 17:06:31 -07:00
<div
ref='mentionlist'
className='command-box'
style={{height: (this.state.suggestions.length * 56) + 2}}
>
{suggestions}
2015-06-14 23:53:32 -08:00
</div>
);
}
2015-09-01 17:06:31 -07:00
}
CommandList.defaultProps = {
channelId: null
};
CommandList.propTypes = {
addCommand: React.PropTypes.func,
channelId: React.PropTypes.string
};