1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-18 01:03:35 +00:00
Files
browser/libs/common/src/vault/linked-field-option.decorator.ts
Nick Krantz decc7a3031 [PM-9926] Linked Fields order (#10174)
* refactor linkedFieldOption to take in an attributes object

* add sort positions for existing linked fields

* sort linked fields on new custom fields
2024-07-23 12:26:02 -05:00

49 lines
1.5 KiB
TypeScript

import { LinkedIdType } from "./enums";
import { ItemView } from "./models/view/item.view";
type LinkedMetadataAttributes = {
/**
* The i18n key used to describe the decorated class property in the UI.
* If it is null, then the name of the class property will be used as the i18n key.
*/
i18nKey?: string;
/**
* The position of the individual field to be applied when sorted.
*/
sortPosition: number;
};
export class LinkedMetadata {
private readonly _i18nKey: string;
readonly sortPosition: number;
constructor(
readonly propertyKey: string,
attributes: LinkedMetadataAttributes,
) {
this._i18nKey = attributes?.i18nKey;
this.sortPosition = attributes.sortPosition;
}
get i18nKey() {
return this._i18nKey ?? this.propertyKey;
}
}
/**
* A decorator used to set metadata used by Linked custom fields. Apply it to a class property or getter to make it
* available as a Linked custom field option.
* @param id - A unique value that is saved in the Field model. It is used to look up the decorated class property.
* @param options - {@link LinkedMetadataAttributes}
*/
export function linkedFieldOption(id: LinkedIdType, attributes: LinkedMetadataAttributes) {
return (prototype: ItemView, propertyKey: string) => {
if (prototype.linkedFieldOptions == null) {
prototype.linkedFieldOptions = new Map<LinkedIdType, LinkedMetadata>();
}
prototype.linkedFieldOptions.set(id, new LinkedMetadata(propertyKey, attributes));
};
}