aboutsummaryrefslogtreecommitdiff
path: root/src/web/js/stores/MiddlePanelStore.js
blob: d5744e20be1254bd8717dd34067b8081580e3541 (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
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;
    },
    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.filter_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