keeweb/app/scripts/auto-type/auto-type-filter.js

91 lines
3.2 KiB
JavaScript
Raw Normal View History

2019-09-15 14:16:32 +02:00
import { EntryCollection } from 'collections/entry-collection';
import { Ranking } from 'util/data/ranking';
2016-08-02 22:10:05 +02:00
const urlPartsRegex = /^(\w+:\/\/)?(?:(?:www|wwws|secure)\.)?([^\/]+)\/?(.*)/;
2016-08-02 22:10:05 +02:00
2017-01-31 07:50:28 +01:00
const AutoTypeFilter = function(windowInfo, appModel) {
2016-07-25 20:27:22 +02:00
this.title = windowInfo.title;
this.url = windowInfo.url;
this.text = '';
this.ignoreWindowInfo = false;
this.appModel = appModel;
};
AutoTypeFilter.prototype.getEntries = function() {
2017-01-31 07:50:28 +01:00
const filter = {
2017-01-29 19:03:38 +01:00
text: this.text,
autoType: true
2016-07-31 20:38:08 +02:00
};
2017-05-07 19:45:53 +02:00
this.prepareFilter();
2019-08-16 23:05:39 +02:00
let entries = this.appModel.getEntriesByFilter(filter).map(e => [e, this.getEntryRank(e)]);
2017-05-07 19:45:53 +02:00
if (!this.ignoreWindowInfo) {
entries = entries.filter(e => e[1]);
2016-08-02 22:10:05 +02:00
}
2019-08-18 08:05:38 +02:00
entries = entries.sort((x, y) =>
x[1] === y[1] ? x[0].title.localeCompare(y[0].title) : y[1] - x[1]
);
2017-05-07 19:45:53 +02:00
entries = entries.map(p => p[0]);
2019-08-16 23:05:39 +02:00
return new EntryCollection(entries, { comparator: 'none' });
2016-08-02 22:10:05 +02:00
};
AutoTypeFilter.prototype.hasWindowInfo = function() {
return this.title || this.url;
};
AutoTypeFilter.prototype.prepareFilter = function() {
this.titleLower = this.title ? this.title.toLowerCase() : null;
this.urlLower = this.url ? this.url.toLowerCase() : null;
2016-08-03 19:20:50 +02:00
this.urlParts = this.url ? urlPartsRegex.exec(this.urlLower) : null;
2016-08-02 22:10:05 +02:00
};
AutoTypeFilter.prototype.getEntryRank = function(entry) {
let rank = 0;
if (this.titleLower && entry.title) {
2019-08-16 23:05:39 +02:00
rank += Ranking.getStringRank(entry.title.toLowerCase(), this.titleLower);
2016-08-02 22:10:05 +02:00
}
if (this.urlParts) {
if (entry.url) {
const entryUrlParts = urlPartsRegex.exec(entry.url.toLowerCase());
if (entryUrlParts) {
const [, scheme, domain, path] = entryUrlParts;
const [, thisScheme, thisDomain, thisPath] = this.urlParts;
if (domain === thisDomain || thisDomain.indexOf('.' + domain) > 0) {
if (domain === thisDomain) {
rank += 20;
} else {
rank += 10;
}
if (path === thisPath) {
rank += 10;
} else if (path && thisPath) {
if (path.lastIndexOf(thisPath, 0) === 0) {
rank += 5;
} else if (thisPath.lastIndexOf(path, 0) === 0) {
rank += 3;
}
}
if (scheme === thisScheme) {
rank += 1;
}
2017-11-26 22:49:57 +01:00
} else {
if (entry.searchText.indexOf(this.urlLower) >= 0) {
// the url is in some field; include it
2016-08-02 22:34:00 +02:00
rank += 5;
} else {
// another domain; don't show this record at all, ignore title match
return 0;
2016-08-02 22:34:00 +02:00
}
}
}
} else {
if (entry.searchText.indexOf(this.urlLower) >= 0) {
// the url is in some field; include it
rank += 5;
2016-08-02 22:10:05 +02:00
}
}
}
return rank;
};
2019-09-15 14:16:32 +02:00
export { AutoTypeFilter };