From 22f0f97cda0286859ceb889b9c80b9b5bb88affa Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Thu, 5 Apr 2018 11:12:00 -0400 Subject: [PATCH] move ciphers, groupings, and search pipe to jslib --- src/angular/components/ciphers.component.ts | 60 +++++++++++ src/angular/components/groupings.component.ts | 99 +++++++++++++++++++ src/angular/pipes/search-ciphers.pipe.ts | 36 +++++++ 3 files changed, 195 insertions(+) create mode 100644 src/angular/components/ciphers.component.ts create mode 100644 src/angular/components/groupings.component.ts create mode 100644 src/angular/pipes/search-ciphers.pipe.ts diff --git a/src/angular/components/ciphers.component.ts b/src/angular/components/ciphers.component.ts new file mode 100644 index 0000000000..40f1899346 --- /dev/null +++ b/src/angular/components/ciphers.component.ts @@ -0,0 +1,60 @@ +import { + EventEmitter, + Input, + Output, +} from '@angular/core'; + +import { CipherService } from '../../abstractions/cipher.service'; + +import { CipherView } from '../../models/view/cipherView'; + +export class CiphersComponent { + @Input() activeCipherId: string = null; + @Output() onCipherClicked = new EventEmitter(); + @Output() onCipherRightClicked = new EventEmitter(); + @Output() onAddCipher = new EventEmitter(); + @Output() onAddCipherOptions = new EventEmitter(); + + loaded: boolean = false; + ciphers: CipherView[] = []; + searchText: string; + searchPlaceholder: string = null; + private filter: (cipher: CipherView) => boolean = null; + + constructor(protected cipherService: CipherService) { } + + async load(filter: (cipher: CipherView) => boolean = null) { + this.filter = filter; + const ciphers = await this.cipherService.getAllDecrypted(); + + if (this.filter == null) { + this.ciphers = ciphers; + } else { + this.ciphers = ciphers.filter(this.filter); + } + + this.loaded = true; + } + + async refresh() { + this.loaded = false; + this.ciphers = []; + await this.load(this.filter); + } + + selectCipher(cipher: CipherView) { + this.onCipherClicked.emit(cipher); + } + + rightClickCipher(cipher: CipherView) { + this.onCipherRightClicked.emit(cipher); + } + + addCipher() { + this.onAddCipher.emit(); + } + + addCipherOptions() { + this.onAddCipherOptions.emit(); + } +} diff --git a/src/angular/components/groupings.component.ts b/src/angular/components/groupings.component.ts new file mode 100644 index 0000000000..3bec8a866f --- /dev/null +++ b/src/angular/components/groupings.component.ts @@ -0,0 +1,99 @@ +import { + Component, + EventEmitter, + Input, + Output, +} from '@angular/core'; + +import { CipherType } from '../../enums/cipherType'; + +import { CollectionView } from '../../models/view/collectionView'; +import { FolderView } from '../../models/view/folderView'; + +import { CollectionService } from '../../abstractions/collection.service'; +import { FolderService } from '../../abstractions/folder.service'; + +export class GroupingsComponent { + @Output() onAllClicked = new EventEmitter(); + @Output() onFavoritesClicked = new EventEmitter(); + @Output() onCipherTypeClicked = new EventEmitter(); + @Output() onFolderClicked = new EventEmitter(); + @Output() onAddFolder = new EventEmitter(); + @Output() onEditFolder = new EventEmitter(); + @Output() onCollectionClicked = new EventEmitter(); + + folders: FolderView[]; + collections: CollectionView[]; + loaded: boolean = false; + cipherType = CipherType; + selectedAll: boolean = false; + selectedFavorites: boolean = false; + selectedType: CipherType = null; + selectedFolder: boolean = false; + selectedFolderId: string = null; + selectedCollectionId: string = null; + + constructor(protected collectionService: CollectionService, protected folderService: FolderService) { } + + async load() { + await this.loadFolders(); + await this.loadCollections(); + this.loaded = true; + } + + async loadCollections() { + this.collections = await this.collectionService.getAllDecrypted(); + } + + async loadFolders() { + this.folders = await this.folderService.getAllDecrypted(); + } + + selectAll() { + this.clearSelections(); + this.selectedAll = true; + this.onAllClicked.emit(); + } + + selectFavorites() { + this.clearSelections(); + this.selectedFavorites = true; + this.onFavoritesClicked.emit(); + } + + selectType(type: CipherType) { + this.clearSelections(); + this.selectedType = type; + this.onCipherTypeClicked.emit(type); + } + + selectFolder(folder: FolderView) { + this.clearSelections(); + this.selectedFolder = true; + this.selectedFolderId = folder.id; + this.onFolderClicked.emit(folder); + } + + addFolder() { + this.onAddFolder.emit(); + } + + editFolder(folder: FolderView) { + this.onEditFolder.emit(folder); + } + + selectCollection(collection: CollectionView) { + this.clearSelections(); + this.selectedCollectionId = collection.id; + this.onCollectionClicked.emit(collection); + } + + clearSelections() { + this.selectedAll = false; + this.selectedFavorites = false; + this.selectedType = null; + this.selectedFolder = false; + this.selectedFolderId = null; + this.selectedCollectionId = null; + } +} diff --git a/src/angular/pipes/search-ciphers.pipe.ts b/src/angular/pipes/search-ciphers.pipe.ts new file mode 100644 index 0000000000..1d255885ca --- /dev/null +++ b/src/angular/pipes/search-ciphers.pipe.ts @@ -0,0 +1,36 @@ +import { + Pipe, + PipeTransform, +} from '@angular/core'; + +import { CipherView } from '../../models/view/cipherView'; + +@Pipe({ + name: 'searchCiphers', +}) +export class SearchCiphersPipe implements PipeTransform { + transform(ciphers: CipherView[], searchText: string): CipherView[] { + if (ciphers == null || ciphers.length === 0) { + return []; + } + + if (searchText == null || searchText.length < 2) { + return ciphers; + } + + searchText = searchText.toLowerCase(); + return ciphers.filter((c) => { + if (c.name != null && c.name.toLowerCase().indexOf(searchText) > -1) { + return true; + } + if (c.subTitle != null && c.subTitle.toLowerCase().indexOf(searchText) > -1) { + return true; + } + if (c.login && c.login.uri != null && c.login.uri.toLowerCase().indexOf(searchText) > -1) { + return true; + } + + return false; + }); + } +}