import Entry from "../core/Entry" ; import { generateUUID } from "../tools/uuid" ; import { EntryFacadeField , EntryFacadeFieldFormatting , EntryPropertyType , EntryPropertyValueType } from "../types" ; export interface CreateFieldDescriptorOptions { formatting ? : boolean | EntryFacadeFieldFormatting ; removeable ? : boolean ; valueType ? : EntryPropertyValueType ; } const VALID_VALUE_TYPES = [ EntryPropertyValueType . Note , EntryPropertyValueType . OTP , EntryPropertyValueType . Password , EntryPropertyValueType . Text ] ; param entry param title param entryPropertyType param entryPropertyName param options returns memberof export function createFieldDescriptor ( entry , title , entryPropertyType , entryPropertyName , options = { } ) { const { formatting = false , removeable = false , valueType = null } = options ; const value = entry ? getEntryValue ( entry , entryPropertyType , entryPropertyName ) : "" ; return { id : generateUUID ( ) , title , propertyType : entryPropertyType , property : entryPropertyName , value , valueType : valueType ? valueType : entryPropertyType === "attribute" ? null : getEntryPropertyValueType ( entry , entryPropertyName ) , formatting , removeable } ; } param entry param propertyType param name returns throws deprecated export function getEntryValue ( entry , propertyType , name ) { switch ( propertyType ) { case EntryPropertyType . Property : return entry . getProperty ( name ) as string | undefined ; case EntryPropertyType . Attribute : return entry . getAttribute ( name ) as string | undefined ; default : throw new Error ( `Cannot retrieve value: Unknown property type: ${ propertyType }` ) ; } } param entry param propertyName returns export function getEntryPropertyValueType ( entry , propertyName ) { if ( ! entry ) { return EntryPropertyValueType . Text ; } const type = entry . getAttribute ( `${ Entry . Attributes . FieldTypePrefix }${ propertyName }` ) as EntryPropertyValueType ; return VALID_VALUE_TYPES . indexOf ( type ) >= 0 ? type : EntryPropertyValueType . Text ; } param id export function idSignifiesNew ( id , mergeMode = false ) { if ( /^\d+$/ . test ( id ) === false ) return mergeMode ; const numerical = parseInt ( id , 10 ) ; return ! isNaN ( numerical ) && numerical > 0 && numerical <= 999999 ; } param entry param propertyName param valueType export function setEntryPropertyValueType ( entry , propertyName , valueType ) { entry . setAttribute ( `${ Entry . Attributes . FieldTypePrefix }${ propertyName }` , valueType ) ; }