import EventEmitter from "eventemitter3" ; import { getDefaultFormat } from "../io/formatRouter" ; import { findGroupsByTitle } from "../search/groups" ; import { findEntriesByProperty } from "../search/entries" ; import Group from "./Group" ; import Entry from "./Entry" ; import { EntryID , GroupID , History } from "../types" ; augments EventEmitter memberof export default class Vault extends EventEmitter { static Attribute = Object . freeze ( { AttachmentsKey : "BC_ATTACHMENTS_KEY" } ) ; param history param format returns static memberof static createFromHistory ( history , format = getDefaultFormat ( ) ) { const vault = new Vault ( format ) ; vault . format . erase ( ) ; vault . format . execute ( history ) ; vault . format . dirty = false ; if ( ! vault . id ) { vault . format . generateID ( ) ; } return vault ; } returns memberof static static createWithDefaults ( ) { const vault = new Vault ( ) ; vault . createGroup ( "General" ) ; vault . createGroup ( "Trash" ) . setAttribute ( Group . Attribute . Role , "trash" ) ; return vault ; } _entries : Array < Entry > = [ ] ; _format : any ; _groups : Array < Group > = [ ] ; _onCommandExec : ( ) => void ; readonly memberof get format ( ) : any { return this . _format ; } readonly memberof get id ( ) : string { return this . format . getVaultID ( ) ; } readonly memberof get readOnly ( ) : boolean { return this . format . readOnly ; } param Format constructor ( Format = getDefaultFormat ( ) ) { super ( ) ; this . _updateFormat ( new Format ( ) ) ; this . format . initialise ( ) ; this . _rebuild ( ) ; } param title returns memberof createGroup ( title ) { const group = Group . createNew ( this ) ; if ( title ) { group . setTitle ( title ) ; } return group ; } param attribute returns memberof deleteAttribute ( attribute ) { this . format . deleteVaultAttribute ( attribute ) ; return this ; } returns memberof emptyTrash ( ) { const trash = this . getTrashGroup ( ) ; if ( ! trash ) return ; trash . getGroups ( ) . forEach ( group => { group . delete ( true ) ; } ) ; trash . getEntries ( ) . forEach ( entry => { entry . delete ( true ) ; } ) ; return this ; } param id returns memberof findEntryByID ( id ) { return this . _entries . find ( entry => entry . id === id ) || null ; } param property param value returns memberof findEntriesByProperty ( property , value ) { return findEntriesByProperty ( this . _entries , property , value ) ; } param id returns memberof findGroupByID ( id ) { return this . _groups . find ( group => group . id === id ) || null ; } param title returns memberof findGroupsByTitle ( title ) { return findGroupsByTitle ( this . _groups , title ) ; } returns memberof getAllEntries ( ) { return [ ... this . _entries ] ; } returns memberof getAllGroups ( ) { return [ ... this . _groups ] ; } param attributeName returns memberof getAttribute ( attributeName ? ) { const attributes = this . format . getVaultAttributes ( ) ; if ( ! attributeName ) { return Object . assign ( { } , attributes ) ; } if ( attributes . hasOwnProperty ( attributeName ) ) { return attributes [ attributeName ] ; } return undefined ; } memberof getGroups ( ) { return this . _groups . filter ( group => group . getParentGroup ( ) === null ) ; } returns memberof getTrashGroup ( ) { const trashGroup = this . getGroups ( ) . find ( group => group . isTrash ( ) ) ; return trashGroup || null ; } param attribute param value returns memberof setAttribute ( attribute , value ) { this . format . setVaultAttribute ( attribute , value ) ; return this ; } _rebuild ( clear = false ) { if ( clear ) { this . _groups = [ ] ; this . _entries = [ ] ; } this . format . getAllGroups ( ) . forEach ( rawGroup => { const id = this . format . getItemID ( rawGroup ) ; if ( ! this . _groups . find ( g => g . id === id ) ) { this . _groups . push ( new Group ( this , rawGroup ) ) ; } } ) ; this . format . getAllEntries ( ) . forEach ( rawEntry => { const id = this . format . getItemID ( rawEntry ) ; if ( ! this . _entries . find ( e => e . id === id ) ) { this . _entries . push ( new Entry ( this , rawEntry ) ) ; } } ) ; } param format memberof protected _updateFormat ( format ) { if ( this . _format ) { this . _format . removeAllListeners ( ) ; } this . _format = format ; this . _groups . forEach ( group => { group . _updateRefs ( ) ; } ) ; this . _entries . forEach ( entry => { entry . _updateRefs ( ) ; } ) ; this . _rebuild ( ) ; this . format . on ( "commandsExecuted" , ( ) => { this . _rebuild ( ) ; this . emit ( "vaultUpdated" ) ; } ) ; this . format . on ( "erased" , ( ) => { this . _rebuild ( true ) ; this . emit ( "vaultUpdated" ) ; } ) ; } }