Skip to content

Commit dc76217

Browse files
authoredFeb 3, 2017
Merge pull request #1351 from plotly/vanishing-titles
Vanishing titles
2 parents 4695758 + 8c72936 commit dc76217

File tree

3 files changed

+146
-4
lines changed

3 files changed

+146
-4
lines changed
 

‎src/components/titles/index.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ var Lib = require('../../lib');
1818
var Drawing = require('../drawing');
1919
var Color = require('../color');
2020
var svgTextUtils = require('../../lib/svg_text_utils');
21+
var interactConstants = require('../../constants/interactions');
2122

2223

2324
var Titles = module.exports = {};
@@ -192,21 +193,21 @@ Titles.draw = function(gd, titleClass, options) {
192193
opacity = 0;
193194
isplaceholder = true;
194195
txt = placeholderText;
195-
fullLayout._infolayer.select('.' + titleClass)
196-
.attr({'data-unformatted': txt})
196+
el.attr({'data-unformatted': txt})
197197
.text(txt)
198198
.on('mouseover.opacity', function() {
199199
d3.select(this).transition()
200-
.duration(100).style('opacity', 1);
200+
.duration(interactConstants.SHOW_PLACEHOLDER).style('opacity', 1);
201201
})
202202
.on('mouseout.opacity', function() {
203203
d3.select(this).transition()
204-
.duration(1000).style('opacity', 0);
204+
.duration(interactConstants.HIDE_PLACEHOLDER).style('opacity', 0);
205205
});
206206
}
207207

208208
if(gd._context.editable) {
209209
if(!txt) setPlaceholder();
210+
else el.on('.opacity', null);
210211

211212
el.call(svgTextUtils.makeEditable)
212213
.on('edit', function(text) {

‎src/constants/interactions.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Copyright 2012-2017, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
'use strict';
10+
11+
12+
module.exports = {
13+
/**
14+
* Timing information for interactive elements
15+
*/
16+
SHOW_PLACEHOLDER: 100,
17+
HIDE_PLACEHOLDER: 1000
18+
};

‎test/jasmine/tests/titles_test.js

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
var d3 = require('d3');
2+
3+
var Plotly = require('@lib/index');
4+
var interactConstants = require('@src/constants/interactions');
5+
6+
var createGraphDiv = require('../assets/create_graph_div');
7+
var destroyGraphDiv = require('../assets/destroy_graph_div');
8+
var mouseEvent = require('../assets/mouse_event');
9+
10+
describe('editable titles', function() {
11+
'use strict';
12+
13+
var data = [{x: [1, 2, 3], y: [1, 2, 3]}];
14+
15+
var gd;
16+
17+
afterEach(destroyGraphDiv);
18+
19+
beforeEach(function() {
20+
gd = createGraphDiv();
21+
});
22+
23+
function checkTitle(letter, text, opacityOut, opacityIn) {
24+
var titleEl = d3.select('.' + letter + 'title');
25+
expect(titleEl.text()).toBe(text);
26+
expect(+titleEl.style('opacity')).toBe(opacityOut);
27+
28+
var bb = titleEl.node().getBoundingClientRect(),
29+
xCenter = (bb.left + bb.right) / 2,
30+
yCenter = (bb.top + bb.bottom) / 2,
31+
done,
32+
promise = new Promise(function(resolve) { done = resolve; });
33+
34+
mouseEvent('mouseover', xCenter, yCenter);
35+
setTimeout(function() {
36+
expect(+titleEl.style('opacity')).toBe(opacityIn);
37+
38+
mouseEvent('mouseout', xCenter, yCenter);
39+
setTimeout(function() {
40+
expect(+titleEl.style('opacity')).toBe(opacityOut);
41+
done();
42+
}, interactConstants.HIDE_PLACEHOLDER + 50);
43+
}, interactConstants.SHOW_PLACEHOLDER + 50);
44+
45+
return promise;
46+
}
47+
48+
function editTitle(letter, attr, text) {
49+
return new Promise(function(resolve) {
50+
gd.once('plotly_relayout', function(eventData) {
51+
expect(eventData[attr]).toEqual(text, [letter, attr, eventData]);
52+
setTimeout(resolve, 10);
53+
});
54+
55+
var textNode = document.querySelector('.' + letter + 'title');
56+
textNode.dispatchEvent(new window.MouseEvent('click'));
57+
58+
var editNode = document.querySelector('.plugin-editable.editable');
59+
editNode.dispatchEvent(new window.FocusEvent('focus'));
60+
editNode.textContent = text;
61+
editNode.dispatchEvent(new window.FocusEvent('focus'));
62+
editNode.dispatchEvent(new window.FocusEvent('blur'));
63+
});
64+
}
65+
66+
it('shows default titles semi-opaque with no hover effects', function(done) {
67+
Plotly.plot(gd, data, {}, {editable: true})
68+
.then(function() {
69+
return Promise.all([
70+
// Check all three titles in parallel. This only works because
71+
// we're using synthetic events, not a real mouse. It's a big
72+
// win though because the test takes 1.2 seconds with the
73+
// animations...
74+
checkTitle('x', 'Click to enter X axis title', 0.2, 0.2),
75+
checkTitle('y', 'Click to enter Y axis title', 0.2, 0.2),
76+
checkTitle('g', 'Click to enter Plot title', 0.2, 0.2)
77+
]);
78+
})
79+
.then(done);
80+
});
81+
82+
it('has hover effects for blank titles', function(done) {
83+
Plotly.plot(gd, data, {
84+
xaxis: {title: ''},
85+
yaxis: {title: ''},
86+
title: ''
87+
}, {editable: true})
88+
.then(function() {
89+
return Promise.all([
90+
checkTitle('x', 'Click to enter X axis title', 0, 1),
91+
checkTitle('y', 'Click to enter Y axis title', 0, 1),
92+
checkTitle('g', 'Click to enter Plot title', 0, 1)
93+
]);
94+
})
95+
.then(done);
96+
});
97+
98+
it('has no hover effects for titles that used to be blank', function(done) {
99+
Plotly.plot(gd, data, {
100+
xaxis: {title: ''},
101+
yaxis: {title: ''},
102+
title: ''
103+
}, {editable: true})
104+
.then(function() {
105+
return editTitle('x', 'xaxis.title', 'XXX');
106+
})
107+
.then(function() {
108+
return editTitle('y', 'yaxis.title', 'YYY');
109+
})
110+
.then(function() {
111+
return editTitle('g', 'title', 'TTT');
112+
})
113+
.then(function() {
114+
return Promise.all([
115+
checkTitle('x', 'XXX', 1, 1),
116+
checkTitle('y', 'YYY', 1, 1),
117+
checkTitle('g', 'TTT', 1, 1)
118+
]);
119+
})
120+
.then(done);
121+
});
122+
123+
});

0 commit comments

Comments
 (0)
Please sign in to comment.