2018-10-23 18:04:21 +02:00
|
|
|
import {
|
2020-08-17 18:14:40 +02:00
|
|
|
Directive,
|
2018-10-23 18:04:21 +02:00
|
|
|
EventEmitter,
|
|
|
|
Input,
|
|
|
|
OnInit,
|
|
|
|
Output,
|
|
|
|
} from '@angular/core';
|
|
|
|
|
2021-06-03 18:58:57 +02:00
|
|
|
import { CipherService } from 'jslib-common/abstractions/cipher.service';
|
|
|
|
import { CollectionService } from 'jslib-common/abstractions/collection.service';
|
|
|
|
import { I18nService } from 'jslib-common/abstractions/i18n.service';
|
2021-10-19 10:32:14 +02:00
|
|
|
import { LogService } from 'jslib-common/abstractions/log.service';
|
2021-06-03 18:58:57 +02:00
|
|
|
import { PlatformUtilsService } from 'jslib-common/abstractions/platformUtils.service';
|
2018-10-23 18:04:21 +02:00
|
|
|
|
2021-06-03 18:58:57 +02:00
|
|
|
import { CipherView } from 'jslib-common/models/view/cipherView';
|
|
|
|
import { CollectionView } from 'jslib-common/models/view/collectionView';
|
2018-10-23 18:04:21 +02:00
|
|
|
|
2021-06-03 18:58:57 +02:00
|
|
|
import { Cipher } from 'jslib-common/models/domain/cipher';
|
2018-10-23 18:04:21 +02:00
|
|
|
|
2020-08-17 18:14:40 +02:00
|
|
|
@Directive()
|
2018-10-23 18:04:21 +02:00
|
|
|
export class CollectionsComponent implements OnInit {
|
|
|
|
@Input() cipherId: string;
|
2019-06-26 23:43:03 +02:00
|
|
|
@Input() allowSelectNone = false;
|
2018-10-23 18:04:21 +02:00
|
|
|
@Output() onSavedCollections = new EventEmitter();
|
|
|
|
|
|
|
|
formPromise: Promise<any>;
|
|
|
|
cipher: CipherView;
|
|
|
|
collectionIds: string[];
|
|
|
|
collections: CollectionView[] = [];
|
|
|
|
|
|
|
|
protected cipherDomain: Cipher;
|
|
|
|
|
|
|
|
constructor(protected collectionService: CollectionService, protected platformUtilsService: PlatformUtilsService,
|
2021-10-19 10:32:14 +02:00
|
|
|
protected i18nService: I18nService, protected cipherService: CipherService, private logService: LogService) { }
|
2018-10-23 18:04:21 +02:00
|
|
|
|
|
|
|
async ngOnInit() {
|
|
|
|
await this.load();
|
|
|
|
}
|
|
|
|
|
|
|
|
async load() {
|
|
|
|
this.cipherDomain = await this.loadCipher();
|
|
|
|
this.collectionIds = this.loadCipherCollections();
|
|
|
|
this.cipher = await this.cipherDomain.decrypt();
|
|
|
|
this.collections = await this.loadCollections();
|
|
|
|
|
2021-02-04 16:49:23 +01:00
|
|
|
this.collections.forEach(c => (c as any).checked = false);
|
2018-10-23 18:04:21 +02:00
|
|
|
if (this.collectionIds != null) {
|
2021-02-04 16:49:23 +01:00
|
|
|
this.collections.forEach(c => {
|
2018-10-23 18:04:21 +02:00
|
|
|
(c as any).checked = this.collectionIds != null && this.collectionIds.indexOf(c.id) > -1;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async submit() {
|
2019-06-26 23:43:03 +02:00
|
|
|
const selectedCollectionIds = this.collections
|
2021-02-04 16:49:23 +01:00
|
|
|
.filter(c => !!(c as any).checked)
|
|
|
|
.map(c => c.id);
|
2019-06-26 23:43:03 +02:00
|
|
|
if (!this.allowSelectNone && selectedCollectionIds.length === 0) {
|
|
|
|
this.platformUtilsService.showToast('error', this.i18nService.t('errorOccurred'),
|
|
|
|
this.i18nService.t('selectOneCollection'));
|
2019-06-26 23:50:37 +02:00
|
|
|
return;
|
2019-06-26 23:43:03 +02:00
|
|
|
}
|
|
|
|
this.cipherDomain.collectionIds = selectedCollectionIds;
|
2018-10-23 18:04:21 +02:00
|
|
|
try {
|
|
|
|
this.formPromise = this.saveCollections();
|
|
|
|
await this.formPromise;
|
|
|
|
this.onSavedCollections.emit();
|
|
|
|
this.platformUtilsService.showToast('success', null, this.i18nService.t('editedItem'));
|
2021-10-19 10:32:14 +02:00
|
|
|
} catch (e) {
|
|
|
|
this.logService.error(e);
|
|
|
|
}
|
2018-10-23 18:04:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected loadCipher() {
|
|
|
|
return this.cipherService.get(this.cipherId);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected loadCipherCollections() {
|
|
|
|
return this.cipherDomain.collectionIds;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected async loadCollections() {
|
|
|
|
const allCollections = await this.collectionService.getAllDecrypted();
|
2021-02-04 16:49:23 +01:00
|
|
|
return allCollections.filter(c => !c.readOnly && c.organizationId === this.cipher.organizationId);
|
2018-10-23 18:04:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected saveCollections() {
|
|
|
|
return this.cipherService.saveCollectionsWithServer(this.cipherDomain);
|
|
|
|
}
|
|
|
|
}
|