aboutsummaryrefslogtreecommitdiff
path: root/src/web/js/stores/MiddlePanelStore.js
blob: 201bebd198f365e5e2c6d0665eeffcb77ee316a3 (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
var JarrDispatcher = require('../dispatcher/JarrDispatcher');
var MiddlePanelActionTypes = require('../constants/JarrConstants').MiddlePanelActionTypes;
var MenuActionTypes = require('../constants/JarrConstants').MenuActionTypes;
var EventEmitter = require('events').EventEmitter;
var CHANGE_EVENT = 'change_middle_panel';
var assign = require('object-assign');


var MiddlePanelStore = assign({}, EventEmitter.prototype, {
    _datas: {filter: 'unread', articles: [],
             filter_type: null, filter_id: null},
    getAll: function() {
        return this._datas;
    },
    getRequestFilter: function() {
        return {'filter': this._datas.filter,
                'filter_type': this._datas.filter_type,
                'filter_id': this._datas.filter_id};
    },
    getArticles: function() {
        var key = null;
        var id = null;
        var filter = this._datas.filter;
        if (this._datas.filter_type) {
            key = this._datas.filter_type;
            id = this._datas.filter_id;
        }
        return this._datas.articles.filter(function(article) {
            return ((!key || article[key] == id)
                    && (filter == 'all'
                        || (filter == 'unread' && !article.read)
                        || (filter == 'liked' && article.liked)));
        });
    },
    setArticles: function(articles) {
        if(articles || articles == []) {
            this._datas.articles = articles;
            return true;
        }
        return false;
    },
    setFilter: function(value) {
        if(this._datas.filter != value) {
            this._datas.filter = value;
            return true;
        }
        return false;
    },
    setParentFilter: function(type, value) {
        if(this._datas.filter_id != value || this._datas.filter_type != type) {
            this._datas.filter_type = type;
            this._datas.filter_id = value;
            return true;
        }
        return false;
    },
    emitChange: function() {
        this.emit(CHANGE_EVENT);
    },
    addChangeListener: function(callback) {
        this.on(CHANGE_EVENT, callback);
    },
    removeChangeListener: function(callback) {
        this.removeListener(CHANGE_EVENT, callback);
    },
});


MiddlePanelStore.dispatchToken = JarrDispatcher.register(function(action) {
    var changed = false;
    switch(action.type) {
        case MiddlePanelActionTypes.RELOAD_MIDDLE_PANEL:
            MiddlePanelStore.setArticles(action.articles);
            MiddlePanelStore.emitChange();
            break;
        case MiddlePanelActionTypes.PARENT_FILTER:
            changed = MiddlePanelStore.setParentFilter(action.filter_type,
                                                       action.filter_id);
            changed = MiddlePanelStore.setArticles(action.articles) || changed;
            if(changed) {MiddlePanelStore.emitChange();}
            break;
        case MiddlePanelActionTypes.MIDDLE_PANEL_FILTER:
            changed = MiddlePanelStore.setFilter(action.filter);
            changed = MiddlePanelStore.setArticles(action.articles) || changed;
            if(changed) {MiddlePanelStore.emitChange();}
            break;
        case MiddlePanelActionTypes.CHANGE_ATTR:
            var id = action.article_id;
            var attr = action.attribute;
            var val = action.value;
            for (var i in MiddlePanelStore._datas.articles) {
                if(MiddlePanelStore._datas.articles[i].article_id == id) {
                    if (MiddlePanelStore._datas.articles[i][attr] != val) {
                        MiddlePanelStore._datas.articles[i][attr] = val;
                        MiddlePanelStore.emitChange();
                    }
                    break;
                }
            }
            break;
        default:
            // pass
    }
});

module.exports = MiddlePanelStore;
bgstack15