import VaultItem from "./VaultItem" ; import Entry from "./Entry" ; import Vault from "./Vault" ; import { generateUUID } from "../tools/uuid" ; import { moveGroupBetweenVaults } from "../tools/sharing" ; import { findGroupsByTitle , getAllChildGroups } from "../search/groups" ; import { findEntriesByProperty , getAllChildEntries } from "../search/entries" ; import { EntryID , GroupID } from "../types" ; augments VaultItem memberof export default class Group extends VaultItem { static Attribute = Object . freeze ( { Role : "bc_group_role" } ) ; param vault param parentID returns memberof static static createNew ( vault , parentID = "0" , id = generateUUID ( ) ) { if ( parentID !== "0" ) { const group = vault . findGroupByID ( parentID ) ; if ( ! group ) { throw new Error ( `Failed creating group: no group found for ID: ${ parentID }` ) ; } else if ( group . isTrash ( ) || group . isInTrash ( ) ) { throw new Error ( "Failed creating group: cannot create within Trash group" ) ; } } vault . format . createGroup ( parentID , id ) ; vault . _rebuild ( ) ; return vault . findGroupByID ( id ) ; } param title returns memberof createEntry ( title ? ) { const entry = Entry . createNew ( this . vault , this . id ) ; if ( title ) { entry . setProperty ( "title" , title ) ; } return entry ; } param title returns memberof createGroup ( title ? ) { const group = Group . createNew ( this . vault , this . id ) ; if ( title ) { group . setTitle ( title ) ; } return group ; } param skipTrash returns memberof delete ( skipTrash = false ) { if ( this . isTrash ( ) ) { throw new Error ( "Trash group cannot be deleted" ) ; } const trashGroup = this . vault . getTrashGroup ( ) ; const hasTrash = trashGroup !== null ; const inTrash = this . isInTrash ( ) ; if ( ! inTrash && hasTrash && ! skipTrash ) { this . moveTo ( trashGroup ) ; return false ; } this . getGroups ( ) . forEach ( group => group . delete ( true ) ) ; this . getEntries ( ) . forEach ( entry => entry . delete ( true ) ) ; this . vault . format . deleteGroup ( this . id ) ; const ind = this . vault . _groups . indexOf ( this ) ; if ( ind >= 0 ) { this . vault . _groups . splice ( ind , 1 ) ; } this . _cleanUp ( ) ; return true ; } param attr returns memberof deleteAttribute ( attr ) { this . vault . format . deleteGroupAttribute ( this . id , attr ) ; return this ; } param id returns memberof findEntryByID ( id ) { return getAllChildEntries ( this . vault . _entries , this . id ) . find ( entry => entry . id === id ) || null ; } name param property param value returns memberof findEntriesByProperty ( property , value ) { return findEntriesByProperty ( getAllChildEntries ( this . vault . _entries , this . id ) , property , value ) ; } param id returns memberof findGroupByID ( id ) { return getAllChildGroups ( this . vault . _groups , this . id ) . find ( group => group . id === id ) || null ; } name param title returns memberof findGroupsByTitle ( title ) { return findGroupsByTitle ( getAllChildGroups ( this . vault . _groups , this . id ) , title ) ; } param attribute returns memberof getAttribute ( attribute ? ) { const attributes = this . vault . format . getGroupAttributes ( this . _source ) || { } ; if ( typeof attribute === "undefined" ) { return Object . assign ( { } , attributes ) ; } return attributes . hasOwnProperty ( attribute ) ? attributes [ attribute ] : undefined ; } returns memberof getEntries ( ) { return this . vault . _entries . filter ( entry => entry . getGroup ( ) === this ) ; } returns memberof getGroups ( ) { return this . vault . _groups . filter ( group => group . getParentGroup ( ) === this ) ; } returns throws memberof getParentGroup ( ) { const parentID = this . vault . format . getItemParentID ( this . _source ) ; if ( parentID === "0" ) return null ; const parentGroup = this . vault . _groups . find ( g => g . id === parentID ) ; if ( ! parentGroup ) { throw new Error ( `Failed getting parent Group: No group containing child ID found: ${ this . id }` ) ; } return parentGroup ; } returns memberof getTitle ( ) { return this . vault . format . getGroupTitle ( this . _source ) ; } returns memberof isInTrash ( ) { const trash = this . vault . getTrashGroup ( ) ; if ( trash ) { const thisGroup = trash . findGroupByID ( this . id ) ; return thisGroup !== null ; } return false ; } returns memberof isTrash ( ) { return this . getAttribute ( Group . Attribute . Role ) === "trash" ; } param target returns memberof moveTo ( target ) { if ( this . isTrash ( ) ) { throw new Error ( "Trash group cannot be moved" ) ; } let targetVault , targetGroupID ; if ( target instanceof Group ) { targetVault = target . vault ; targetGroupID = target . id ; } else if ( target instanceof Vault ) { targetVault = target ; targetGroupID = "0" ; } else { throw new Error ( "Failed moving group: Unknown target type" ) ; } if ( this . vault . readOnly ) { throw new Error ( "Cannot move group: origin archive is read-only" ) ; } if ( targetVault . readOnly ) { throw new Error ( "Cannot move group: target archive is read-only" ) ; } if ( this . vault . id === targetVault . id ) { this . vault . format . moveGroup ( this . id , targetGroupID ) ; } else { moveGroupBetweenVaults ( this , target ) ; } return this ; } param attribute param value returns memberof setAttribute ( attribute , value ) { this . vault . format . setGroupAttribute ( this . id , attribute , value ) ; return this ; } param title returns setTitle ( title ) { this . vault . format . setGroupTitle ( this . id , title ) ; return this ; } _updateRefs ( ) { this . _source = this . vault . format . findGroupByID ( this . id ) ; } }