import { Record , RecordChangedEvent , RecordStateData , RecordErrorMessages } from './record' ; import { Subscription , Subject , Observable } from 'rxjs' ; import { ListAddedEvent , ListRemovedEvent } from './data-item-list' ; import { List } from './list' ; export interface RecordListMessage { [ uid ] : RecordErrorMessages ; } export class RecordList extends List { items : Record [ ] ; messagesChanged = new Subject < Record > ( ) ; recordLoaded = new Subject < Record > ( ) ; protected messagesSubscriptions : Map < Record , Subscription > = new Map ( ) ; protected loadedSubscriptions : Map < Record , Subscription > = new Map ( ) ; constructor ( items ? , callInitItems = true ) { super ( items , false ) ; if ( items && callInitItems ) this . initItems ( items ) ; } protected _addItem ( item ) { super . _addItem ( item ) ; if ( item instanceof Record ) { var s = item . __director__ . messagesChanged . subscribe ( record => { this . messagesChanged . next ( record ) ; } ) ; this . messagesSubscriptions . set ( item , s ) ; var s3 = item . __director__ . loaded . subscribe ( record => { this . recordLoaded . next ( record ) ; } ) ; this . loadedSubscriptions . set ( item , s3 ) ; } } remove ( ... records ) { super . remove ( ... records ) ; } _remove ( records , emitChanged = true ) { records . forEach ( record => { if ( record instanceof Record ) { if ( this . messagesSubscriptions . has ( record ) ) { this . messagesSubscriptions . get ( record ) . unsubscribe ( ) ; this . messagesSubscriptions . delete ( record ) ; } if ( this . loadedSubscriptions . has ( record ) ) { this . loadedSubscriptions . get ( record ) . unsubscribe ( ) ; this . loadedSubscriptions . delete ( record ) ; } } } ) ; super . _remove ( records , emitChanged ) ; } clear ( ) { super . clear ( ) ; } setMessages ( messages ) { for ( let handle in messages ) { var actionsName ; var uid ; [ actionsName , uid ] = handle . split ( '__' ) ; this . findRecordByUID ( uid ) . __director__ . setMessages ( messages [ handle ] ) ; } } findRecordByUID ( uid ) { if ( ! this . items ) return null ; for ( let key in this . items ) { var record = this . items [ key ] ; if ( record . __director__ . uid == uid ) { return record ; } } } }