rework account-folders relation

This commit is contained in:
Christoph Wurst 2016-09-20 23:33:10 +02:00
Родитель 39757c9f2d
Коммит d4ad3af802
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: CC42AC2A7F0E56D8
12 изменённых файлов: 81 добавлений и 64 удалений

Просмотреть файл

@ -91,7 +91,7 @@ define(function(require) {
function checkForNotifications(accounts) { function checkForNotifications(accounts) {
accounts.each(function(account) { accounts.each(function(account) {
var folders = account.get('folders'); var folders = account.folders;
var url = OC.generateUrl('apps/mail/accounts/{id}/folders/detectChanges', var url = OC.generateUrl('apps/mail/accounts/{id}/folders/detectChanges',
{ {
@ -130,7 +130,9 @@ define(function(require) {
// reload if current selected folder has changed // reload if current selected folder has changed
if (State.currentAccount === changedAccount && if (State.currentAccount === changedAccount &&
State.currentFolder.get('id') === changes.id) { 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); var messages = new MessageCollection(changes.messages).slice(0);
Radio.message.trigger('fetch:bodies', changedAccount, changedFolder, messages); Radio.message.trigger('fetch:bodies', changedAccount, changedFolder, messages);
} }

Просмотреть файл

@ -89,8 +89,6 @@ define(function(require) {
// Fade out the message composer // Fade out the message composer
$('#mail_new_message').prop('disabled', false); $('#mail_new_message').prop('disabled', false);
Radio.ui.trigger('messagesview:messages:add', messages);
if (messages.length > 0) { if (messages.length > 0) {
// Fetch first 10 messages in background // Fetch first 10 messages in background
Radio.message.trigger('fetch:bodies', account, folder, messages.slice(0, 10)); Radio.message.trigger('fetch:bodies', account, folder, messages.slice(0, 10));

Просмотреть файл

@ -21,7 +21,6 @@ define(function(require) {
*/ */
var Account = Backbone.Model.extend({ var Account = Backbone.Model.extend({
defaults: { defaults: {
folders: [],
aliases: [] aliases: []
}, },
idAttribute: 'accountId', idAttribute: 'accountId',
@ -29,7 +28,7 @@ define(function(require) {
return OC.generateUrl('apps/mail/accounts'); return OC.generateUrl('apps/mail/accounts');
}, },
initialize: function() { initialize: function() {
this.set('folders', new FolderCollection(this.get('folders'))); this.folders = new FolderCollection();
this.set('aliases', new AliasesCollection(this.get('aliases'))); this.set('aliases', new AliasesCollection(this.get('aliases')));
}, },
_getFolderByIdRecursively: function(folder, folderId) { _getFolderByIdRecursively: function(folder, folderId) {
@ -41,7 +40,7 @@ define(function(require) {
return folder; return folder;
} }
var subFolders = folder.get('folders'); var subFolders = folder.folders;
if (!subFolders) { if (!subFolders) {
return null; return null;
} }
@ -54,13 +53,20 @@ define(function(require) {
return null; return null;
}, },
/**
* @param {Folder} folder
* @returns {undefined}
*/
addFolder: function(folder) {
folder = this.folders.add(folder);
folder.account = this;
},
getFolderById: function(folderId) { getFolderById: function(folderId) {
var folders = this.get('folders'); if (!this.folders) {
if (!folders) {
return undefined; return undefined;
} }
for (var i = 0; i < folders.length; i++) { for (var i = 0; i < this.folders.length; i++) {
var result = this._getFolderByIdRecursively(folders.at(i), folderId); var result = this._getFolderByIdRecursively(this.folders.at(i), folderId);
if (result) { if (result) {
return result; return result;
} }

Просмотреть файл

@ -18,14 +18,17 @@ define(function(require) {
*/ */
var Folder = Backbone.Model.extend({ var Folder = Backbone.Model.extend({
messages: undefined, messages: undefined,
account: undefined,
folder: undefined,
folders: undefined,
defaults: { defaults: {
open: false, open: false,
folders: [] folders: [],
}, },
initialize: function() { initialize: function() {
var FolderCollection = require('models/foldercollection'); var FolderCollection = require('models/foldercollection');
var MessageCollection = require('models/messagecollection'); var MessageCollection = require('models/messagecollection');
this.set('folders', new FolderCollection(this.get('folders'))); this.folders = new FolderCollection(this.get('folders') || []);
this.messages = new MessageCollection(); this.messages = new MessageCollection();
}, },
toggleOpen: function() { toggleOpen: function() {
@ -36,8 +39,16 @@ define(function(require) {
* @returns {undefined} * @returns {undefined}
*/ */
addMessage: function(message) { addMessage: function(message) {
message.folder = this;
this.messages.add(message); 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() { toJSON: function() {
var data = Backbone.Model.prototype.toJSON.call(this); var data = Backbone.Model.prototype.toJSON.call(this);

Просмотреть файл

@ -95,7 +95,7 @@ define(function(require) {
// Show first folder of first account // Show first folder of first account
var firstAccount = this.accounts.at(0); 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')); _this.showFolder(firstAccount.get('accountId'), firstFolder.get('id'));
}, },
showFolder: function(accountId, folderId, noSelect) { showFolder: function(accountId, folderId, noSelect) {
@ -111,7 +111,7 @@ define(function(require) {
var folder = account.getFolderById(folderId); var folder = account.getFolderById(folderId);
if (_.isUndefined(folder)) { if (_.isUndefined(folder)) {
folder = account.get('folders').at(0); folder = account.folders.at(0);
Radio.ui.trigger('error:show', t('mail', 'Invalid folder')); Radio.ui.trigger('error:show', t('mail', 'Invalid folder'));
this._navigate('accounts/' + accountId + '/folders/' + folder.get('id')); this._navigate('accounts/' + accountId + '/folders/' + folder.get('id'));
} }
@ -134,7 +134,7 @@ define(function(require) {
var folder = account.getFolderById(folderId); var folder = account.getFolderById(folderId);
if (_.isUndefined(folder)) { if (_.isUndefined(folder)) {
folder = account.get('folders').at(0); folder = account.folders.at(0);
Radio.ui.trigger('error:show', t('mail', 'Invalid folder')); Radio.ui.trigger('error:show', t('mail', 'Invalid folder'));
this._navigate('accounts/' + accountId + '/folders/' + folder.get('id')); this._navigate('accounts/' + accountId + '/folders/' + folder.get('id'));
} }

Просмотреть файл

@ -31,13 +31,13 @@ define(function(require) {
promise.done(function(data) { promise.done(function(data) {
for (var prop in data) { for (var prop in data) {
if (prop === 'folders') { if (prop === 'folders') {
account.get('folders').reset(); account.folders.reset();
account.get('folders').add(data.folders); account.addFolder(data.folders);
} else { } else {
account.set(prop, data[prop]); account.set(prop, data[prop]);
} }
} }
defer.resolve(account.get('folders')); defer.resolve(account.folders);
}); });
promise.fail(function() { promise.fail(function() {

Просмотреть файл

@ -20,35 +20,43 @@
define(['models/account', define(['models/account',
'models/foldercollection', 'models/foldercollection',
'models/aliasescollection', 'models/aliasescollection',
'OC'], 'models/folder',
function(Account, FolderCollection, AliasCollection, OC) { 'OC'
describe('Account test', function() { ], function(Account, FolderCollection, AliasCollection, Folder, OC) {
var account; 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');
});
beforeEach(function() {
account = new Account();
}); });
});
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);
});
});
});

Просмотреть файл

@ -58,7 +58,7 @@ define(function(require) {
menuShown: false, menuShown: false,
initialize: function(options) { initialize: function(options) {
this.model = options.model; this.model = options.model;
this.collection = this.model.get('folders'); this.collection = this.model.folders;
}, },
filter: function(child) { filter: function(child) {
if (!this.collapsed) { if (!this.collapsed) {
@ -104,9 +104,9 @@ define(function(require) {
}, },
onClick: function(e) { onClick: function(e) {
e.preventDefault(); e.preventDefault();
if (this.model.get('folders').length > 0) { if (this.model.folders.length > 0) {
var accountId = this.model.get('accountId'); 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); Radio.navigation.trigger('folder', accountId, folderId);
} }
}, },

Просмотреть файл

@ -56,7 +56,7 @@ define(function(require) {
if (folderId === this.model.get('id')) { if (folderId === this.model.get('id')) {
folder = this.model; folder = this.model;
} else { } else {
folder = this.model.get('folders').get(folderId); folder = this.model.folders.get(folderId);
} }
var noSelect = $(e.currentTarget).parent().data('no_select'); var noSelect = $(e.currentTarget).parent().data('no_select');
Radio.navigation.trigger('folder', account.get('accountId'), folder.get('id'), noSelect); Radio.navigation.trigger('folder', account.get('accountId'), folder.get('id'), noSelect);

Просмотреть файл

@ -50,7 +50,6 @@ define(function(require) {
return _this.collection; return _this.collection;
}); });
this.listenTo(Radio.ui, 'messagesview:messages:update', this.refresh); 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:messageflag:set', this.setMessageFlag);
this.listenTo(Radio.ui, 'messagesview:filter', this.filterCurrentMailbox); this.listenTo(Radio.ui, 'messagesview:filter', this.filterCurrentMailbox);
this.listenTo(Radio.ui, 'messagesview:message:setactive', this.setActiveMessage); 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 // scroll event is fired, which we want to ignore
_this.reloaded = reload; _this.reloaded = reload;
}); });
},
addMessages: function(message) {
var _this = this;
// TODO: merge?
message.each(function(msg) {
_this.collection.add(msg);
});
} }
}); });
}); });

Просмотреть файл

@ -43,7 +43,7 @@ define(function(require) {
if (_.isUndefined(localAccount)) { if (_.isUndefined(localAccount)) {
return; return;
} }
var folders = localAccount.get('folders'); var folders = localAccount.folders;
_.each(folders.models, function(folder) { _.each(folders.models, function(folder) {
folders.get(folder).set('active', false); folders.get(folder).set('active', false);
}); });

Просмотреть файл

@ -153,7 +153,7 @@ define(function(require) {
// Let's assume there's at least one account after a successful // 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) // setup, so let's show the first one (could be the unified inbox)
var firstAccount = accounts.first(); 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')); Radio.navigation.trigger('folder', firstAccount.get('accountId'), firstFolder.get('id'));
}); });
}); });