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

Commit 0f1ea5a

Browse files
authored
Merge pull request #304 from ckeditor/t/303
Feature: Implemented `View#removeChildren`, the opposite of `View#addChildren`. Closes #303.
2 parents 6060459 + fe24760 commit 0f1ea5a

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

src/view.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,22 @@ export default class View {
274274
children.map( c => this._unboundChildren.add( c ) );
275275
}
276276

277+
/**
278+
* The opposite of {@link #addChildren}. Removes a child view from this view instance.
279+
* Once removed, the child is no longer managed by its parent, e.g. it can be safely used elsewhere,
280+
* becoming a child of another parent view.
281+
*
282+
* @see #addChildren
283+
* @param {module:ui/view~View|Iterable.<module:ui/view~View>} children Child views to be removed.
284+
*/
285+
removeChildren( children ) {
286+
if ( !isIterable( children ) ) {
287+
children = [ children ];
288+
}
289+
290+
children.map( c => this._unboundChildren.remove( c ) );
291+
}
292+
277293
/**
278294
* Initializes the view and child views located in {@link #_viewCollections}.
279295
*/

tests/view.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,39 @@ describe( 'View', () => {
105105
} );
106106
} );
107107

108+
describe( 'removeChildren()', () => {
109+
beforeEach( () => {
110+
setTestViewClass();
111+
setTestViewInstance();
112+
} );
113+
114+
it( 'should remove a single view from #_unboundChildren', () => {
115+
const child1 = {};
116+
const child2 = {};
117+
118+
view.addChildren( child1 );
119+
view.addChildren( child2 );
120+
expect( view._unboundChildren ).to.have.length( 2 );
121+
122+
view.removeChildren( child2 );
123+
expect( view._unboundChildren ).to.have.length( 1 );
124+
expect( view._unboundChildren.get( 0 ) ).to.equal( child1 );
125+
} );
126+
127+
it( 'should support iterables', () => {
128+
const child1 = {};
129+
const child2 = {};
130+
const child3 = {};
131+
132+
view.addChildren( [ child1, child2, child3 ] );
133+
expect( view._unboundChildren ).to.have.length( 3 );
134+
135+
view.removeChildren( [ child2, child3 ] );
136+
expect( view._unboundChildren ).to.have.length( 1 );
137+
expect( view._unboundChildren.get( 0 ) ).to.equal( child1 );
138+
} );
139+
} );
140+
108141
describe( 'init()', () => {
109142
beforeEach( createViewWithChildren );
110143

0 commit comments

Comments
 (0)