aboutsummaryrefslogtreecommitdiff
path: root/src/web/js/stores/MiddlePanelStore.js
blob: 4e6e04c4e797b07cd753a82de201cb199392abca (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
var JarrDispatcher = require('../dispatcher/JarrDispatcher');
var MiddlePanelActionTypes = require('../constants/JarrConstants').MiddlePanelActionTypes;
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: [],
             parent_filter_type: null, parent_filter_id: null},
    getAll: function() {
        return this._datas;
    },
    getArticles: function() {
        var articles = [];
        var key = null;
        var id = null;
        if (this._datas.parent_filter_type) {
            key = this._datas.parent_filter_type + '_id';
            id = this._datas.parent_filter_id;
        }
        this._datas.articles.map(function(article) {
            if(!key || article[key] == id) {
                articles.push(article);
            }
        });
        return articles;
    },
    setFilter: function(value) {
        if(this._datas.filter != value) {
            this._datas.filter = value;
            this.emitChange();
        }
    },
    setParentFilter: function(type, value) {
        if(this._datas['parent_filter_id'] != value
                || this._datas['parent_filter_type'] != type) {
            this._datas['parent_filter_type'] = type;
            this._datas['parent_filter_id'] = value;
            this.emitChange();
        }
    },
    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) {
    switch(action.type) {
        case MiddlePanelActionTypes.RELOAD_MIDDLE_PANEL:
            MiddlePanelStore._datas['articles'] = action.articles;
            MiddlePanelStore.emitChange();
            break;
        // PARENT FILTER
        case MiddlePanelActionTypes.MIDDLE_PANEL_PARENT_FILTER:
            MiddlePanelStore.setParentFilter(action.parent_type,
                                             action.parent_id);
            break;
        // FILTER
        case MiddlePanelActionTypes.MIDDLE_PANEL_FILTER_ALL:
            MiddlePanelStore.setFilter('all');
            break;
        case MiddlePanelActionTypes.MIDDLE_PANEL_FILTER_UNREAD:
            MiddlePanelStore.setFilter('unread');
            break;
        case MiddlePanelActionTypes.MIDDLE_PANEL_FILTER_LIKED:
            MiddlePanelStore.setFilter('liked');
            break;


        default:
            // do nothing
    }
});

module.exports = MiddlePanelStore;
bgstack15