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

Commit 1588c82

Browse files
authored
Merge pull request #284 from ckeditor/t/283
Fix: The `TooltipView` should hide when the `TooltipView#text` is empty. The `ButtonView's` ability to get a tooltip should not be restricted after `View` initialization. Closes #283.
2 parents 1ce7c7f + 6402a5e commit 1588c82

File tree

4 files changed

+50
-70
lines changed

4 files changed

+50
-70
lines changed

src/button/buttonview.js

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -147,17 +147,18 @@ export default class ButtonView extends View {
147147
);
148148

149149
/**
150-
* Icon of the button view.
150+
* Tooltip of the button view.
151151
*
152152
* @readonly
153-
* @member {module:ui/icon/iconview~IconView} #iconView
153+
* @member {module:ui/tooltip/tooltipview~TooltipView} #tooltipView
154154
*/
155+
this.tooltipView = this._createTooltipView();
155156

156157
/**
157-
* Tooltip of the button view.
158+
* Icon of the button view.
158159
*
159160
* @readonly
160-
* @member {module:ui/tooltip/tooltipview~TooltipView} #tooltipView
161+
* @member {module:ui/icon/iconview~IconView} #iconView
161162
*/
162163

163164
const bind = this.bindTemplate;
@@ -190,7 +191,8 @@ export default class ButtonView extends View {
190191
text: bind.to( 'label' )
191192
}
192193
]
193-
}
194+
},
195+
this.tooltipView
194196
],
195197

196198
on: {
@@ -233,17 +235,6 @@ export default class ButtonView extends View {
233235
this.addChildren( iconView );
234236
}
235237

236-
if ( this.tooltip ) {
237-
const tooltipView = this.tooltipView = new TooltipView();
238-
239-
tooltipView.bind( 'text' ).to( this, '_tooltipString' );
240-
tooltipView.bind( 'position' ).to( this, 'tooltipPosition' );
241-
this.element.appendChild( tooltipView.element );
242-
243-
// Make sure the tooltip will be destroyed along with the button.
244-
this.addChildren( tooltipView );
245-
}
246-
247238
super.init();
248239
}
249240

@@ -254,6 +245,21 @@ export default class ButtonView extends View {
254245
this.element.focus();
255246
}
256247

248+
/**
249+
* Creates TooltipView instance and bind with button properties.
250+
*
251+
* @private
252+
* @returns {module:ui/tooltip/tooltipview~TooltipView}
253+
*/
254+
_createTooltipView() {
255+
const tooltipView = new TooltipView();
256+
257+
tooltipView.bind( 'text' ).to( this, '_tooltipString' );
258+
tooltipView.bind( 'position' ).to( this, 'tooltipPosition' );
259+
260+
return tooltipView;
261+
}
262+
257263
/**
258264
* Gets the text for the {@link #tooltipView} from the combination of
259265
* {@link #tooltip}, {@link #label} and {@link #keystroke} attributes.
@@ -277,12 +283,12 @@ export default class ButtonView extends View {
277283

278284
if ( tooltip instanceof Function ) {
279285
return tooltip( label, keystroke );
280-
} else if ( tooltip === true ) {
286+
} else {
281287
return `${ label }${ keystroke ? ` (${ keystroke })` : '' }`;
282288
}
283289
}
284290
}
285291

286-
return false;
292+
return '';
287293
}
288294
}

src/tooltip/tooltipview.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export default class TooltipView extends View {
2828
* @observable
2929
* @member {String} #text
3030
*/
31-
this.set( 'text' );
31+
this.set( 'text', '' );
3232

