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

250 lines
9.3 KiB
TypeScript
Raw Normal View History

2018-04-11 05:28:50 +02:00
import { Angulartics2 } from 'angulartics2';
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';
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;
showAdd = true;
2018-04-10 06:04:49 +02:00
folderId: string = null;
type: CipherType = null;
2018-04-11 16:23:12 +02:00
pagedCiphers: CipherView[] = [];
nestedFolders: Array<TreeNode<FolderView>>;
nestedCollections: Array<TreeNode<CollectionView>>;
2018-04-11 16:23:12 +02:00
private didScroll = false;
2018-04-12 21:56:27 +02:00
private selectedTimeout: number;
private preventSelected = false;
2018-04-14 05:22:20 +02:00
private pageSize = 100;
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,
2018-04-14 05:22:20 +02:00
private analytics: Angulartics2, private platformUtilsService: PlatformUtilsService) {
2018-08-13 17:53:16 +02:00
super(searchService);
2018-04-17 19:06:57 +02:00
this.pageSize = platformUtilsService.isEdge() ? 25 : 100;
}
async ngOnInit() {
this.route.queryParams.subscribe(async (params) => {
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;
}
2018-04-10 06:04:49 +02:00
await super.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');
}
2018-04-10 06:04:49 +02:00
await super.load((c) => c.folderId === this.folderId);
} else if (params.collectionId) {
this.showAdd = false;
2018-04-09 20:45:35 +02:00
this.searchPlaceholder = this.i18nService.t('searchCollection');
const collectionNode = await this.collectionService.getNested(params.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
}
2018-04-10 06:04:49 +02:00
await super.load((c) => c.collectionIds != null && c.collectionIds.indexOf(params.collectionId) > -1);
} else {
2018-04-09 20:45:35 +02:00
this.groupingTitle = this.i18nService.t('allItems');
await super.load();
}
2018-04-09 20:17:58 +02:00
2018-04-11 16:23:12 +02:00
this.loadMore();
2018-04-09 20:17:58 +02:00
this.state = (await this.stateService.get<any>(ComponentId)) || {};
if (this.state.searchText) {
this.searchText = this.state.searchText;
}
window.setTimeout(() => this.popupUtils.setContentScrollY(window, this.state.scrollY), 0);
// TODO: This is pushing a new page onto the browser navigation history. Figure out how to now do that
// so that we don't have to hit back button twice
const newUrl = this.router.createUrlTree([], {
queryParams: { direction: null },
queryParamsHandling: 'merge',
preserveFragment: true,
replaceUrl: true,
}).toString();
this.location.go(newUrl);
});
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, direction: 'f' } });
}
}
selectCollection(collection: CollectionView) {
this.router.navigate(['/ciphers'], { queryParams: { collectionId: collection.id, direction: 'f' } });
}
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;
this.analytics.eventTrack.next({ action: 'Launched URI From Listing' });
BrowserApi.createNewTab(cipher.login.uri);
if (this.popupUtils.inPopup(window)) {
BrowserApi.closePopup(window);
}
}
2018-04-06 05:49:04 +02:00
addCipher() {
super.addCipher();
2018-04-10 06:04:49 +02:00
this.router.navigate(['/add-cipher'], { queryParams: { folderId: this.folderId, type: this.type } });
2018-04-06 05:49:04 +02:00
}
back() {
this.location.back();
}
2018-04-09 20:17:58 +02:00
2018-04-11 16:23:12 +02:00
loadMore() {
2018-04-14 05:22:20 +02:00
if (this.ciphers.length <= this.pageSize) {
2018-04-11 16:34:39 +02:00
return;
}
2018-04-11 16:23:12 +02:00
const pagedLength = this.pagedCiphers.length;
if (this.ciphers.length > pagedLength) {
2018-04-14 05:22:20 +02:00
this.pagedCiphers = this.pagedCiphers.concat(this.ciphers.slice(pagedLength, pagedLength + this.pageSize));
2018-04-11 16:23:12 +02:00
}
2018-04-14 05:22:20 +02:00
this.didScroll = this.pagedCiphers.length > this.pageSize;
2018-04-11 16:23:12 +02:00
}
isSearching() {
2018-08-13 17:53:16 +02:00
return !this.searchPending && this.searchService.isSearchable(this.searchText);
2018-04-11 16:34:39 +02:00
}
isPaging() {
const searching = this.isSearching();
2018-04-11 16:23:12 +02:00
if (searching && this.didScroll) {
this.resetPaging();
}
2018-04-14 05:22:20 +02:00
return !searching && this.ciphers.length > this.pageSize;
2018-04-11 16:23:12 +02:00
}
routerCanReuse() {
return false;
}
2018-04-11 16:23:12 +02:00
async resetPaging() {
this.pagedCiphers = [];
this.loadMore();
}
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);
}
}