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

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

247 lines
8.2 KiB
TypeScript
Raw Normal View History

2018-04-06 05:49:04 +02:00
import { Location } from "@angular/common";
import { ChangeDetectorRef, Component, NgZone, OnDestroy, OnInit } from "@angular/core";
import { ActivatedRoute, Router } from "@angular/router";
import { first } from "rxjs/operators";
2018-04-11 05:28:50 +02:00
import { BrowserApi } from "../../browser/browserApi";
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 { I18nService } from "jslib-common/abstractions/i18n.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 { CipherType } from "jslib-common/enums/cipherType";
2018-04-09 20:45:35 +02: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";
import { TreeNode } from "jslib-common/models/domain/treeNode";
import { CiphersComponent as BaseCiphersComponent } from "jslib-angular/components/ciphers.component";
2018-04-09 20:17:58 +02:00
import { PopupUtilsService } from "../services/popup-utils.service";
const ComponentId = "CiphersComponent";
2018-04-06 21:33:20 +02:00
@Component({
selector: "app-vault-ciphers",
2018-04-06 17:48:45 +02:00
templateUrl: "ciphers.component.html",
})
2018-04-06 21:33:20 +02:00
export class CiphersComponent extends BaseCiphersComponent implements OnInit, OnDestroy {
2018-04-09 20:45:35 +02:00
groupingTitle: string;
2018-04-09 20:17:58 +02:00
state: any;
2018-04-10 06:04:49 +02:00
folderId: string = null;
2018-10-30 13:56:38 +01:00
collectionId: string = null;
2018-04-10 06:04:49 +02:00
type: CipherType = null;
2018-04-11 05:28:50 +02:00
nestedFolders: TreeNode<FolderView>[];
nestedCollections: TreeNode<CollectionView>[];
2019-03-14 03:54:18 +01:00
searchTypeSearch = false;
2021-12-21 15:43:35 +01:00
private selectedTimeout: number;
private preventSelected = false;
private applySavedState = true;
private scrollingContainer = "cdk-virtual-scroll-viewport";
2021-12-21 15:43:35 +01:00
constructor(
2018-04-09 20:45:35 +02:00
searchService: SearchService,
private route: ActivatedRoute,
private router: Router,
2021-03-02 19:31:52 +01:00
private location: Location,
2018-04-10 06:04:49 +02:00
private ngZone: NgZone,
private broadcasterService: BroadcasterService,
private changeDetectorRef: ChangeDetectorRef,
private stateService: StateService,
2018-04-09 20:45:35 +02:00
private popupUtils: PopupUtilsService,
private i18nService: I18nService,
private folderService: FolderService,
2018-10-30 13:56:38 +01:00
private collectionService: CollectionService,
private platformUtilsService: PlatformUtilsService,
private cipherService: CipherService
) {
2018-04-09 20:45:35 +02:00
super(searchService);
this.applySavedState =
(window as any).previousPopupUrl != null &&
2018-04-09 20:17:58 +02:00
!(window as any).previousPopupUrl.startsWith("/ciphers");
2018-08-20 23:40:39 +02:00
}
2021-12-21 15:43:35 +01:00
2018-04-06 21:33:20 +02:00
async ngOnInit() {
this.searchTypeSearch = !this.platformUtilsService.isSafari();
this.route.queryParams.pipe(first()).subscribe(async (params) => {
2018-04-09 20:17:58 +02:00
if (this.applySavedState) {
this.state = (await this.stateService.get<any>(ComponentId)) || {};
2018-04-11 05:28:50 +02:00
if (this.state.searchText) {
this.searchText = this.state.searchText;
}
}
2021-12-21 15:43:35 +01:00
if (params.deleted) {
this.groupingTitle = this.i18nService.t("trash");
this.searchPlaceholder = this.i18nService.t("searchTrash");
await this.load(null, true);
} else if (params.type) {
this.searchPlaceholder = this.i18nService.t("searchType");
this.type = parseInt(params.type, null);
2018-04-11 05:49:46 +02:00
switch (this.type) {
case CipherType.Login:
this.groupingTitle = this.i18nService.t("logins");
2021-12-21 15:43:35 +01:00
break;
2018-04-11 05:49:46 +02:00
case CipherType.Card:
2018-04-09 20:45:35 +02:00
this.groupingTitle = this.i18nService.t("cards");
2021-12-21 15:43:35 +01:00
break;
2018-04-11 05:49:46 +02:00
case CipherType.Identity:
2018-04-09 20:45:35 +02:00
this.groupingTitle = this.i18nService.t("identities");
2021-12-21 15:43:35 +01:00
break;
2018-04-09 20:45:35 +02:00
case CipherType.SecureNote:
2018-04-11 05:28:50 +02:00
this.groupingTitle = this.i18nService.t("secureNotes");
2021-12-21 15:43:35 +01:00
break;
default:
break;
2018-04-11 05:28:50 +02:00
}
await this.load((c) => c.type === this.type);
} else if (params.folderId) {
2018-04-10 06:04:49 +02:00
this.folderId = params.folderId === "none" ? null : params.folderId;
2018-04-11 05:28:50 +02:00
this.searchPlaceholder = this.i18nService.t("searchFolder");
if (this.folderId != null) {
const folderNode = await this.folderService.getNested(this.folderId);
if (folderNode != null && folderNode.node != null) {
2018-04-11 05:28:50 +02:00
this.groupingTitle = folderNode.node.name;
this.nestedFolders =
folderNode.children != null && folderNode.children.length > 0
? folderNode.children
2021-12-21 15:43:35 +01:00
: null;
}
2018-04-11 05:28:50 +02:00
} else {
this.groupingTitle = this.i18nService.t("noneFolder");
}
await this.load((c) => c.folderId === this.folderId);
} else if (params.collectionId) {
this.collectionId = params.collectionId;
this.searchPlaceholder = this.i18nService.t("searchCollection");
const collectionNode = await this.collectionService.getNested(this.collectionId);
2018-04-11 05:28:50 +02:00
if (collectionNode != null && collectionNode.node != null) {
this.groupingTitle = collectionNode.node.name;
this.nestedCollections =
collectionNode.children != null && collectionNode.children.length > 0
? collectionNode.children
: null;
}
2019-02-02 15:39:11 +01:00
await this.load(
2021-03-02 19:31:52 +01:00
(c) => c.collectionIds != null && c.collectionIds.indexOf(this.collectionId) > -1
2021-12-21 15:43:35 +01:00
);
} else {
2018-04-06 05:49:04 +02:00
this.groupingTitle = this.i18nService.t("allItems");
await this.load();
2021-12-21 15:43:35 +01:00
}
if (this.applySavedState && this.state != null) {
window.setTimeout(
2021-12-21 15:43:35 +01:00
() =>
this.popupUtils.setContentScrollY(window, this.state.scrollY, this.scrollingContainer),
2021-12-21 15:43:35 +01:00
0
);
}
this.stateService.remove(ComponentId);
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":
2018-08-20 23:40:39 +02:00
if (message.successfully) {
2018-04-11 05:28:50 +02:00
window.setTimeout(() => {
2018-08-20 23:40:39 +02:00
this.refresh();
2021-12-21 15:43:35 +01:00
}, 500);
}
break;
default:
2021-12-21 15:43:35 +01:00
break;
}
2021-12-21 15:43:35 +01:00
2018-04-06 05:49:04 +02:00
this.changeDetectorRef.detectChanges();
2021-12-21 15:43:35 +01:00
});
});
}
2018-04-06 21:33:20 +02:00
ngOnDestroy() {
2018-04-09 20:17:58 +02:00
this.saveState();
2018-04-06 05:49:04 +02:00
this.broadcasterService.unsubscribe(ComponentId);
2021-12-21 15:43:35 +01:00
}
selectCipher(cipher: CipherView) {
2018-04-11 05:28:50 +02:00
this.selectedTimeout = window.setTimeout(() => {
if (!this.preventSelected) {
2018-04-06 05:49:04 +02:00
super.selectCipher(cipher);
2018-10-30 13:56:38 +01:00
this.router.navigate(["/view-cipher"], { queryParams: { cipherId: cipher.id } });
2021-12-21 15:43:35 +01:00
}
2018-04-12 21:56:27 +02:00
this.preventSelected = false;
2021-12-21 15:43:35 +01:00
}, 200);
}
2018-10-30 13:56:38 +01:00
selectFolder(folder: FolderView) {
if (folder.id != null) {
2018-10-30 13:56:38 +01:00
this.router.navigate(["/ciphers"], { queryParams: { folderId: folder.id } });
2018-04-06 05:49:04 +02:00
}
2021-12-21 15:43:35 +01:00
}
selectCollection(collection: CollectionView) {
this.router.navigate(["/ciphers"], { queryParams: { collectionId: collection.id } });
2021-12-21 15:43:35 +01:00
}
2018-04-06 05:49:04 +02:00
async launchCipher(cipher: CipherView) {
if (cipher.type !== CipherType.Login || !cipher.login.canLaunch) {
return;
2018-04-06 05:49:04 +02:00
}
2018-04-09 20:17:58 +02:00
if (this.selectedTimeout != null) {
window.clearTimeout(this.selectedTimeout);
2021-12-21 15:43:35 +01:00
}
this.preventSelected = true;
await this.cipherService.updateLastLaunchedDate(cipher.id);
BrowserApi.createNewTab(cipher.login.launchUri);
if (this.popupUtils.inPopup(window)) {
2018-04-11 05:28:50 +02:00
BrowserApi.closePopup(window);
}
2021-12-21 15:43:35 +01:00
}
2018-04-09 20:17:58 +02:00
addCipher() {
if (this.deleted) {
return false;
}
2018-04-11 05:28:50 +02:00
super.addCipher();
this.router.navigate(["/add-cipher"], {
queryParams: {
folderId: this.folderId,
2018-10-30 13:56:38 +01:00
type: this.type,
collectionId: this.collectionId,
2021-12-21 15:43:35 +01:00
},
});
}
back() {
(window as any).routeDirection = "b";
2018-04-06 05:49:04 +02:00
this.location.back();
2021-12-21 15:43:35 +01:00
}
showGroupings() {
2021-12-21 15:43:35 +01:00
return (
!this.isSearching() &&
((this.nestedFolders && this.nestedFolders.length) ||
(this.nestedCollections && this.nestedCollections.length))
2021-12-21 15:43:35 +01:00
);
}
2018-04-09 20:17:58 +02:00
private async saveState() {
this.state = {
scrollY: this.popupUtils.getContentScrollY(window, this.scrollingContainer),
2018-04-09 20:17:58 +02:00
searchText: this.searchText,
2021-12-21 15:43:35 +01:00
};
2018-04-09 20:17:58 +02:00
await this.stateService.save(ComponentId, this.state);
2021-12-21 15:43:35 +01:00
}
}