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

Commit bf90ad5

Browse files
authored
Merge pull request #332 from ckeditor/t/262
Other: Implemented `View#render` method which replaces the `#element` rendering upon the first reference and incorporates the `#init` method functionality. Closes #262. Closes #302. From now on `View#setTemplate` and `View#extendTemplate` methods are recommended as a shorthand for `view.template = new Template( { ... } )` and `Template.extend( view.template )`. BREAKING CHANGE: The `init()` method in various UI components has been renamed to `render()`. Please refer to the documentation to learn more. BREAKING CHANGE: The `View#element` is no longer a getter which renders an element when first referenced. Use the `View#render()` method instead. BREAKING CHANGE: `Template#children` property became an `Array` (previously `ViewCollection`). BREAKING CHANGE: `View#addChildren` and `View#removeChildren` methods became `#registerChildren` and `#deregisterChildren`. BREAKING CHANGE: The DOM structure of the `StickyPanelView` has changed along with the class names. Please refer to the component documentation to learn more.
2 parents 94417e9 + 64c7ad5 commit bf90ad5

File tree

69 files changed

+979
-923
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+979
-923
lines changed

docs/_snippets/examples/bootstrap-ui-inner.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ export default class BootstrapEditor extends StandardEditor {
5959

6060
resolve(
6161
editor.initPlugins()
62-
// Initialize the editable view in DOM first.
63-
.then( () => editable.init() )
62+
// Render the editable view in DOM first.
63+
.then( () => editable.render() )
6464
// Replace the editor#element with editor.editable#element.
6565
.then( () => editor._elementReplacer.replace( element, editable.element ) )
6666
// Handle the UI of the editor.

docs/framework/guides/external-ui.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ export default class BootstrapEditor extends StandardEditor {
8383

8484
resolve(
8585
editor.initPlugins()
86-
// Initialize the editable view in DOM first.
87-
.then( () => editable.init() )
86+
// Render the editable view in DOM first.
87+
.then( () => editable.render() )
8888
// Replace the editor#element with editor.editable#element.
8989
.then( () => editor._elementReplacer.replace( element, editable.element ) )
9090
// Handle the UI of the editor.

src/bindings/preventdefault.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*
1818
* // ...
1919
*
20-
* this.template = new Template( {
20+
* this.setTemplate( {
2121
* tag: 'div',
2222
*
2323
* on: {

src/button/buttonview.js

+61-34
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
*/
99

1010
import View from '../view';
11-
import Template from '../template';
1211
import IconView from '../icon/iconview';
1312
import TooltipView from '../tooltip/tooltipview';
1413

@@ -26,7 +25,7 @@ import { getEnvKeystrokeText } from '@ckeditor/ckeditor5-utils/src/keyboard';
2625
* withText: true
2726
* } );
2827
*
29-
* view.init();
28+
* view.render();
3029
*
3130
* document.body.append( view.element );
3231
*
@@ -39,6 +38,8 @@ export default class ButtonView extends View {
3938
constructor( locale ) {
4039
super( locale );
4140

41+
const bind = this.bindTemplate;
42+
4243
/**
4344
* The label of the button view visible to the user when {@link #withText} is `true`.
4445
* It can also be used to create a {@link #tooltip}.
@@ -151,6 +152,30 @@ export default class ButtonView extends View {
151152
*/
152153
this.set( 'tabindex', -1 );
153154

155+
/**
156+
* Collection of the child views inside of the button {@link #element}.
157+
*
158+
* @readonly
159+
* @member {module:ui/viewcollection~ViewCollection}
160+
*/
161+
this.children = this.createCollection();
162+
163+
/**
164+
* Tooltip of the button view. It is configurable using the {@link #tooltip tooltip attribute}.
165+
*
166+
* @readonly
167+
* @member {module:ui/tooltip/tooltipview~TooltipView} #tooltipView
168+
*/
169+
this.tooltipView = this._createTooltipView();
170+
171+
/**
172+
* Label of the button view. It is configurable using the {@link #label label attribute}.
173+
*
174+
* @readonly
175+
* @member {module:ui/view~View} #labelView
176+
*/
177+
this.labelView = this._createLabelView();
178+
154179
/**
155180
* Tooltip of the button bound to the template.
156181
*
@@ -167,24 +192,14 @@ export default class ButtonView extends View {
167192
this._getTooltipString.bind( this )
168193
);
169194

170-
/**
171-
* Tooltip of the button view. It is configurable using the {@link #tooltip tooltip attribute}.
172-
*
173-
* @readonly
174-
* @member {module:ui/tooltip/tooltipview~TooltipView} #tooltipView
175-
*/
176-
this.tooltipView = this._createTooltipView();
177-
178195
/**
179196
* (Optional) The icon view of the button. Only present when the {@link #icon icon attribute} is defined.
180197
*
181198
* @readonly
182199
* @member {module:ui/icon/iconview~IconView} #iconView
183200
*/
184201

185-
const bind = this.bindTemplate;
186-
187-
this.template = new Template( {
202+
this.setTemplate( {
188203
tag: 'button',
189204

190205
attributes: {
@@ -199,22 +214,7 @@ export default class ButtonView extends View {
199214
tabindex: bind.to( 'tabindex' )
200215
},
201216

202-
children: [
203-
{
204-
tag: 'span',
205-
206-
attributes: {
207-
class: [ 'ck-button__label' ]
208-
},
209-
210-
children: [
211-
{
212-
text: bind.to( 'label' )
213-
}
214-
]
215-
},
216-
this.tooltipView
217-
],
217+
children: this.children,
218218

219219
on: {
220220
mousedown: bind.to( evt => {
@@ -246,18 +246,19 @@ export default class ButtonView extends View {
246246
/**
247247
* @inheritDoc
248248
*/
249-
init() {
249+
render() {
250+
super.render();
251+
250252
if ( this.icon ) {
251253
const iconView = this.iconView = new IconView();
252254

253255
iconView.bind( 'content' ).to( this, 'icon' );
254-
this.element.insertBefore( iconView.element, this.element.firstChild );
255256

256-
// Make sure the icon will be destroyed along with the button.
257-
this.addChildren( iconView );
257+
this.children.add( iconView );
258258
}
259259

260-
super.init();
260+
this.children.add( this.tooltipView );
261+
this.children.add( this.labelView );
261262
}
262263

263264
/**
@@ -283,6 +284,32 @@ export default class ButtonView extends View {
283284
return tooltipView;
284285
}
285286

287+
/**
288+
* Creates a label view instance and binds it with button attributes.
289+
*
290+
* @private
291+
* @returns {module:ui/view~View}
292+
*/
293+
_createLabelView() {
294+
const labelView = new View();
295+
296+
labelView.setTemplate( {
297+
tag: 'span',
298+
299+
attributes: {
300+
class: [ 'ck-button__label' ]
301+
},
302+
303+
children: [
304+
{
305+
text: this.bindTemplate.to( 'label' )
306+
}
307+
]
308+
} );
309+
310+
return labelView;
311+
}
312+
286313
/**
287314
* Gets the text for the {@link #tooltipView} from the combination of
288315
* {@link #tooltip}, {@link #label} and {@link #keystroke} attributes.

src/dropdown/createdropdown.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import DropdownPanelView from './dropdownpanelview';
2424
*
2525
* const dropdown = createDropdown( model );
2626
*
27-
* dropdown.init();
27+
* dropdown.render();
2828
*
2929
* // Will render a dropdown labeled "A dropdown" with an empty panel.
3030
* document.body.appendChild( dropdown.element );

src/dropdown/dropdownpanelview.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
*/
99

1010
import View from '../view';
11-
import Template from '../template';
1211

1312
/**
1413
* The dropdown panel view class.
@@ -46,7 +45,7 @@ export default class DropdownPanelView extends View {
4645
*/
4746
this.children = this.createCollection();
4847

49-
this.template = new Template( {
48+
this.setTemplate( {
5049
tag: 'div',
5150

5251
attributes: {

src/dropdown/dropdownview.js

+12-13
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
*/
99

1010
import View from '../view';
11-
import Template from '../template';
1211
import FocusTracker from '@ckeditor/ckeditor5-utils/src/focustracker';
1312
import KeystrokeHandler from '@ckeditor/ckeditor5-utils/src/keystrokehandler';
1413

@@ -25,7 +24,7 @@ import KeystrokeHandler from '@ckeditor/ckeditor5-utils/src/keystrokehandler';
2524
* withText: true
2625
* } );
2726
*
28-
* dropdown.init();
27+
* dropdown.render();
2928
*
3029
* // Will render a dropdown with a panel containing a "Content of the panel" text.
3130
* document.body.appendChild( dropdown.element );
@@ -46,7 +45,7 @@ export default class DropdownView extends View {
4645
// Extend button's template before it's registered as a child of the dropdown because
4746
// by doing so, its #element is rendered and any post–render template extension will
4847
// not be reflected in DOM.
49-
Template.extend( buttonView.template, {
48+
buttonView.extendTemplate( {
5049
attributes: {
5150
class: [
5251
'ck-dropdown__button'
@@ -106,7 +105,7 @@ export default class DropdownView extends View {
106105
*/
107106
this.keystrokes = new KeystrokeHandler();
108107

109-
this.template = new Template( {
108+
this.setTemplate( {
110109
tag: 'div',
111110

112111
attributes: {
@@ -120,20 +119,22 @@ export default class DropdownView extends View {
120119
panelView
121120
]
122121
} );
122+
}
123+
124+
/**
125+
* @inheritDoc
126+
*/
127+
render() {
128+
super.render();
123129

124130
// Toggle the the dropdown when it's button has been clicked.
125-
this.listenTo( buttonView, 'execute', () => {
131+
this.listenTo( this.buttonView, 'execute', () => {
126132
this.isOpen = !this.isOpen;
127133
} );
128134

129135
// Toggle the visibility of the panel when the dropdown becomes open.
130-
panelView.bind( 'isVisible' ).to( this, 'isOpen' );
131-
}
136+
this.panelView.bind( 'isVisible' ).to( this, 'isOpen' );
132137

133-
/**
134-
* @inheritDoc
135-
*/
136-
init() {
137138
// Listen for keystrokes coming from within #element.
138139
this.keystrokes.listenTo( this.element );
139140

@@ -167,8 +168,6 @@ export default class DropdownView extends View {
167168
// Close the dropdown using the arrow left/escape key.
168169
this.keystrokes.set( 'arrowleft', closeDropdown );
169170
this.keystrokes.set( 'esc', closeDropdown );
170-
171-
super.init();
172171
}
173172

174173
/**

src/dropdown/list/createlistdropdown.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,9 @@ import createDropdown from '../createdropdown';
3131
*
3232
* const dropdown = createListDropdown( model, locale );
3333
*
34-
* dropdown.init();
35-
*
3634
* // Will render a dropdown labeled "A dropdown" with a list in the panel
3735
* // containing two items.
36+
* dropdown.render()
3837
* document.body.appendChild( dropdown.element );
3938
*
4039
* The model instance remains in control of the dropdown after it has been created. E.g. changes to the
@@ -54,7 +53,6 @@ import createDropdown from '../createdropdown';
5453
*/
5554
export default function createListDropdown( model, locale ) {
5655
const dropdownView = createDropdown( model, locale );
57-
5856
const listView = dropdownView.listView = new ListView( locale );
5957

6058
listView.items.bindTo( model.items ).using( itemModel => {

src/editableui/editableuiview.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
*/
99

1010
import View from '../view';
11-
import Template from '../template';
1211

1312
/**
1413
* The editable UI view class.
@@ -32,7 +31,7 @@ export default class EditableUIView extends View {
3231
this.element = this.editableElement = editableElement;
3332
}
3433

35-
this.template = new Template( {
34+
this.setTemplate( {
3635
tag: 'div',
3736
attributes: {
3837
class: [
@@ -77,17 +76,17 @@ export default class EditableUIView extends View {
7776
}
7877

7978
/**
80-
* Initializes the view by either applying the {@link #template} to the existing
79+
* Renders the view by either applying the {@link #template} to the existing
8180
* {@link #editableElement} or assigning {@link #element} as {@link #editableElement}.
8281
*/
83-
init() {
82+
render() {
83+
super.render();
84+
8485
if ( this.externalElement ) {
85-
this.template.apply( this.externalElement );
86+
this.template.apply( this.element = this.externalElement );
8687
} else {
8788
this.editableElement = this.element;
8889
}
89-
90-
super.init();
9190
}
9291

9392
/**

src/editableui/inline/inlineeditableuiview.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
*/
99

1010
import EditableUIView from '../../editableui/editableuiview';
11-
import Template from '../../template';
1211

1312
/**
1413
* The inline editable UI class implementing an inline {@link module:ui/editableui/editableuiview~EditableUIView}.
@@ -42,7 +41,7 @@ export default class InlineEditableUIView extends EditableUIView {
4241
return t( 'Rich Text Editor, %0', [ value ] );
4342
};
4443

45-
Template.extend( this.template, {
44+
this.extendTemplate( {
4645
attributes: {
4746
role: 'textbox',
4847
'aria-label': bind.to( 'name', getLabel ),

src/editorui/boxed/boxededitoruiview.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
import EditorUIView from '../../editorui/editoruiview';
1111
import uid from '@ckeditor/ckeditor5-utils/src/uid';
12-
import Template from '../../template';
1312

1413
/**
1514
* The boxed editor UI view class. This class represents an editor interface
@@ -47,7 +46,7 @@ export default class BoxedEditorUIView extends EditorUIView {
4746
*/
4847
this.main = this.createCollection();
4948

50-
this.template = new Template( {
49+
this.setTemplate( {
5150
tag: 'div',
5251

5352
attributes: {

0 commit comments

Comments
 (0)