move sort to service

This commit is contained in:
Kyle Spearrin 2017-08-30 08:35:34 -04:00
parent 7ce6634120
commit ae6c609c91
2 changed files with 20 additions and 32 deletions

View File

@ -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);

View File

@ -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);
}