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
|
var JarrDispatcher = require('../dispatcher/JarrDispatcher');
var ActionTypes = require('../constants/JarrConstants');
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 ActionTypes.RELOAD_MIDDLE_PANEL:
MiddlePanelStore.setArticles(action.articles);
MiddlePanelStore.emitChange();
break;
case ActionTypes.PARENT_FILTER:
changed = MiddlePanelStore.setParentFilter(action.filter_type,
action.filter_id);
changed = MiddlePanelStore.setArticles(action.articles) || changed;
if(changed) {MiddlePanelStore.emitChange();}
break;
case ActionTypes.MIDDLE_PANEL_FILTER:
changed = MiddlePanelStore.setFilter(action.filter);
changed = MiddlePanelStore.setArticles(action.articles) || changed;
if(changed) {MiddlePanelStore.emitChange();}
break;
case ActionTypes.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;
// avoiding redraw if not filter, display won't change anyway
if(MiddlePanelStore._datas.filter != 'all') {
MiddlePanelStore.emitChange();
}
}
break;
}
}
break;
default:
// pass
}
});
module.exports = MiddlePanelStore;
|