summaryrefslogtreecommitdiffstats
path: root/web/react/components/search_autocomplete.jsx
blob: 284b475c1c5f3454a37fbd30c4b767baaac2ed6a (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

const ChannelStore = require('../stores/channel_store.jsx');
const UserStore = require('../stores/user_store.jsx');
const Utils = require('../utils/utils.jsx');

const patterns = {
    channels: /\b(?:in|channel):\s*(\S*)$/i,
    users: /\bfrom:\s*(\S*)$/i
};

export default class SearchAutocomplete extends React.Component {
    constructor(props) {
        super(props);

        this.handleClick = this.handleClick.bind(this);
        this.handleDocumentClick = this.handleDocumentClick.bind(this);
        this.handleInputChange = this.handleInputChange.bind(this);

        this.state = {
            show: false,
            mode: '',
            filter: ''
        };
    }

    componentDidMount() {
        $(document).on('click', this.handleDocumentClick);
    }

    componentWillUnmount() {
        $(document).off('click', this.handleDocumentClick);
    }

    handleClick(value) {
        this.props.completeWord(this.state.filter, value);

        this.setState({
            show: false,
            mode: '',
            filter: ''
        });
    }

    handleDocumentClick(e) {
        const container = $(ReactDOM.findDOMNode(this.refs.container));

        if (!(container.is(e.target) || container.has(e.target).length > 0)) {
            this.setState({
                show: false
            });
        }
    }

    handleInputChange(textbox, text) {
        const caret = Utils.getCaretPosition(textbox);
        const preText = text.substring(0, caret);

        let mode = '';
        let filter = '';
        for (const pattern in patterns) {
            const result = patterns[pattern].exec(preText);

            if (result) {
                mode = pattern;
                filter = result[1];
                break;
            }
        }

        this.setState({
            mode,
            filter,
            show: mode || filter
        });
    }

    render() {
        if (!this.state.show) {
            return null;
        }

        let suggestions = [];

        if (this.state.mode === 'channels') {
            let channels = ChannelStore.getAll();

            if (this.state.filter) {
                channels = channels.filter((channel) => channel.name.startsWith(this.state.filter));
            }

            suggestions = channels.map((channel) => {
                return (
                    <div
                        key={channel.id}
                        onClick={this.handleClick.bind(this, channel.name)}
                    >
                        {channel.name}
                    </div>
                );
            });
        } else if (this.state.mode === 'users') {
            let users = UserStore.getActiveOnlyProfileList();

            if (this.state.filter) {
                users = users.filter((user) => user.username.startsWith(this.state.filter));
            }

            suggestions = users.map((user) => {
                return (
                    <div
                        key={user.id}
                        onClick={this.handleClick.bind(this, user.username)}
                    >
                        {user.username}
                    </div>
                );
            });
        }

        if (suggestions.length === 0) {
            return null;
        }

        return (
            <div
                ref='container'
                style={{overflow: 'visible', position: 'absolute', zIndex: '100', background: 'yellow'}}
            >
                {suggestions}
            </div>
        );
    }
}

SearchAutocomplete.propTypes = {
    completeWord: React.PropTypes.func.isRequired
};