aboutsummaryrefslogtreecommitdiff
path: root/src/web/js/stores/MenuStore.js
blob: 49c61bc1317fb7339475eb7f6c122c81ba45c4f3 (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
var JarrDispatcher = require('../dispatcher/JarrDispatcher');
var ActionTypes = require('../constants/JarrConstants');
var EventEmitter = require('events').EventEmitter;
var CHANGE_EVENT = 'change_menu';
var assign = require('object-assign');


var MenuStore = assign({}, EventEmitter.prototype, {
    _datas: {filter: 'all', feeds: {}, categories: {}, categories_order: [],
             active_type: null, active_id: null,
             is_admin: false, crawling_method: 'classic',
             all_unread_count: 0, max_error: 0, error_threshold: 0,
             all_folded: false},
    getAll: function() {
        return this._datas;
    },
    setFilter: function(value) {
        if(this._datas.filter != value) {
            this._datas.filter = value;
            this.emitChange();
        }
    },
    setActive: function(type, value) {
        if(this._datas.active_id != value || this._datas.active_type != type) {
            this._datas.active_type = type;
            this._datas.active_id = value;
            this.emitChange();
        }
    },
    readFeedArticle: function(feed_id) {
        // TODO
    },
    emitChange: function() {
        this.emit(CHANGE_EVENT);
    },
    addChangeListener: function(callback) {
        this.on(CHANGE_EVENT, callback);
    },
    removeChangeListener: function(callback) {
        this.removeListener(CHANGE_EVENT, callback);
    },
});


MenuStore.dispatchToken = JarrDispatcher.register(function(action) {
    switch(action.type) {
        case ActionTypes.RELOAD_MENU:
            MenuStore._datas['feeds'] = action.feeds;
            MenuStore._datas['categories'] = action.categories;
            MenuStore._datas['categories_order'] = action.categories_order;
            MenuStore._datas['is_admin'] = action.is_admin;
            MenuStore._datas['max_error'] = action.max_error;
            MenuStore._datas['error_threshold'] = action.error_threshold;
            MenuStore._datas['crawling_method'] = action.crawling_method;
            MenuStore._datas['all_unread_count'] = action.all_unread_count;
            MenuStore.emitChange();
            break;
        case ActionTypes.PARENT_FILTER:
            MenuStore.setActive(action.filter_type, action.filter_id);
            if(action.filters && action.articles && !action.filters.query
                    && action.filters.filter == 'unread') {
                var new_unread = {};
                action.articles.map(function(article) {
                    if(!(article.feed_id in new_unread)) {
                        new_unread[article.feed_id] = 0;
                    }
                    if(!article.read) {
                        new_unread[article.feed_id] += 1;
                    }
                });
                var changed = false;
                for(var feed_id in new_unread) {
                    var old_unread = MenuStore._datas.feeds[feed_id].unread;
                    if(old_unread == new_unread[feed_id]) {
                        continue;
                    }
                    changed = true;
                    MenuStore._datas.feeds[feed_id].unread = new_unread[feed_id];
                    var cat_id = MenuStore._datas.feeds[feed_id].category_id;
                    MenuStore._datas.categories[cat_id].unread -= old_unread;
                    MenuStore._datas.categories[cat_id].unread += new_unread[feed_id];
                }
                if(changed) {
                    MenuStore.emitChange();
                }
            }

            break;
        case ActionTypes.MENU_FILTER:
            MenuStore.setFilter(action.filter);
            break;
        case ActionTypes.CHANGE_ATTR:
            if(action.attribute != 'read') {
                return;
            }
            var val = action.value_num;
            action.articles.map(function(article) {
                MenuStore._datas.categories[article.category_id].unread += val;
                MenuStore._datas.feeds[article.feed_id].unread += val;
            });
            MenuStore.emitChange();
            break;
        case ActionTypes.LOAD_ARTICLE:
            if(!action.was_read_before) {
                MenuStore._datas.categories[action.article.category_id].unread -= 1;
                MenuStore._datas.feeds[action.article.feed_id].unread -= 1;
                MenuStore.emitChange();
            }
            break;
        case ActionTypes.TOGGLE_MENU_FOLD:
            MenuStore._datas.all_folded = action.all_folded;
            MenuStore.emitChange();
        default:
            // do nothing
    }
});

module.exports = MenuStore;
bgstack15