1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-10-10 06:08:34 +02:00
bitwarden-browser/src/app/vault/bulk-share.component.ts

98 lines
3.5 KiB
TypeScript
Raw Normal View History

2018-06-13 06:03:48 +02:00
import {
Component,
EventEmitter,
Input,
OnInit,
Output,
} from '@angular/core';
import { ToasterService } from 'angular2-toaster';
import { CipherService } from 'jslib-common/abstractions/cipher.service';
import { CollectionService } from 'jslib-common/abstractions/collection.service';
import { I18nService } from 'jslib-common/abstractions/i18n.service';
import { UserService } from 'jslib-common/abstractions/user.service';
2018-06-13 06:03:48 +02:00
import { CipherView } from 'jslib-common/models/view';
import { CollectionView } from 'jslib-common/models/view/collectionView';
2018-06-13 06:03:48 +02:00
import { Organization } from 'jslib-common/models/domain/organization';
2018-06-13 06:03:48 +02:00
@Component({
selector: 'app-vault-bulk-share',
templateUrl: 'bulk-share.component.html',
})
export class BulkShareComponent implements OnInit {
@Input() ciphers: CipherView[] = [];
@Input() organizationId: string;
@Output() onShared = new EventEmitter();
nonShareableCount = 0;
collections: CollectionView[] = [];
organizations: Organization[] = [];
2018-07-09 22:56:09 +02:00
shareableCiphers: CipherView[] = [];
2018-06-13 06:03:48 +02:00
formPromise: Promise<any>;
private writeableCollections: CollectionView[] = [];
constructor(private cipherService: CipherService, private toasterService: ToasterService,
private i18nService: I18nService, private collectionService: CollectionService,
private userService: UserService) { }
2018-06-13 06:03:48 +02:00
async ngOnInit() {
this.shareableCiphers = this.ciphers.filter(c => !c.hasOldAttachments && c.organizationId == null);
2018-06-13 06:03:48 +02:00
this.nonShareableCount = this.ciphers.length - this.shareableCiphers.length;
const allCollections = await this.collectionService.getAllDecrypted();
this.writeableCollections = allCollections.filter(c => !c.readOnly);
2018-06-13 06:03:48 +02:00
this.organizations = await this.userService.getAllOrganizations();
if (this.organizationId == null && this.organizations.length > 0) {
this.organizationId = this.organizations[0].id;
}
this.filterCollections();
}
ngOnDestroy() {
this.selectAll(false);
}
filterCollections() {
this.selectAll(false);
if (this.organizationId == null || this.writeableCollections.length === 0) {
this.collections = [];
} else {
this.collections = this.writeableCollections.filter(c => c.organizationId === this.organizationId);
2018-06-13 06:03:48 +02:00
}
}
async submit() {
const checkedCollectionIds = this.collections.filter(c => (c as any).checked).map(c => c.id);
try {
this.formPromise = this.cipherService.shareManyWithServer(this.shareableCiphers, this.organizationId,
checkedCollectionIds);
await this.formPromise;
this.onShared.emit();
this.toasterService.popAsync('success', null, this.i18nService.t('sharedItems'));
} catch { }
2018-06-13 06:03:48 +02:00
}
2018-07-09 22:27:54 +02:00
check(c: CollectionView, select?: boolean) {
(c as any).checked = select == null ? !(c as any).checked : select;
2018-06-13 06:03:48 +02:00
}
2018-07-09 22:27:54 +02:00
selectAll(select: boolean) {
2018-06-13 06:03:48 +02:00
const collections = select ? this.collections : this.writeableCollections;
collections.forEach(c => this.check(c, select));
2018-06-13 06:03:48 +02:00
}
get canSave() {
if (this.shareableCiphers != null && this.shareableCiphers.length > 0 && this.collections != null) {
for (let i = 0; i < this.collections.length; i++) {
if ((this.collections[i] as any).checked) {
return true;
}
}
}
return false;
}
2018-06-13 06:03:48 +02:00
}