summaryrefslogtreecommitdiffstats
path: root/web/react/components/command_list.jsx
blob: 63bd57c2a80f9ab9a655f085b0baeb81b6152061 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved.
// See License.txt for license information.

var client = require('../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 (<div/>);
        }

        var suggestions = [];

        for (var i = 0; i < this.state.suggestions.length; i++) {
            if (this.state.suggestions[i].suggestion !== this.state.cmd) {
                suggestions.push(
                    <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>
                    </div>
                );
            }
        }

        return (
            <div
                ref='mentionlist'
                className='command-box'
                style={{height: (this.state.suggestions.length * 56) + 2}}
            >
                {suggestions}
            </div>
        );
    }
}

CommandList.defaultProps = {
    channelId: null
};

CommandList.propTypes = {
    addCommand: React.PropTypes.func,
    channelId: React.PropTypes.string
};