AnnotationCollection
A collection of annotations.
It implements methods for managing annotations and creates a focus tracker for them to make it easier to manage the focus for all annotations.
AnnotationCollection fires event-focus when an annotation becomes focused and event-blur when all annotations lose focus.
Properties
first : null | Treadonlyinheritedmodule:comments/annotations/annotationcollection~AnnotationCollection#firstReturns the first item from the collection or null when collection is empty.
isFocused : booleanreadonlymodule:comments/annotations/annotationcollection~AnnotationCollection#isFocusedEquals to
truewhen one of the annotation in the collection is focused.last : null | Treadonlyinheritedmodule:comments/annotations/annotationcollection~AnnotationCollection#lastReturns the last item from the collection or null when collection is empty.
length : numberreadonlyinheritedmodule:comments/annotations/annotationcollection~AnnotationCollection#lengthThe number of items available in the collection.
Methods
constructor( annotations )module:comments/annotations/annotationcollection~AnnotationCollection#constructorParameters
annotations : Iterable<Annotation>Initial annotations.
Defaults to
[]
Symbol.iterator() → Iterator<Annotation>inheritedmodule:comments/annotations/annotationcollection~AnnotationCollection#Symbol.iteratoradd( annotation ) → thismodule:comments/annotations/annotationcollection~AnnotationCollection#addaddMany( items, [ index ] ) → thisinheritedmodule:comments/annotations/annotationcollection~AnnotationCollection#addManyAdds multiple items into the collection.
Any item not containing an id will get an automatically generated one.
Parameters
items : Iterable<Annotation>[ index ] : numberThe position of the insertion. Items will be appended if no
indexis specified.
Returns
this
Fires
bindTo( externalCollection ) → CollectionBindToChain<S, Annotation>inheritedmodule:comments/annotations/annotationcollection~AnnotationCollection#bindToBinds and synchronizes the collection with another one.
The binding can be a simple factory:
class FactoryClass { public label: string; constructor( data: { label: string } ) { this.label = data.label; } } const source = new Collection<{ label: string }>( { idProperty: 'label' } ); const target = new Collection<FactoryClass>(); target.bindTo( source ).as( FactoryClass ); source.add( { label: 'foo' } ); source.add( { label: 'bar' } ); console.log( target.length ); // 2 console.log( target.get( 1 ).label ); // 'bar' source.remove( 0 ); console.log( target.length ); // 1 console.log( target.get( 0 ).label ); // 'bar'Copy codeor the factory driven by a custom callback:
class FooClass { public label: string; constructor( data: { label: string } ) { this.label = data.label; } } class BarClass { public label: string; constructor( data: { label: string } ) { this.label = data.label; } } const source = new Collection<{ label: string }>( { idProperty: 'label' } ); const target = new Collection<FooClass | BarClass>(); target.bindTo( source ).using( ( item ) => { if ( item.label == 'foo' ) { return new FooClass( item ); } else { return new BarClass( item ); } } ); source.add( { label: 'foo' } ); source.add( { label: 'bar' } ); console.log( target.length ); // 2 console.log( target.get( 0 ) instanceof FooClass ); // true console.log( target.get( 1 ) instanceof BarClass ); // trueCopy codeor the factory out of property name:
const source = new Collection<{ nested: { value: string } }>(); const target = new Collection<{ value: string }>(); target.bindTo( source ).using( 'nested' ); source.add( { nested: { value: 'foo' } } ); source.add( { nested: { value: 'bar' } } ); console.log( target.length ); // 2 console.log( target.get( 0 ).value ); // 'foo' console.log( target.get( 1 ).value ); // 'bar'Copy codeIt's possible to skip specified items by returning null value:
const source = new Collection<{ hidden: boolean }>(); const target = new Collection<{ hidden: boolean }>(); target.bindTo( source ).using( item => { if ( item.hidden ) { return null; } return item; } ); source.add( { hidden: true } ); source.add( { hidden: false } ); console.log( source.length ); // 2 console.log( target.length ); // 1Copy codeNote:
clearcan be used to break the binding.Type parameters
S : extends Record<string, any>The type of
externalCollectionelement.
Parameters
externalCollection : Collection<S>A collection to be bound.
Returns
CollectionBindToChain<S, Annotation>The binding chain object.
clear() → voidinheritedmodule:comments/annotations/annotationcollection~AnnotationCollection#cleardelegate( events ) → EmitterMixinDelegateChaininheritedmodule:comments/annotations/annotationcollection~AnnotationCollection#delegateDelegates selected events to another
Emitter. For instance:emitterA.delegate( 'eventX' ).to( emitterB ); emitterA.delegate( 'eventX', 'eventY' ).to( emitterC );Copy codethen
eventXis delegated (fired by)emitterBandemitterCalong withdata:emitterA.fire( 'eventX', data );Copy codeand
eventYis delegated (fired by)emitterCalong withdata:emitterA.fire( 'eventY', data );Copy codeParameters
events : Array<string>Event names that will be delegated to another emitter.
Returns
destroy() → voidmodule:comments/annotations/annotationcollection~AnnotationCollection#destroyDestroys all bindings and clears the collection.
Returns
void
filter( callback, [ ctx ] ) → Array<Annotation>inheritedmodule:comments/annotations/annotationcollection~AnnotationCollection#filterReturns an array with items for which the
callbackreturned a true value.Parameters
callback : ( item: Annotation, index: number ) => boolean[ ctx ] : anyContext in which the
callbackwill be called.
Returns
Array<Annotation>The array with matching items.
find( callback, [ ctx ] ) → undefined | Annotationinheritedmodule:comments/annotations/annotationcollection~AnnotationCollection#findFinds the first item in the collection for which the
callbackreturns a true value.Parameters
callback : ( item: Annotation, index: number ) => boolean[ ctx ] : anyContext in which the
callbackwill be called.
Returns
undefined | AnnotationThe item for which
callbackreturned a true value.
fire( eventOrInfo, args ) → GetEventInfo<TEvent>[ 'return' ]inheritedmodule:comments/annotations/annotationcollection~AnnotationCollection#fireFires an event, executing all callbacks registered for it.
The first parameter passed to callbacks is an
EventInfoobject, followed by the optionalargsprovided in thefire()method call.Type parameters
Parameters
eventOrInfo : GetNameOrEventInfo<TEvent>The name of the event or
EventInfoobject if event is delegated.args : TEvent[ 'args' ]Additional arguments to be passed to the callbacks.
Returns
GetEventInfo<TEvent>[ 'return' ]By default the method returns
undefined. However, the return value can be changed by listeners through modification of theevt.return's property (the event info is the first param of every callback).
forEach( callback, [ ctx ] ) → voidinheritedmodule:comments/annotations/annotationcollection~AnnotationCollection#forEachPerforms the specified action for each item in the collection.
Parameters
callback : ( item: Annotation, index: number ) => unknown[ ctx ] : anyContext in which the
callbackwill be called.
Returns
void
get( idOrIndex ) → null | Annotationinheritedmodule:comments/annotations/annotationcollection~AnnotationCollection#getGets an item by its ID or index.
Parameters
idOrIndex : string | numberThe item ID or index in the collection.
Returns
null | AnnotationThe requested item or
nullif such item does not exist.
getByInnerView( innerView ) → undefined | Annotationmodule:comments/annotations/annotationcollection~AnnotationCollection#getByInnerViewGets the annotation for a given annotation view's inner view.
Parameters
innerView : View
Returns
undefined | Annotation
getByView( view ) → undefined | Annotationmodule:comments/annotations/annotationcollection~AnnotationCollection#getByViewgetIndex( itemOrId ) → numberinheritedmodule:comments/annotations/annotationcollection~AnnotationCollection#getIndexGets an index of an item in the collection. When an item is not defined in the collection, the index will equal -1.
Parameters
itemOrId : string | AnnotationThe item or its ID in the collection.
Returns
numberThe index of a given item.
has( itemOrId ) → booleaninheritedmodule:comments/annotations/annotationcollection~AnnotationCollection#hasReturns a Boolean indicating whether the collection contains an item.
Parameters
itemOrId : string | AnnotationThe item or its ID in the collection.
Returns
booleantrueif the collection contains the item,falseotherwise.
listenTo( emitter, event, callback, [ options ] ) → voidinheritedmodule:comments/annotations/annotationcollection~AnnotationCollection#listenTo:BASE_EMITTERRegisters a callback function to be executed when an event is fired in a specific (emitter) object.
Events can be grouped in namespaces using
:. When namespaced event is fired, it additionally fires all callbacks for that namespace.// myEmitter.on( ... ) is a shorthand for myEmitter.listenTo( myEmitter, ... ). myEmitter.on( 'myGroup', genericCallback ); myEmitter.on( 'myGroup:myEvent', specificCallback ); // genericCallback is fired. myEmitter.fire( 'myGroup' ); // both genericCallback and specificCallback are fired. myEmitter.fire( 'myGroup:myEvent' ); // genericCallback is fired even though there are no callbacks for "foo". myEmitter.fire( 'myGroup:foo' );Copy codeAn event callback can stop the event and set the return value of the
firemethod.Type parameters
Parameters
emitter : EmitterThe object that fires the event.
event : TEvent[ 'name' ]The name of the event.
callback : GetCallback<TEvent>The function to be called on event.
[ options ] : GetCallbackOptions<TEvent>Additional options.
Returns
void
map( callback, [ ctx ] ) → Array<U>inheritedmodule:comments/annotations/annotationcollection~AnnotationCollection#mapExecutes the callback for each item in the collection and composes an array or values returned by this callback.
Type parameters
UThe result type of the callback.
Parameters
callback : ( item: Annotation, index: number ) => U[ ctx ] : anyContext in which the
callbackwill be called.
Returns
Array<U>The result of mapping.
off( event, callback ) → voidinheritedmodule:comments/annotations/annotationcollection~AnnotationCollection#offStops executing the callback on the given event. Shorthand for
this.stopListening( this, event, callback ).Parameters
event : stringThe name of the event.
callback : FunctionThe function to stop being called.
Returns
void
on( event, callback, [ options ] ) → voidinheritedmodule:comments/annotations/annotationcollection~AnnotationCollection#onRegisters a callback function to be executed when an event is fired.
Shorthand for
this.listenTo( this, event, callback, options )(it makes the emitter listen on itself).Type parameters
Parameters
event : TEvent[ 'name' ]The name of the event.
callback : GetCallback<TEvent>The function to be called on event.
[ options ] : GetCallbackOptions<TEvent>Additional options.
Returns
void
once( event, callback, [ options ] ) → voidinheritedmodule:comments/annotations/annotationcollection~AnnotationCollection#onceRegisters a callback function to be executed on the next time the event is fired only. This is similar to calling
onfollowed byoffin the callback.Type parameters
Parameters
event : TEvent[ 'name' ]The name of the event.
callback : GetCallback<TEvent>The function to be called on event.
[ options ] : GetCallbackOptions<TEvent>Additional options.
Returns
void
refreshPositioning() → voidmodule:comments/annotations/annotationcollection~AnnotationCollection#refreshPositioningRefreshes the positioning of all annotations and sorts them topmost and leftmost.
Returns
void
remove( annotation ) → Annotationmodule:comments/annotations/annotationcollection~AnnotationCollection#removestopDelegating( [ event ], [ emitter ] ) → voidinheritedmodule:comments/annotations/annotationcollection~AnnotationCollection#stopDelegatingStops delegating events. It can be used at different levels:
- To stop delegating all events.
- To stop delegating a specific event to all emitters.
- To stop delegating a specific event to a specific emitter.
Parameters
[ event ] : stringThe name of the event to stop delegating. If omitted, stops it all delegations.
[ emitter ] : Emitter(requires
event) The object to stop delegating a particular event to. If omitted, stops delegation ofeventto all emitters.
Returns
void
stopListening( [ emitter ], [ event ], [ callback ] ) → voidinheritedmodule:comments/annotations/annotationcollection~AnnotationCollection#stopListening:BASE_STOPStops listening for events. It can be used at different levels:
- To stop listening to a specific callback.
- To stop listening to a specific event.
- To stop listening to all events fired by a specific object.
- To stop listening to all events fired by all objects.
Parameters
[ emitter ] : EmitterThe object to stop listening to. If omitted, stops it for all objects.
[ event ] : string(Requires the
emitter) The name of the event to stop listening to. If omitted, stops it for all events fromemitter.[ callback ] : Function(Requires the
event) The function to be removed from the call list for the givenevent.
Returns
void
Events
add( eventInfo, item, index )inheritedmodule:comments/annotations/annotationcollection~AnnotationCollection#event:addFired when an item is added to the collection.
Parameters
eventInfo : EventInfoAn object containing information about the fired event.
item : TThe added item.
index : numberAn index where the addition occurred.
blur( eventInfo )module:comments/annotations/annotationcollection~AnnotationCollection#event:blurFired when all annotations become blurred.
Parameters
eventInfo : EventInfoAn object containing information about the fired event.
change( eventInfo, data )inheritedmodule:comments/annotations/annotationcollection~AnnotationCollection#event:changeFired when the collection was changed due to adding or removing items.
Parameters
eventInfo : EventInfoAn object containing information about the fired event.
data : CollectionChangeEventData<T>Changed items.
focus( eventInfo, annotation )module:comments/annotations/annotationcollection~AnnotationCollection#event:focusFired when an annotation becomes active.
Parameters
eventInfo : EventInfoAn object containing information about the fired event.
annotation : AnnotationAn annotation that was focused.
remove( eventInfo, item, index )inheritedmodule:comments/annotations/annotationcollection~AnnotationCollection#event:removeFired when an item is removed from the collection.
Parameters
eventInfo : EventInfoAn object containing information about the fired event.
item : TThe removed item.
index : numberIndex from which item was removed.