@ckeditor/ckeditor5-ui 35.2.0 → 35.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/package.json +31 -23
  2. package/src/bindings/addkeyboardhandlingforgrid.js +45 -57
  3. package/src/bindings/clickoutsidehandler.js +15 -21
  4. package/src/bindings/injectcsstransitiondisabler.js +16 -20
  5. package/src/bindings/preventdefault.js +6 -8
  6. package/src/bindings/submithandler.js +5 -7
  7. package/src/button/button.js +5 -0
  8. package/src/button/buttonview.js +220 -259
  9. package/src/button/switchbuttonview.js +56 -71
  10. package/src/colorgrid/colorgridview.js +135 -197
  11. package/src/colorgrid/colortileview.js +37 -47
  12. package/src/colorgrid/utils.js +57 -66
  13. package/src/componentfactory.js +79 -93
  14. package/src/dropdown/button/dropdownbutton.js +5 -0
  15. package/src/dropdown/button/dropdownbuttonview.js +44 -57
  16. package/src/dropdown/button/splitbuttonview.js +159 -207
  17. package/src/dropdown/dropdownpanelfocusable.js +5 -0
  18. package/src/dropdown/dropdownpanelview.js +101 -112
  19. package/src/dropdown/dropdownview.js +396 -438
  20. package/src/dropdown/utils.js +164 -213
  21. package/src/editableui/editableuiview.js +125 -141
  22. package/src/editableui/inline/inlineeditableuiview.js +44 -54
  23. package/src/editorui/bodycollection.js +61 -75
  24. package/src/editorui/boxed/boxededitoruiview.js +91 -104
  25. package/src/editorui/editoruiview.js +30 -39
  26. package/src/focuscycler.js +214 -245
  27. package/src/formheader/formheaderview.js +58 -70
  28. package/src/icon/iconview.js +145 -111
  29. package/src/iframe/iframeview.js +37 -49
  30. package/src/index.js +0 -17
  31. package/src/input/inputview.js +170 -198
  32. package/src/inputnumber/inputnumberview.js +48 -56
  33. package/src/inputtext/inputtextview.js +14 -18
  34. package/src/label/labelview.js +44 -53
  35. package/src/labeledfield/labeledfieldview.js +212 -235
  36. package/src/labeledfield/utils.js +39 -57
  37. package/src/labeledinput/labeledinputview.js +190 -221
  38. package/src/list/listitemview.js +40 -50
  39. package/src/list/listseparatorview.js +15 -19
  40. package/src/list/listview.js +94 -115
  41. package/src/model.js +19 -25
  42. package/src/notification/notification.js +151 -202
  43. package/src/panel/balloon/balloonpanelview.js +535 -628
  44. package/src/panel/balloon/contextualballoon.js +611 -732
  45. package/src/panel/sticky/stickypanelview.js +238 -270
  46. package/src/template.js +1049 -1479
  47. package/src/toolbar/balloon/balloontoolbar.js +337 -424
  48. package/src/toolbar/block/blockbuttonview.js +32 -42
  49. package/src/toolbar/block/blocktoolbar.js +375 -477
  50. package/src/toolbar/normalizetoolbarconfig.js +17 -21
  51. package/src/toolbar/toolbarlinebreakview.js +15 -19
  52. package/src/toolbar/toolbarseparatorview.js +15 -19
  53. package/src/toolbar/toolbarview.js +866 -1053
  54. package/src/tooltipmanager.js +324 -353
  55. package/src/view.js +389 -430
  56. package/src/viewcollection.js +147 -178
  57. package/src/button/button.jsdoc +0 -165
  58. package/src/dropdown/button/dropdownbutton.jsdoc +0 -22
  59. package/src/dropdown/dropdownpanelfocusable.jsdoc +0 -27
package/src/view.js CHANGED
@@ -2,22 +2,18 @@
2
2
  * @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
-
5
+ /* eslint-disable @typescript-eslint/no-invalid-void-type, new-cap */
6
6
  /**
7
7
  * @module ui/view
8
8
  */
