aboutsummaryrefslogtreecommitdiff
path: root/src/web/js/stores
diff options
context:
space:
mode:
Diffstat (limited to 'src/web/js/stores')
-rw-r--r--src/web/js/stores/MenuStore.js118
-rw-r--r--src/web/js/stores/MiddlePanelStore.js132
-rw-r--r--src/web/js/stores/RightPanelStore.js77
-rw-r--r--src/web/js/stores/__tests__/TodoStore-test.js90
4 files changed, 417 insertions, 0 deletions
diff --git a/src/web/js/stores/MenuStore.js b/src/web/js/stores/MenuStore.js
new file mode 100644
index 00000000..49c61bc1
--- /dev/null
+++ b/src/web/js/stores/MenuStore.js
@@ -0,0 +1,118 @@
+var JarrDispatcher = require('../dispatcher/JarrDispatcher');
+var ActionTypes = require('../constants/JarrConstants');
+var EventEmitter = require('events').EventEmitter;
+var CHANGE_EVENT = 'change_menu';
+var assign = require('object-assign');
+
+
+var MenuStore = assign({}, EventEmitter.prototype, {
+ _datas: {filter: 'all', feeds: {}, categories: {}, categories_order: [],
+ active_type: null, active_id: null,
+ is_admin: false, crawling_method: 'classic',
+ all_unread_count: 0, max_error: 0, error_threshold: 0,
+ all_folded: false},
+ getAll: function() {
+ return this._datas;
+ },
+ setFilter: function(value) {
+ if(this._datas.filter != value) {
+ this._datas.filter = value;
+ this.emitChange();
+ }
+ },
+ setActive: function(type, value) {
+ if(this._datas.active_id != value || this._datas.active_type != type) {
+ this._datas.active_type = type;
+ this._datas.active_id = value;
+ this.emitChange();
+ }
+ },
+ readFeedArticle: function(feed_id) {
+ // TODO
+ },
+ emitChange: function() {
+ this.emit(CHANGE_EVENT);
+ },
+ addChangeListener: function(callback) {
+ this.on(CHANGE_EVENT, callback);
+ },
+ removeChangeListener: function(callback) {
+ this.removeListener(CHANGE_EVENT, callback);
+ },
+});
+
+
+MenuStore.dispatchToken = JarrDispatcher.register(function(action) {
+ switch(action.type) {
+ case ActionTypes.RELOAD_MENU:
+ MenuStore._datas['feeds'] = action.feeds;
+ MenuStore._datas['categories'] = action.categories;
+ MenuStore._datas['categories_order'] = action.categories_order;
+ MenuStore._datas['is_admin'] = action.is_admin;
+ MenuStore._datas['max_error'] = action.max_error;
+ MenuStore._datas['error_threshold'] = action.error_threshold;
+ MenuStore._datas['crawling_method'] = action.crawling_method;
+ MenuStore._datas['all_unread_count'] = action.all_unread_count;
+ MenuStore.emitChange();
+ break;
+ case ActionTypes.PARENT_FILTER:
+ MenuStore.setActive(action.filter_type, action.filter_id);
+ if(action.filters && action.articles && !action.filters.query
+ && action.filters.filter == 'unread') {
+ var new_unread = {};
+ action.articles.map(function(article) {
+ if(!(article.feed_id in new_unread)) {
+ new_unread[article.feed_id] = 0;
+ }
+ if(!article.read) {
+ new_unread[article.feed_id] += 1;
+ }
+ });
+ var changed = false;
+ for(var feed_id in new_unread) {
+ var old_unread = MenuStore._datas.feeds[feed_id].unread;
+ if(old_unread == new_unread[feed_id]) {
+ continue;
+ }
+ changed = true;
+ MenuStore._datas.feeds[feed_id].unread = new_unread[feed_id];
+ var cat_id = MenuStore._datas.feeds[feed_id].category_id;
+ MenuStore._datas.categories[cat_id].unread -= old_unread;
+ MenuStore._datas.categories[cat_id].unread += new_unread[feed_id];
+ }
+ if(changed) {
+ MenuStore.emitChange();
+ }
+ }
+
+ break;
+ case ActionTypes.MENU_FILTER:
+ MenuStore.setFilter(action.filter);
+ break;
+ case ActionTypes.CHANGE_ATTR:
+ if(action.attribute != 'read') {
+ return;
+ }
+ var val = action.value_num;
+ action.articles.map(function(article) {
+ MenuStore._datas.categories[article.category_id].unread += val;
+ MenuStore._datas.feeds[article.feed_id].unread += val;
+ });
+ MenuStore.emitChange();
+ break;
+ case ActionTypes.LOAD_ARTICLE:
+ if(!action.was_read_before) {
+ MenuStore._datas.categories[action.article.category_id].unread -= 1;
+ MenuStore._datas.feeds[action.article.feed_id].unread -= 1;
+ MenuStore.emitChange();
+ }
+ break;
+ case ActionTypes.TOGGLE_MENU_FOLD:
+ MenuStore._datas.all_folded = action.all_folded;
+ MenuStore.emitChange();
+ default:
+ // do nothing
+ }
+});
+
+module.exports = MenuStore;
diff --git a/src/web/js/stores/MiddlePanelStore.js b/src/web/js/stores/MiddlePanelStore.js
new file mode 100644
index 00000000..4a5efd00
--- /dev/null
+++ b/src/web/js/stores/MiddlePanelStore.js
@@ -0,0 +1,132 @@
+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, {
+ filter_whitelist: ['filter', 'filter_id', 'filter_type', 'display_search',
+ 'query', 'search_title', 'search_content'],
+ _datas: {articles: [], selected_article: null,
+ filter: 'unread', filter_type: null, filter_id: null,
+ display_search: false, query: null,
+ search_title: true, search_content: false},
+ getAll: function() {
+ return this._datas;
+ },
+ getRequestFilter: function(display_search) {
+ var filters = {'filter': this._datas.filter,
+ 'filter_type': this._datas.filter_type,
+ 'filter_id': this._datas.filter_id,
+ };
+ if(display_search || (display_search == undefined && this._datas.display_search)) {
+ filters.query = this._datas.query;
+ filters.search_title = this._datas.search_title;
+ filters.search_content = this._datas.search_content;
+ };
+ return filters;
+ },
+ getArticles: function() {
+ var key = null;
+ var id = null;
+ if (this._datas.filter_type) {
+ key = this._datas.filter_type;
+ id = this._datas.filter_id;
+ }
+ return this._datas.articles
+ .map(function(article) {
+ if(article.article_id == this._datas.selected_article) {
+ article.selected = true;
+ } else if(article.selected) {
+ article.selected = false;
+ }
+ return article;
+ }.bind(this))
+ .filter(function(article) {
+ return (article.selected || ((!key || article[key] == id)
+ && (this._datas.filter == 'all'
+ || (this._datas.filter == 'unread' && !article.read)
+ || (this._datas.filter == 'liked' && article.liked))));
+ }.bind(this));
+
+ },
+ setArticles: function(articles) {
+ if(articles || articles == []) {
+ this._datas.articles = articles;
+ return true;
+ }
+ return false;
+ },
+ registerFilter: function(action) {
+ var changed = false;
+ this.filter_whitelist.map(function(key) {
+ if(key in action && this._datas[key] != action[key]) {
+ changed = true;
+ this._datas[key] = action[key];
+ }
+ }.bind(this));
+ return changed;
+ },
+ 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:
+ changed = MiddlePanelStore.registerFilter(action);
+ changed = MiddlePanelStore.setArticles(action.articles) || changed;
+ break;
+ case ActionTypes.PARENT_FILTER:
+ changed = MiddlePanelStore.registerFilter(action);
+ changed = MiddlePanelStore.setArticles(action.articles) || changed;
+ break;
+ case ActionTypes.MIDDLE_PANEL_FILTER:
+ changed = MiddlePanelStore.registerFilter(action);
+ changed = MiddlePanelStore.setArticles(action.articles) || changed;
+ break;
+ case ActionTypes.CHANGE_ATTR:
+ var attr = action.attribute;
+ var val = action.value_bool;
+ action.articles.map(function(article) {
+ for (var i in MiddlePanelStore._datas.articles) {
+ if(MiddlePanelStore._datas.articles[i].article_id == article.article_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') {
+ changed = true;
+ }
+ }
+ break;
+ }
+ }
+ });
+ break;
+ case ActionTypes.LOAD_ARTICLE:
+ changed = true;
+ MiddlePanelStore._datas.selected_article = action.article.id;
+ for (var i in MiddlePanelStore._datas.articles) {
+ if(MiddlePanelStore._datas.articles[i].article_id == action.article.id) {
+ MiddlePanelStore._datas.articles[i].read = true;
+ break;
+ }
+ }
+ break;
+ default:
+ // pass
+ }
+ if(changed) {MiddlePanelStore.emitChange();}
+});
+
+module.exports = MiddlePanelStore;
diff --git a/src/web/js/stores/RightPanelStore.js b/src/web/js/stores/RightPanelStore.js
new file mode 100644
index 00000000..6c268dfd
--- /dev/null
+++ b/src/web/js/stores/RightPanelStore.js
@@ -0,0 +1,77 @@
+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 MenuStore = require('../stores/MenuStore');
+
+
+var RightPanelStore = assign({}, EventEmitter.prototype, {
+ category: null,
+ feed: null,
+ article: null,
+ current: null,
+ getAll: function() {
+ return {category: this.category, feed: this.feed,
+ article: this.article, current: this.current};
+ },
+ emitChange: function() {
+ this.emit(CHANGE_EVENT);
+ },
+ addChangeListener: function(callback) {
+ this.on(CHANGE_EVENT, callback);
+ },
+ removeChangeListener: function(callback) {
+ this.removeListener(CHANGE_EVENT, callback);
+ },
+});
+
+
+RightPanelStore.dispatchToken = JarrDispatcher.register(function(action) {
+ switch(action.type) {
+ case ActionTypes.PARENT_FILTER:
+ RightPanelStore.article = null;
+ if(action.filter_id == null) {
+ RightPanelStore.category = null;
+ RightPanelStore.feed = null;
+ RightPanelStore.current = null;
+ } else if(action.filter_type == 'category_id') {
+ RightPanelStore.category = MenuStore._datas.categories[action.filter_id];
+ RightPanelStore.feed = null;
+ RightPanelStore.current = 'category';
+ RightPanelStore.emitChange();
+ } else {
+
+ RightPanelStore.feed = MenuStore._datas.feeds[action.filter_id];
+ RightPanelStore.category = MenuStore._datas.categories[RightPanelStore.feed.category_id];
+ RightPanelStore.current = 'feed';
+ RightPanelStore.emitChange();
+ }
+ break;
+ case ActionTypes.LOAD_ARTICLE:
+ RightPanelStore.feed = MenuStore._datas.feeds[action.article.feed_id];
+ RightPanelStore.category = MenuStore._datas.categories[action.article.category_id];
+ RightPanelStore.article = action.article;
+ RightPanelStore.current = 'article';
+ RightPanelStore.emitChange();
+ break;
+ case ActionTypes.RELOAD_MENU:
+ RightPanelStore.article = null;
+ if(RightPanelStore.category && !(RightPanelStore.category.id.toString() in action.categories)) {
+ RightPanelStore.category = null;
+ RightPanelStore.current = null;
+ }
+ if(RightPanelStore.feed && !(RightPanelStore.feed.id.toString() in action.feeds)) {
+ RightPanelStore.feed = null;
+ RightPanelStore.current = null;
+ }
+ if(RightPanelStore.current == 'article') {
+ RightPanelStore.current = null;
+ }
+ RightPanelStore.emitChange();
+ default:
+ // pass
+ }
+});
+
+module.exports = RightPanelStore;
diff --git a/src/web/js/stores/__tests__/TodoStore-test.js b/src/web/js/stores/__tests__/TodoStore-test.js
new file mode 100644
index 00000000..6da6cd3c
--- /dev/null
+++ b/src/web/js/stores/__tests__/TodoStore-test.js
@@ -0,0 +1,90 @@
+/*
+ * Copyright (c) 2014-2015, Facebook, Inc.
+ * All rights reserved.
+ *
+ * This source code is licensed under the BSD-style license found in the
+ * LICENSE file in the root directory of this source tree. An additional grant
+ * of patent rights can be found in the PATENTS file in the same directory.
+ *
+ * TodoStore-test
+ */
+
+jest.dontMock('../../constants/TodoConstants');
+jest.dontMock('../TodoStore');
+jest.dontMock('object-assign');
+
+describe('TodoStore', function() {
+
+ var TodoConstants = require('../../constants/TodoConstants');
+ var AppDispatcher;
+ var TodoStore;
+ var callback;
+
+ // mock actions
+ var actionTodoCreate = {
+ actionType: TodoConstants.TODO_CREATE,
+ text: 'foo'
+ };
+ var actionTodoDestroy = {
+ actionType: TodoConstants.TODO_DESTROY,
+ id: 'replace me in test'
+ };
+
+ beforeEach(function() {
+ AppDispatcher = require('../../dispatcher/AppDispatcher');
+ TodoStore = require('../TodoStore');
+ callback = AppDispatcher.register.mock.calls[0][0];
+ });
+
+ it('registers a callback with the dispatcher', function() {
+ expect(AppDispatcher.register.mock.calls.length).toBe(1);
+ });
+
+ it('should initialize with no to-do items', function() {
+ var all = TodoStore.getAll();
+ expect(all).toEqual({});
+ });
+
+ it('creates a to-do item', function() {
+ callback(actionTodoCreate);
+ var all = TodoStore.getAll();
+ var keys = Object.keys(all);
+ expect(keys.length).toBe(1);
+ expect(all[keys[0]].text).toEqual('foo');
+ });
+
+ it('destroys a to-do item', function() {
+ callback(actionTodoCreate);
+ var all = TodoStore.getAll();
+ var keys = Object.keys(all);
+ expect(keys.length).toBe(1);
+ actionTodoDestroy.id = keys[0];
+ callback(actionTodoDestroy);
+ expect(all[keys[0]]).toBeUndefined();
+ });
+
+ it('can determine whether all to-do items are complete', function() {
+ var i = 0;
+ for (; i < 3; i++) {
+ callback(actionTodoCreate);
+ }
+ expect(Object.keys(TodoStore.getAll()).length).toBe(3);
+ expect(TodoStore.areAllComplete()).toBe(false);
+
+ var all = TodoStore.getAll();
+ for (key in all) {
+ callback({
+ actionType: TodoConstants.TODO_COMPLETE,
+ id: key
+ });
+ }
+ expect(TodoStore.areAllComplete()).toBe(true);
+
+ callback({
+ actionType: TodoConstants.TODO_UNDO_COMPLETE,
+ id: key
+ });
+ expect(TodoStore.areAllComplete()).toBe(false);
+ });
+
+});
bgstack15