2021-12-16 13:36:21 +01:00
|
|
|
import { CdkDragDrop, moveItemInArray } from "@angular/cdk/drag-drop";
|
2022-02-22 15:39:11 +01:00
|
|
|
import { Directive, Input, OnChanges, SimpleChanges } from "@angular/core";
|
2021-09-20 02:36:40 +02:00
|
|
|
|
2022-06-14 17:10:53 +02:00
|
|
|
import { EventService } from "@bitwarden/common/abstractions/event.service";
|
|
|
|
import { I18nService } from "@bitwarden/common/abstractions/i18n.service";
|
|
|
|
import { CipherType } from "@bitwarden/common/enums/cipherType";
|
|
|
|
import { EventType } from "@bitwarden/common/enums/eventType";
|
|
|
|
import { FieldType } from "@bitwarden/common/enums/fieldType";
|
|
|
|
import { Utils } from "@bitwarden/common/misc/utils";
|
|
|
|
import { CipherView } from "@bitwarden/common/models/view/cipherView";
|
|
|
|
import { FieldView } from "@bitwarden/common/models/view/fieldView";
|
2021-11-02 23:03:37 +01:00
|
|
|
|
2021-09-20 02:36:40 +02:00
|
|
|
@Directive()
|
2021-11-02 23:03:37 +01:00
|
|
|
export class AddEditCustomFieldsComponent implements OnChanges {
|
2021-12-16 13:36:21 +01:00
|
|
|
@Input() cipher: CipherView;
|
|
|
|
@Input() thisCipherType: CipherType;
|
|
|
|
@Input() editMode: boolean;
|
|
|
|
|
|
|
|
addFieldType: FieldType = FieldType.Text;
|
|
|
|
addFieldTypeOptions: any[];
|
|
|
|
addFieldLinkedTypeOption: any;
|
|
|
|
linkedFieldOptions: any[] = [];
|
|
|
|
|
|
|
|
cipherType = CipherType;
|
|
|
|
fieldType = FieldType;
|
|
|
|
eventType = EventType;
|
|
|
|
|
|
|
|
constructor(private i18nService: I18nService, private eventService: EventService) {
|
|
|
|
this.addFieldTypeOptions = [
|
|
|
|
{ name: i18nService.t("cfTypeText"), value: FieldType.Text },
|
|
|
|
{ name: i18nService.t("cfTypeHidden"), value: FieldType.Hidden },
|
|
|
|
{ name: i18nService.t("cfTypeBoolean"), value: FieldType.Boolean },
|
|
|
|
];
|
|
|
|
this.addFieldLinkedTypeOption = {
|
|
|
|
name: this.i18nService.t("cfTypeLinked"),
|
|
|
|
value: FieldType.Linked,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnChanges(changes: SimpleChanges) {
|
|
|
|
if (changes.thisCipherType != null) {
|
|
|
|
this.setLinkedFieldOptions();
|
|
|
|
|
|
|
|
if (!changes.thisCipherType.firstChange) {
|
|
|
|
this.resetCipherLinkedFields();
|
|
|
|
}
|
2021-11-02 23:03:37 +01:00
|
|
|
}
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
2021-11-02 23:03:37 +01:00
|
|
|
|
2021-12-16 13:36:21 +01:00
|
|
|
addField() {
|
|
|
|
if (this.cipher.fields == null) {
|
|
|
|
this.cipher.fields = [];
|
2021-09-20 02:36:40 +02:00
|
|
|
}
|
|
|
|
|
2021-12-16 13:36:21 +01:00
|
|
|
const f = new FieldView();
|
|
|
|
f.type = this.addFieldType;
|
|
|
|
f.newField = true;
|
2021-09-20 02:36:40 +02:00
|
|
|
|
2021-12-16 13:36:21 +01:00
|
|
|
if (f.type === FieldType.Linked) {
|
|
|
|
f.linkedId = this.linkedFieldOptions[0].value;
|
2021-09-20 02:36:40 +02:00
|
|
|
}
|
|
|
|
|
2021-12-16 13:36:21 +01:00
|
|
|
this.cipher.fields.push(f);
|
|
|
|
}
|
2021-09-20 02:36:40 +02:00
|
|
|
|
2021-12-16 13:36:21 +01:00
|
|
|
removeField(field: FieldView) {
|
|
|
|
const i = this.cipher.fields.indexOf(field);
|
|
|
|
if (i > -1) {
|
|
|
|
this.cipher.fields.splice(i, 1);
|
2021-09-20 02:36:40 +02:00
|
|
|
}
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
2021-09-20 02:36:40 +02:00
|
|
|
|
2021-12-16 13:36:21 +01:00
|
|
|
toggleFieldValue(field: FieldView) {
|
|
|
|
const f = field as any;
|
|
|
|
f.showValue = !f.showValue;
|
|
|
|
if (this.editMode && f.showValue) {
|
|
|
|
this.eventService.collect(EventType.Cipher_ClientToggledHiddenFieldVisible, this.cipher.id);
|
2021-09-20 02:36:40 +02:00
|
|
|
}
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
2021-09-20 02:36:40 +02:00
|
|
|
|
2021-12-16 13:36:21 +01:00
|
|
|
trackByFunction(index: number, item: any) {
|
|
|
|
return index;
|
|
|
|
}
|
2021-11-02 23:03:37 +01:00
|
|
|
|
2021-12-16 13:36:21 +01:00
|
|
|
drop(event: CdkDragDrop<string[]>) {
|
|
|
|
moveItemInArray(this.cipher.fields, event.previousIndex, event.currentIndex);
|
|
|
|
}
|
2021-11-02 23:03:37 +01:00
|
|
|
|
2021-12-16 13:36:21 +01:00
|
|
|
private setLinkedFieldOptions() {
|
|
|
|
if (this.cipher.linkedFieldOptions == null) {
|
|
|
|
return;
|
2021-11-09 12:57:33 +01:00
|
|
|
}
|
|
|
|
|
2021-12-16 13:36:21 +01:00
|
|
|
const options: any = [];
|
|
|
|
this.cipher.linkedFieldOptions.forEach((linkedFieldOption, id) =>
|
|
|
|
options.push({ name: this.i18nService.t(linkedFieldOption.i18nKey), value: id })
|
|
|
|
);
|
|
|
|
this.linkedFieldOptions = options.sort(Utils.getSortFunction(this.i18nService, "name"));
|
|
|
|
}
|
2021-11-03 22:24:45 +01:00
|
|
|
|
2021-12-16 13:36:21 +01:00
|
|
|
private resetCipherLinkedFields() {
|
|
|
|
if (this.cipher.fields == null || this.cipher.fields.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
2021-11-09 12:57:33 +01:00
|
|
|
|
2021-12-16 13:36:21 +01:00
|
|
|
// Delete any Linked custom fields if the item type does not support them
|
|
|
|
if (this.cipher.linkedFieldOptions == null) {
|
|
|
|
this.cipher.fields = this.cipher.fields.filter((f) => f.type !== FieldType.Linked);
|
|
|
|
return;
|
2021-11-02 23:03:37 +01:00
|
|
|
}
|
2021-12-16 13:36:21 +01:00
|
|
|
|
|
|
|
this.cipher.fields
|
|
|
|
.filter((f) => f.type === FieldType.Linked)
|
|
|
|
.forEach((f) => (f.linkedId = this.linkedFieldOptions[0].value));
|
|
|
|
}
|
2021-09-20 02:36:40 +02:00
|
|
|
}
|