diff options
Diffstat (limited to 'js')
-rw-r--r-- | js/app/controllers/controllers.coffee | 15 | ||||
-rw-r--r-- | js/app/controllers/feedcontroller.coffee | 35 | ||||
-rw-r--r-- | js/app/services/models/feedmodel.coffee | 33 | ||||
-rw-r--r-- | js/app/services/models/itemmodel.coffee | 15 | ||||
-rw-r--r-- | js/app/services/showall.coffee | 3 | ||||
-rw-r--r-- | js/app/services/starredcount.coffee | 4 | ||||
-rw-r--r-- | js/public/app.js | 123 | ||||
-rw-r--r-- | js/tests/controllers/feedcontrollerSpec.coffee | 174 | ||||
-rw-r--r-- | js/tests/services/showallSpec.coffee | 17 | ||||
-rw-r--r-- | js/tests/services/starredcountSpec.coffee | 15 |
10 files changed, 382 insertions, 52 deletions
diff --git a/js/app/controllers/controllers.coffee b/js/app/controllers/controllers.coffee index 59f2db39e..a302b1033 100644 --- a/js/app/controllers/controllers.coffee +++ b/js/app/controllers/controllers.coffee @@ -29,13 +29,14 @@ angular.module('News').controller 'SettingsController', angular.module('News').controller 'FeedController', -['$scope', '_FeedController', 'FolderModel', 'FeedModel', 'ActiveFeed', -'ShowAll', 'FeedType', 'StarredCount', -($scope, _FeedController, FolderModel, FeedModel, ActiveFeed, -ShowAll, FeedType, StarredCount)-> - - return new _FeedController($scope, FolderModel, FeedModel, ActiveFeed, - ShowAll, FeedType, StarredCount) +['$scope', '_FeedController', 'FolderModel', 'FeedModel', 'ActiveFeed', +'ShowAll', 'FeedType', 'StarredCount', 'Persistence', 'ItemModel', +($scope, _FeedController, FolderModel, FeedModel, ActiveFeed, +ShowAll, FeedType, StarredCount, Persistence, ItemModel)-> + + return new _FeedController($scope, FolderModel, FeedModel, ActiveFeed, + ShowAll, FeedType, StarredCount, Persistence, + ItemModel) ] angular.module('News').controller 'ItemController', diff --git a/js/app/controllers/feedcontroller.coffee b/js/app/controllers/feedcontroller.coffee index 535ed00ac..ddd35d2f8 100644 --- a/js/app/controllers/feedcontroller.coffee +++ b/js/app/controllers/feedcontroller.coffee @@ -25,8 +25,9 @@ angular.module('News').factory '_FeedController', -> class FeedController - constructor: (@$scope, @_folderModel, @_feedModel, @_active, - @_showAll, @_feedType, @_starredCount) -> + constructor: (@$scope, @_folderModel, @_feedModel, @_active, + @_showAll, @_feedType, @_starredCount, @_persistence, + @_itemModel) -> # bind internal stuff to scope @$scope.feeds = @_feedModel.getAll() @@ -80,12 +81,34 @@ angular.module('News').factory '_FeedController', -> getUnreadCount: (type, id) -> + switch type + when @_feedType.Subscriptions + count = @_feedModel.getUnreadCount() + when @_feedType.Starred + count = @_starredCount.getStarredCount() + when @_feedType.Feed + count = @_feedModel.getFeedUnreadCount(id) + when @_feedType.Folder + count = @_feedModel.getFolderUnreadCount(id) + + if count > 999 + count = '999+' + + return count loadFeed: (type, id) -> + if type != @_active.getType() or id != @_active.getId() + @_itemModel.clear() + @_persistence.getItems(type, id, 0) + @_active.handle({id: id, type: type}) + else + lastModified = @_itemModel.getLastModified() + @_persistence.getItems(type, id, 0, null, lastModified) hasFeeds: (folderId) -> + return @_feedModel.getAllOfFolder(folderId).length delete: (type, id) -> @@ -95,7 +118,15 @@ angular.module('News').factory '_FeedController', -> getFeedsOfFolder: (folderId) -> + return @_feedModel.getAllOfFolder(folderId) + setShowAll: (showAll) -> + @_showAll.setShowAll(showAll) + if showAll + @_persistence.userSettingsReadShow() + else + @_persistence.userSettingsReadHide() + return FeedController
\ No newline at end of file diff --git a/js/app/services/models/feedmodel.coffee b/js/app/services/models/feedmodel.coffee index 25cc48d88..838f240c1 100644 --- a/js/app/services/models/feedmodel.coffee +++ b/js/app/services/models/feedmodel.coffee @@ -21,7 +21,9 @@ License along with this library. If not, see <http://www.gnu.org/licenses/>. ### -angular.module('News').factory '_FeedModel', ['_Model', (_Model) -> +angular.module('News').factory '_FeedModel', +['_Model', '_EqualQuery', +(_Model, _EqualQuery) -> class FeedModel extends _Model @@ -35,6 +37,35 @@ angular.module('News').factory '_FeedModel', ['_Model', (_Model) -> super(item) + getUnreadCount: -> + count = 0 + for feed in @getAll() + count += feed.unreadCount + + return count + + + getFeedUnreadCount: (feedId) -> + feed = @getById(feedId) + if angular.isDefined(feed) + return feed.unreadCount + else + return 0 + + + getFolderUnreadCount: (folderId) -> + query = new _EqualQuery('folderId', folderId) + count = 0 + for feed in @get(query) + count += feed.unreadCount + + return count + + + getAllOfFolder: (folderId) -> + query = new _EqualQuery('folderId', folderId) + return @get(query) + return FeedModel ]
\ No newline at end of file diff --git a/js/app/services/models/itemmodel.coffee b/js/app/services/models/itemmodel.coffee index d53f17f15..a2191fdbf 100644 --- a/js/app/services/models/itemmodel.coffee +++ b/js/app/services/models/itemmodel.coffee @@ -20,9 +20,22 @@ License along with this library. If not, see <http://www.gnu.org/licenses/>. ### -angular.module('News').factory '_ItemModel', ['_Model', (_Model) -> +angular.module('News').factory '_ItemModel', +['_Model', '_MaximumQuery', '_MinimumQuery', +(_Model, _MaximumQuery, _MinimumQuery) -> class ItemModel extends _Model + + getLastModified: -> + query = new _MaximumQuery('lastModified') + lastModified = @get(query) + + if angular.isDefined(lastModified) + return lastModified.lastModified + else + return null + + return ItemModel ]
\ No newline at end of file diff --git a/js/app/services/showall.coffee b/js/app/services/showall.coffee index 25b093735..a40f176bc 100644 --- a/js/app/services/showall.coffee +++ b/js/app/services/showall.coffee @@ -37,5 +37,8 @@ angular.module('News').factory '_ShowAll', -> return @_showAll + setShowAll: (showAll) -> + @_showAll = showAll + return ShowAll diff --git a/js/app/services/starredcount.coffee b/js/app/services/starredcount.coffee index aaa8fdaff..9d3d9deb5 100644 --- a/js/app/services/starredcount.coffee +++ b/js/app/services/starredcount.coffee @@ -33,6 +33,10 @@ angular.module('News').factory '_StarredCount', -> @_count = data + setStarredCount: (count) -> + @_count = count + + getStarredCount: -> return @_count diff --git a/js/public/app.js b/js/public/app.js index 12d4dd9aa..51f6dcad8 100644 --- a/js/public/app.js +++ b/js/public/app.js @@ -97,8 +97,8 @@ License along with this library. If not, see <http://www.gnu.org/licenses/>. ]); angular.module('News').controller('FeedController', [ - '$scope', '_FeedController', 'FolderModel', 'FeedModel', 'ActiveFeed', 'ShowAll', 'FeedType', 'StarredCount', function($scope, _FeedController, FolderModel, FeedModel, ActiveFeed, ShowAll, FeedType, StarredCount) { - return new _FeedController($scope, FolderModel, FeedModel, ActiveFeed, ShowAll, FeedType, StarredCount); + '$scope', '_FeedController', 'FolderModel', 'FeedModel', 'ActiveFeed', 'ShowAll', 'FeedType', 'StarredCount', 'Persistence', 'ItemModel', function($scope, _FeedController, FolderModel, FeedModel, ActiveFeed, ShowAll, FeedType, StarredCount, Persistence, ItemModel) { + return new _FeedController($scope, FolderModel, FeedModel, ActiveFeed, ShowAll, FeedType, StarredCount, Persistence, ItemModel); } ]); @@ -140,7 +140,7 @@ License along with this library. If not, see <http://www.gnu.org/licenses/>. var FeedController; FeedController = (function() { - function FeedController($scope, _folderModel, _feedModel, _active, _showAll, _feedType, _starredCount) { + function FeedController($scope, _folderModel, _feedModel, _active, _showAll, _feedType, _starredCount, _persistence, _itemModel) { var _this = this; this.$scope = $scope; this._folderModel = _folderModel; @@ -149,6 +149,8 @@ License along with this library. If not, see <http://www.gnu.org/licenses/>. this._showAll = _showAll; this._feedType = _feedType; this._starredCount = _starredCount; + this._persistence = _persistence; + this._itemModel = _itemModel; this.$scope.feeds = this._feedModel.getAll(); this.$scope.folders = this._folderModel.getAll(); this.$scope.feedType = this._feedType; @@ -200,17 +202,62 @@ License along with this library. If not, see <http://www.gnu.org/licenses/>. return this._showAll.getShowAll(); }; - FeedController.prototype.getUnreadCount = function(type, id) {}; + FeedController.prototype.getUnreadCount = function(type, id) { + var count; + switch (type) { + case this._feedType.Subscriptions: + count = this._feedModel.getUnreadCount(); + break; + case this._feedType.Starred: + count = this._starredCount.getStarredCount(); + break; + case this._feedType.Feed: + count = this._feedModel.getFeedUnreadCount(id); + break; + case this._feedType.Folder: + count = this._feedModel.getFolderUnreadCount(id); + } + if (count > 999) { + count = '999+'; + } + return count; + }; - FeedController.prototype.loadFeed = function(type, id) {}; + FeedController.prototype.loadFeed = function(type, id) { + var lastModified; + if (type !== this._active.getType() || id !== this._active.getId()) { + this._itemModel.clear(); + this._persistence.getItems(type, id, 0); + return this._active.handle({ + id: id, + type: type + }); + } else { + lastModified = this._itemModel.getLastModified(); + return this._persistence.getItems(type, id, 0, null, lastModified); + } + }; - FeedController.prototype.hasFeeds = function(folderId) {}; + FeedController.prototype.hasFeeds = function(folderId) { + return this._feedModel.getAllOfFolder(folderId).length; + }; FeedController.prototype["delete"] = function(type, id) {}; FeedController.prototype.markAllRead = function(type, id) {}; - FeedController.prototype.getFeedsOfFolder = function(folderId) {}; + FeedController.prototype.getFeedsOfFolder = function(folderId) { + return this._feedModel.getAllOfFolder(folderId); + }; + + FeedController.prototype.setShowAll = function(showAll) { + this._showAll.setShowAll(showAll); + if (showAll) { + return this._persistence.userSettingsReadShow(); + } else { + return this._persistence.userSettingsReadHide(); + } + }; return FeedController; @@ -437,7 +484,7 @@ License along with this library. If not, see <http://www.gnu.org/licenses/>. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; angular.module('News').factory('_FeedModel', [ - '_Model', function(_Model) { + '_Model', '_EqualQuery', function(_Model, _EqualQuery) { var FeedModel; FeedModel = (function(_super) { @@ -455,6 +502,45 @@ License along with this library. If not, see <http://www.gnu.org/licenses/>. return FeedModel.__super__.add.call(this, item); }; + FeedModel.prototype.getUnreadCount = function() { + var count, feed, _i, _len, _ref; + count = 0; + _ref = this.getAll(); + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + feed = _ref[_i]; + count += feed.unreadCount; + } + return count; + }; + + FeedModel.prototype.getFeedUnreadCount = function(feedId) { + var feed; + feed = this.getById(feedId); + if (angular.isDefined(feed)) { + return feed.unreadCount; + } else { + return 0; + } + }; + + FeedModel.prototype.getFolderUnreadCount = function(folderId) { + var count, feed, query, _i, _len, _ref; + query = new _EqualQuery('folderId', folderId); + count = 0; + _ref = this.get(query); + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + feed = _ref[_i]; + count += feed.unreadCount; + } + return count; + }; + + FeedModel.prototype.getAllOfFolder = function(folderId) { + var query; + query = new _EqualQuery('folderId', folderId); + return this.get(query); + }; + return FeedModel; })(_Model); @@ -541,7 +627,7 @@ License along with this library. If not, see <http://www.gnu.org/licenses/>. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; angular.module('News').factory('_ItemModel', [ - '_Model', function(_Model) { + '_Model', '_MaximumQuery', '_MinimumQuery', function(_Model, _MaximumQuery, _MinimumQuery) { var ItemModel; ItemModel = (function(_super) { @@ -551,6 +637,17 @@ License along with this library. If not, see <http://www.gnu.org/licenses/>. return ItemModel.__super__.constructor.apply(this, arguments); } + ItemModel.prototype.getLastModified = function() { + var lastModified, query; + query = new _MaximumQuery('lastModified'); + lastModified = this.get(query); + if (angular.isDefined(lastModified)) { + return lastModified.lastModified; + } else { + return null; + } + }; + return ItemModel; })(_Model); @@ -1224,6 +1321,10 @@ License along with this library. If not, see <http://www.gnu.org/licenses/>. return this._showAll; }; + ShowAll.prototype.setShowAll = function(showAll) { + return this._showAll = showAll; + }; + return ShowAll; })(); @@ -1270,6 +1371,10 @@ License along with this library. If not, see <http://www.gnu.org/licenses/>. return this._count = data; }; + StarredCount.prototype.setStarredCount = function(count) { + return this._count = count; + }; + StarredCount.prototype.getStarredCount = function() { return this._count; }; diff --git a/js/tests/controllers/feedcontrollerSpec.coffee b/js/tests/controllers/feedcontrollerSpec.coffee index a356e8f81..f338dcf75 100644 --- a/js/tests/controllers/feedcontrollerSpec.coffee +++ b/js/tests/controllers/feedcontrollerSpec.coffee @@ -28,36 +28,33 @@ describe '_FeedController', -> beforeEach inject (@_FeedController, @ActiveFeed, @ShowAll, @FeedType, - @StarredCount) => + @StarredCount, @FeedModel, @FolderModel, @ItemModel) => @scope = {} - @feedModel = - getAll: -> - @folderModel = - getAll: -> - @controller = new @_FeedController(@scope, @folderModel, @feedModel, @ActiveFeed, - @ShowAll, @FeedType, @StarredCount) + @persistence = { + getItems: -> + } + @controller = new @_FeedController(@scope, @FolderModel, @FeedModel, + @ActiveFeed, @ShowAll, @FeedType, + @StarredCount, @persistence, + @ItemModel) it 'should make folders available', => - @folderModel = - getAll: jasmine.createSpy('FolderModel') - - new @_FeedController(@scope, @folderModel, @feedModel, @_ActiveFeed) + @FolderModel.getAll = jasmine.createSpy('FolderModel') + new @_FeedController(@scope, @FolderModel, @FeedModel, @_ActiveFeed) - expect(@folderModel.getAll).toHaveBeenCalled() + expect(@FolderModel.getAll).toHaveBeenCalled() it 'should make feeds availabe', => - @feedModel = - getAll: jasmine.createSpy('FeedModel') - - new @_FeedController(@scope, @folderModel, @feedModel, @_ActiveFeed) + @FeedModel.getAll = jasmine.createSpy('FeedModel') + new @_FeedController(@scope, @FolderModel, @FeedModel, @_ActiveFeed) - expect(@feedModel.getAll).toHaveBeenCalled() + expect(@FeedModel.getAll).toHaveBeenCalled() it 'should make feedtype available', => - expect(@scope.feedType).toBe(@FeedType) + expect(@scope.feedType).toBe(@FeedType) it 'should check the active feed', => @@ -72,5 +69,142 @@ describe '_FeedController', -> it 'should provide ShowAll', => expect(@scope.isShowAll()).toBeFalsy() - @ShowAll.handle(true) - expect(@scope.isShowAll()).toBeTruthy()
\ No newline at end of file + @ShowAll.setShowAll(true) + expect(@scope.isShowAll()).toBeTruthy() + + + it 'should handle show all correctly', => + @persistence.userSettingsReadHide = jasmine.createSpy('hide') + @persistence.userSettingsReadShow = jasmine.createSpy('show') + + @scope.setShowAll(true) + expect(@ShowAll.getShowAll()).toBeTruthy() + expect(@persistence.userSettingsReadShow).toHaveBeenCalled() + expect(@persistence.userSettingsReadHide).not.toHaveBeenCalled() + + + it 'should handle hide all correctly', => + @persistence.userSettingsReadHide = jasmine.createSpy('hide') + @persistence.userSettingsReadShow = jasmine.createSpy('show') + + @scope.setShowAll(false) + expect(@ShowAll.getShowAll()).toBeFalsy() + expect(@persistence.userSettingsReadShow).not.toHaveBeenCalled() + expect(@persistence.userSettingsReadHide).toHaveBeenCalled() + + + it 'should get the correct count for starred items', => + @StarredCount.setStarredCount(133) + count = @scope.getUnreadCount(@FeedType.Starred, 0) + + expect(count).toBe(133) + + + it 'should set the count to 999+ if the count is over 999', => + @StarredCount.setStarredCount(1000) + count = @scope.getUnreadCount(@FeedType.Starred, 0) + + expect(count).toBe('999+') + + + it 'should get the correct unread count for feeds', => + @FeedModel.add({id: 3, unreadCount:134}) + count = @scope.getUnreadCount(@FeedType.Feed, 3) + + expect(count).toBe(134) + + + it 'should get the correct unread count for subscribtions', => + @FeedModel.add({id: 3, unreadCount:134}) + @FeedModel.add({id: 5, unreadCount:2}) + count = @scope.getUnreadCount(@FeedType.Subscriptions, 0) + + expect(count).toBe(136) + + + it 'should get the correct unread count for folders', => + @FeedModel.add({id: 3, unreadCount:134, folderId: 3}) + @FeedModel.add({id: 5, unreadCount:2, folderId: 2}) + @FeedModel.add({id: 1, unreadCount:12, folderId: 5}) + @FeedModel.add({id: 2, unreadCount:35, folderId: 3}) + count = @scope.getUnreadCount(@FeedType.Folder, 3) + + expect(count).toBe(169) + + + it 'should reset the item cache when a different feed is being loaded', => + @ItemModel.clear = jasmine.createSpy('clear') + @ActiveFeed.handle({id: 3, type: 3}) + @scope.loadFeed(3, 3) + + expect(@ItemModel.clear).not.toHaveBeenCalled() + + @scope.loadFeed(3, 4) + expect(@ItemModel.clear).toHaveBeenCalled() + + + it 'should send a get latest items query when feed did not change', => + @ItemModel.add({id: 1, lastModified: 5}) + @ItemModel.add({id: 2, lastModified: 1}) + @ItemModel.add({id: 4, lastModified: 323}) + @ItemModel.add({id: 6, lastModified: 44}) + @persistence.getItems = jasmine.createSpy('latest') + @ActiveFeed.handle({id: 3, type: 3}) + @scope.loadFeed(3, 3) + + expect(@persistence.getItems).toHaveBeenCalledWith(3, 3, 0, null, 323) + + + it 'should send a get all items query when feed changed', => + @persistence.getItems = jasmine.createSpy('latest') + @ActiveFeed.handle({id: 3, type: 3}) + @scope.loadFeed(4, 3) + + expect(@persistence.getItems).toHaveBeenCalledWith(4, 3, 0) + + + it 'should set active feed to new feed if changed', => + @ActiveFeed.handle({id: 3, type: 3}) + @scope.loadFeed(4, 3) + + expect(@ActiveFeed.getId()).toBe(3) + expect(@ActiveFeed.getType()).toBe(4) + + + it 'should return true when calling isShown and ShowAll is set to true', => + @ShowAll.setShowAll(true) + expect(@scope.isShown(3, 4)).toBeTruthy() + + @ShowAll.setShowAll(false) + expect(@scope.isShown(3, 4)).toBeFalsy() + + + it 'should return true if ShowAll is false but unreadcount is not 0', => + @ShowAll.setShowAll(false) + @FeedModel.add({id: 4, unreadCount: 0}) + expect(@scope.isShown(@FeedType.Feed, 4)).toBeFalsy() + + @FeedModel.add({id: 4, unreadCount: 12}) + expect(@scope.isShown(@FeedType.Feed, 4)).toBeTruthy() + + + it 'should return all feeds of a folder', => + @FeedModel.add({id: 3, unreadCount:134, folderId: 3}) + @FeedModel.add({id: 5, unreadCount:2, folderId: 2}) + @FeedModel.add({id: 1, unreadCount:12, folderId: 5}) + @FeedModel.add({id: 2, unreadCount:35, folderId: 3}) + + result = @scope.getFeedsOfFolder(3) + + expect(result).toContain(@FeedModel.getById(3)) + expect(result).toContain(@FeedModel.getById(2)) + expect(result).not.toContain(@FeedModel.getById(1)) + expect(result).not.toContain(@FeedModel.getById(5)) + + + it 'should return true when folder has feeds', => + @FeedModel.add({id: 5, unreadCount:2, folderId: 2}) + expect(@scope.hasFeeds(3)).toBeFalsy() + + @FeedModel.add({id: 2, unreadCount:35, folderId: 3}) + expect(@scope.hasFeeds(3)).toBeTruthy()
\ No newline at end of file diff --git a/js/tests/services/showallSpec.coffee b/js/tests/services/showallSpec.coffee index 65c163ccf..7eba354f4 100644 --- a/js/tests/services/showallSpec.coffee +++ b/js/tests/services/showallSpec.coffee @@ -27,16 +27,23 @@ describe '_ShowAll', -> beforeEach module 'News' beforeEach inject (@_ShowAll) => + @showAll = new @_ShowAll() it 'should be false by default', => - showAll = new @_ShowAll() + - expect(showAll.getShowAll()).toBeFalsy() + expect(@showAll.getShowAll()).toBeFalsy() it 'should set the correct showAll value', => - showAll = new @_ShowAll() - showAll.handle(true) + @showAll.handle(true) + expect(@showAll.getShowAll()).toBeTruthy() - expect(showAll.getShowAll()).toBeTruthy()
\ No newline at end of file + + it 'should provide a set showall setter', => + @showAll.setShowAll(true) + expect(@showAll.getShowAll()).toBeTruthy() + + @showAll.setShowAll(false) + expect(@showAll.getShowAll()).toBeFalsy() diff --git a/js/tests/services/starredcountSpec.coffee b/js/tests/services/starredcountSpec.coffee index f7b619a95..61598324a 100644 --- a/js/tests/services/starredcountSpec.coffee +++ b/js/tests/services/starredcountSpec.coffee @@ -27,16 +27,17 @@ describe '_StarredCount', -> beforeEach module 'News' beforeEach inject (@_StarredCount) => - + @starred = new @_StarredCount() it 'should be 0 by default', => - starred = new @_StarredCount() - - expect(starred.getStarredCount()).toBe(0) + expect(@starred.getStarredCount()).toBe(0) it 'should set the correct starred count', => - starred = new @_StarredCount() - starred.handle(3) + @starred.handle(3) + expect(@starred.getStarredCount()).toBe(3) + - expect(starred.getStarredCount()).toBe(3)
\ No newline at end of file + it 'should provide a setter', => + @starred.setStarredCount(15) + expect(@starred.getStarredCount()).toBe(15)
\ No newline at end of file |