import VaultItem from "./VaultItem" ; import { generateUUID } from "../tools/uuid" ; import { getEntryURLs , EntryURLType } from "../tools/entry" ; import Group from "./Group" ; import Vault from "./Vault" ; import { EntryChange , EntryPropertyValueType , EntryType , GroupID , PropertyKeyValueObject } from "../types" ; augments VaultItem memberof export default class Entry extends VaultItem { static Attributes = Object . freeze ( { AttachmentPrefix : "BC_ENTRY_ATTACHMENT:" , FacadeType : "BC_ENTRY_FACADE_TYPE" , FieldTypePrefix : "BC_ENTRY_FIELD_TYPE:" } ) ; param vault param parentGroupID memberof static static createNew ( vault , parentGroupID ) { const group = vault . findGroupByID ( parentGroupID ) ; if ( ! group ) { throw new Error ( `Failed creating entry: no group found for ID: ${ parentGroupID }` ) ; } else if ( group . isTrash ( ) || group . isInTrash ( ) ) { throw new Error ( "Failed creating entry: cannot create within Trash group" ) ; } const id = generateUUID ( ) ; vault . format . createEntry ( parentGroupID , id ) ; vault . _rebuild ( ) ; return vault . findEntryByID ( id ) ; } param skipTrash see moveToGroup see Vault . getTrashGroup returns memberof delete ( skipTrash = false ) { const trashGroup = this . vault . getTrashGroup ( ) ; const parentGroup = this . getGroup ( ) ; const canTrash = trashGroup && parentGroup && ! parentGroup . isTrash ( ) && ! parentGroup . isInTrash ( ) ; if ( canTrash && ! skipTrash ) { this . moveToGroup ( trashGroup ) ; return false ; } this . vault . format . deleteEntry ( this . id ) ; const ind = this . vault . _entries . indexOf ( this ) ; if ( ind >= 0 ) { this . vault . _entries . splice ( ind , 1 ) ; } this . _cleanUp ( ) ; return true ; } param attribute throws memberof returns deleteAttribute ( attribute ) { this . vault . format . deleteEntryAttribute ( this . id , attribute ) ; return this ; } throws param property memberof returns deleteProperty ( property ) { this . vault . format . deleteEntryProperty ( this . id , property ) ; return this ; } param attribute returns memberof getAttribute ( attribute ? ) { const attributes = this . vault . format . getEntryAttributes ( this . _source ) || { } ; if ( typeof attribute === "undefined" ) { return Object . assign ( { } , attributes ) ; } return attributes . hasOwnProperty ( attribute ) ? attributes [ attribute ] : undefined ; } memberof getChanges ( ) { return this . vault . format . getEntryChanges ( this . _source ) ; } returns memberof throws getGroup ( ) { const parentGroup = this . vault . format . findGroupContainingEntryID ( this . id ) ; if ( ! parentGroup ) { throw new Error ( `No parent group found for entry: ${ this . id }` ) ; } const parentID = this . vault . format . getItemID ( parentGroup ) ; return this . vault . _groups . find ( group => group . id === parentID ) ; } param property returns memberof getProperty ( property ? ) { const raw = this . vault . format . getEntryProperties ( this . _source ) ; if ( typeof property === "undefined" ) { return Object . assign ( { } , raw ) ; } return raw . hasOwnProperty ( property ) ? raw [ property ] : undefined ; } param property returns memberof getPropertyValueType ( property ) { const attr = this . getAttribute ( `${ Entry . Attributes . FieldTypePrefix }${ property }` ) as EntryPropertyValueType ; return attr || EntryPropertyValueType . Text ; } param propertyExpression returns memberof getProperties ( propertyExpression ? ) { const raw = this . vault . format . getEntryProperties ( this . _source ) ; if ( typeof propertyExpression === "undefined" ) { return Object . assign ( { } , raw ) ; } const isRexp = propertyExpression instanceof RegExp ; return Object . keys ( raw ) . reduce ( ( aggr , key ) => { const matches = isRexp ? ( < RegExp > propertyExpression ) . test ( key ) : propertyExpression === key ; return matches ? Object . assign ( aggr , { [ key ] : raw [ key ] } ) : aggr ; } , { } ) ; } returns memberof getType ( ) { return < EntryType | undefined > this . getAttribute ( Entry . Attributes . FacadeType ) || EntryType . Login ; } param urlTypePreference returns memberof getURLs ( urlTypePreference ) { return getEntryURLs ( this . getProperty ( ) as { [ key ] : string ; } , urlTypePreference ) ; } returns memberof isInTrash ( ) { const group = this . getGroup ( ) ; return group . isTrash ( ) || group . isInTrash ( ) ; } returns param group memberof moveToGroup ( group ) { this . vault . format . moveEntry ( this . id , group . id ) ; return this ; } param attribute param value returns memberof setAttribute ( attribute , value ) { this . vault . format . setEntryAttribute ( this . id , attribute , value ) ; return this ; } param property param value returns memberof setProperty ( property , value ) { this . vault . format . setEntryProperty ( this . id , property , value ) ; return this ; } param property param valueType returns memberof setPropertyValueType ( property , valueType ) { if ( valueType === null ) { this . deleteAttribute ( `${ Entry . Attributes . FieldTypePrefix }${ property }` ) ; } else { this . setAttribute ( `${ Entry . Attributes . FieldTypePrefix }${ property }` , valueType ) ; } return this ; } _updateRefs ( ) { this . _source = this . vault . format . findEntryByID ( this . id ) ; } }