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

236 lines
9.0 KiB
TypeScript
Raw Normal View History

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