diff --git a/src/background.js b/src/background.js index 2de715fad2..2fa2479264 100644 --- a/src/background.js +++ b/src/background.js @@ -276,7 +276,7 @@ function loadMenuAndUpdateBadge(url, tabId, contextMenuEnabled) { menuOptionsLoaded = []; bg_loginService.getAllDecryptedForDomain(tabDomain).then(function (logins) { - sortLogins(logins); + logins.sort(bg_loginService.sortLoginsByLastUsedThenName); if (contextMenuEnabled) { for (var i = 0; i < logins.length; i++) { @@ -588,37 +588,6 @@ function autofillPage() { pageDetailsToAutoFill = []; } -function sortLogins(logins) { - logins.sort(function (a, b) { - var lastUsedA = a.localData && a.localData.lastUsedDate ? a.localData.lastUsedDate : null; - var lastUsedB = b.localData && b.localData.lastUsedDate ? b.localData.lastUsedDate : null; - if (lastUsedA && lastUsedB && lastUsedA > lastUsedB) { - return -1; - } - if (lastUsedA && lastUsedB && lastUsedA < lastUsedB) { - return 1; - } - if (lastUsedA && !lastUsedB) { - return -1; - } - if (!lastUsedA && lastUsedB) { - return 1; - } - - var nameA = (a.name + '_' + a.username).toUpperCase(); - var nameB = (b.name + '_' + b.username).toUpperCase(); - - if (nameA < nameB) { - return -1; - } - if (nameA > nameB) { - return 1; - } - - return 0; - }); -} - function loadLoginContextMenuOptions(login) { var title = login.name + (login.username && login.username !== '' ? ' (' + login.username + ')' : ''); loadContextMenuOptions(title, login.id, login); diff --git a/src/services/loginService.js b/src/services/loginService.js index d58d8aedfd..266b9fdeb0 100644 --- a/src/services/loginService.js +++ b/src/services/loginService.js @@ -518,6 +518,25 @@ function initLoginService() { return 0; }; + LoginService.prototype.sortLoginsByLastUsedThenName = function (a, b) { + var result = this.sortLoginsByLastUsed(a, b); + if (result !== 0) { + return result; + } + + var nameA = (a.name + '_' + a.username).toUpperCase(); + var nameB = (b.name + '_' + b.username).toUpperCase(); + + if (nameA < nameB) { + return -1; + } + if (nameA > nameB) { + return 1; + } + + return 0; + }; + function handleError(error, deferred) { deferred.reject(error); }