blob: c64b6001f7e01f948b4a8eac90d483b95a02c3aa (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
import SearchQuery from '@js/Search/Query/SearchQuery';
import Url from 'url-parse';
import TabManager from '@js/Manager/TabManager';
import SearchIndex from '@js/Search/Index/SearchIndex';
import EventQueue from '@js/Event/EventQueue';
class RecommendationManager {
get listen() {
return this._change;
}
constructor() {
this._change = new EventQueue();
this._tabEvent = (tab) => {
if(!tab.hasOwnProperty('recommended') || tab.lastUrl !== tab.url) {
this._updateRecommended(tab);
} else {
this._change.emit(tab.recommended);
}
};
this._searchEvent = () => {
this._clearRecommended(TabManager.getAll());
let tab = TabManager.get();
if(tab) {
this._updateRecommended(tab);
}
};
}
init() {
TabManager.tabChanged.on(this._tabEvent);
TabManager.urlChanged.on(this._tabEvent);
SearchIndex.listen.on(this._searchEvent);
}
/**
*
* @returns {Password[]}
*/
getRecommendations() {
if(!TabManager.has('recommended')) return [];
return TabManager.get('recommended');
}
/**
*
* @return {Boolean}
*/
hasRecommendations() {
return TabManager.has('recommended');
}
/**
*
* @param {Object} tab
* @private
*/
_updateRecommended(tab) {
let url = Url(tab.url);
delete tab.recommended;
if(url.host.length === 0) {
this._change.emit([]);
return;
}
let query = new SearchQuery('or');
query
.where(
query.field('host').equals(url.host),
query.field('url').contains(url.pathname.length > 1 ? url.host + url.pathname:url.host)
)
.type('password')
.score(0.3)
.limit(8)
.sortBy('favorite')
.sortBy('shared')
.sortBy('score')
.sortBy('label');
if(tab.tab.incognito) {
query.hidden(true);
}
let recommendations = query.execute();
if(recommendations.length !== 0) {
tab.recommended = recommendations;
}
this._change.emit(recommendations);
}
/**
*
* @param {Object[]} tabs
* @private
*/
_clearRecommended(tabs) {
for(let tab of tabs) {
if(tab && tab.hasOwnProperty('recommended')) {
delete tab.recommended;
}
}
}
}
export default new RecommendationManager();
|