浏览代码

Improved adding views to the balloon stack.

Oskar Wróbel 8 年之前
父节点
当前提交
895cd59fda
共有 2 个文件被更改,包括 37 次插入1 次删除
  1. 7 1
      packages/ckeditor5-ui/src/contextualballoon.js
  2. 30 0
      packages/ckeditor5-ui/tests/contextualballoon.js

+ 7 - 1
packages/ckeditor5-ui/src/contextualballoon.js

@@ -177,7 +177,13 @@ export default class ContextualBalloon extends Plugin {
 	 */
 	_show( view ) {
 		this.view.content.add( view );
-		this.view.attachTo( this._getBalloonPosition() );
+
+		// When view is not rendered we need to wait for it. See: https://github.com/ckeditor/ckeditor5-ui/issues/187.
+		if ( !view.ready ) {
+			view.once( 'change:ready', () => this.view.attachTo( this._getBalloonPosition() ) );
+		} else {
+			this.view.attachTo( this._getBalloonPosition() );
+		}
 	}
 
 	/**

+ 30 - 0
packages/ckeditor5-ui/tests/contextualballoon.js

@@ -97,12 +97,38 @@ describe( 'ContextualBalloon', () => {
 				position: { target: 'fake' }
 			} );
 
+			viewA.ready = true;
+
 			expect( balloon.view.content.length ).to.equal( 1 );
 			expect( balloon.view.content.get( 0 ) ).to.deep.equal( viewA );
 			expect( balloon.view.attachTo.calledOnce ).to.true;
 			expect( balloon.view.attachTo.firstCall.args[ 0 ] ).to.deep.equal( { target: 'fake' } );
 		} );
 
+		it( 'should wait for view init', () => {
+			balloon.add( {
+				view: viewA,
+				position: { target: 'fake' }
+			} );
+
+			sinon.assert.notCalled( balloon.view.attachTo );
+
+			viewA.ready = true;
+
+			sinon.assert.calledOnce( balloon.view.attachTo );
+		} );
+
+		it( 'should not wait when view is ready', () => {
+			viewA.ready = true;
+
+			balloon.add( {
+				view: viewA,
+				position: { target: 'fake' }
+			} );
+
+			sinon.assert.calledOnce( balloon.view.attachTo );
+		} );
+
 		it( 'should throw an error when try to add the same view more than once', () => {
 			balloon.add( {
 				view: viewA,
@@ -138,11 +164,15 @@ describe( 'ContextualBalloon', () => {
 				position: { target: 'fake', foo: 'bar' }
 			} );
 
+			viewA.ready = true;
+
 			balloon.add( {
 				view: viewB,
 				position: { target: 'fake', bar: 'biz' }
 			} );
 
+			viewB.ready = true;
+
 			expect( balloon.view.attachTo.calledTwice ).to.true;
 
 			expect( balloon.view.attachTo.firstCall.args[ 0 ] ).to.deep.equal( {