aboutsummaryrefslogtreecommitdiff
path: root/src/web/js/components/Menu.react.js
blob: caf8c3a8164b00b56720686734d19f14b0d3dc56 (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
var React = require('react');
var MenuStore = require('../stores/MenuStore');
var MenuActions = require('../actions/MenuActions');
var MiddlePanelActions = require('../actions/MiddlePanelActions');

var FeedItem = React.createClass({
    propTypes: {feed_id: React.PropTypes.number.isRequired,
                title: React.PropTypes.string.isRequired,
                unread: React.PropTypes.number.isRequired,
                icon_url: React.PropTypes.string,
    },
    getInitialState: function() {
        return {feed_id: this.props.feed_id,
                title: this.props.title,
                unread: this.props.unread,
                icon_url: this.props.icon_url,
        };
    },
    render: function() {
        var unread = undefined;
        var icon = undefined;
        if(this.state.icon_url){
            icon = (<img width="16px" src={this.state.icon_url} />);
        } else {
            icon = (<span className="glyphicon glyphicon-ban-circle" />);
        }
        if(this.state.unread){
            unread = (
                    <span className="badge pull-right">
                        {this.state.unread}
                    </span>
            );
        }
        return (<li onMouseDown={this.handleClick}>
                    {icon} {this.state.title} {unread}
                </li>
        );
    },
    handleClick: function() {
        MiddlePanelActions.setFeedFilter(this.state.feed_id);
    },
});

var Category = React.createClass({
    propTypes: {category_id: React.PropTypes.number.isRequired,
                name: React.PropTypes.string.isRequired,
                feeds: React.PropTypes.array.isRequired,
                unread: React.PropTypes.number.isRequired,
    },
    getInitialState: function() {
        return {category_id: this.props.category_id,
                name: this.props.name,
                feeds: this.props.feeds,
                unread: this.props.unread,
        };
    },
    render: function() {
        unread = undefined;
        if(this.state.unread){
            unread = (
                    <span className="badge pull-right">
                        {this.state.unread}
                    </span>
            );
        }
        return (<div>
                    <h3 onMouseDown={this.handleClick}>
                        {this.state.name} {unread}
                    </h3>
                    <ul className="nav nav-sidebar">
                    {this.state.feeds.map(function(feed){
                     return <FeedItem key={"feed" + feed.id}
                                      feed_id={feed.id}
                                      title={feed.title}
                                      unread={feed.unread}
                                      icon_url={feed.icon_url} />;})}
                    </ul>
                </div>
        );
    },
    handleClick: function() {
        MiddlePanelActions.setCategoryFilter(this.state.category_id);
    },
});

var Menu = React.createClass({
    getInitialState: function() {
        return {categories: [], all_unread_count: 0};
    },
    render: function() {
        return (<div id="sidebar" data-spy="affix" role="navigation"
                     className="col-md-2 sidebar sidebar-offcanvas pre-scrollable hidden-sm hidden-xs affix">
                    {this.state.categories.map(function(category){
                        return (<Category key={"cat" + category.id}
                                          category_id={category.id}
                                          feeds={category.feeds}
                                          name={category.name}
                                          unread={category.unread} />);
                        })}

                </div>
        );
    },

    componentDidMount: function() {
        MenuActions.reload();
        MenuStore.addChangeListener(this._onChange);
    },
    componentWillUnmount: function() {
        MenuStore.removeChangeListener(this._onChange);
    },
    _onChange: function() {
        var datas = MenuStore.getAll();
        this.setState({categories: datas.categories,
                       all_unread_count: datas.all_unread_count});
    },
});

module.exports = Menu;
bgstack15