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

215 lines
7.6 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';
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-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';
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 16:50:28 +02:00
searchText: 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[] = [];
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-04-06 05:09:49 +02:00
constructor(cipherService: CipherService, 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) {
super(cipherService);
2018-04-14 05:22:20 +02:00
this.pageSize = platformUtilsService.isEdge() ? 50 : 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 folder = await this.folderService.get(this.folderId);
2018-04-09 20:45:35 +02:00
if (folder != null) {
this.groupingTitle = (await folder.decrypt()).name;
}
} 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 collection = await this.collectionService.get(params.collectionId);
if (collection != null) {
this.groupingTitle = (await collection.decrypt()).name;
}
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);
});
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':
window.setTimeout(() => {
2018-04-11 04:49:19 +02:00
this.refresh();
2018-04-06 21:33:20 +02:00
}, 500);
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);
}
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-04-11 16:34:39 +02:00
return this.searchText != null && this.searchText.length > 1;
}
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
}
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);
}
}