BodyCollection (ui/editorui)
@ckeditor/ckeditor5-ui/src/editorui/bodycollection
This is a special ViewCollection
dedicated to elements that are detached
from the DOM structure of the editor, like panels, icons, etc.
The body collection is available in the editor.ui.view.body
property.
Any plugin can add a view to this collection.
These views will render in a container placed directly in the <body>
element.
The editor will detach and destroy this collection when the editor will be destroyed.
If you need to control the life cycle of the body collection on your own, you can create your own instance of this class.
A body collection will render itself automatically in the DOM body element as soon as you call attachToDom
.
If you create multiple body collections, this class will create a special wrapper element in the DOM to limit the number of
elements created directly in the body and remove it when the last body collection will be
detached.
Filtering
Properties
-
readonly
bodyCollectionContainer : undefined | HTMLElement
module:ui/editorui/bodycollection~BodyCollection#bodyCollectionContainer
The element holding elements of the body region.
-
Returns the first item from the collection or null when collection is empty.
-
Returns the last item from the collection or null when collection is empty.
-
The number of items available in the collection.
-
The editor's locale instance. See the view locale property.
-
private
_bodyCollectionContainer : HTMLElement | undefined
module:ui/editorui/bodycollection~BodyCollection#_bodyCollectionContainer
The element holding elements of the body region.
Methods
-
constructor( locale, initialItems )
module:ui/editorui/bodycollection~BodyCollection#constructor
Creates a new instance of the
BodyCollection
.Parameters
locale : Locale
The editor's locale instance.
initialItems : Iterable<View<HTMLElement>>
The initial items of the collection.
Defaults to
[]
-
inherited
Symbol.iterator() → Iterator<View<HTMLElement>, any, undefined>
module:ui/editorui/bodycollection~BodyCollection#Symbol.iterator
-
inherited
add( item, [ index ] ) → BodyCollection
module:ui/editorui/bodycollection~BodyCollection#add
Adds an item into the collection.
If the item does not have an id, then it will be automatically generated and set on the item.
Parameters
item : View<HTMLElement>
[ index ] : number
The position of the item in the collection. The item is pushed to the collection when
index
not specified.
Returns
Fires
-
inherited
addMany( items, [ index ] ) → BodyCollection
module:ui/editorui/bodycollection~BodyCollection#addMany
Adds multiple items into the collection.
Any item not containing an id will get an automatically generated one.
Parameters
items : Iterable<View<HTMLElement>>
[ index ] : number
The position of the insertion. Items will be appended if no
index
is specified.
Returns
Fires
-
attachToDom() → void
module:ui/editorui/bodycollection~BodyCollection#attachToDom
Attaches the body collection to the DOM body element. You need to execute this method to render the content of the body collection.
Returns
void
-
inherited
bindTo( externalCollection ) → CollectionBindToChain<S, View<HTMLElement>>
module:ui/editorui/bodycollection~BodyCollection#bindTo
Binds 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'
or 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 ); // true
or 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'
It'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 ); // 1
Note:
clear
can be used to break the binding.Type parameters
S : extends Record<string, any>
The type of
externalCollection
element.
Parameters
externalCollection : Collection<S>
A collection to be bound.
Returns
CollectionBindToChain<S, View<HTMLElement>>
The binding chain object.
-
Removes all items from the collection and destroys the binding created using
bindTo
.Returns
void
Fires
-
inherited
delegate( events ) → EmitterMixinDelegateChain
module:ui/editorui/bodycollection~BodyCollection#delegate
Delegates selected events coming from within views in the collection to any
Emitter
.For the following views and collection:
const viewA = new View(); const viewB = new View(); const viewC = new View(); const views = parentView.createCollection(); views.delegate( 'eventX' ).to( viewB ); views.delegate( 'eventX', 'eventY' ).to( viewC ); views.add( viewA );
the
eventX
is delegated (fired by)viewB
andviewC
along withcustomData
:viewA.fire( 'eventX', customData );
and
eventY
is delegated (fired by)viewC
along withcustomData
:viewA.fire( 'eventY', customData );
See
delegate
.Parameters
Returns
EmitterMixinDelegateChain
Object with
to
property, a function which accepts the destination of delegated events.
-
-
detachFromDom() → void
module:ui/editorui/bodycollection~BodyCollection#detachFromDom
Detaches the collection from the DOM structure. Use this method when you do not need to use the body collection anymore to clean-up the DOM structure.
Returns
void
-
inherited
filter( callback, [ ctx ] ) → Array<View<HTMLElement>>
module:ui/editorui/bodycollection~BodyCollection#filter
-
inherited
find( callback, [ ctx ] ) → undefined | View<HTMLElement>
module:ui/editorui/bodycollection~BodyCollection#find
-
inherited
fire( eventOrInfo, args ) → GetEventInfo<TEvent>[ 'return' ]
module:ui/editorui/bodycollection~BodyCollection#fire
Fires an event, executing all callbacks registered for it.
The first parameter passed to callbacks is an
EventInfo
object, followed by the optionalargs
provided in thefire()
method call.Type parameters
Parameters
eventOrInfo : GetNameOrEventInfo<TEvent>
The name of the event or
EventInfo
object 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).
-
inherited
forEach( callback, [ ctx ] ) → void
module:ui/editorui/bodycollection~BodyCollection#forEach
Performs the specified action for each item in the collection.
Parameters
callback : ( View<HTMLElement>, number ) => unknown
[ ctx ] : any
Context in which the
callback
will be called.
Returns
void
-
inherited
get( idOrIndex ) → null | View<HTMLElement>
module:ui/editorui/bodycollection~BodyCollection#get
Gets an item by its ID or index.
Parameters
idOrIndex : string | number
The item ID or index in the collection.
Returns
null | View<HTMLElement>
The requested item or
null
if such item does not exist.
-
Gets 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 | View<HTMLElement>
The item or its ID in the collection.
Returns
number
The index of a given item.
-
Returns a Boolean indicating whether the collection contains an item.
Parameters
itemOrId : string | View<HTMLElement>
The item or its ID in the collection.
Returns
boolean
true
if the collection contains the item,false
otherwise.
-
inherited
listenTo( emitter, event, callback, [ options ] ) → void
module:ui/editorui/bodycollection~BodyCollection#listenTo:BASE_EMITTER
Registers 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' );
An event callback can stop the event and set the return value of the
fire
method.Type parameters
Parameters
emitter : Emitter
The 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
-
Executes the callback for each item in the collection and composes an array or values returned by this callback.
Type parameters
U
The result type of the callback.
Parameters
callback : ( View<HTMLElement>, number ) => U
[ ctx ] : any
Context in which the
callback
will be called.
Returns
Array<U>
The result of mapping.
-
Stops executing the callback on the given event. Shorthand for
this.stopListening( this, event, callback )
.Parameters
event : string
The name of the event.
callback : Function
The function to stop being called.
Returns
void
-
inherited
on( event, callback, [ options ] ) → void
module:ui/editorui/bodycollection~BodyCollection#on
Registers 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
-
inherited
once( event, callback, [ options ] ) → void
module:ui/editorui/bodycollection~BodyCollection#once
Registers a callback function to be executed on the next time the event is fired only. This is similar to calling
on
followed byoff
in 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
-
inherited
remove( subject ) → View<HTMLElement>
module:ui/editorui/bodycollection~BodyCollection#remove
Removes a child view from the collection. If the parent element of the collection has been set, the element of the view is also removed in DOM, reflecting the order of the collection.
See the
add
method.Parameters
subject : string | number | View<HTMLElement>
The view to remove, its id or index in the collection.
Returns
View<HTMLElement>
The removed view.
-
inherited
setParent( elementOrDocFragment ) → void
module:ui/editorui/bodycollection~BodyCollection#setParent
-
inherited
stopDelegating( [ event ], [ emitter ] ) → void
module:ui/editorui/bodycollection~BodyCollection#stopDelegating
Stops 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 ] : string
The 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 ofevent
to all emitters.
Returns
void
-
inherited
stopListening( [ emitter ], [ event ], [ callback ] ) → void
module:ui/editorui/bodycollection~BodyCollection#stopListening:BASE_STOP
Stops 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 ] : Emitter
The 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
-
Fired when an item is added to the collection.
Parameters
eventInfo : EventInfo
An object containing information about the fired event.
item : T
The added item.
index : number
An index where the addition occurred.
-
Fired when the collection was changed due to adding or removing items.
Parameters
eventInfo : EventInfo
An object containing information about the fired event.
data : CollectionChangeEventData<T>
Changed items.
-
inherited
remove( eventInfo, item, index )
module:ui/editorui/bodycollection~BodyCollection#event:remove
Fired when an item is removed from the collection.
Parameters
eventInfo : EventInfo
An object containing information about the fired event.
item : T
The removed item.
index : number
Index from which item was removed.
Every day, we work hard to keep our documentation complete. Have you spotted outdated information? Is something missing? Please report it via our issue tracker.
With the release of version 42.0.0, we have rewritten much of our documentation to reflect the new import paths and features. We appreciate your feedback to help us ensure its accuracy and completeness.