From fa1a415c2921d8fbe7072d49d76fa3a6355bad2d Mon Sep 17 00:00:00 2001 From: Marek Lewandowski Date: Wed, 19 Feb 2020 10:09:59 +0100 Subject: [PATCH 01/10] Feature: View collection can now also be initialized though construction. Ported changes from https://github.com/ckeditor/ckeditor5-utils/pull/309 (1aac49db1ca9ea914f9fc44f00e295474faf3ed2). Note this breaks BodyCollection integration, so there's more work to be done. --- packages/ckeditor5-ui/src/view.js | 5 +- packages/ckeditor5-ui/src/viewcollection.js | 51 ++++++++----- packages/ckeditor5-ui/tests/focuscycler.js | 75 +++++-------------- packages/ckeditor5-ui/tests/view.js | 11 +++ packages/ckeditor5-ui/tests/viewcollection.js | 33 +++++++- 5 files changed, 95 insertions(+), 80 deletions(-) diff --git a/packages/ckeditor5-ui/src/view.js b/packages/ckeditor5-ui/src/view.js index ae1c3f40b0c..893c70c9a17 100644 --- a/packages/ckeditor5-ui/src/view.js +++ b/packages/ckeditor5-ui/src/view.js @@ -276,10 +276,11 @@ export default class View { * //

