bitwarden-estensione-browser/src/popup/vault/groupings.component.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

377 lines
12 KiB
TypeScript
Raw Normal View History

import { Location } from "@angular/common";
2018-04-05 04:59:42 +02:00
import { ChangeDetectorRef, Component, NgZone, OnDestroy, OnInit } from "@angular/core";
2018-04-09 16:50:28 +02:00
import { ActivatedRoute, Router } from "@angular/router";
2021-12-21 15:43:35 +01:00
import { first } from "rxjs/operators";
2021-12-21 15:43:35 +01:00
2018-04-11 05:28:50 +02:00
import { BrowserApi } from "../../browser/browserApi";
2021-12-21 15:43:35 +01:00
import { CipherType } from "jslib-common/enums/cipherType";
2021-12-21 15:43:35 +01:00
import { CipherView } from "jslib-common/models/view/cipherView";
import { CollectionView } from "jslib-common/models/view/collectionView";
import { FolderView } from "jslib-common/models/view/folderView";
2021-12-21 15:43:35 +01:00
2021-12-06 12:21:07 +01:00
import { BroadcasterService } from "jslib-common/abstractions/broadcaster.service";
import { CipherService } from "jslib-common/abstractions/cipher.service";
import { CollectionService } from "jslib-common/abstractions/collection.service";
import { FolderService } from "jslib-common/abstractions/folder.service";
import { PlatformUtilsService } from "jslib-common/abstractions/platformUtils.service";
import { SearchService } from "jslib-common/abstractions/search.service";
import { StateService } from "jslib-common/abstractions/state.service";
import { StorageService } from "jslib-common/abstractions/storage.service";
import { SyncService } from "jslib-common/abstractions/sync.service";
import { UserService } from "jslib-common/abstractions/user.service";
2021-12-21 15:43:35 +01:00
import { GroupingsComponent as BaseGroupingsComponent } from "jslib-angular/components/groupings.component";
2021-12-21 15:43:35 +01:00
2018-04-09 20:17:58 +02:00
import { PopupUtilsService } from "../services/popup-utils.service";
2021-12-21 15:43:35 +01:00
2018-04-09 20:17:58 +02:00
const ComponentId = "GroupingsComponent";
2018-04-12 21:56:27 +02:00
const ScopeStateId = ComponentId + "Scope";
2018-04-06 21:33:20 +02:00
2018-04-05 04:59:42 +02:00
@Component({
selector: "app-vault-groupings",
2018-04-06 17:48:45 +02:00
templateUrl: "groupings.component.html",
2018-04-05 04:59:42 +02:00
})
2018-04-06 21:33:20 +02:00
export class GroupingsComponent extends BaseGroupingsComponent implements OnInit, OnDestroy {
get showNoFolderCiphers(): boolean {
2021-12-21 15:43:35 +01:00
return (
this.noFolderCiphers != null &&
this.noFolderCiphers.length < this.noFolderListSize &&
this.collections.length === 0
2021-12-21 15:43:35 +01:00
);
}
get folderCount(): number {
return this.nestedFolders.length - (this.showNoFolderCiphers ? 0 : 1);
2021-12-21 15:43:35 +01:00
}
2018-04-05 16:29:11 +02:00
ciphers: CipherView[];
favoriteCiphers: CipherView[];
2018-04-05 23:27:31 +02:00
noFolderCiphers: CipherView[];
2018-04-05 16:29:11 +02:00
folderCounts = new Map<string, number>();
collectionCounts = new Map<string, number>();
typeCounts = new Map<CipherType, number>();
2018-04-09 16:50:28 +02:00
searchText: string;
2018-04-09 20:17:58 +02:00
state: any;
2018-04-12 21:56:27 +02:00
scopeState: any;
2018-04-14 05:37:57 +02:00
showLeftHeader = true;
2018-08-13 17:53:16 +02:00
searchPending = false;
2019-03-14 03:54:18 +01:00
searchTypeSearch = false;
deletedCount = 0;
2021-12-21 15:43:35 +01:00
2018-04-12 21:56:27 +02:00
private loadedTimeout: number;
private selectedTimeout: number;
private preventSelected = false;
2018-04-14 05:22:20 +02:00
private noFolderListSize = 100;
2018-08-13 17:53:16 +02:00
private searchTimeout: any = null;
private hasSearched = false;
private hasLoadedAllCiphers = false;
private allCiphers: CipherView[] = null;
2021-12-21 15:43:35 +01:00
2018-04-05 16:29:11 +02:00
constructor(
collectionService: CollectionService,
folderService: FolderService,
2018-11-10 04:35:00 +01:00
storageService: StorageService,
userService: UserService,
2018-04-06 21:33:20 +02:00
private cipherService: CipherService,
private router: Router,
private ngZone: NgZone,
private broadcasterService: BroadcasterService,
2018-04-09 20:17:58 +02:00
private changeDetectorRef: ChangeDetectorRef,
private route: ActivatedRoute,
2018-04-11 04:49:19 +02:00
private stateService: StateService,
private popupUtils: PopupUtilsService,
private syncService: SyncService,
private platformUtilsService: PlatformUtilsService,
private searchService: SearchService,
private location: Location
2021-12-21 15:43:35 +01:00
) {
2018-11-10 04:35:00 +01:00
super(collectionService, folderService, storageService, userService);
2020-09-15 16:50:45 +02:00
this.noFolderListSize = 100;
2021-12-21 15:43:35 +01:00
}
2018-04-12 21:56:27 +02:00
async ngOnInit() {
2019-03-14 03:54:18 +01:00
this.searchTypeSearch = !this.platformUtilsService.isSafari();
this.showLeftHeader = !(
this.popupUtils.inSidebar(window) && this.platformUtilsService.isFirefox()
2021-12-21 15:43:35 +01:00
);
2018-04-09 20:17:58 +02:00
this.stateService.remove("CiphersComponent");
2021-12-21 15:43:35 +01:00
2018-04-09 20:17:58 +02:00
this.broadcasterService.subscribe(ComponentId, (message: any) => {
2018-04-06 21:33:20 +02:00
this.ngZone.run(async () => {
switch (message.command) {
case "syncCompleted":
window.setTimeout(() => {
this.load();
2021-12-21 15:43:35 +01:00
}, 500);
break;
default:
break;
}
this.changeDetectorRef.detectChanges();
2021-12-21 15:43:35 +01:00
});
});
2018-04-12 21:56:27 +02:00
const restoredScopeState = await this.restoreState();
this.route.queryParams.pipe(first()).subscribe(async (params) => {
this.state = (await this.stateService.get<any>(ComponentId)) || {};
if (this.state.searchText) {
this.searchText = this.state.searchText;
} else if (params.searchText) {
2018-04-09 16:50:28 +02:00
this.searchText = params.searchText;
this.location.replaceState("vault");
2021-12-21 15:43:35 +01:00
}
if (!this.syncService.syncInProgress) {
2018-04-12 21:56:27 +02:00
this.load();
2021-12-21 15:43:35 +01:00
} else {
this.loadedTimeout = window.setTimeout(() => {
if (!this.loaded) {
2018-04-12 21:56:27 +02:00
this.load();
2021-12-21 15:43:35 +01:00
}
2018-04-14 19:40:25 +02:00
}, 5000);
2021-12-21 15:43:35 +01:00
}
if (!this.syncService.syncInProgress || restoredScopeState) {
window.setTimeout(() => this.popupUtils.setContentScrollY(window, this.state.scrollY), 0);
2021-12-21 15:43:35 +01:00
}
});
}
ngOnDestroy() {
if (this.loadedTimeout != null) {
window.clearTimeout(this.loadedTimeout);
}
if (this.selectedTimeout != null) {
window.clearTimeout(this.selectedTimeout);
}
2018-04-05 16:29:11 +02:00
this.saveState();
this.broadcasterService.unsubscribe(ComponentId);
2021-12-21 15:43:35 +01:00
}
2018-04-05 16:29:11 +02:00
async load() {
2019-03-14 03:54:18 +01:00
await super.load(false);
2018-08-13 17:53:16 +02:00
await this.loadCiphers();
if (this.showNoFolderCiphers && this.nestedFolders.length > 0) {
2018-04-05 16:29:11 +02:00
// Remove "No Folder" from folder listing
this.nestedFolders = this.nestedFolders.slice(0, this.nestedFolders.length - 1);
}
super.loaded = true;
2018-04-06 21:33:20 +02:00
}
async loadCiphers() {
2018-04-09 20:17:58 +02:00
this.allCiphers = await this.cipherService.getAllDecrypted();
2018-04-11 04:49:19 +02:00
if (!this.hasLoadedAllCiphers) {
2018-04-12 21:56:27 +02:00
this.hasLoadedAllCiphers = !this.searchService.isSearchable(this.searchText);
2018-04-06 21:33:20 +02:00
}
2018-04-11 05:28:50 +02:00
this.deletedCount = this.allCiphers.filter((c) => c.isDeleted).length;
2018-08-13 17:53:16 +02:00
await this.search(null);
2018-04-12 21:56:27 +02:00
let favoriteCiphers: CipherView[] = null;
let noFolderCiphers: CipherView[] = null;
const folderCounts = new Map<string, number>();
2018-04-11 05:28:50 +02:00
const collectionCounts = new Map<string, number>();
2018-04-12 21:56:27 +02:00
const typeCounts = new Map<CipherType, number>();
2021-12-21 15:43:35 +01:00
2021-03-02 19:31:52 +01:00
this.ciphers.forEach((c) => {
if (c.isDeleted) {
2021-12-21 15:43:35 +01:00
return;
}
2018-04-05 16:29:11 +02:00
if (c.favorite) {
2018-04-11 05:28:50 +02:00
if (favoriteCiphers == null) {
favoriteCiphers = [];
}
favoriteCiphers.push(c);
2021-12-21 15:43:35 +01:00
}
2018-04-11 05:28:50 +02:00
if (c.folderId == null) {
2018-04-12 21:56:27 +02:00
if (noFolderCiphers == null) {
2018-04-11 05:28:50 +02:00
noFolderCiphers = [];
}
2018-04-12 21:56:27 +02:00
noFolderCiphers.push(c);
2021-12-21 15:43:35 +01:00
}
2018-04-12 21:56:27 +02:00
if (typeCounts.has(c.type)) {
2018-04-09 20:17:58 +02:00
typeCounts.set(c.type, typeCounts.get(c.type) + 1);
2021-12-21 15:43:35 +01:00
} else {
2018-04-09 20:17:58 +02:00
typeCounts.set(c.type, 1);
2021-12-21 15:43:35 +01:00
}
2018-04-09 20:17:58 +02:00
if (folderCounts.has(c.folderId)) {
folderCounts.set(c.folderId, folderCounts.get(c.folderId) + 1);
2021-12-21 15:43:35 +01:00
} else {
2018-04-12 21:56:27 +02:00
folderCounts.set(c.folderId, 1);
2021-12-21 15:43:35 +01:00
}
2018-04-05 16:29:11 +02:00
if (c.collectionIds != null) {
2021-03-02 19:31:52 +01:00
c.collectionIds.forEach((colId) => {
2018-04-12 21:56:27 +02:00
if (collectionCounts.has(colId)) {
2018-04-09 20:17:58 +02:00
collectionCounts.set(colId, collectionCounts.get(colId) + 1);
2021-12-21 15:43:35 +01:00
} else {
2018-04-12 21:56:27 +02:00
collectionCounts.set(colId, 1);
2021-12-21 15:43:35 +01:00
}
});
}
});
2018-04-09 20:17:58 +02:00
this.favoriteCiphers = favoriteCiphers;
2018-04-12 21:56:27 +02:00
this.noFolderCiphers = noFolderCiphers;
this.typeCounts = typeCounts;
this.folderCounts = folderCounts;
this.collectionCounts = collectionCounts;
2021-12-21 15:43:35 +01:00
}
2018-04-09 20:17:58 +02:00
async search(timeout: number = null) {
this.searchPending = false;
2018-08-13 17:53:16 +02:00
if (this.searchTimeout != null) {
clearTimeout(this.searchTimeout);
2018-04-06 21:33:20 +02:00
}
const filterDeleted = (c: CipherView) => !c.isDeleted;
2018-08-13 17:53:16 +02:00
if (timeout == null) {
this.hasSearched = this.searchService.isSearchable(this.searchText);
2018-04-06 21:33:20 +02:00
this.ciphers = await this.searchService.searchCiphers(
this.searchText,
filterDeleted,
this.allCiphers
2021-12-21 15:43:35 +01:00
);
return;
}
2018-08-13 17:53:16 +02:00
this.searchPending = true;
2018-04-06 21:33:20 +02:00
this.searchTimeout = setTimeout(async () => {
2018-04-12 21:56:27 +02:00
this.hasSearched = this.searchService.isSearchable(this.searchText);
if (!this.hasLoadedAllCiphers && !this.hasSearched) {
2018-04-05 16:29:11 +02:00
await this.loadCiphers();
2021-12-21 15:43:35 +01:00
} else {
this.ciphers = await this.searchService.searchCiphers(
this.searchText,
filterDeleted,
this.allCiphers
2021-12-21 15:43:35 +01:00
);
}
this.searchPending = false;
2018-08-13 17:53:16 +02:00
}, timeout);
2021-12-21 15:43:35 +01:00
}
async selectType(type: CipherType) {
super.selectType(type);
2018-04-05 21:56:52 +02:00
this.router.navigate(["/ciphers"], { queryParams: { type: type } });
2021-12-21 15:43:35 +01:00
}
2018-04-09 20:17:58 +02:00
async selectFolder(folder: FolderView) {
super.selectFolder(folder);
this.router.navigate(["/ciphers"], { queryParams: { folderId: folder.id || "none" } });
2021-12-21 15:43:35 +01:00
}
2018-04-09 20:17:58 +02:00
async selectCollection(collection: CollectionView) {
2018-04-05 23:27:31 +02:00
super.selectCollection(collection);
this.router.navigate(["/ciphers"], { queryParams: { collectionId: collection.id } });
2021-12-21 15:43:35 +01:00
}
async selectTrash() {
super.selectTrash();
this.router.navigate(["/ciphers"], { queryParams: { deleted: true } });
2021-12-21 15:43:35 +01:00
}
2018-04-09 20:17:58 +02:00
async selectCipher(cipher: CipherView) {
this.selectedTimeout = window.setTimeout(() => {
if (!this.preventSelected) {
this.router.navigate(["/view-cipher"], { queryParams: { cipherId: cipher.id } });
2021-12-21 15:43:35 +01:00
}
this.preventSelected = false;
2021-12-21 15:43:35 +01:00
}, 200);
}
2018-04-11 05:28:50 +02:00
async launchCipher(cipher: CipherView) {
if (cipher.type !== CipherType.Login || !cipher.login.canLaunch) {
2021-12-21 15:43:35 +01:00
return;
2018-04-05 16:29:11 +02:00
}
2018-04-12 21:56:27 +02:00
if (this.selectedTimeout != null) {
window.clearTimeout(this.selectedTimeout);
2018-04-05 04:59:42 +02:00
}
2018-08-13 17:53:16 +02:00
this.preventSelected = true;
await this.cipherService.updateLastLaunchedDate(cipher.id);
BrowserApi.createNewTab(cipher.login.launchUri);
if (this.popupUtils.inPopup(window)) {
2018-08-13 17:53:16 +02:00
BrowserApi.closePopup(window);
}
2021-12-21 15:43:35 +01:00
}
2018-04-09 20:17:58 +02:00
async addCipher() {
2018-04-05 21:56:52 +02:00
this.router.navigate(["/add-cipher"]);
}
2021-12-21 15:43:35 +01:00
2018-04-05 21:56:52 +02:00
showSearching() {
2021-12-21 15:43:35 +01:00
return (
2018-04-05 21:56:52 +02:00
this.hasSearched || (!this.searchPending && this.searchService.isSearchable(this.searchText))
2021-12-21 15:43:35 +01:00
);
2018-04-05 21:56:52 +02:00
}
2021-12-21 15:43:35 +01:00
2018-04-05 21:56:52 +02:00
closeOnEsc(e: KeyboardEvent) {
// If input not empty, use browser default behavior of clearing input instead
if (e.key === "Escape" && (this.searchText == null || this.searchText === "")) {
BrowserApi.closePopup(window);
}
2021-12-21 15:43:35 +01:00
}
2018-04-09 20:17:58 +02:00
private async saveState() {
2018-04-12 21:56:27 +02:00
this.state = {
2018-04-09 20:17:58 +02:00
scrollY: this.popupUtils.getContentScrollY(window),
searchText: this.searchText,
};
await this.stateService.save(ComponentId, this.state);
2021-12-21 15:43:35 +01:00
this.scopeState = {
favoriteCiphers: this.favoriteCiphers,
noFolderCiphers: this.noFolderCiphers,
2018-04-12 21:56:27 +02:00
ciphers: this.ciphers,
collectionCounts: this.collectionCounts,
folderCounts: this.folderCounts,
typeCounts: this.typeCounts,
2018-04-12 21:56:27 +02:00
folders: this.folders,
collections: this.collections,
deletedCount: this.deletedCount,
};
await this.stateService.save(ScopeStateId, this.scopeState);
}
2021-12-21 15:43:35 +01:00
private async restoreState(): Promise<boolean> {
2018-04-12 21:56:27 +02:00
this.scopeState = await this.stateService.get<any>(ScopeStateId);
if (this.scopeState == null) {
return false;
}
2018-04-09 20:17:58 +02:00
if (this.scopeState.favoriteCiphers != null) {
this.favoriteCiphers = this.scopeState.favoriteCiphers;
}
if (this.scopeState.noFolderCiphers != null) {
this.noFolderCiphers = this.scopeState.noFolderCiphers;
}
2018-04-11 05:28:50 +02:00
if (this.scopeState.ciphers != null) {
this.ciphers = this.scopeState.ciphers;
}
if (this.scopeState.collectionCounts != null) {
this.collectionCounts = this.scopeState.collectionCounts;
2018-04-06 05:49:04 +02:00
}
2018-04-09 20:17:58 +02:00
if (this.scopeState.folderCounts != null) {
2018-04-06 05:49:04 +02:00
this.folderCounts = this.scopeState.folderCounts;
}
2018-08-13 17:53:16 +02:00
if (this.scopeState.typeCounts != null) {
this.typeCounts = this.scopeState.typeCounts;
}
if (this.scopeState.folders != null) {
this.folders = this.scopeState.folders;
}
2018-04-12 21:56:27 +02:00
if (this.scopeState.collections != null) {
this.collections = this.scopeState.collections;
}
if (this.scopeState.deletedCiphers != null) {
this.deletedCount = this.scopeState.deletedCount;
2018-04-09 20:17:58 +02:00
}
2021-12-21 15:43:35 +01:00
2018-04-12 21:56:27 +02:00
return true;
2021-12-21 15:43:35 +01:00
}
2018-04-05 04:59:42 +02:00
}