2018-04-05 17:12:00 +02:00
|
|
|
import {
|
|
|
|
EventEmitter,
|
|
|
|
Input,
|
|
|
|
Output,
|
|
|
|
} from '@angular/core';
|
|
|
|
|
2018-08-13 15:42:52 +02:00
|
|
|
import { SearchService } from '../../abstractions/search.service';
|
2018-04-05 17:12:00 +02:00
|
|
|
|
|
|
|
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[] = [];
|
|
|
|
searchText: string;
|
|
|
|
searchPlaceholder: string = null;
|
2018-08-23 04:35:18 +02:00
|
|
|
filter: (cipher: CipherView) => boolean = null;
|
2018-07-04 05:33:15 +02:00
|
|
|
|
2018-08-13 17:52:55 +02:00
|
|
|
protected searchPending = false;
|
2018-04-05 17:12:00 +02:00
|
|
|
|
2018-08-13 15:42:52 +02:00
|
|
|
private searchTimeout: any = null;
|
|
|
|
|
|
|
|
constructor(protected searchService: SearchService) { }
|
2018-04-05 17:12:00 +02:00
|
|
|
|
|
|
|
async load(filter: (cipher: CipherView) => boolean = null) {
|
2018-08-13 15:42:52 +02:00
|
|
|
await this.applyFilter(filter);
|
2018-04-05 17:12:00 +02:00
|
|
|
this.loaded = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
async refresh() {
|
|
|
|
this.loaded = false;
|
|
|
|
this.ciphers = [];
|
|
|
|
await this.load(this.filter);
|
|
|
|
}
|
|
|
|
|
2018-08-13 15:42:52 +02:00
|
|
|
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 15:42:52 +02:00
|
|
|
}
|
|
|
|
|
2018-08-13 17:52:55 +02:00
|
|
|
async search(timeout: number = null) {
|
|
|
|
this.searchPending = false;
|
2018-08-13 15:42:52 +02:00
|
|
|
if (this.searchTimeout != null) {
|
|
|
|
clearTimeout(this.searchTimeout);
|
2018-07-04 05:33:15 +02:00
|
|
|
}
|
2018-08-13 17:52:55 +02:00
|
|
|
if (timeout == null) {
|
|
|
|
this.ciphers = await this.searchService.searchCiphers(this.searchText, this.filter);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.searchPending = true;
|
2018-08-13 15:42:52 +02:00
|
|
|
this.searchTimeout = setTimeout(async () => {
|
|
|
|
this.ciphers = await this.searchService.searchCiphers(this.searchText, this.filter);
|
2018-08-13 17:52:55 +02:00
|
|
|
this.searchPending = false;
|
2018-08-13 15:42:52 +02:00
|
|
|
}, timeout);
|
2018-07-04 05:33:15 +02:00
|
|
|
}
|
|
|
|
|
2018-04-05 17:12:00 +02:00
|
|
|
selectCipher(cipher: CipherView) {
|
|
|
|
this.onCipherClicked.emit(cipher);
|
|
|
|
}
|
|
|
|
|
|
|
|
rightClickCipher(cipher: CipherView) {
|
|
|
|
this.onCipherRightClicked.emit(cipher);
|
|
|
|
}
|
|
|
|
|
|
|
|
addCipher() {
|
|
|
|
this.onAddCipher.emit();
|
|
|
|
}
|
|
|
|
|
|
|
|
addCipherOptions() {
|
|
|
|
this.onAddCipherOptions.emit();
|
|
|
|
}
|
|
|
|
}
|