3333
/**
3434
* The position of the tooltip (south or north).
@@ -58,7 +58,8 @@ export default class TooltipView extends View {
5858
attributes: {
5959
class: [
6060
'ck-tooltip',
61-
bind.to( 'position', position => 'ck-tooltip_' + position )
61+
bind.to( 'position', position => 'ck-tooltip_' + position ),
62+
bind.if( 'text', 'ck-hidden', value => !value.trim() )
6263
]
6364
},
6465
children: [

tests/button/buttonview.js

Lines changed: 15 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -80,56 +80,12 @@ describe( 'ButtonView', () => {
8080
view = new ButtonView( locale );
8181
} );
8282

83-
it( 'is not initially set', () => {
84-
expect( view.tooltipView ).to.be.undefined;
85-
expect( view.element.childNodes ).to.have.length( 1 );
83+
it( 'is initially set', () => {
84+
expect( view.tooltipView ).to.be.instanceof( TooltipView );
85+
expect( Array.from( view.template.children ) ).to.include( view.tooltipView );
8686
} );
8787

88-
it( 'is not initially set (despite #label and #keystroke)', () => {
89-
view.label = 'foo';
90-
view.keystroke = 'A';
91-
view.init();
92-
93-
expect( view.tooltipView ).to.be.undefined;
94-
} );
95-
96-
it( 'is not set if neither `true`, String or Function', () => {
97-
view.label = 'foo';
98-
view.keystroke = 'A';
99-
view.tooltip = false;
100-
view.init();
101-
102-
expect( view.tooltipView ).to.be.undefined;
103-
104-
view.tooltip = 3;
105-
expect( view.tooltipView ).to.be.undefined;
106-
107-
view.tooltip = new Date();
108-
expect( view.tooltipView ).to.be.undefined;
109-
} );
110-
111-
it( 'when set, is added to the DOM', () => {
112-
view.tooltip = 'foo';
113-
view.icon = 'bar';
114-
view.init();
115-
116-
expect( view.element.childNodes ).to.have.length( 3 );
117-
expect( view.element.childNodes[ 2 ] ).to.equal( view.tooltipView.element );
118-
expect( view.tooltipView ).to.instanceOf( TooltipView );
119-
expect( view.tooltipView.position ).to.equal( 's' );
120-
} );
121-
122-
it( 'when set, is destroyed along with the view', () => {
123-
view.tooltip = 'foo';
124-
view.init();
125-
126-
const spy = sinon.spy( view.tooltipView, 'destroy' );
127-
128-
view.destroy();
129-
sinon.assert.calledOnce( spy );
130-
} );
131-
132-
it( 'when set, reacts to #tooltipPosition attribute', () => {
88+
it( 'it reacts to #tooltipPosition attribute', () => {
13389
view.tooltip = 'foo';
13490
view.icon = 'bar';
13591
view.init();
@@ -151,6 +107,15 @@ describe( 'ButtonView', () => {
151107
expect( view.tooltipView.text ).to.equal( 'bar (A)' );
152108
} );
153109

110+
it( 'not render tooltip text when #tooltip value is false', () => {
111+
view.tooltip = false;
112+
view.label = 'bar';
113+
view.keystroke = 'A';
114+
view.init();
115+
116+
expect( view.tooltipView.text ).to.equal( '' );
117+
} );
118+
154119
it( 'reacts to changes in #label and #keystroke', () => {
155120
view.tooltip = true;
156121
view.label = 'foo';
@@ -265,7 +230,7 @@ describe( 'ButtonView', () => {
265230

266231
describe( 'icon', () => {
267232
it( 'is not initially set', () => {
268-
expect( view.element.childNodes ).to.have.length( 1 );
233+
expect( view.element.childNodes ).to.have.length( 2 );
269234
expect( view.iconView ).to.be.undefined;
270235
} );
271236

@@ -274,7 +239,7 @@ describe( 'ButtonView', () => {
274239
view.icon = 'foo';
275240

276241
view.init();
277-
expect( view.element.childNodes ).to.have.length( 2 );
242+
expect( view.element.childNodes ).to.have.length( 3 );
278243
expect( view.element.childNodes[ 0 ] ).to.equal( view.iconView.element );
279244

280245
expect( view.iconView ).to.instanceOf( IconView );

tests/tooltip/tooltipview.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ describe( 'TooltipView', () => {
4444
} );
4545

4646
describe( 'class', () => {
47+
it( 'should react on view#text', () => {
48+
expect( view.element.classList.contains( 'ck-hidden' ) ).to.be.false;
49+
50+
view.text = '';
51+
52+
expect( view.element.classList.contains( 'ck-hidden' ) ).to.be.true;
53+
} );
54+
4755
it( 'should react on view#position', () => {
4856
expect( view.element.classList.contains( 'ck-tooltip_n' ) ).to.be.false;
4957
expect( view.element.classList.contains( 'ck-tooltip_s' ) ).to.be.true;

0 commit comments

Comments
 (0)