Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Improved ContextualBalloon to deal with an async Views. #201

Merged
merged 6 commits into from
Apr 12, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions src/contextualballoon.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ export default class ContextualBalloon extends Plugin {
* Adds a new view to the stack and makes it visible.
*
* @param {Object} data Configuration of the view.
* @param {module:ui/view~View} view Content of the balloon.
* @param {module:utils/dom/position~Options} position Positioning options.
* @param {module:ui/view~View} [data.view] Content of the balloon.
* @param {module:utils/dom/position~Options} [data.position] Positioning options.
* @returns {Promise} A Promise resolved when the child {@link module:ui/view~View#init} is done.
*/
add( data ) {
if ( this.hasView( data.view ) ) {
Expand All @@ -105,7 +106,7 @@ export default class ContextualBalloon extends Plugin {
// Add new view to the stack.
this._stack.set( data.view, data );
// And display it.
this._show( data.view );
return this._show( data.view );
}

/**
Expand All @@ -114,6 +115,7 @@ export default class ContextualBalloon extends Plugin {
* When there is no view in the stack then balloon will hide.
*
* @param {module:ui/view~View} view A view to be removed from the balloon.
* @returns {Promise} A Promise resolved when the preceding view is ready.
*/
remove( view ) {
if ( !this.hasView( view ) ) {
Expand All @@ -125,6 +127,9 @@ export default class ContextualBalloon extends Plugin {
throw new CKEditorError( 'contextualballoon-remove-view-not-exist: Cannot remove configuration of not existing view.' );
}

// A Promise resolved when the preceding view is ready.
let promise = Promise.resolve();

// When visible view is being removed.
if ( this.visibleView === view ) {
// We need to remove it from the view content.
Expand All @@ -139,7 +144,7 @@ export default class ContextualBalloon extends Plugin {
// If it is some other view.
if ( last ) {
// Just show it.
this._show( last.view );
promise = this._show( last.view );
} else {
// Hide the balloon panel.
this.view.hide();
Expand All @@ -148,6 +153,8 @@ export default class ContextualBalloon extends Plugin {
// Just remove given view from the stack.
this._stack.delete( view );
}

return promise;
}

/**
Expand All @@ -164,10 +171,12 @@ export default class ContextualBalloon extends Plugin {
*
* @private
* @param {module:ui/view~View} view View to show in the balloon.
* @returns {Promise} A Promise resolved when the child {@link module:ui/view~View#init} is done.
*/
_show( view ) {
this.view.content.add( view );
this.view.attachTo( this._getBalloonPosition() );
return this.view.content.add( view ).then( () => {
this.view.attachTo( this._getBalloonPosition() );
Copy link
Contributor Author

@oskarwrobel oskarwrobel Apr 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still logs errors on the browser console in synchronous tests because this.view.attachTo is called in the next event loop even when given view is ready.

} );
}

/**
Expand Down
Loading