summaryrefslogtreecommitdiffstats
path: root/web/react/components/sidebar_right.jsx
blob: df75e3adf65094eb160d19863b97eb62614495c3 (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
// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved.
// See License.txt for license information.

var SearchResults = require('./search_results.jsx');
var RhsThread = require('./rhs_thread.jsx');
var PostStore = require('../stores/post_store.jsx');
var utils = require('../utils/utils.jsx');

function getStateFromStores(from_search) {
    return { search_visible: PostStore.getSearchResults() != null, post_right_visible:  PostStore.getSelectedPost() != null, is_mention_search: PostStore.getIsMentionSearch() };
}

module.exports = React.createClass({
    componentDidMount: function() {
        PostStore.addSearchChangeListener(this._onSearchChange);
        PostStore.addSelectedPostChangeListener(this._onSelectedChange);
    },
    componentWillUnmount: function() {
        PostStore.removeSearchChangeListener(this._onSearchChange);
        PostStore.removeSelectedPostChangeListener(this._onSelectedChange);
    },
    _onSelectedChange: function(from_search) {
        if (this.isMounted()) {
            var newState = getStateFromStores(from_search);
            newState.from_search = from_search;
            if (!utils.areStatesEqual(newState, this.state)) {
                this.setState(newState);
            }
        }
    },
    _onSearchChange: function() {
        if (this.isMounted()) {
            var newState = getStateFromStores();
            if (!utils.areStatesEqual(newState, this.state)) {
                this.setState(newState);
            }
        }
    },
    resize: function() {
        var postHolder = $('.post-list-holder-by-time');
        postHolder[0].scrollTop = postHolder[0].scrollHeight - 224;
    },
    getInitialState: function() {
        return getStateFromStores();
    },
    render: function() {
        if (! (this.state.search_visible || this.state.post_right_visible)) {
            $('.inner__wrap').removeClass('move--left').removeClass('move--right');
            $('.sidebar--right').removeClass('move--left');
            this.resize();
            return (
                <div></div>
            );
        }

        $('.inner__wrap').removeClass('.move--right').addClass('move--left');
        $('.sidebar--left').removeClass('move--right');
        $('.sidebar--right').addClass('move--left');
        $('.sidebar--right').prepend('<div class="sidebar__overlay"></div>');
        this.resize();
        setTimeout(function(){
            $('.sidebar__overlay').fadeOut("200", function(){
                $(this).remove();
            });
        },500)

        var content = "";

        if (this.state.search_visible) {
            content = <SearchResults isMentionSearch={this.state.is_mention_search} />;
        }
        else if (this.state.post_right_visible) {
            content = <RhsThread fromSearch={this.state.from_search} isMentionSearch={this.state.is_mention_search} />;
        }

        return (
            <div className="sidebar-right-container">
                { content }
            </div>
        );
    }
});