-
Notifications
You must be signed in to change notification settings - Fork 18
Added support for initializing ViewCollection and BodyCollection items using a constructor #545
Changes from all commits
bfe8744
8843250
85da301
d1e4ab7
bcf5b4f
eafba4e
0e34bef
8838e08
db27824
ab499c1
de36478
016c666
b20a754
e470ac5
c71c323
bd93bd0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,21 +266,16 @@ export default class View { | |
* } | ||
* | ||
* const view = new SampleView( locale ); | ||
* const child = new ChildView( locale ); | ||
* | ||
* view.render(); | ||
* | ||
* // It will append <p></p> to the <body>. | ||
* // It will append <p><child#element></p> to the <body>. | ||
* document.body.appendChild( view.element ); | ||
* | ||
* // From now on the child is nested under its parent, which is also reflected in DOM. | ||
* // <p><child#element></p> | ||
* view.items.add( child ); | ||
* | ||
* @param {Iterable.<module:ui/view~View>} [views] Initial views of the collection. | ||
* @returns {module:ui/viewcollection~ViewCollection} A new collection of view instances. | ||
*/ | ||
createCollection() { | ||
const collection = new ViewCollection(); | ||
createCollection( views ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing docs for the usage with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed in e470ac5. |
||
const collection = new ViewCollection( views ); | ||
|
||
this._viewCollections.add( collection ); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,24 +51,18 @@ 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 {Iterable.<module:ui/view~View>} [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' | ||
} ); | ||
|
||
// 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was it really obsolete? Because it does not feel like the scope of the PR. OTOH it makes the arguments discovery harder in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As you pointed out back in #524 - locale in More on that in the PR main description:
I do agree on the fact that it goes outside of the scope, I'm fine with extracting this - let me know if clarification are good enough or you'd prefer to extract it to a separate issue. |
||
|
||
/** | ||
* 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 ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How can a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's unmodified original code from the Giving it a quick look the element could be unavailable if the view has no template sounds like an edge case, bot something that might happen? Eventually this refactoring might be extracted to a follow up in order not to block this PR. |
||
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 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see no test that verifies that
initialItems
is passed to theViewCollection#constructor
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in c71c323