9
-
10
9
  import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
11
10
  import ViewCollection from './viewcollection';
12
11
  import Template from './template';
13
12
  import DomEmitterMixin from '@ckeditor/ckeditor5-utils/src/dom/emittermixin';
14
- import ObservableMixin from '@ckeditor/ckeditor5-utils/src/observablemixin';
13
+ import { Observable } from '@ckeditor/ckeditor5-utils/src/observablemixin';
15
14
  import Collection from '@ckeditor/ckeditor5-utils/src/collection';
16
- import mix from '@ckeditor/ckeditor5-utils/src/mix';
17
15
  import isIterable from '@ckeditor/ckeditor5-utils/src/isiterable';
18
-
19
16
  import '../theme/globals/globals.css';
20
-
21
17
  /**
22
18
  * The basic view class, which represents an HTML element created out of a
23
19
  * {@link module:ui/view~View#template}. Views are building blocks of the user interface and handle
@@ -83,428 +79,391 @@ import '../theme/globals/globals.css';
83
79
  *
84
80
  * @mixes module:utils/observablemixin~ObservableMixin
85
81
  */
86
- export default class View {
87
- /**
88
- * Creates an instance of the {@link module:ui/view~View} class.
89
- *
90
- * Also see {@link #render}.
91
- *
92
- * @param {module:utils/locale~Locale} [locale] The localization services instance.
93
- */
94
- constructor( locale ) {
95
- /**
96
- * An HTML element of the view. `null` until {@link #render rendered}
97
- * from the {@link #template}.
98
- *
99
- * class SampleView extends View {
100
- * constructor() {
101
- * super();
102
- *
103
- * // A template instance the #element will be created from.
104
- * this.setTemplate( {
105
- * tag: 'p'
106
- *
107
- * // ...
108
- * } );
109
- * }
110
- * }
111
- *
112
- * const view = new SampleView();
113
- *
114
- * // Renders the #template.
115
- * view.render();
116
- *
117
- * // Append the HTML element of the view to <body>.
118
- * document.body.appendChild( view.element );
119
- *
120
- * **Note**: The element of the view can also be assigned directly:
121
- *
122
- * view.element = document.querySelector( '#my-container' );
123
- *
124
- * @member {HTMLElement}
125
- */
126
- this.element = null;
127
-
128
- /**
129
- * Set `true` when the view has already been {@link module:ui/view~View#render rendered}.
130
- *
131
- * @readonly
132
- * @member {Boolean} #isRendered
133
- */
134
- this.isRendered = false;
135
-
136
- /**
137
- * A set of tools to localize the user interface.
138
- *
139
- * Also see {@link module:core/editor/editor~Editor#locale}.
140
- *
141
- * @readonly
142
- * @member {module:utils/locale~Locale}
143
- */
144
- this.locale = locale;
145
-
146
- /**
147
- * Shorthand for {@link module:utils/locale~Locale#t}.
148
- *
149
- * Note: If {@link #locale} instance hasn't been passed to the view this method may not
150
- * be available.
151
- *
152
- * @see module:utils/locale~Locale#t
153
- * @method
154
- */
155
- this.t = locale && locale.t;
156
-
157
- /**
158
- * Collections registered with {@link #createCollection}.
159
- *
160
- * @protected
161
- * @member {Set.<module:ui/viewcollection~ViewCollection>}
162
- */
163
- this._viewCollections = new Collection();
164
-
165
- /**
166
- * A collection of view instances, which have been added directly
167
- * into the {@link module:ui/template~Template#children}.
168
- *
169
- * @protected
170
- * @member {module:ui/viewcollection~ViewCollection}
171
- */
172
- this._unboundChildren = this.createCollection();
173
-
174
- // Pass parent locale to its children.
175
- this._viewCollections.on( 'add', ( evt, collection ) => {
176
- collection.locale = locale;
177
- } );
178
-
179
- /**
180
- * Template of this view. It provides the {@link #element} representing
181
- * the view in DOM, which is {@link #render rendered}.
182
- *
183
- * @member {module:ui/template~Template} #template
184
- */
185
-
186
- /**
187
- * Cached {@link module:ui/template~BindChain bind chain} object created by the
188
- * {@link #template}. See {@link #bindTemplate}.
189
- *
190
- * @private
191
- * @member {Object} #_bindTemplate
192
- */
193
-
194
- this.decorate( 'render' );
195
- }
196
-
197
- /**
198
- * Shorthand for {@link module:ui/template~Template.bind}, a binding
199
- * {@link module:ui/template~BindChain interface} pre–configured for the view instance.
200
- *
201
- * It provides {@link module:ui/template~BindChain#to `to()`} and
202
- * {@link module:ui/template~BindChain#if `if()`} methods that initialize bindings with
203
- * observable attributes and attach DOM listeners.
204
- *
205
- * class SampleView extends View {
206
- * constructor( locale ) {
207
- * super( locale );
208
- *
209
- * const bind = this.bindTemplate;
210
- *
211
- * // These {@link module:utils/observablemixin~Observable observable} attributes will control
212
- * // the state of the view in DOM.
213
- * this.set( {
214
- * elementClass: 'foo',
215
- * isEnabled: true
216
- * } );
217
- *
218
- * this.setTemplate( {
219
- * tag: 'p',
220
- *
221
- * attributes: {
222
- * // The class HTML attribute will follow elementClass
223
- * // and isEnabled view attributes.
224
- * class: [
225
- * bind.to( 'elementClass' )
226
- * bind.if( 'isEnabled', 'present-when-enabled' )
227
- * ]
228
- * },
229
- *
230
- * on: {
231
- * // The view will fire the "clicked" event upon clicking <p> in DOM.
232
- * click: bind.to( 'clicked' )
233
- * }
234
- * } );
235
- * }
236
- * }
237
- *
238
- * @method #bindTemplate
239
- */
240
- get bindTemplate() {
241
- if ( this._bindTemplate ) {
242
- return this._bindTemplate;
243
- }
244
-
245
- return ( this._bindTemplate = Template.bind( this, this ) );
246
- }
247
-
248
- /**
249
- * Creates a new collection of views, which can be used as
250
- * {@link module:ui/template~Template#children} of this view.
251
- *
252
- * class SampleView extends View {
253
- * constructor( locale ) {
254
- * super( locale );
255
- *
256
- * const child = new ChildView( locale );
257
- * this.items = this.createCollection( [ child ] );
258
- *
259
- * this.setTemplate( {
260
- * tag: 'p',
261
- *
262
- * // `items` collection will render here.
263
- * children: this.items
264
- * } );
265
- * }
266
- * }
267
- *
268
- * const view = new SampleView( locale );
269
- * view.render();
270
- *
271
- * // It will append <p><child#element></p> to the <body>.
272
- * document.body.appendChild( view.element );
273
- *
274
- * @param {Iterable.<module:ui/view~View>} [views] Initial views of the collection.
275
- * @returns {module:ui/viewcollection~ViewCollection} A new collection of view instances.
276
- */
277
- createCollection( views ) {
278
- const collection = new ViewCollection( views );
279
-
280
- this._viewCollections.add( collection );
281
-
282
- return collection;
283
- }
284
-
285
- /**
286
- * Registers a new child view under the view instance. Once registered, a child
287
- * view is managed by its parent, including {@link #render rendering}
288
- * and {@link #destroy destruction}.
289
- *
290
- * To revert this, use {@link #deregisterChild}.
291
- *
292
- * class SampleView extends View {
293
- * constructor( locale ) {
294
- * super( locale );
295
- *
296
- * this.childA = new SomeChildView( locale );
297
- * this.childB = new SomeChildView( locale );
298
- *
299
- * this.setTemplate( { tag: 'p' } );
300
- *
301
- * // Register the children.
302
- * this.registerChild( [ this.childA, this.childB ] );
303
- * }
304
- *
305
- * render() {
306
- * super.render();
307
- *
308
- * this.element.appendChild( this.childA.element );
309
- * this.element.appendChild( this.childB.element );
310
- * }
311
- * }
312
- *
313
- * const view = new SampleView( locale );
314
- *
315
- * view.render();
316
- *
317
- * // Will append <p><childA#element><b></b><childB#element></p>.
318
- * document.body.appendChild( view.element );
319
- *
320
- * **Note**: There's no need to add child views if they're already referenced in the
321
- * {@link #template}:
322
- *
323
- * class SampleView extends View {
324
- * constructor( locale ) {
325
- * super( locale );
326
- *
327
- * this.childA = new SomeChildView( locale );
328
- * this.childB = new SomeChildView( locale );
329
- *
330
- * this.setTemplate( {
331
- * tag: 'p',
332
- *
333
- * // These children will be added automatically. There's no
334
- * // need to call {@link #registerChild} for any of them.
335
- * children: [ this.childA, this.childB ]
336
- * } );
337
- * }
338
- *
339
- * // ...
340
- * }
341
- *
342
- * @param {module:ui/view~View|Iterable.<module:ui/view~View>} children Children views to be registered.
343
- */
344
- registerChild( children ) {
345
- if ( !isIterable( children ) ) {
346
- children = [ children ];
347
- }
348
-
349
- for ( const child of children ) {
350
- this._unboundChildren.add( child );
351
- }
352
- }
353
-
354
- /**
355
- * The opposite of {@link #registerChild}. Removes a child view from this view instance.
356
- * Once removed, the child is no longer managed by its parent, e.g. it can safely
357
- * become a child of another parent view.
358
- *
359
- * @see #registerChild
360
- * @param {module:ui/view~View|Iterable.<module:ui/view~View>} children Child views to be removed.
361
- */
362
- deregisterChild( children ) {
363
- if ( !isIterable( children ) ) {
364
- children = [ children ];
365
- }
366
-
367
- for ( const child of children ) {
368
- this._unboundChildren.remove( child );
369
- }
370
- }
371
-
372
- /**
373
- * Sets the {@link #template} of the view with with given definition.
374
- *
375
- * A shorthand for:
376
- *
377
- * view.setTemplate( definition );
378
- *
379
- * @param {module:ui/template~TemplateDefinition} definition Definition of view's template.
380
- */
381
- setTemplate( definition ) {
382
- this.template = new Template( definition );
383
- }
384
-
385
- /**
386
- * {@link module:ui/template~Template.extend Extends} the {@link #template} of the view with
387
- * with given definition.
388
- *
389
- * A shorthand for:
390
- *
391
- * Template.extend( view.template, definition );
392
- *
393
- * **Note**: Is requires the {@link #template} to be already set. See {@link #setTemplate}.
394
- *
395
- * @param {module:ui/template~TemplateDefinition} definition Definition which
396
- * extends the {@link #template}.
397
- */
398
- extendTemplate( definition ) {
399
- Template.extend( this.template, definition );
400
- }
401
-
402
- /**
403
- * Recursively renders the view.
404
- *
405
- * Once the view is rendered:
406
- * * the {@link #element} becomes an HTML element out of {@link #template},
407
- * * the {@link #isRendered} flag is set `true`.
408
- *
409
- * **Note**: The children of the view:
410
- * * defined directly in the {@link #template}
411
- * * residing in collections created by the {@link #createCollection} method,
412
- * * and added by {@link #registerChild}
413
- * are also rendered in the process.
414
- *
415
- * In general, `render()` method is the right place to keep the code which refers to the
416
- * {@link #element} and should be executed at the very beginning of the view's life cycle.
417
- *
418
- * It is possible to {@link module:ui/template~Template.extend} the {@link #template} before
419
- * the view is rendered. To allow an early customization of the view (e.g. by its parent),
420
- * such references should be done in `render()`.
421
- *
422
- * class SampleView extends View {
423
- * constructor() {
424
- * this.setTemplate( {
425
- * // ...
426
- * } );
427
- * },
428
- *
429
- * render() {
430
- * // View#element becomes available.
431
- * super.render();
432
- *
433
- * // The "scroll" listener depends on #element.
434
- * this.listenTo( window, 'scroll', () => {
435
- * // A reference to #element would render the #template and make it non-extendable.
436
- * if ( window.scrollY > 0 ) {
437
- * this.element.scrollLeft = 100;
438
- * } else {
439
- * this.element.scrollLeft = 0;
440
- * }
441
- * } );
442
- * }
443
- * }
444
- *
445
- * const view = new SampleView();
446
- *
447
- * // Let's customize the view before it gets rendered.
448
- * view.extendTemplate( {
449
- * attributes: {
450
- * class: [
451
- * 'additional-class'
452
- * ]
453
- * }
454
- * } );
455
- *
456
- * // Late rendering allows customization of the view.
457
- * view.render();
458
- */
459
- render() {
460
- if ( this.isRendered ) {
461
- /**
462
- * This View has already been rendered.
463
- *
464
- * @error ui-view-render-already-rendered
465
- */
466
- throw new CKEditorError( 'ui-view-render-already-rendered', this );
467
- }
468
-
469
- // Render #element of the view.
470
- if ( this.template ) {
471
- this.element = this.template.render();
472
-
473
- // Auto–register view children from #template.
474
- this.registerChild( this.template.getViews() );
475
- }
476
-
477
- this.isRendered = true;
478
- }
479
-
480
- /**
481
- * Recursively destroys the view instance and child views added by {@link #registerChild} and
482
- * residing in collections created by the {@link #createCollection}.
483
- *
484
- * Destruction disables all event listeners:
485
- * * created on the view, e.g. `view.on( 'event', () => {} )`,
486
- * * defined in the {@link #template} for DOM events.
487
- */
488
- destroy() {
489
- this.stopListening();
490
-
491
- this._viewCollections.map( c => c.destroy() );
492
-
493
- // Template isn't obligatory for views.
494
- if ( this.template && this.template._revertData ) {
495
- this.template.revert( this.element );
496
- }
497
- }
498
-
499
- /**
500
- * Event fired by the {@link #render} method. Actual rendering is executed as a listener to
501
- * this event with the default priority.
502
- *
503
- * See {@link module:utils/observablemixin~ObservableMixin#decorate} for more information and samples.
504
- *
505
- * @event render
506
- */
82
+ export default class View extends DomEmitterMixin(Observable) {
83
+ /**
84
+ * Creates an instance of the {@link module:ui/view~View} class.
85
+ *
86
+ * Also see {@link #render}.
87
+ *
88
+ * @param {module:utils/locale~Locale} [locale] The localization services instance.
89
+ */
90
+ constructor(locale) {
91
+ super();
92
+ /**
93
+ * An HTML element of the view. `null` until {@link #render rendered}
94
+ * from the {@link #template}.
95
+ *
96
+ * class SampleView extends View {
97
+ * constructor() {
98
+ * super();
99
+ *
100
+ * // A template instance the #element will be created from.
101
+ * this.setTemplate( {
102
+ * tag: 'p'
103
+ *
104
+ * // ...
105
+ * } );
106
+ * }
107
+ * }
108
+ *
109
+ * const view = new SampleView();
110
+ *
111
+ * // Renders the #template.
112
+ * view.render();
113
+ *
114
+ * // Append the HTML element of the view to <body>.
115
+ * document.body.appendChild( view.element );
116
+ *
117
+ * **Note**: The element of the view can also be assigned directly:
118
+ *
119
+ * view.element = document.querySelector( '#my-container' );
120
+ *
121
+ * @member {HTMLElement}
122
+ */
123
+ this.element = null;
124
+ /**
125
+ * Set `true` when the view has already been {@link module:ui/view~View#render rendered}.
126
+ *
127
+ * @readonly
128
+ * @member {Boolean} #isRendered
129
+ */
130
+ this.isRendered = false;
131
+ /**
132
+ * A set of tools to localize the user interface.
133
+ *
134
+ * Also see {@link module:core/editor/editor~Editor#locale}.
135
+ *
136
+ * @readonly
137
+ * @member {module:utils/locale~Locale}
138
+ */
139
+ this.locale = locale;
140
+ /**
141
+ * Shorthand for {@link module:utils/locale~Locale#t}.
142
+ *
143
+ * Note: If {@link #locale} instance hasn't been passed to the view this method may not
144
+ * be available.
145
+ *
146
+ * @see module:utils/locale~Locale#t
147
+ * @method
148
+ */
149
+ this.t = locale && locale.t;
150
+ /**
151
+ * Collections registered with {@link #createCollection}.
152
+ *
153
+ * @protected
154
+ * @member {Set.<module:ui/viewcollection~ViewCollection>}
155
+ */
156
+ this._viewCollections = new Collection();
157
+ /**
158
+ * A collection of view instances, which have been added directly
159
+ * into the {@link module:ui/template~Template#children}.
160
+ *
161
+ * @protected
162
+ * @member {module:ui/viewcollection~ViewCollection}
163
+ */
164
+ this._unboundChildren = this.createCollection();
165
+ // Pass parent locale to its children.
166
+ this._viewCollections.on('add', (evt, collection) => {
167
+ collection.locale = locale;
168
+ collection.t = locale && locale.t;
169
+ });
170
+ /**
171
+ * Template of this view. It provides the {@link #element} representing
172
+ * the view in DOM, which is {@link #render rendered}.
173
+ *
174
+ * @member {module:ui/template~Template} #template
175
+ */
176
+ /**
177
+ * Cached {@link module:ui/template~BindChain bind chain} object created by the
178
+ * {@link #template}. See {@link #bindTemplate}.
179
+ *
180
+ * @private
181
+ * @member {Object} #_bindTemplate
182
+ */
183
+ this.decorate('render');
184
+ }
185
+ /**
186
+ * Shorthand for {@link module:ui/template~Template.bind}, a binding
187
+ * {@link module:ui/template~BindChain interface} pre–configured for the view instance.
188
+ *
189
+ * It provides {@link module:ui/template~BindChain#to `to()`} and
190
+ * {@link module:ui/template~BindChain#if `if()`} methods that initialize bindings with
191
+ * observable attributes and attach DOM listeners.
192
+ *
193
+ * class SampleView extends View {
194
+ * constructor( locale ) {
195
+ * super( locale );
196
+ *
197
+ * const bind = this.bindTemplate;
198
+ *
199
+ * // These {@link module:utils/observablemixin~Observable observable} attributes will control
200
+ * // the state of the view in DOM.
201
+ * this.set( {
202
+ * elementClass: 'foo',
203
+ * isEnabled: true
204
+ * } );
205
+ *
206
+ * this.setTemplate( {
207
+ * tag: 'p',
208
+ *
209
+ * attributes: {
210
+ * // The class HTML attribute will follow elementClass
211
+ * // and isEnabled view attributes.
212
+ * class: [
213
+ * bind.to( 'elementClass' )
214
+ * bind.if( 'isEnabled', 'present-when-enabled' )
215
+ * ]
216
+ * },
217
+ *
218
+ * on: {
219
+ * // The view will fire the "clicked" event upon clicking <p> in DOM.
220
+ * click: bind.to( 'clicked' )
221
+ * }
222
+ * } );
223
+ * }
224
+ * }
225
+ *
226
+ * @method #bindTemplate
227
+ */
228
+ get bindTemplate() {
229
+ if (this._bindTemplate) {
230
+ return this._bindTemplate;
231
+ }
232
+ return (this._bindTemplate = Template.bind(this, this));
233
+ }
234
+ /**
235
+ * Creates a new collection of views, which can be used as
236
+ * {@link module:ui/template~Template#children} of this view.
237
+ *
238
+ * class SampleView extends View {
239
+ * constructor( locale ) {
240
+ * super( locale );
241
+ *
242
+ * const child = new ChildView( locale );
243
+ * this.items = this.createCollection( [ child ] );
244
+ *
245
+ * this.setTemplate( {
246
+ * tag: 'p',
247
+ *
248
+ * // `items` collection will render here.
249
+ * children: this.items
250
+ * } );
251
+ * }
252
+ * }
253
+ *
254
+ * const view = new SampleView( locale );
255
+ * view.render();
256
+ *
257
+ * // It will append <p><child#element></p> to the <body>.
258
+ * document.body.appendChild( view.element );
259
+ *
260
+ * @param {Iterable.<module:ui/view~View>} [views] Initial views of the collection.
261
+ * @returns {module:ui/viewcollection~ViewCollection} A new collection of view instances.
262
+ */
263
+ createCollection(views) {
264
+ const collection = new ViewCollection(views);
265
+ this._viewCollections.add(collection);
266
+ return collection;
267
+ }
268
+ /**
269
+ * Registers a new child view under the view instance. Once registered, a child
270
+ * view is managed by its parent, including {@link #render rendering}
271
+ * and {@link #destroy destruction}.
272
+ *
273
+ * To revert this, use {@link #deregisterChild}.
274
+ *
275
+ * class SampleView extends View {
276
+ * constructor( locale ) {
277
+ * super( locale );
278
+ *
279
+ * this.childA = new SomeChildView( locale );
280
+ * this.childB = new SomeChildView( locale );
281
+ *
282
+ * this.setTemplate( { tag: 'p' } );
283
+ *
284
+ * // Register the children.
285
+ * this.registerChild( [ this.childA, this.childB ] );
286
+ * }
287
+ *
288
+ * render() {
289
+ * super.render();
290
+ *
291
+ * this.element.appendChild( this.childA.element );
292
+ * this.element.appendChild( this.childB.element );
293
+ * }
294
+ * }
295
+ *
296
+ * const view = new SampleView( locale );
297
+ *
298
+ * view.render();
299
+ *
300
+ * // Will append <p><childA#element><b></b><childB#element></p>.
301
+ * document.body.appendChild( view.element );
302
+ *
303
+ * **Note**: There's no need to add child views if they're already referenced in the
304
+ * {@link #template}:
305
+ *
306
+ * class SampleView extends View {
307
+ * constructor( locale ) {
308
+ * super( locale );
309
+ *
310
+ * this.childA = new SomeChildView( locale );
311
+ * this.childB = new SomeChildView( locale );
312
+ *
313
+ * this.setTemplate( {
314
+ * tag: 'p',
315
+ *
316
+ * // These children will be added automatically. There's no
317
+ * // need to call {@link #registerChild} for any of them.
318
+ * children: [ this.childA, this.childB ]
319
+ * } );
320
+ * }
321
+ *
322
+ * // ...
323
+ * }
324
+ *
325
+ * @param {module:ui/view~View|Iterable.<module:ui/view~View>} children Children views to be registered.
326
+ */
327
+ registerChild(children) {
328
+ if (!isIterable(children)) {
329
+ children = [children];
330
+ }
331
+ for (const child of children) {
332
+ this._unboundChildren.add(child);
333
+ }
334
+ }
335
+ /**
336
+ * The opposite of {@link #registerChild}. Removes a child view from this view instance.
337
+ * Once removed, the child is no longer managed by its parent, e.g. it can safely
338
+ * become a child of another parent view.
339
+ *
340
+ * @see #registerChild
341
+ * @param {module:ui/view~View|Iterable.<module:ui/view~View>} children Child views to be removed.
342
+ */
343
+ deregisterChild(children) {
344
+ if (!isIterable(children)) {
345
+ children = [children];
346
+ }
347
+ for (const child of children) {
348
+ this._unboundChildren.remove(child);
349
+ }
350
+ }
351
+ /**
352
+ * Sets the {@link #template} of the view with with given definition.
353
+ *
354
+ * A shorthand for:
355
+ *
356
+ * view.setTemplate( definition );
357
+ *
358
+ * @param {module:ui/template~TemplateDefinition} definition Definition of view's template.
359
+ */
360
+ setTemplate(definition) {
361
+ this.template = new Template(definition);
362
+ }
363
+ /**
364
+ * {@link module:ui/template~Template.extend Extends} the {@link #template} of the view with
365
+ * with given definition.
366
+ *
367
+ * A shorthand for:
368
+ *
369
+ * Template.extend( view.template, definition );
370
+ *
371
+ * **Note**: Is requires the {@link #template} to be already set. See {@link #setTemplate}.
372
+ *
373
+ * @param {module:ui/template~TemplateDefinition} definition Definition which
374
+ * extends the {@link #template}.
375
+ */
376
+ extendTemplate(definition) {
377
+ Template.extend(this.template, definition);
378
+ }
379
+ /**
380
+ * Recursively renders the view.
381
+ *
382
+ * Once the view is rendered:
383
+ * * the {@link #element} becomes an HTML element out of {@link #template},
384
+ * * the {@link #isRendered} flag is set `true`.
385
+ *
386
+ * **Note**: The children of the view:
387
+ * * defined directly in the {@link #template}
388
+ * * residing in collections created by the {@link #createCollection} method,
389
+ * * and added by {@link #registerChild}
390
+ * are also rendered in the process.
391
+ *
392
+ * In general, `render()` method is the right place to keep the code which refers to the
393
+ * {@link #element} and should be executed at the very beginning of the view's life cycle.
394
+ *
395
+ * It is possible to {@link module:ui/template~Template.extend} the {@link #template} before
396
+ * the view is rendered. To allow an early customization of the view (e.g. by its parent),
397
+ * such references should be done in `render()`.
398
+ *
399
+ * class SampleView extends View {
400
+ * constructor() {
401
+ * this.setTemplate( {
402
+ * // ...
403
+ * } );
404
+ * },
405
+ *
406
+ * render() {
407
+ * // View#element becomes available.
408
+ * super.render();
409
+ *
410
+ * // The "scroll" listener depends on #element.
411
+ * this.listenTo( window, 'scroll', () => {
412
+ * // A reference to #element would render the #template and make it non-extendable.
413
+ * if ( window.scrollY > 0 ) {
414
+ * this.element.scrollLeft = 100;
415
+ * } else {
416
+ * this.element.scrollLeft = 0;
417
+ * }
418
+ * } );
419
+ * }
420
+ * }
421
+ *
422
+ * const view = new SampleView();
423
+ *
424
+ * // Let's customize the view before it gets rendered.
425
+ * view.extendTemplate( {
426
+ * attributes: {
427
+ * class: [
428
+ * 'additional-class'
429
+ * ]
430
+ * }
431
+ * } );
432
+ *
433
+ * // Late rendering allows customization of the view.
434
+ * view.render();
435
+ */
436
+ render() {
437
+ if (this.isRendered) {
438
+ /**
439
+ * This View has already been rendered.
440
+ *
441
+ * @error ui-view-render-already-rendered
442
+ */
443
+ throw new CKEditorError('ui-view-render-already-rendered', this);
444
+ }
445
+ // Render #element of the view.
446
+ if (this.template) {
447
+ this.element = this.template.render();
448
+ // Auto–register view children from #template.
449
+ this.registerChild(this.template.getViews());
450
+ }
451
+ this.isRendered = true;
452
+ }
453
+ /**
454
+ * Recursively destroys the view instance and child views added by {@link #registerChild} and
455
+ * residing in collections created by the {@link #createCollection}.
456
+ *
457
+ * Destruction disables all event listeners:
458
+ * * created on the view, e.g. `view.on( 'event', () => {} )`,
459
+ * * defined in the {@link #template} for DOM events.
460
+ */
461
+ destroy() {
462
+ this.stopListening();
463
+ this._viewCollections.map(c => c.destroy());
464
+ // Template isn't obligatory for views.
465
+ if (this.template && this.template._revertData) {
466
+ this.template.revert(this.element);
467
+ }
468
+ }
507
469
  }
508
-
509
- mix( View, DomEmitterMixin );
510
- mix( View, ObservableMixin );