bitwarden-estensione-browser/src/angular/components/ciphers.component.ts

130 lines
3.9 KiB
TypeScript
Raw Normal View History

import {
EventEmitter,
Input,
Output,
} from '@angular/core';
import { SearchService } from '../../abstractions/search.service';
import { CipherView } from '../../models/view/cipherView';
export class CiphersComponent {
@Input() activeCipherId: string = null;
@Output() onCipherClicked = new EventEmitter<CipherView>();
@Output() onCipherRightClicked = new EventEmitter<CipherView>();
@Output() onAddCipher = new EventEmitter();
@Output() onAddCipherOptions = new EventEmitter();
loaded: boolean = false;
ciphers: CipherView[] = [];
pagedCiphers: CipherView[] = [];
searchText: string;
searchPlaceholder: string = null;
2018-08-23 04:35:18 +02:00
filter: (cipher: CipherView) => boolean = null;
deleted: boolean = false;
2018-07-04 05:33:15 +02:00
2018-08-13 17:52:55 +02:00
protected searchPending = false;
protected didScroll = false;
protected pageSize = 100;
private searchTimeout: any = null;
private pagedCiphersCount = 0;
private refreshing = false;
constructor(protected searchService: SearchService) { }
async load(filter: (cipher: CipherView) => boolean = null, deleted: boolean = false) {
this.deleted = deleted || false;
await this.applyFilter(filter);
this.loaded = true;
}
loadMore() {
if (this.ciphers.length <= this.pageSize) {
return;
}
const pagedLength = this.pagedCiphers.length;
let pagedSize = this.pageSize;
if (this.refreshing && pagedLength === 0 && this.pagedCiphersCount > this.pageSize) {
pagedSize = this.pagedCiphersCount;
}
if (this.ciphers.length > pagedLength) {
this.pagedCiphers = this.pagedCiphers.concat(this.ciphers.slice(pagedLength, pagedLength + pagedSize));
}
this.pagedCiphersCount = this.pagedCiphers.length;
this.didScroll = this.pagedCiphers.length > this.pageSize;
}
async reload(filter: (cipher: CipherView) => boolean = null, deleted: boolean = false) {
this.loaded = false;
this.ciphers = [];
await this.load(filter, deleted);
}
async refresh() {
try {
this.refreshing = true;
await this.reload(this.filter, this.deleted);
} finally {
this.refreshing = false;
}
}
async applyFilter(filter: (cipher: CipherView) => boolean = null) {
2018-07-04 05:33:15 +02:00
this.filter = filter;
2018-08-13 17:52:55 +02:00
await this.search(null);
}
2018-08-13 17:52:55 +02:00
async search(timeout: number = null) {
this.searchPending = false;
if (this.searchTimeout != null) {
clearTimeout(this.searchTimeout);
2018-07-04 05:33:15 +02:00
}
const deletedFilter: (cipher: CipherView) => boolean = (c) => c.isDeleted === this.deleted;
2018-08-13 17:52:55 +02:00
if (timeout == null) {
this.ciphers = await this.searchService.searchCiphers(this.searchText, [this.filter, deletedFilter], null);
await this.resetPaging();
2018-08-13 17:52:55 +02:00
return;
}
this.searchPending = true;
this.searchTimeout = setTimeout(async () => {
this.ciphers = await this.searchService.searchCiphers(this.searchText, [this.filter, deletedFilter], null);
await this.resetPaging();
2018-08-13 17:52:55 +02:00
this.searchPending = false;
}, timeout);
2018-07-04 05:33:15 +02:00
}
selectCipher(cipher: CipherView) {
this.onCipherClicked.emit(cipher);
}
rightClickCipher(cipher: CipherView) {
this.onCipherRightClicked.emit(cipher);
}
addCipher() {
this.onAddCipher.emit();
}
addCipherOptions() {
this.onAddCipherOptions.emit();
}
isSearching() {
return !this.searchPending && this.searchService.isSearchable(this.searchText);
}
isPaging() {
const searching = this.isSearching();
if (searching && this.didScroll) {
this.resetPaging();
}
return !searching && this.ciphers.length > this.pageSize;
}
async resetPaging() {
this.pagedCiphers = [];
this.loadMore();
}
}