Răsfoiți Sursa

Remove View element from Region element when View removed from views Collection.

Aleksander Nowodzinski 10 ani în urmă
părinte
comite
655dd09dca

+ 1 - 0
packages/ckeditor5-engine/src/ui/region.js

@@ -40,6 +40,7 @@ CKEDITOR.define( [ 'collection', 'model' ], function( Collection, Model ) {
 			this.views = new Collection();
 
 			this.views.on( 'add', ( evt, view ) => this.el && this.el.appendChild( view.el ) );
+			this.views.on( 'remove', ( evt, view ) => view.el.remove() );
 		}
 
 		destroy() {}

+ 34 - 0
packages/ckeditor5-engine/tests/ui/view/region.js

@@ -49,4 +49,38 @@ describe( 'Region', function() {
 		region.views.add( new TestView() );
 		expect( region.el.childNodes.length ).to.be.equal( 2 );
 	} );
+
+	it( 'removes views from collection', function() {
+		var View = modules[ 'ui/view' ];
+
+		class TestViewA extends View {
+			constructor() {
+				super();
+				this.template = { tag: 'a' };
+			}
+		}
+
+		class TestViewB extends View {
+			constructor() {
+				super();
+				this.template = { tag: 'b' };
+			}
+		}
+
+		var tva = new TestViewA();
+		var tvb = new TestViewB();
+
+		region.views.add( tva );
+		region.views.add( tvb );
+
+		var childNodes = region.el.childNodes;
+
+		expect( [].map.call( childNodes, n => n.nodeName ).join( ',' ) ).to.be.equal( 'A,B' );
+
+		region.views.remove( tva );
+		expect( [].map.call( childNodes, n => n.nodeName ).join( ',' ) ).to.be.equal( 'B' );
+
+		region.views.remove( tvb );
+		expect( childNodes.length ).to.be.equal( 0 );
+	} );
 } );