* view.items.add( child ); * + * @param {Array.} [views] Initial views of the collection. * @returns {module:ui/viewcollection~ViewCollection} A new collection of view instances. */ - createCollection() { - const collection = new ViewCollection(); + createCollection( views ) { + const collection = new ViewCollection( views ); this._viewCollections.add( collection ); diff --git a/packages/ckeditor5-ui/src/viewcollection.js b/packages/ckeditor5-ui/src/viewcollection.js index fea06ed81be..7d964d984fb 100644 --- a/packages/ckeditor5-ui/src/viewcollection.js +++ b/packages/ckeditor5-ui/src/viewcollection.js @@ -51,10 +51,10 @@ export default class ViewCollection extends Collection { /** * Creates a new instance of the {@link module:ui/viewcollection~ViewCollection}. * - * @param {module:utils/locale~Locale} [locale] The {@link module:core/editor/editor~Editor editor's locale} instance. + * @param {Array.} [initialItems] The initial items of the collection. */ - constructor( locale ) { - super( { + constructor( initialItems = [] ) { + super( initialItems, { // An #id Number attribute should be legal and not break the `ViewCollection` instance. // https://github.com/ckeditor/ckeditor5-ui/issues/93 idProperty: 'viewUid' @@ -62,13 +62,7 @@ export default class ViewCollection extends Collection { // Handle {@link module:ui/view~View#element} in DOM when a new view is added to the collection. this.on( 'add', ( evt, view, index ) => { - if ( !view.isRendered ) { - view.render(); - } - - if ( view.element && this._parentElement ) { - this._parentElement.insertBefore( view.element, this._parentElement.children[ index ] ); - } + this._renderViewIntoCollectionParent( view, index ); } ); // Handle {@link module:ui/view~View#element} in DOM when a view is removed from the collection. @@ -78,14 +72,6 @@ export default class ViewCollection extends Collection { } } ); - /** - * The {@link module:core/editor/editor~Editor#locale editor's locale} instance. - * See the view {@link module:ui/view~View#locale locale} property. - * - * @member {module:utils/locale~Locale} - */ - this.locale = locale; - /** * A parent element within which child views are rendered and managed in DOM. * @@ -112,6 +98,11 @@ export default class ViewCollection extends Collection { */ setParent( elementOrDocFragment ) { this._parentElement = elementOrDocFragment; + + // Take care of the initial collection items passed to the constructor. + for ( const view of this ) { + this._renderViewIntoCollectionParent( view ); + } } /** @@ -194,6 +185,30 @@ export default class ViewCollection extends Collection { }; } + /** + * This method {@link module:ui/view~View#render renders} a new view added to the collection. + * + * If the {@link #_parentElement parent element} of the collection is set, this method also adds + * the view's {@link module:ui/view~View#element} as a child of the parent in DOM at a specified index. + * + * **Note**: If index is not specified, the view's element is pushed as the last child + * of the parent element. + * + * @private + * @param {module:ui/view~View} view A new view added to the collection. + * @param {Number} [index] An index the view holds in the collection. When not specified, + * the view is added at the end. + */ + _renderViewIntoCollectionParent( view, index ) { + if ( !view.isRendered ) { + view.render(); + } + + if ( view.element && this._parentElement ) { + this._parentElement.insertBefore( view.element, this._parentElement.children[ index ] ); + } + } + /** * Removes a child view from the collection. If the {@link #setParent parent element} of the * collection has been set, the {@link module:ui/view~View#element element} of the view is also removed diff --git a/packages/ckeditor5-ui/tests/focuscycler.js b/packages/ckeditor5-ui/tests/focuscycler.js index 82163e0e7d2..815610c4dc6 100644 --- a/packages/ckeditor5-ui/tests/focuscycler.js +++ b/packages/ckeditor5-ui/tests/focuscycler.js @@ -17,7 +17,14 @@ describe( 'FocusCycler', () => { testUtils.createSinonSandbox(); beforeEach( () => { - focusables = new ViewCollection(); + testUtils.sinon.stub( global.window, 'getComputedStyle' ); + focusables = new ViewCollection( [ + nonFocusable(), + focusable(), + focusable(), + focusable(), + nonFocusable() + ] ); focusTracker = { focusedElement: null }; @@ -25,14 +32,6 @@ describe( 'FocusCycler', () => { focusables, focusTracker } ); - - testUtils.sinon.stub( global.window, 'getComputedStyle' ); - - focusables.add( nonFocusable() ); - focusables.add( focusable() ); - focusables.add( focusable() ); - focusables.add( focusable() ); - focusables.add( nonFocusable() ); } ); describe( 'constructor()', () => { @@ -60,12 +59,9 @@ describe( 'FocusCycler', () => { } ); it( 'returns null when no focusable items', () => { - focusables = new ViewCollection(); + focusables = new ViewCollection( [ nonFocusable(), nonFocusable() ] ); cycler = new FocusCycler( { focusables, focusTracker } ); - focusables.add( nonFocusable() ); - focusables.add( nonFocusable() ); - expect( cycler.first ).to.be.null; } ); @@ -83,12 +79,9 @@ describe( 'FocusCycler', () => { } ); it( 'returns null when no focusable items', () => { - focusables = new ViewCollection(); + focusables = new ViewCollection( [ nonFocusable(), nonFocusable() ] ); cycler = new FocusCycler( { focusables, focusTracker } ); - focusables.add( nonFocusable() ); - focusables.add( nonFocusable() ); - expect( cycler.last ).to.be.null; } ); @@ -126,23 +119,16 @@ describe( 'FocusCycler', () => { } ); it( 'returns null when no focusable items', () => { - focusables = new ViewCollection(); + focusables = new ViewCollection( [ nonFocusable(), nonFocusable() ] ); cycler = new FocusCycler( { focusables, focusTracker } ); - focusables.add( nonFocusable() ); - focusables.add( nonFocusable() ); - expect( cycler.next ).to.be.null; } ); it( 'returns null if the only focusable in focusables', () => { - focusables = new ViewCollection(); + focusables = new ViewCollection( [ nonFocusable(), focusable(), nonFocusable() ] ); cycler = new FocusCycler( { focusables, focusTracker } ); - focusables.add( nonFocusable() ); - focusables.add( focusable() ); - focusables.add( nonFocusable() ); - focusTracker.focusedElement = focusables.get( 1 ).element; expect( cycler.first ).to.equal( focusables.get( 1 ) ); @@ -176,23 +162,16 @@ describe( 'FocusCycler', () => { } ); it( 'returns null when no focusable items', () => { - focusables = new ViewCollection(); + focusables = new ViewCollection( [ nonFocusable(), nonFocusable() ] ); cycler = new FocusCycler( { focusables, focusTracker } ); - focusables.add( nonFocusable() ); - focusables.add( nonFocusable() ); - expect( cycler.previous ).to.be.null; } ); it( 'returns null if the only focusable in focusables', () => { - focusables = new ViewCollection(); + focusables = new ViewCollection( [ nonFocusable(), focusable(), nonFocusable() ] ); cycler = new FocusCycler( { focusables, focusTracker } ); - focusables.add( nonFocusable() ); - focusables.add( focusable() ); - focusables.add( nonFocusable() ); - focusTracker.focusedElement = focusables.get( 1 ).element; expect( cycler.first ).to.equal( focusables.get( 1 ) ); @@ -208,12 +187,9 @@ describe( 'FocusCycler', () => { } ); it( 'does not throw when no focusable items', () => { - focusables = new ViewCollection(); + focusables = new ViewCollection( [ nonFocusable(), nonFocusable() ] ); cycler = new FocusCycler( { focusables, focusTracker } ); - focusables.add( nonFocusable() ); - focusables.add( nonFocusable() ); - expect( () => { cycler.focusFirst(); } ).to.not.throw(); @@ -231,11 +207,7 @@ describe( 'FocusCycler', () => { it( 'ignores invisible items', () => { const item = focusable(); - focusables = new ViewCollection(); - focusables.add( nonFocusable() ); - focusables.add( focusable( true ) ); - focusables.add( item ); - + focusables = new ViewCollection( [ nonFocusable(), focusable( true ), item ] ); cycler = new FocusCycler( { focusables, focusTracker } ); cycler.focusFirst(); @@ -251,12 +223,9 @@ describe( 'FocusCycler', () => { } ); it( 'does not throw when no focusable items', () => { - focusables = new ViewCollection(); + focusables = new ViewCollection( [ nonFocusable(), nonFocusable() ] ); cycler = new FocusCycler( { focusables, focusTracker } ); - focusables.add( nonFocusable() ); - focusables.add( nonFocusable() ); - expect( () => { cycler.focusLast(); } ).to.not.throw(); @@ -281,12 +250,9 @@ describe( 'FocusCycler', () => { } ); it( 'does not throw when no focusable items', () => { - focusables = new ViewCollection(); + focusables = new ViewCollection( [ nonFocusable(), nonFocusable() ] ); cycler = new FocusCycler( { focusables, focusTracker } ); - focusables.add( nonFocusable() ); - focusables.add( nonFocusable() ); - expect( () => { cycler.focusNext(); } ).to.not.throw(); @@ -311,12 +277,9 @@ describe( 'FocusCycler', () => { } ); it( 'does not throw when no focusable items', () => { - focusables = new ViewCollection(); + focusables = new ViewCollection( [ nonFocusable(), nonFocusable() ] ); cycler = new FocusCycler( { focusables, focusTracker } ); - focusables.add( nonFocusable() ); - focusables.add( nonFocusable() ); - expect( () => { cycler.focusPrevious(); } ).to.not.throw(); diff --git a/packages/ckeditor5-ui/tests/view.js b/packages/ckeditor5-ui/tests/view.js index e99bbc50d31..226cd4c4611 100644 --- a/packages/ckeditor5-ui/tests/view.js +++ b/packages/ckeditor5-ui/tests/view.js @@ -89,6 +89,17 @@ describe( 'View', () => { expect( view._viewCollections ).to.have.length( 2 ); expect( view._viewCollections.get( 1 ) ).to.equal( collection ); } ); + + it( 'accepts initial views', () => { + const viewA = new View(); + const viewB = new View(); + + const collection = view.createCollection( [ viewA, viewB ] ); + + expect( collection ).to.have.length( 2 ); + expect( collection.get( 0 ) ).to.equal( viewA ); + expect( collection.get( 1 ) ).to.equal( viewB ); + } ); } ); describe( 'registerChild()', () => { diff --git a/packages/ckeditor5-ui/tests/viewcollection.js b/packages/ckeditor5-ui/tests/viewcollection.js index f366a90c4da..58191f2bd9a 100644 --- a/packages/ckeditor5-ui/tests/viewcollection.js +++ b/packages/ckeditor5-ui/tests/viewcollection.js @@ -20,15 +20,18 @@ describe( 'ViewCollection', () => { describe( 'constructor()', () => { it( 'sets basic properties and attributes', () => { - expect( collection.locale ).to.be.undefined; expect( collection._parentElement ).to.be.null; expect( collection._idProperty ).to.equal( 'viewUid' ); } ); - it( 'accepts locale and defines the locale property', () => { - const locale = { t() {} }; + it( 'allows setting initial collection items', () => { + const view1 = new View(); + const view2 = new View(); + const collection = new ViewCollection( [ view1, view2 ] ); - expect( new ViewCollection( locale ).locale ).to.equal( locale ); + expect( collection ).to.have.length( 2 ); + expect( collection.get( 0 ) ).to.equal( view1 ); + expect( collection.get( 1 ) ).to.equal( view2 ); } ); describe( 'child view management in DOM', () => { @@ -165,6 +168,28 @@ describe( 'ViewCollection', () => { collection.setParent( el ); expect( collection._parentElement ).to.equal( el ); } ); + + it( 'udpates initial collection items in DOM', () => { + const view1 = new View(); + view1.element = document.createElement( 'i' ); + sinon.spy( view1, 'render' ); + + const view2 = new View(); + view2.element = document.createElement( 'b' ); + sinon.spy( view2, 'render' ); + + const collection = new ViewCollection( [ view1, view2 ] ); + const parentElement = document.createElement( 'div' ); + + expect( collection ).to.have.length( 2 ); + expect( collection.get( 0 ) ).to.equal( view1 ); + expect( collection.get( 1 ) ).to.equal( view2 ); + + collection.setParent( parentElement ); + expect( normalizeHtml( parentElement.outerHTML ) ).to.equal( '
' ); + sinon.assert.calledOnce( view1.render ); + sinon.assert.calledOnce( view2.render ); + } ); } ); describe( 'delegate()', () => { From d90bb7c5413de634f67785efdd7e0b56735934c1 Mon Sep 17 00:00:00 2001 From: Marek Lewandowski Date: Wed, 19 Feb 2020 10:20:20 +0100 Subject: [PATCH 02/10] Internal: Restored locale support for BodyCollection to work correctly. --- packages/ckeditor5-ui/src/viewcollection.js | 16 ++++++++++++++-- packages/ckeditor5-ui/tests/view.js | 3 ++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/packages/ckeditor5-ui/src/viewcollection.js b/packages/ckeditor5-ui/src/viewcollection.js index 7d964d984fb..8a990799c4b 100644 --- a/packages/ckeditor5-ui/src/viewcollection.js +++ b/packages/ckeditor5-ui/src/viewcollection.js @@ -9,6 +9,7 @@ import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror'; import Collection from '@ckeditor/ckeditor5-utils/src/collection'; +import Locale from '@ckeditor/ckeditor5-utils/src/locale'; /** * Collects {@link module:ui/view~View} instances. @@ -53,8 +54,11 @@ export default class ViewCollection extends Collection { * * @param {Array.} [initialItems] The initial items of the collection. */ - constructor( initialItems = [] ) { - super( initialItems, { + constructor( itemsOrLocale = [] ) { + const locale = itemsOrLocale instanceof Locale ? itemsOrLocale : new Locale(); + const items = itemsOrLocale instanceof Locale ? [] : itemsOrLocale; + + super( items, { // An #id Number attribute should be legal and not break the `ViewCollection` instance. // https://github.com/ckeditor/ckeditor5-ui/issues/93 idProperty: 'viewUid' @@ -72,6 +76,14 @@ export default class ViewCollection extends Collection { } } ); + /** + * The {@link module:core/editor/editor~Editor#locale editor's locale} instance. + * See the view {@link module:ui/view~View#locale locale} property. + * + * @member {module:utils/locale~Locale} + */ + this.locale = locale; + /** * A parent element within which child views are rendered and managed in DOM. * diff --git a/packages/ckeditor5-ui/tests/view.js b/packages/ckeditor5-ui/tests/view.js index 226cd4c4611..a110450ccf3 100644 --- a/packages/ckeditor5-ui/tests/view.js +++ b/packages/ckeditor5-ui/tests/view.js @@ -13,6 +13,7 @@ import Collection from '@ckeditor/ckeditor5-utils/src/collection'; import ViewCollection from '../src/viewcollection'; import normalizeHtml from '@ckeditor/ckeditor5-utils/tests/_utils/normalizehtml'; import { assertCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils'; +import Locale from '@ckeditor/ckeditor5-utils/src/locale'; let TestView, view, childA, childB; @@ -63,7 +64,7 @@ describe( 'View', () => { const collection = new ViewCollection(); expect( view.locale ).to.equal( locale ); - expect( collection.locale ).to.be.undefined; + expect( collection.locale ).to.be.an.instanceOf( Locale ); view._viewCollections.add( collection ); expect( collection.locale ).to.equal( view.locale ); From e57b1b9c06d2a426b2481e7881e35f292b46e788 Mon Sep 17 00:00:00 2001 From: Marek Lewandowski Date: Wed, 19 Feb 2020 14:47:39 +0100 Subject: [PATCH 03/10] Internal: Locale instance is required only for BodyCollection - so this is where it is added. Next commit will remove locale handling from the ViewCollection class. --- .../src/editorui/bodycollection.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/packages/ckeditor5-ui/src/editorui/bodycollection.js b/packages/ckeditor5-ui/src/editorui/bodycollection.js index 521a0286683..24c4729253e 100644 --- a/packages/ckeditor5-ui/src/editorui/bodycollection.js +++ b/packages/ckeditor5-ui/src/editorui/bodycollection.js @@ -33,6 +33,24 @@ import createElement from '@ckeditor/ckeditor5-utils/src/dom/createelement'; * @extends module:ui/viewcollection~ViewCollection */ export default class BodyCollection extends ViewCollection { + /** + * Creates a new instance of the {@link module:ui/editorui/bodycollection~BodyCollection}. + * + * @param {module:utils/locale~Locale} locale The {@link module:core/editor/editor~Editor editor's locale} instance. + * @param {Array.} [initialItems] The initial items of the collection. + */ + constructor( locale, initialItems = [] ) { + super( initialItems ); + + /** + * The {@link module:core/editor/editor~Editor#locale editor's locale} instance. + * See the view {@link module:ui/view~View#locale locale} property. + * + * @member {module:utils/locale~Locale} + */ + this.locale = locale; + } + /** * Attaches the body collection to the DOM body element. You need to execute this method to render the content of * the body collection. From c63b46012247cd34091cc2952d56349a1711f514 Mon Sep 17 00:00:00 2001 From: Marek Lewandowski Date: Wed, 19 Feb 2020 14:48:59 +0100 Subject: [PATCH 04/10] Revert "Internal: Restored locale support for BodyCollection to work correctly." This reverts commit 8843250a180d048d8672512390632123f4cd5e74. --- packages/ckeditor5-ui/src/viewcollection.js | 16 ++-------------- packages/ckeditor5-ui/tests/view.js | 3 +-- 2 files changed, 3 insertions(+), 16 deletions(-) diff --git a/packages/ckeditor5-ui/src/viewcollection.js b/packages/ckeditor5-ui/src/viewcollection.js index 8a990799c4b..7d964d984fb 100644 --- a/packages/ckeditor5-ui/src/viewcollection.js +++ b/packages/ckeditor5-ui/src/viewcollection.js @@ -9,7 +9,6 @@ import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror'; import Collection from '@ckeditor/ckeditor5-utils/src/collection'; -import Locale from '@ckeditor/ckeditor5-utils/src/locale'; /** * Collects {@link module:ui/view~View} instances. @@ -54,11 +53,8 @@ export default class ViewCollection extends Collection { * * @param {Array.} [initialItems] The initial items of the collection. */ - constructor( itemsOrLocale = [] ) { - const locale = itemsOrLocale instanceof Locale ? itemsOrLocale : new Locale(); - const items = itemsOrLocale instanceof Locale ? [] : itemsOrLocale; - - super( items, { + constructor( initialItems = [] ) { + super( initialItems, { // An #id Number attribute should be legal and not break the `ViewCollection` instance. // https://github.com/ckeditor/ckeditor5-ui/issues/93 idProperty: 'viewUid' @@ -76,14 +72,6 @@ export default class ViewCollection extends Collection { } } ); - /** - * The {@link module:core/editor/editor~Editor#locale editor's locale} instance. - * See the view {@link module:ui/view~View#locale locale} property. - * - * @member {module:utils/locale~Locale} - */ - this.locale = locale; - /** * A parent element within which child views are rendered and managed in DOM. * diff --git a/packages/ckeditor5-ui/tests/view.js b/packages/ckeditor5-ui/tests/view.js index a110450ccf3..226cd4c4611 100644 --- a/packages/ckeditor5-ui/tests/view.js +++ b/packages/ckeditor5-ui/tests/view.js @@ -13,7 +13,6 @@ import Collection from '@ckeditor/ckeditor5-utils/src/collection'; import ViewCollection from '../src/viewcollection'; import normalizeHtml from '@ckeditor/ckeditor5-utils/tests/_utils/normalizehtml'; import { assertCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils'; -import Locale from '@ckeditor/ckeditor5-utils/src/locale'; let TestView, view, childA, childB; @@ -64,7 +63,7 @@ describe( 'View', () => { const collection = new ViewCollection(); expect( view.locale ).to.equal( locale ); - expect( collection.locale ).to.be.an.instanceOf( Locale ); + expect( collection.locale ).to.be.undefined; view._viewCollections.add( collection ); expect( collection.locale ).to.equal( view.locale ); From 7c313a469572a3ebef1e4c9b094e9def8fdcfd91 Mon Sep 17 00:00:00 2001 From: Marek Lewandowski Date: Wed, 19 Feb 2020 14:51:54 +0100 Subject: [PATCH 05/10] Tests: Added unit test coverage for BodyCollection constructor. --- packages/ckeditor5-ui/tests/editorui/bodycollection.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/ckeditor5-ui/tests/editorui/bodycollection.js b/packages/ckeditor5-ui/tests/editorui/bodycollection.js index cca259bbca2..a1b96d0620a 100644 --- a/packages/ckeditor5-ui/tests/editorui/bodycollection.js +++ b/packages/ckeditor5-ui/tests/editorui/bodycollection.js @@ -28,6 +28,14 @@ describe( 'BodyCollection', () => { } } ); + describe( 'constructor', () => { + it( 'assigns locale', () => { + const instance = new BodyCollection( locale ); + + expect( instance.locale ).to.be.equal( locale ); + } ); + } ); + describe( 'attachToDom', () => { it( 'should create wrapper and put the collection in that wrapper', () => { const body = new BodyCollection( locale ); From f86a54376b83dda3fa4529ea44e5fdef1d11bac6 Mon Sep 17 00:00:00 2001 From: Marek Lewandowski Date: Wed, 19 Feb 2020 15:11:17 +0100 Subject: [PATCH 06/10] Internal: Collection now works on iterables. --- packages/ckeditor5-ui/src/editorui/bodycollection.js | 2 +- packages/ckeditor5-ui/src/viewcollection.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/ckeditor5-ui/src/editorui/bodycollection.js b/packages/ckeditor5-ui/src/editorui/bodycollection.js index 24c4729253e..1412b8be694 100644 --- a/packages/ckeditor5-ui/src/editorui/bodycollection.js +++ b/packages/ckeditor5-ui/src/editorui/bodycollection.js @@ -37,7 +37,7 @@ export default class BodyCollection extends ViewCollection { * Creates a new instance of the {@link module:ui/editorui/bodycollection~BodyCollection}. * * @param {module:utils/locale~Locale} locale The {@link module:core/editor/editor~Editor editor's locale} instance. - * @param {Array.} [initialItems] The initial items of the collection. + * @param {Iterable.} [initialItems] The initial items of the collection. */ constructor( locale, initialItems = [] ) { super( initialItems ); diff --git a/packages/ckeditor5-ui/src/viewcollection.js b/packages/ckeditor5-ui/src/viewcollection.js index 7d964d984fb..10341f8e803 100644 --- a/packages/ckeditor5-ui/src/viewcollection.js +++ b/packages/ckeditor5-ui/src/viewcollection.js @@ -51,7 +51,7 @@ export default class ViewCollection extends Collection { /** * Creates a new instance of the {@link module:ui/viewcollection~ViewCollection}. * - * @param {Array.} [initialItems] The initial items of the collection. + * @param {Iterable.} [initialItems] The initial items of the collection. */ constructor( initialItems = [] ) { super( initialItems, { From b2c408c762cd4ef9c619d95aff1d6f52e08e0120 Mon Sep 17 00:00:00 2001 From: Marek Lewandowski Date: Wed, 26 Feb 2020 08:54:21 +0100 Subject: [PATCH 07/10] Docs: Views can be actually an iterable. --- packages/ckeditor5-ui/src/view.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/ckeditor5-ui/src/view.js b/packages/ckeditor5-ui/src/view.js index 893c70c9a17..2fc2280c289 100644 --- a/packages/ckeditor5-ui/src/view.js +++ b/packages/ckeditor5-ui/src/view.js @@ -276,7 +276,7 @@ export default class View { * //

* view.items.add( child ); * - * @param {Array.} [views] Initial views of the collection. + * @param {Iterable.} [views] Initial views of the collection. * @returns {module:ui/viewcollection~ViewCollection} A new collection of view instances. */ createCollection( views ) { From 1ed989f5be672b9dd3152f420fc9cdfea13c87b0 Mon Sep 17 00:00:00 2001 From: Marek Lewandowski Date: Fri, 3 Apr 2020 12:55:47 +0200 Subject: [PATCH 08/10] Docs: Adjusted code snippet for the view#createCollection() method. --- packages/ckeditor5-ui/src/view.js | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/packages/ckeditor5-ui/src/view.js b/packages/ckeditor5-ui/src/view.js index 2fc2280c289..6a55ad8e68d 100644 --- a/packages/ckeditor5-ui/src/view.js +++ b/packages/ckeditor5-ui/src/view.js @@ -253,7 +253,8 @@ export default class View { * constructor( locale ) { * super( locale ); * - * this.items = this.createCollection(); + * const child = new ChildView( locale ); + * this.items = this.createCollection( [ child ] ); * * this.setTemplate( { * tag: 'p', @@ -265,17 +266,11 @@ export default class View { * } * * const view = new SampleView( locale ); - * const child = new ChildView( locale ); - * * view.render(); * - * // It will append

to the . + * // It will append

to the . * document.body.appendChild( view.element ); * - * // From now on the child is nested under its parent, which is also reflected in DOM. - * //

- * view.items.add( child ); - * * @param {Iterable.} [views] Initial views of the collection. * @returns {module:ui/viewcollection~ViewCollection} A new collection of view instances. */ From 819c5381c8c36c00917d1f20f3b491c2cbf2c2c2 Mon Sep 17 00:00:00 2001 From: Marek Lewandowski Date: Fri, 3 Apr 2020 13:15:34 +0200 Subject: [PATCH 09/10] Tests: Added a test ensuring that the BodyCollection constructor is properly handling preinitialized collection items. --- packages/ckeditor5-ui/tests/editorui/bodycollection.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/packages/ckeditor5-ui/tests/editorui/bodycollection.js b/packages/ckeditor5-ui/tests/editorui/bodycollection.js index a1b96d0620a..304042b1414 100644 --- a/packages/ckeditor5-ui/tests/editorui/bodycollection.js +++ b/packages/ckeditor5-ui/tests/editorui/bodycollection.js @@ -34,6 +34,15 @@ describe( 'BodyCollection', () => { expect( instance.locale ).to.be.equal( locale ); } ); + + it( 'stores pre-initialized collection', () => { + const collectionItems = [ new View(), new View() ]; + const instance = new BodyCollection( locale, collectionItems ); + + expect( instance ).to.have.lengthOf( 2 ); + expect( instance.get( 0 ) ).to.be.equal( collectionItems[ 0 ] ); + expect( instance.get( 1 ) ).to.be.equal( collectionItems[ 1 ] ); + } ); } ); describe( 'attachToDom', () => { From 47341f807e176d55f60f7c8a760d54b1ad5906d1 Mon Sep 17 00:00:00 2001 From: Kamil Piechaczek Date: Mon, 6 Apr 2020 13:01:04 +0200 Subject: [PATCH 10/10] Internal: Updated the version of Node.js on Travis. See ckeditor/ckeditor5#6542. --- packages/ckeditor5-ui/.travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/ckeditor5-ui/.travis.yml b/packages/ckeditor5-ui/.travis.yml index d839093ade1..a8cc5428bd4 100644 --- a/packages/ckeditor5-ui/.travis.yml +++ b/packages/ckeditor5-ui/.travis.yml @@ -7,7 +7,7 @@ language: node_js services: - xvfb node_js: -- '8' +- '10' cache: yarn: true branches: