diff --git a/src/js/dao/email-dao.js b/src/js/dao/email-dao.js index 8c89ba6..a5362de 100644 --- a/src/js/dao/email-dao.js +++ b/src/js/dao/email-dao.js @@ -632,7 +632,7 @@ EmailDAO.prototype.setFlags = function(options, callback) { /** * Moves a message to another folder - * + * * @param {Object} options.folder The origin folder * @param {Object} options.destination The destination folder * @param {Object} options.message The message that should be moved @@ -1436,14 +1436,18 @@ EmailDAO.prototype._initFoldersFromImap = function(callback) { // // check for the well known folders to be displayed in the uppermost ui part - [ + // in that order + var wellknownTypes = [ FOLDER_TYPE_INBOX, FOLDER_TYPE_SENT, config.outboxMailboxType, FOLDER_TYPE_DRAFTS, FOLDER_TYPE_FLAGGED, FOLDER_TYPE_TRASH - ].forEach(function(mbxType) { + ]; + + // make sure the well known folders are detected + wellknownTypes.forEach(function(mbxType) { // check if there is a well known folder of this type var wellknownFolder = _.findWhere(self._account.folders, { type: mbxType, @@ -1471,6 +1475,23 @@ EmailDAO.prototype._initFoldersFromImap = function(callback) { foldersChanged = true; }); + // order folders + self._account.folders.sort(function(a, b) { + if (a.wellknown && b.wellknown) { + // well known folders should be ordered like the types in the wellknownTypes array + return wellknownTypes.indexOf(a.type) - wellknownTypes.indexOf(b.type); + } else if (a.wellknown && !b.wellknown) { + // wellknown folders should always appear BEFORE the other folders + return -1; + } else if (!a.wellknown && b.wellknown) { + // non-wellknown folders should always appear AFTER wellknown folders + return 1; + } else { + // non-wellknown folders should be sorted case-insensitive + return a.name.toLowerCase().localeCompare(b.name.toLowerCase()); + } + }); + // if folders have not changed, can fill them with messages directly if (!foldersChanged) { return self._initMessagesFromDisk(done); diff --git a/test/unit/email-dao-test.js b/test/unit/email-dao-test.js index 646aac6..8f467c0 100644 --- a/test/unit/email-dao-test.js +++ b/test/unit/email-dao-test.js @@ -2114,18 +2114,18 @@ describe('Email DAO unit tests', function() { expect(arg[0][1].name).to.deep.equal(sentFolder.name); expect(arg[0][1].path).to.deep.equal(sentFolder.path); expect(arg[0][1].type).to.deep.equal(sentFolder.type); - expect(arg[0][2].name).to.deep.equal(draftsFolder.name); - expect(arg[0][2].path).to.deep.equal(draftsFolder.path); - expect(arg[0][2].type).to.deep.equal(draftsFolder.type); - expect(arg[0][3].name).to.deep.equal(trashFolder.name); - expect(arg[0][3].path).to.deep.equal(trashFolder.path); - expect(arg[0][3].type).to.deep.equal(trashFolder.type); - expect(arg[0][4].name).to.deep.equal(otherFolder.name); - expect(arg[0][4].path).to.deep.equal(otherFolder.path); - expect(arg[0][4].type).to.deep.equal(otherFolder.type); - expect(arg[0][5].name).to.deep.equal(outboxFolder.name); - expect(arg[0][5].path).to.deep.equal(outboxFolder.path); - expect(arg[0][5].type).to.deep.equal(outboxFolder.type); + expect(arg[0][2].name).to.deep.equal(outboxFolder.name); + expect(arg[0][2].path).to.deep.equal(outboxFolder.path); + expect(arg[0][2].type).to.deep.equal(outboxFolder.type); + expect(arg[0][3].name).to.deep.equal(draftsFolder.name); + expect(arg[0][3].path).to.deep.equal(draftsFolder.path); + expect(arg[0][3].type).to.deep.equal(draftsFolder.type); + expect(arg[0][4].name).to.deep.equal(trashFolder.name); + expect(arg[0][4].path).to.deep.equal(trashFolder.path); + expect(arg[0][4].type).to.deep.equal(trashFolder.type); + expect(arg[0][5].name).to.deep.equal(otherFolder.name); + expect(arg[0][5].path).to.deep.equal(otherFolder.path); + expect(arg[0][5].type).to.deep.equal(otherFolder.type); return true; }), 'folders').yieldsAsync(); @@ -2159,26 +2159,26 @@ describe('Email DAO unit tests', function() { path: inboxFolder.path, type: inboxFolder.type, wellknown: true - }, { - name: outboxFolder.name, - path: outboxFolder.path, - type: outboxFolder.type, - wellknown: true - }, { - name: trashFolder.name, - path: trashFolder.path, - type: trashFolder.type, - wellknown: true }, { name: sentFolder.name, path: sentFolder.path, type: sentFolder.type, wellknown: true + }, { + name: outboxFolder.name, + path: outboxFolder.path, + type: outboxFolder.type, + wellknown: true }, { name: draftsFolder.name, path: draftsFolder.path, type: draftsFolder.type, wellknown: true + }, { + name: trashFolder.name, + path: trashFolder.path, + type: trashFolder.type, + wellknown: true }, { name: otherFolder.name, path: otherFolder.path,