Made ranking calculation stateless

This commit is contained in:
Dennis Ploeger 2019-02-05 18:08:06 +00:00
parent e89eac9a32
commit 8cd66607ef
9 changed files with 19 additions and 32 deletions

1
.gitignore vendored
View File

@ -14,3 +14,4 @@ obj/
xcuserdata/
*.suo
.idea/
.vscode

View File

@ -1,5 +1,5 @@
const EntryCollection = require('../collections/entry-collection');
const Ranking = require('../mixins/ranking');
const Ranking = require('../util/ranking');
const urlPartsRegex = /^(\w+:\/\/)?(?:(?:www|wwws|secure)\.)?([^\/]+)\/?(.*)/;
@ -40,7 +40,10 @@ AutoTypeFilter.prototype.prepareFilter = function() {
AutoTypeFilter.prototype.getEntryRank = function(entry) {
let rank = 0;
if (this.titleLower && entry.title) {
rank += this.getStringRank(entry.title.toLowerCase(), this.titleLower);
rank += Ranking.getStringRank(
entry.title.toLowerCase(),
this.titleLower
);
}
if (this.urlParts && entry.url) {
const entryUrlParts = urlPartsRegex.exec(entry.url.toLowerCase());
@ -79,6 +82,4 @@ AutoTypeFilter.prototype.getEntryRank = function(entry) {
return rank;
};
_.extend(AutoTypeFilter.prototype, Ranking);
module.exports = AutoTypeFilter;

View File

@ -20,18 +20,20 @@ const EntryCollection = Backbone.Collection.extend({
'updated': Comparators.dateComparator('updated', true),
'-updated': Comparators.dateComparator('updated', false),
'-attachments': function(x, y) { return this.attachmentSortVal(x).localeCompare(this.attachmentSortVal(y)); },
'-rank': Comparators.rankComparator(false),
'rank': Comparators.rankComparator(true),
'-rank': Comparators.rankComparator()
},
defaultComparator: 'title',
filter: null,
initialize: function(models, options) {
const comparatorName = options && options.comparator || this.defaultComparator;
this.comparator = this.comparators[comparatorName];
},
sortEntries: function(comparator) {
sortEntries: function(comparator, filter) {
this.filter = filter;
this.comparator = this.comparators[comparator] || this.comparators[this.defaultComparator];
this.sort();
},

View File

@ -274,7 +274,7 @@ const AppModel = Backbone.Model.extend({
getEntries: function() {
const entries = this.getEntriesByFilter(this.filter);
entries.sortEntries(this.sort);
entries.sortEntries(this.sort, this.filter);
if (this.filter.trash) {
this.addTrashGroups(entries);
}

View File

@ -5,7 +5,7 @@ const Color = require('../util/color');
const IconUrl = require('../util/icon-url');
const Otp = require('../util/otp');
const kdbxweb = require('kdbxweb');
const Ranking = require('../mixins/ranking');
const Ranking = require('../util/ranking');
const EntryModel = Backbone.Model.extend({
defaults: {},
@ -17,10 +17,6 @@ const EntryModel = Backbone.Model.extend({
fieldRefFields: ['title', 'password', 'user', 'url', 'notes'],
fieldRefIds: { T: 'Title', U: 'UserName', P: 'Password', A: 'URL', N: 'Notes' },
initialize: function() {
this.set({rank: 0});
},
setEntry: function(entry, group, file) {
this.entry = entry;
this.group = group;
@ -187,10 +183,6 @@ const EntryModel = Backbone.Model.extend({
},
matches: function(filter) {
if (filter && filter.textLower && (!filter.advanced || filter.advanced.rank)) {
// update rank, when rank calculation is enabled
this.updateRank(filter.textLower);
}
return !filter ||
(!filter.tagLower || this.searchTags.indexOf(filter.tagLower) >= 0) &&
(!filter.textLower || (filter.advanced ? this.matchesAdv(filter) : this.searchText.indexOf(filter.textLower) >= 0)) &&
@ -643,7 +635,7 @@ const EntryModel = Backbone.Model.extend({
this._fillByEntry();
},
updateRank: function(searchString) {
getRank: function(searchString) {
let rank = 0;
const ranking = [
@ -677,7 +669,7 @@ const EntryModel = Backbone.Model.extend({
_.forEach(ranking, rankingEntry => {
if (this._getFieldString(rankingEntry.field).toLowerCase() !== '') {
const calculatedRank = this.getStringRank(
const calculatedRank = Ranking.getStringRank(
searchString,
this._getFieldString(rankingEntry.field).toLowerCase()
) * rankingEntry.multiplicator;
@ -685,7 +677,7 @@ const EntryModel = Backbone.Model.extend({
}
});
this.set({rank: rank});
return rank;
}
});
@ -708,6 +700,4 @@ EntryModel.newEntry = function(group, file) {
return model;
};
_.extend(EntryModel.prototype, Ranking);
module.exports = EntryModel;

View File

@ -13,12 +13,8 @@ const Comparators = {
}
},
rankComparator: function(asc) {
if (asc) {
return function (x, y) { return x.get('rank') - y.get('rank'); };
} else {
return function (x, y) { return y.get('rank') - x.get('rank'); };
}
rankComparator: function() {
return function (x, y) { return y.getRank(this.filter.text) - x.getRank(this.filter.text); };
},
dateComparator: function(field, asc) {

View File

@ -44,8 +44,7 @@ const ListSearchView = Backbone.View.extend({
{ value: 'updated', icon: 'sort-numeric-asc', loc: () => Locale.searchUpdated + ' ' + this.addArrow(Locale.searchON) },
{ value: '-updated', icon: 'sort-numeric-desc', loc: () => Locale.searchUpdated + ' ' + this.addArrow(Locale.searchNO) },
{ value: '-attachments', icon: 'sort-amount-desc', loc: () => Locale.searchAttachments },
{ value: '-rank', icon: 'sort-numeric-desc', loc: () => Locale.searchRank + ' ' + this.addArrow(Locale.searchBestWorst) },
{ value: 'rank', icon: 'sort-numeric-asc', loc: () => Locale.searchRank + ' ' + this.addArrow(Locale.searchWorstBest) },
{ value: '-rank', icon: 'sort-numeric-desc', loc: () => Locale.searchRank + ' ' + this.addArrow(Locale.searchBestWorst) }
];
this.sortIcons = {};
this.sortOptions.forEach(function(opt) {

View File

@ -106,9 +106,7 @@ const OpenView = Backbone.View.extend({
},
focusInput: function() {
console.log('focus');
if (FocusDetector.hasFocus()) {
console.log('hasFocus');
this.inputEl.focus();
}
},