From 94a18fbdecaa798d67a5bf7ad0f2b8ee4e0c7851 Mon Sep 17 00:00:00 2001 From: François Schmidts Date: Sat, 23 Jan 2016 03:21:41 +0100 Subject: meh, kinda works, sleep now --- src/web/js/stores/MiddlePanelStore.js | 72 +++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 32 deletions(-) (limited to 'src/web/js/stores/MiddlePanelStore.js') diff --git a/src/web/js/stores/MiddlePanelStore.js b/src/web/js/stores/MiddlePanelStore.js index 4e6e04c4..80ac8198 100644 --- a/src/web/js/stores/MiddlePanelStore.js +++ b/src/web/js/stores/MiddlePanelStore.js @@ -7,38 +7,51 @@ var assign = require('object-assign'); var MiddlePanelStore = assign({}, EventEmitter.prototype, { _datas: {filter: 'unread', articles: [], - parent_filter_type: null, parent_filter_id: null}, + 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 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; + var filter = this._datas.filter; + if (this._datas.filter_type) { + key = this._datas.filter_type + '_id'; + id = this._datas.filter_id; } - this._datas.articles.map(function(article) { - if(!key || article[key] == id) { - articles.push(article); - } + return this._datas.articles.filter(function(article) { + return ((!key || article[key] == id) + && (filter == 'all' + || (filter == 'unread' && !article.read) + || (filter == 'liked' && article.liked))); }); - return articles; + }, + 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; - this.emitChange(); + return true; } + return false; }, 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(); + 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); @@ -53,30 +66,25 @@ var MiddlePanelStore = assign({}, EventEmitter.prototype, { MiddlePanelStore.dispatchToken = JarrDispatcher.register(function(action) { + var changed = false; switch(action.type) { case MiddlePanelActionTypes.RELOAD_MIDDLE_PANEL: - MiddlePanelStore._datas['articles'] = action.articles; + MiddlePanelStore.setArticles(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'); + 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_UNREAD: - MiddlePanelStore.setFilter('unread'); + case MiddlePanelActionTypes.MIDDLE_PANEL_FILTER: + changed = MiddlePanelStore.setFilter(action.filter); + changed = MiddlePanelStore.setArticles(action.articles) || changed; + if(changed) {MiddlePanelStore.emitChange();} break; - case MiddlePanelActionTypes.MIDDLE_PANEL_FILTER_LIKED: - MiddlePanelStore.setFilter('liked'); - break; - - default: - // do nothing + // pass } }); -- cgit