2018-04-06 05:49:04 +02:00
|
|
|
import { Location } from "@angular/common";
|
2018-04-05 21:35:56 +02:00
|
|
|
import { ChangeDetectorRef, Component, NgZone, OnDestroy, OnInit } from "@angular/core";
|
|
|
|
import { ActivatedRoute, Router } from "@angular/router";
|
|
|
|
|
2021-10-14 23:58:59 +02:00
|
|
|
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";
|
2021-06-07 19:25:37 +02:00
|
|
|
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";
|
2018-04-05 21:35:56 +02:00
|
|
|
|
2021-06-07 19:25:37 +02:00
|
|
|
import { CipherType } from "jslib-common/enums/cipherType";
|
2018-04-09 20:45:35 +02:00
|
|
|
|
2021-06-07 19:25:37 +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";
|
2018-10-26 18:37:55 +02:00
|
|
|
|
2021-06-07 19:25:37 +02:00
|
|
|
import { TreeNode } from "jslib-common/models/domain/treeNode";
|
2018-04-05 21:35:56 +02:00
|
|
|
|
2021-06-07 19:25:37 +02:00
|
|
|
import { CiphersComponent as BaseCiphersComponent } from "jslib-angular/components/ciphers.component";
|
2018-04-05 21:35:56 +02:00
|
|
|
|
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
|
|
|
|
2018-04-05 21:35:56 +02:00
|
|
|
@Component({
|
|
|
|
selector: "app-vault-ciphers",
|
2018-04-06 17:48:45 +02:00
|
|
|
templateUrl: "ciphers.component.html",
|
2018-04-05 21:35:56 +02:00
|
|
|
})
|
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
|
|
|
|
2021-10-14 23:58:59 +02:00
|
|
|
private selectedTimeout: number;
|
|
|
|
private preventSelected = false;
|
2019-11-20 22:31:45 +01:00
|
|
|
private applySavedState = true;
|
|
|
|
private scrollingContainer = "cdk-virtual-scroll-viewport";
|
2021-12-21 15:43:35 +01:00
|
|
|
|
2020-04-13 16:26:11 +02: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,
|
2018-10-26 18:37:55 +02:00
|
|
|
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,
|
2018-10-26 18:37:55 +02:00
|
|
|
private platformUtilsService: PlatformUtilsService,
|
2018-04-05 21:35:56 +02:00
|
|
|
private cipherService: CipherService
|
|
|
|
) {
|
2018-04-09 20:45:35 +02:00
|
|
|
super(searchService);
|
2019-11-20 22:31:45 +01:00
|
|
|
this.applySavedState =
|
2021-05-21 04:42:56 +02:00
|
|
|
(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;
|
2018-10-26 18:37:55 +02:00
|
|
|
}
|
|
|
|
}
|
2021-12-21 15:43:35 +01:00
|
|
|
|
2018-10-26 18:37:55 +02:00
|
|
|
if (params.deleted) {
|
2018-10-29 13:58:20 +01:00
|
|
|
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);
|
2018-10-26 18:37:55 +02:00
|
|
|
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
|
2018-10-26 18:37:55 +02:00
|
|
|
? 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;
|
2020-10-12 19:33:26 +02:00
|
|
|
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
|
|
|
);
|
2018-04-05 21:35:56 +02: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
|
|
|
}
|
|
|
|
|
2020-04-13 16:26:11 +02:00
|
|
|
if (this.applySavedState && this.state != null) {
|
|
|
|
window.setTimeout(
|
2021-12-21 15:43:35 +01:00
|
|
|
() =>
|
2020-04-13 16:26:11 +02:00
|
|
|
this.popupUtils.setContentScrollY(window, this.state.scrollY, this.scrollingContainer),
|
2021-12-21 15:43:35 +01:00
|
|
|
0
|
|
|
|
);
|
|
|
|
}
|
2020-04-13 16:26:11 +02:00
|
|
|
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;
|
2020-04-13 16:26:11 +02:00
|
|
|
default:
|
2021-12-21 15:43:35 +01:00
|
|
|
break;
|
2020-04-13 16:26:11 +02:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2018-04-05 21:35:56 +02: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) {
|
2018-10-26 18:37:55 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-10-26 18:37:55 +02:00
|
|
|
selectCollection(collection: CollectionView) {
|
2018-10-29 13:58:20 +01:00
|
|
|
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) {
|
2018-10-29 13:58:20 +01:00
|
|
|
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
|
|
|
|
2018-10-29 14:29:21 +01:00
|
|
|
if (this.selectedTimeout != null) {
|
|
|
|
window.clearTimeout(this.selectedTimeout);
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
2018-10-29 14:29:21 +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);
|
2018-10-29 14:29:21 +01:00
|
|
|
}
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
2018-10-29 14:29:21 +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"], {
|
2018-10-29 13:58:20 +01:00
|
|
|
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() {
|
2018-10-29 13:58:20 +01:00
|
|
|
(window as any).routeDirection = "b";
|
2018-04-06 05:49:04 +02:00
|
|
|
this.location.back();
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
|
2018-10-29 14:29:21 +01:00
|
|
|
showGroupings() {
|
2021-12-21 15:43:35 +01:00
|
|
|
return (
|
2018-10-29 14:29:21 +01:00
|
|
|
!this.isSearching() &&
|
|
|
|
((this.nestedFolders && this.nestedFolders.length) ||
|
2018-10-26 18:37:55 +02:00
|
|
|
(this.nestedCollections && this.nestedCollections.length))
|
2021-12-21 15:43:35 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-04-09 20:17:58 +02:00
|
|
|
private async saveState() {
|
2019-11-20 22:31:45 +01:00
|
|
|
this.state = {
|
2021-05-21 04:42:56 +02:00
|
|
|
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
|
|
|
}
|
2018-04-05 21:35:56 +02:00
|
|
|
}
|