diff --git a/src/js/dao/email-dao.js b/src/js/dao/email-dao.js index 8c89ba6..28097e9 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) { + FOLDER_TYPE_TRASH, + FOLDER_TYPE_FLAGGED + ]; + + // 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..e602163 100644 --- a/test/unit/email-dao-test.js +++ b/test/unit/email-dao-test.js @@ -25,7 +25,7 @@ describe('Email DAO unit tests', function() { var emailAddress, passphrase, asymKeySize, account; // test data - var folders, inboxFolder, sentFolder, draftsFolder, outboxFolder, trashFolder, otherFolder, mockKeyPair; + var folders, inboxFolder, sentFolder, draftsFolder, outboxFolder, trashFolder, flaggedFolder, otherFolder, mockKeyPair; beforeEach(function() { // @@ -70,6 +70,13 @@ describe('Email DAO unit tests', function() { messages: [] }; + flaggedFolder = { + name: 'Flagged', + type: 'Flagged', + path: 'FLAGGED', + messages: [] + }; + otherFolder = { name: 'Other', type: 'Other', @@ -2105,6 +2112,7 @@ describe('Email DAO unit tests', function() { Sent: [sentFolder], Drafts: [draftsFolder], Trash: [trashFolder], + Flagged: [flaggedFolder], Other: [otherFolder] }); devicestorageStub.storeList.withArgs(sinon.match(function(arg) { @@ -2114,18 +2122,21 @@ 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(flaggedFolder.name); + expect(arg[0][5].path).to.deep.equal(flaggedFolder.path); + expect(arg[0][5].type).to.deep.equal(flaggedFolder.type); + expect(arg[0][6].name).to.deep.equal(otherFolder.name); + expect(arg[0][6].path).to.deep.equal(otherFolder.path); + expect(arg[0][6].type).to.deep.equal(otherFolder.type); return true; }), 'folders').yieldsAsync(); @@ -2151,6 +2162,7 @@ describe('Email DAO unit tests', function() { Sent: [sentFolder], Drafts: [draftsFolder], Trash: [trashFolder], + Flagged: [flaggedFolder], Other: [otherFolder] }); devicestorageStub.storeList.withArgs(sinon.match(function(arg) { @@ -2159,25 +2171,30 @@ describe('Email DAO unit tests', function() { path: inboxFolder.path, type: inboxFolder.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: sentFolder.name, - path: sentFolder.path, - type: sentFolder.type, - wellknown: true - }, { - name: draftsFolder.name, - path: draftsFolder.path, - type: draftsFolder.type, + name: flaggedFolder.name, + path: flaggedFolder.path, + type: flaggedFolder.type, wellknown: true }, { name: otherFolder.name,