From d4ad3af8026fcda2d3512a40062ff9460989ac64 Mon Sep 17 00:00:00 2001 From: Christoph Wurst Date: Tue, 20 Sep 2016 23:33:10 +0200 Subject: [PATCH] rework account-folders relation --- js/background.js | 6 ++- js/controller/foldercontroller.js | 2 - js/models/account.js | 20 +++++---- js/models/folder.js | 17 ++++++-- js/routecontroller.js | 6 +-- js/service/folderservice.js | 6 +-- js/tests/models_account_spec.js | 68 +++++++++++++++++-------------- js/views/account.js | 6 +-- js/views/folder.js | 2 +- js/views/messagesview.js | 8 ---- js/views/navigation-accounts.js | 2 +- js/views/setup.js | 2 +- 12 files changed, 81 insertions(+), 64 deletions(-) diff --git a/js/background.js b/js/background.js index 71deac053..2c298a083 100644 --- a/js/background.js +++ b/js/background.js @@ -91,7 +91,7 @@ define(function(require) { function checkForNotifications(accounts) { accounts.each(function(account) { - var folders = account.get('folders'); + var folders = account.folders; var url = OC.generateUrl('apps/mail/accounts/{id}/folders/detectChanges', { @@ -130,7 +130,9 @@ define(function(require) { // reload if current selected folder has changed if (State.currentAccount === changedAccount && State.currentFolder.get('id') === changes.id) { - Radio.ui.request('messagesview:collection').add(changes.messages); + _.each(changes.messages, function(msg) { + State.currentFolder.addMessages(msg); + }); var messages = new MessageCollection(changes.messages).slice(0); Radio.message.trigger('fetch:bodies', changedAccount, changedFolder, messages); } diff --git a/js/controller/foldercontroller.js b/js/controller/foldercontroller.js index 97ff32ae0..5e45f52a6 100644 --- a/js/controller/foldercontroller.js +++ b/js/controller/foldercontroller.js @@ -89,8 +89,6 @@ define(function(require) { // Fade out the message composer $('#mail_new_message').prop('disabled', false); - Radio.ui.trigger('messagesview:messages:add', messages); - if (messages.length > 0) { // Fetch first 10 messages in background Radio.message.trigger('fetch:bodies', account, folder, messages.slice(0, 10)); diff --git a/js/models/account.js b/js/models/account.js index fb11918d2..f10557b3e 100644 --- a/js/models/account.js +++ b/js/models/account.js @@ -21,7 +21,6 @@ define(function(require) { */ var Account = Backbone.Model.extend({ defaults: { - folders: [], aliases: [] }, idAttribute: 'accountId', @@ -29,7 +28,7 @@ define(function(require) { return OC.generateUrl('apps/mail/accounts'); }, initialize: function() { - this.set('folders', new FolderCollection(this.get('folders'))); + this.folders = new FolderCollection(); this.set('aliases', new AliasesCollection(this.get('aliases'))); }, _getFolderByIdRecursively: function(folder, folderId) { @@ -41,7 +40,7 @@ define(function(require) { return folder; } - var subFolders = folder.get('folders'); + var subFolders = folder.folders; if (!subFolders) { return null; } @@ -54,13 +53,20 @@ define(function(require) { return null; }, + /** + * @param {Folder} folder + * @returns {undefined} + */ + addFolder: function(folder) { + folder = this.folders.add(folder); + folder.account = this; + }, getFolderById: function(folderId) { - var folders = this.get('folders'); - if (!folders) { + if (!this.folders) { return undefined; } - for (var i = 0; i < folders.length; i++) { - var result = this._getFolderByIdRecursively(folders.at(i), folderId); + for (var i = 0; i < this.folders.length; i++) { + var result = this._getFolderByIdRecursively(this.folders.at(i), folderId); if (result) { return result; } diff --git a/js/models/folder.js b/js/models/folder.js index d8459df3e..f0485029a 100644 --- a/js/models/folder.js +++ b/js/models/folder.js @@ -18,14 +18,17 @@ define(function(require) { */ var Folder = Backbone.Model.extend({ messages: undefined, + account: undefined, + folder: undefined, + folders: undefined, defaults: { open: false, - folders: [] + folders: [], }, initialize: function() { var FolderCollection = require('models/foldercollection'); var MessageCollection = require('models/messagecollection'); - this.set('folders', new FolderCollection(this.get('folders'))); + this.folders = new FolderCollection(this.get('folders') || []); this.messages = new MessageCollection(); }, toggleOpen: function() { @@ -36,8 +39,16 @@ define(function(require) { * @returns {undefined} */ addMessage: function(message) { - message.folder = this; this.messages.add(message); + message.folder = this; + }, + /** + * @param {Folder} folder + * @returns {undefined} + */ + addFolder: function(folder) { + folder = this.folder.add(folder); + folder.account = this.account; }, toJSON: function() { var data = Backbone.Model.prototype.toJSON.call(this); diff --git a/js/routecontroller.js b/js/routecontroller.js index 480dae3d0..a1dfc84b3 100644 --- a/js/routecontroller.js +++ b/js/routecontroller.js @@ -95,7 +95,7 @@ define(function(require) { // Show first folder of first account var firstAccount = this.accounts.at(0); - var firstFolder = firstAccount.get('folders').at(0); + var firstFolder = firstAccount.folders.at(0); _this.showFolder(firstAccount.get('accountId'), firstFolder.get('id')); }, showFolder: function(accountId, folderId, noSelect) { @@ -111,7 +111,7 @@ define(function(require) { var folder = account.getFolderById(folderId); if (_.isUndefined(folder)) { - folder = account.get('folders').at(0); + folder = account.folders.at(0); Radio.ui.trigger('error:show', t('mail', 'Invalid folder')); this._navigate('accounts/' + accountId + '/folders/' + folder.get('id')); } @@ -134,7 +134,7 @@ define(function(require) { var folder = account.getFolderById(folderId); if (_.isUndefined(folder)) { - folder = account.get('folders').at(0); + folder = account.folders.at(0); Radio.ui.trigger('error:show', t('mail', 'Invalid folder')); this._navigate('accounts/' + accountId + '/folders/' + folder.get('id')); } diff --git a/js/service/folderservice.js b/js/service/folderservice.js index 875519d5f..a5be63ae6 100644 --- a/js/service/folderservice.js +++ b/js/service/folderservice.js @@ -31,13 +31,13 @@ define(function(require) { promise.done(function(data) { for (var prop in data) { if (prop === 'folders') { - account.get('folders').reset(); - account.get('folders').add(data.folders); + account.folders.reset(); + account.addFolder(data.folders); } else { account.set(prop, data[prop]); } } - defer.resolve(account.get('folders')); + defer.resolve(account.folders); }); promise.fail(function() { diff --git a/js/tests/models_account_spec.js b/js/tests/models_account_spec.js index 2b26bec47..cee8d5ede 100644 --- a/js/tests/models_account_spec.js +++ b/js/tests/models_account_spec.js @@ -20,35 +20,43 @@ define(['models/account', 'models/foldercollection', 'models/aliasescollection', - 'OC'], - function(Account, FolderCollection, AliasCollection, OC) { - describe('Account test', function() { - var account; - - beforeEach(function() { - account = new Account(); - }); - - it('has collections as default attributes', function() { - var folders = account.get('folders'); - var aliases = account.get('aliases'); - - expect(folders instanceof FolderCollection).toBe(true); - expect(aliases instanceof AliasCollection).toBe(true); - }); - - it('uses accountId as id attribute', function() { - expect(account.idAttribute).toBe('accountId'); - }); - - it('has the correct URL', function() { - spyOn(OC, 'generateUrl').and.returnValue('index.php/apps/mail/accounts'); - - var url = account.url(); - - expect(url).toBe('index.php/apps/mail/accounts'); - }); - + 'models/folder', + 'OC' +], function(Account, FolderCollection, AliasCollection, Folder, OC) { + describe('Account test', function() { + var account; + beforeEach(function() { + account = new Account(); }); - }); \ No newline at end of file + + it('has collections as default attributes', function() { + var folders = account.folders; + var aliases = account.get('aliases'); + + expect(folders instanceof FolderCollection).toBe(true); + expect(aliases instanceof AliasCollection).toBe(true); + }); + + it('uses accountId as id attribute', function() { + expect(account.idAttribute).toBe('accountId'); + }); + + it('has the correct URL', function() { + spyOn(OC, 'generateUrl').and.returnValue('index.php/apps/mail/accounts'); + + var url = account.url(); + + expect(url).toBe('index.php/apps/mail/accounts'); + }); + + it('adds folders to its collection', function() { + var folder = new Folder(); + + account.addFolder(folder); + + expect(account.folders.length).toBe(1); + expect(folder.account).toBe(account); + }); + }); +}); \ No newline at end of file diff --git a/js/views/account.js b/js/views/account.js index 29ab7b398..304322fe3 100644 --- a/js/views/account.js +++ b/js/views/account.js @@ -58,7 +58,7 @@ define(function(require) { menuShown: false, initialize: function(options) { this.model = options.model; - this.collection = this.model.get('folders'); + this.collection = this.model.folders; }, filter: function(child) { if (!this.collapsed) { @@ -104,9 +104,9 @@ define(function(require) { }, onClick: function(e) { e.preventDefault(); - if (this.model.get('folders').length > 0) { + if (this.model.folders.length > 0) { var accountId = this.model.get('accountId'); - var folderId = this.model.get('folders').first().get('id'); + var folderId = this.model.folders.first().get('id'); Radio.navigation.trigger('folder', accountId, folderId); } }, diff --git a/js/views/folder.js b/js/views/folder.js index 3d16d75c4..2d0fb982f 100644 --- a/js/views/folder.js +++ b/js/views/folder.js @@ -56,7 +56,7 @@ define(function(require) { if (folderId === this.model.get('id')) { folder = this.model; } else { - folder = this.model.get('folders').get(folderId); + folder = this.model.folders.get(folderId); } var noSelect = $(e.currentTarget).parent().data('no_select'); Radio.navigation.trigger('folder', account.get('accountId'), folder.get('id'), noSelect); diff --git a/js/views/messagesview.js b/js/views/messagesview.js index e8501c590..a1ac3b199 100644 --- a/js/views/messagesview.js +++ b/js/views/messagesview.js @@ -50,7 +50,6 @@ define(function(require) { return _this.collection; }); this.listenTo(Radio.ui, 'messagesview:messages:update', this.refresh); - this.listenTo(Radio.ui, 'messagesview:messages:add', this.addMessages); this.listenTo(Radio.ui, 'messagesview:messageflag:set', this.setMessageFlag); this.listenTo(Radio.ui, 'messagesview:filter', this.filterCurrentMailbox); this.listenTo(Radio.ui, 'messagesview:message:setactive', this.setActiveMessage); @@ -263,13 +262,6 @@ define(function(require) { // scroll event is fired, which we want to ignore _this.reloaded = reload; }); - }, - addMessages: function(message) { - var _this = this; - // TODO: merge? - message.each(function(msg) { - _this.collection.add(msg); - }); } }); }); diff --git a/js/views/navigation-accounts.js b/js/views/navigation-accounts.js index 50f8430a7..1c1706e89 100644 --- a/js/views/navigation-accounts.js +++ b/js/views/navigation-accounts.js @@ -43,7 +43,7 @@ define(function(require) { if (_.isUndefined(localAccount)) { return; } - var folders = localAccount.get('folders'); + var folders = localAccount.folders; _.each(folders.models, function(folder) { folders.get(folder).set('active', false); }); diff --git a/js/views/setup.js b/js/views/setup.js index 30cb71518..e60dab3e0 100644 --- a/js/views/setup.js +++ b/js/views/setup.js @@ -153,7 +153,7 @@ define(function(require) { // Let's assume there's at least one account after a successful // setup, so let's show the first one (could be the unified inbox) var firstAccount = accounts.first(); - var firstFolder = firstAccount.get('folders').first(); + var firstFolder = firstAccount.folders.first(); Radio.navigation.trigger('folder', firstAccount.get('accountId'), firstFolder.get('id')); }); });