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

149 lines
5.4 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-09 20:45:35 +02:00
import { FolderService } from 'jslib/abstractions/folder.service';
import { CollectionService } from 'jslib/abstractions/collection.service';
import { CipherService } from 'jslib/abstractions/cipher.service';
2018-04-09 20:45:35 +02:00
import { I18nService } from 'jslib/abstractions/i18n.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-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,
private folderService: FolderService, private collectionService: CollectionService) {
super(cipherService);
}
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');
const t = parseInt(params.type, null);
2018-04-09 20:45:35 +02:00
switch (t) {
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;
}
await super.load((c) => c.type === t);
} else if (params.folderId) {
2018-04-09 20:45:35 +02:00
const folderId = params.folderId === 'none' ? null : params.folderId;
this.searchPlaceholder = this.i18nService.t('searchFolder');
if (folderId != null) {
const folder = await this.folderService.get(folderId);
if (folder != null) {
this.groupingTitle = (await folder.decrypt()).name;
}
} else {
this.groupingTitle = this.i18nService.t('noneFolder');
}
await super.load((c) => c.folderId === 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;
}
await super.load((c) => 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
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(() => {
this.load();
}, 500);
break;
default:
break;
}
this.changeDetectorRef.detectChanges();
})
});
}
ngOnDestroy() {
2018-04-09 20:17:58 +02:00
this.saveState();
this.broadcasterService.unsubscribe(ComponentId);
}
selectCipher(cipher: CipherView) {
super.selectCipher(cipher);
2018-04-06 05:09:49 +02:00
this.router.navigate(['/view-cipher'], { queryParams: { cipherId: cipher.id } });
}
2018-04-06 05:49:04 +02:00
addCipher() {
super.addCipher();
this.router.navigate(['/add-cipher']);
}
back() {
this.location.back();
}
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);
}
}