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

293 lines
9.7 KiB
TypeScript
Raw Normal View History

2018-04-11 05:28:50 +02:00
import { Angulartics2 } from 'angulartics2';
2018-04-05 04:59:42 +02:00
import {
2018-04-06 21:33:20 +02:00
ChangeDetectorRef,
2018-04-05 04:59:42 +02:00
Component,
2018-04-06 21:33:20 +02:00
NgZone,
OnDestroy,
2018-04-05 04:59:42 +02:00
OnInit,
} from '@angular/core';
2018-04-09 16:50:28 +02:00
import {
ActivatedRoute,
Router,
} from '@angular/router';
2018-04-05 16:29:11 +02:00
2018-04-11 05:28:50 +02:00
import { BrowserApi } from '../../browser/browserApi';
2018-04-05 16:29:11 +02:00
import { CipherType } from 'jslib/enums/cipherType';
import { CipherView } from 'jslib/models/view/cipherView';
2018-04-11 05:49:46 +02:00
import { CollectionView } from 'jslib/models/view/collectionView';
import { FolderView } from 'jslib/models/view/folderView';
2018-04-05 16:29:11 +02:00
import { CipherService } from 'jslib/abstractions/cipher.service';
2018-04-11 05:49:46 +02:00
import { CollectionService } from 'jslib/abstractions/collection.service';
2018-04-05 16:29:11 +02:00
import { FolderService } from 'jslib/abstractions/folder.service';
2018-04-09 20:17:58 +02:00
import { StateService } from 'jslib/abstractions/state.service';
2018-04-11 04:49:19 +02:00
import { SyncService } from 'jslib/abstractions/sync.service';
2018-04-05 16:29:11 +02:00
2018-04-06 21:33:20 +02:00
import { BroadcasterService } from 'jslib/angular/services/broadcaster.service';
2018-04-05 16:29:11 +02:00
import { GroupingsComponent as BaseGroupingsComponent } from 'jslib/angular/components/groupings.component';
2018-04-05 04:59:42 +02:00
2018-04-09 20:17:58 +02:00
import { PopupUtilsService } from '../services/popup-utils.service';
const ComponentId = 'GroupingsComponent';
2018-04-12 21:56:27 +02:00
const ScopeStateId = ComponentId + 'Scope';
2018-04-06 21:33:20 +02:00
2018-04-05 04:59:42 +02:00
@Component({
selector: 'app-vault-groupings',
2018-04-06 17:48:45 +02:00
templateUrl: 'groupings.component.html',
2018-04-05 04:59:42 +02:00
})
2018-04-06 21:33:20 +02:00
export class GroupingsComponent extends BaseGroupingsComponent implements OnInit, OnDestroy {
2018-04-05 16:29:11 +02:00
ciphers: CipherView[];
favoriteCiphers: CipherView[];
2018-04-05 23:27:31 +02:00
noFolderCiphers: CipherView[];
2018-04-05 16:29:11 +02:00
folderCounts = new Map<string, number>();
collectionCounts = new Map<string, number>();
typeCounts = new Map<CipherType, number>();
2018-04-09 16:50:28 +02:00
searchText: string;
2018-04-09 20:17:58 +02:00
state: any;
2018-04-12 21:56:27 +02:00
scopeState: any;
private loadedTimeout: number;
private selectedTimeout: number;
private preventSelected = false;
2018-04-05 16:29:11 +02:00
constructor(collectionService: CollectionService, folderService: FolderService,
2018-04-06 21:33:20 +02:00
private cipherService: CipherService, private router: Router,
private ngZone: NgZone, private broadcasterService: BroadcasterService,
2018-04-09 20:17:58 +02:00
private changeDetectorRef: ChangeDetectorRef, private route: ActivatedRoute,
2018-04-11 04:49:19 +02:00
private stateService: StateService, private popupUtils: PopupUtilsService,
2018-04-11 05:28:50 +02:00
private syncService: SyncService, private analytics: Angulartics2) {
2018-04-05 16:29:11 +02:00
super(collectionService, folderService);
}
2018-04-12 21:56:27 +02:00
get showNoFolderCiphers(): boolean {
return this.noFolderCiphers != null && this.noFolderCiphers.length < 100 &&
this.collections.length === 0;
}
get folderCount(): number {
return this.folders.length - (this.showNoFolderCiphers ? 0 : 1);
}
async ngOnInit() {
2018-04-09 20:17:58 +02:00
this.stateService.remove('CiphersComponent');
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(() => {
this.load();
}, 500);
break;
default:
break;
}
this.changeDetectorRef.detectChanges();
2018-04-11 05:49:46 +02:00
});
2018-04-06 21:33:20 +02:00
});
2018-04-12 21:56:27 +02:00
const restoredScopeState = await this.restoreState();
2018-04-09 16:50:28 +02:00
this.route.queryParams.subscribe(async (params) => {
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;
} else if (params.searchText) {
2018-04-09 16:50:28 +02:00
this.searchText = params.searchText;
}
2018-04-11 04:49:19 +02:00
if (!this.syncService.syncInProgress) {
this.load();
} else {
2018-04-12 21:56:27 +02:00
this.loadedTimeout = window.setTimeout(() => {
2018-04-11 04:49:19 +02:00
if (!this.loaded) {
2018-04-12 21:56:27 +02:00
this.load();
2018-04-11 04:49:19 +02:00
}
}, 10000);
}
2018-04-12 21:56:27 +02:00
if (!this.syncService.syncInProgress || restoredScopeState) {
window.setTimeout(() => this.popupUtils.setContentScrollY(window, this.state.scrollY), 0);
}
2018-04-09 16:50:28 +02:00
});
2018-04-06 21:33:20 +02:00
}
ngOnDestroy() {
2018-04-11 05:28:50 +02:00
if (this.loadedTimeout != null) {
window.clearTimeout(this.loadedTimeout);
}
if (this.selectedTimeout != null) {
window.clearTimeout(this.selectedTimeout);
}
2018-04-09 20:17:58 +02:00
this.saveState();
this.broadcasterService.unsubscribe(ComponentId);
2018-04-06 21:33:20 +02:00
}
async load() {
2018-04-12 21:56:27 +02:00
await super.load(false);
2018-04-05 16:29:11 +02:00
await this.loadCiphers();
2018-04-05 23:27:31 +02:00
if (this.showNoFolderCiphers) {
// Remove "No Folder" from folder listing
this.folders.pop();
}
super.loaded = true;
2018-04-05 16:29:11 +02:00
}
async loadCiphers() {
this.ciphers = await this.cipherService.getAllDecrypted();
2018-04-12 21:56:27 +02:00
let favoriteCiphers: CipherView[] = null;
let noFolderCiphers: CipherView[] = null;
const folderCounts = new Map<string, number>();
const collectionCounts = new Map<string, number>();
const typeCounts = new Map<CipherType, number>();
2018-04-05 16:29:11 +02:00
this.ciphers.forEach((c) => {
if (c.favorite) {
2018-04-12 21:56:27 +02:00
if (favoriteCiphers == null) {
favoriteCiphers = [];
2018-04-05 16:29:11 +02:00
}
2018-04-12 21:56:27 +02:00
favoriteCiphers.push(c);
2018-04-05 16:29:11 +02:00
}
2018-04-05 23:27:31 +02:00
if (c.folderId == null) {
2018-04-12 21:56:27 +02:00
if (noFolderCiphers == null) {
noFolderCiphers = [];
2018-04-05 23:27:31 +02:00
}
2018-04-12 21:56:27 +02:00
noFolderCiphers.push(c);
2018-04-05 23:27:31 +02:00
}
2018-04-12 21:56:27 +02:00
if (typeCounts.has(c.type)) {
typeCounts.set(c.type, typeCounts.get(c.type) + 1);
2018-04-05 16:29:11 +02:00
} else {
2018-04-12 21:56:27 +02:00
typeCounts.set(c.type, 1);
2018-04-05 16:29:11 +02:00
}
2018-04-12 21:56:27 +02:00
if (folderCounts.has(c.folderId)) {
folderCounts.set(c.folderId, folderCounts.get(c.folderId) + 1);
2018-04-05 16:29:11 +02:00
} else {
2018-04-12 21:56:27 +02:00
folderCounts.set(c.folderId, 1);
2018-04-05 16:29:11 +02:00
}
2018-04-05 04:59:42 +02:00
2018-04-05 16:29:11 +02:00
if (c.collectionIds != null) {
c.collectionIds.forEach((colId) => {
2018-04-12 21:56:27 +02:00
if (collectionCounts.has(colId)) {
collectionCounts.set(colId, collectionCounts.get(colId) + 1);
2018-04-05 16:29:11 +02:00
} else {
2018-04-12 21:56:27 +02:00
collectionCounts.set(colId, 1);
2018-04-05 16:29:11 +02:00
}
});
}
});
2018-04-12 21:56:27 +02:00
this.favoriteCiphers = favoriteCiphers;
this.noFolderCiphers = noFolderCiphers;
this.typeCounts = typeCounts;
this.folderCounts = folderCounts;
this.collectionCounts = collectionCounts;
2018-04-05 04:59:42 +02:00
}
2018-04-09 20:17:58 +02:00
async selectType(type: CipherType) {
super.selectType(type);
2018-04-05 21:56:52 +02:00
this.router.navigate(['/ciphers'], { queryParams: { type: type } });
}
2018-04-09 20:17:58 +02:00
async selectFolder(folder: FolderView) {
super.selectFolder(folder);
2018-04-09 20:45:35 +02:00
this.router.navigate(['/ciphers'], { queryParams: { folderId: folder.id || 'none' } });
}
2018-04-09 20:17:58 +02:00
async selectCollection(collection: CollectionView) {
super.selectCollection(collection);
2018-04-05 21:56:52 +02:00
this.router.navigate(['/ciphers'], { queryParams: { collectionId: collection.id } });
}
2018-04-06 05:49:04 +02:00
2018-04-09 20:17:58 +02:00
async selectCipher(cipher: CipherView) {
2018-04-11 05:28:50 +02:00
this.selectedTimeout = window.setTimeout(() => {
if (!this.preventSelected) {
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
}
2018-04-09 20:17:58 +02:00
async addCipher() {
2018-04-06 05:49:04 +02:00
this.router.navigate(['/add-cipher']);
}
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);
2018-04-12 21:56:27 +02:00
this.scopeState = {
favoriteCiphers: this.favoriteCiphers,
noFolderCiphers: this.noFolderCiphers,
ciphers: this.ciphers,
collectionCounts: this.collectionCounts,
folderCounts: this.folderCounts,
typeCounts: this.typeCounts,
folders: this.folders,
collections: this.collections,
};
await this.stateService.save(ScopeStateId, this.scopeState);
}
private async restoreState(): Promise<boolean> {
this.scopeState = await this.stateService.get<any>(ScopeStateId);
if (this.scopeState == null) {
return false;
}
if (this.scopeState.favoriteCiphers != null) {
this.favoriteCiphers = this.scopeState.favoriteCiphers;
}
if (this.scopeState.noFolderCiphers != null) {
this.noFolderCiphers = this.scopeState.noFolderCiphers;
}
if (this.scopeState.ciphers != null) {
this.ciphers = this.scopeState.ciphers;
}
if (this.scopeState.collectionCounts != null) {
this.collectionCounts = this.scopeState.collectionCounts;
}
if (this.scopeState.folderCounts != null) {
this.folderCounts = this.scopeState.folderCounts;
}
if (this.scopeState.typeCounts != null) {
this.typeCounts = this.scopeState.typeCounts;
}
if (this.scopeState.folders != null) {
this.folders = this.scopeState.folders;
}
if (this.scopeState.collections != null) {
this.collections = this.scopeState.collections;
}
return true;
2018-04-09 20:17:58 +02:00
}
2018-04-05 04:59:42 +02:00
}