2018-04-10 01:05:13 +02:00
|
|
|
import {
|
|
|
|
EventEmitter,
|
|
|
|
Output,
|
|
|
|
} from '@angular/core';
|
|
|
|
|
|
|
|
import { CryptoService } from '../../abstractions/crypto.service';
|
2018-05-17 16:52:06 +02:00
|
|
|
import { ExportService } from '../../abstractions/export.service';
|
2018-04-10 01:05:13 +02:00
|
|
|
import { I18nService } from '../../abstractions/i18n.service';
|
|
|
|
import { PlatformUtilsService } from '../../abstractions/platformUtils.service';
|
|
|
|
|
|
|
|
export class ExportComponent {
|
|
|
|
@Output() onSaved = new EventEmitter();
|
|
|
|
|
2018-07-05 20:39:58 +02:00
|
|
|
formPromise: Promise<string>;
|
2018-04-10 01:05:13 +02:00
|
|
|
masterPassword: string;
|
|
|
|
showPassword = false;
|
|
|
|
|
2018-10-03 06:03:49 +02:00
|
|
|
constructor(protected cryptoService: CryptoService, protected i18nService: I18nService,
|
2018-08-14 21:12:10 +02:00
|
|
|
protected platformUtilsService: PlatformUtilsService, protected exportService: ExportService,
|
|
|
|
protected win: Window) { }
|
2018-04-10 01:05:13 +02:00
|
|
|
|
|
|
|
async submit() {
|
|
|
|
if (this.masterPassword == null || this.masterPassword === '') {
|
2018-10-03 05:09:19 +02:00
|
|
|
this.platformUtilsService.showToast('error', this.i18nService.t('errorOccurred'),
|
2018-04-10 01:05:13 +02:00
|
|
|
this.i18nService.t('invalidMasterPassword'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-14 21:12:10 +02:00
|
|
|
const keyHash = await this.cryptoService.hashPassword(this.masterPassword, null);
|
2018-04-10 01:05:13 +02:00
|
|
|
const storedKeyHash = await this.cryptoService.getKeyHash();
|
|
|
|
if (storedKeyHash != null && keyHash != null && storedKeyHash === keyHash) {
|
2018-07-05 20:39:58 +02:00
|
|
|
try {
|
|
|
|
this.formPromise = this.getExportData();
|
|
|
|
const data = await this.formPromise;
|
2018-10-03 06:03:49 +02:00
|
|
|
this.platformUtilsService.eventTrack('Exported Data');
|
2018-07-05 20:39:58 +02:00
|
|
|
this.downloadFile(data);
|
|
|
|
this.saved();
|
|
|
|
} catch { }
|
2018-04-10 01:05:13 +02:00
|
|
|
} else {
|
2018-10-03 05:09:19 +02:00
|
|
|
this.platformUtilsService.showToast('error', this.i18nService.t('errorOccurred'),
|
2018-04-10 01:05:13 +02:00
|
|
|
this.i18nService.t('invalidMasterPassword'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
togglePassword() {
|
2018-10-03 06:03:49 +02:00
|
|
|
this.platformUtilsService.eventTrack('Toggled Master Password on Export');
|
2018-04-10 01:05:13 +02:00
|
|
|
this.showPassword = !this.showPassword;
|
|
|
|
document.getElementById('masterPassword').focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected saved() {
|
|
|
|
this.onSaved.emit();
|
|
|
|
}
|
|
|
|
|
2018-07-05 20:39:58 +02:00
|
|
|
protected getExportData() {
|
|
|
|
return this.exportService.getExport('csv');
|
|
|
|
}
|
|
|
|
|
|
|
|
protected getFileName(prefix?: string) {
|
|
|
|
return this.exportService.getFileName(prefix);
|
|
|
|
}
|
|
|
|
|
2018-04-10 01:05:13 +02:00
|
|
|
private downloadFile(csv: string): void {
|
2018-07-05 20:39:58 +02:00
|
|
|
const fileName = this.getFileName();
|
2018-04-10 01:05:13 +02:00
|
|
|
this.platformUtilsService.saveFile(this.win, csv, { type: 'text/plain' }, fileName);
|
|
|
|
}
|
|
|
|
}
|