1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-12-17 15:37:57 +01:00

searchCiphersBasic

This commit is contained in:
Kyle Spearrin 2018-08-13 14:28:10 -04:00
parent 364192b27a
commit bdb2efd770
3 changed files with 30 additions and 22 deletions

View File

@ -5,4 +5,5 @@ export abstract class SearchService {
isSearchable: (query: string) => boolean; isSearchable: (query: string) => boolean;
indexCiphers: () => Promise<void>; indexCiphers: () => Promise<void>;
searchCiphers: (query: string, filter?: (cipher: CipherView) => boolean) => Promise<CipherView[]>; searchCiphers: (query: string, filter?: (cipher: CipherView) => boolean) => Promise<CipherView[]>;
searchCiphersBasic: (ciphers: CipherView[], query: string) => CipherView[];
} }

View File

@ -67,10 +67,12 @@ export class CipherService implements CipherServiceAbstraction {
} }
set decryptedCipherCache(value: CipherView[]) { set decryptedCipherCache(value: CipherView[]) {
this._decryptedCipherCache = value; this._decryptedCipherCache = value;
if (value == null) { if (this.searchService != null) {
this.searchService().clearIndex(); if (value == null) {
} else { this.searchService().clearIndex();
this.searchService().indexCiphers(); } else {
this.searchService().indexCiphers();
}
} }
} }

View File

@ -103,24 +103,7 @@ export class SearchService implements SearchServiceAbstraction {
if (this.index == null) { if (this.index == null) {
// Fall back to basic search if index is not available // Fall back to basic search if index is not available
return ciphers.filter((c) => { return this.searchCiphersBasic(ciphers, query);
if (c.name != null && c.name.toLowerCase().indexOf(query) > -1) {
return true;
}
if (this.onlySearchName) {
return false;
}
if (query.length >= 8 && c.id.startsWith(query)) {
return true;
}
if (c.subTitle != null && c.subTitle.toLowerCase().indexOf(query) > -1) {
return true;
}
if (c.login && c.login.uri != null && c.login.uri.toLowerCase().indexOf(query) > -1) {
return true;
}
return false;
});
} }
const ciphersMap = new Map<string, CipherView>(); const ciphersMap = new Map<string, CipherView>();
@ -157,4 +140,26 @@ export class SearchService implements SearchServiceAbstraction {
} }
return results; return results;
} }
searchCiphersBasic(ciphers: CipherView[], query: string) {
query = query.trim().toLowerCase();
return ciphers.filter((c) => {
if (c.name != null && c.name.toLowerCase().indexOf(query) > -1) {
return true;
}
if (this.onlySearchName) {
return false;
}
if (query.length >= 8 && c.id.startsWith(query)) {
return true;
}
if (c.subTitle != null && c.subTitle.toLowerCase().indexOf(query) > -1) {
return true;
}
if (c.login && c.login.uri != null && c.login.uri.toLowerCase().indexOf(query) > -1) {
return true;
}
return false;
});
}
} }