Skip to content

Commit 11a4e27

Browse files
committed
make a separate function to coerce clickmode and hovermode
1 parent 9c07a91 commit 11a4e27

File tree

2 files changed

+59
-35
lines changed

2 files changed

+59
-35
lines changed
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* Copyright 2012-2020, 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+
var Lib = require('../../lib');
12+
var layoutAttributes = require('./layout_attributes');
13+
14+
module.exports = function handleHoverModeDefaults(layoutIn, layoutOut, fullData) {
15+
function coerce(attr, dflt) {
16+
// don't coerce if it is already coerced in other place e.g. in cartesian defaults
17+
if(layoutOut[attr] !== undefined) return layoutOut[attr];
18+
19+
return Lib.coerce(layoutIn, layoutOut, layoutAttributes, attr, dflt);
20+
}
21+
22+
var clickmode = coerce('clickmode');
23+
24+
var hovermodeDflt;
25+
if(layoutOut._has('cartesian')) {
26+
if(clickmode.indexOf('select') > -1) {
27+
hovermodeDflt = 'closest';
28+
} else {
29+
// flag for 'horizontal' plots:
30+
// determines the state of the mode bar 'compare' hovermode button
31+
layoutOut._isHoriz = isHoriz(fullData, layoutOut);
32+
hovermodeDflt = layoutOut._isHoriz ? 'y' : 'x';
33+
}
34+
} else hovermodeDflt = 'closest';
35+
36+
return coerce('hovermode', hovermodeDflt);
37+
};
38+
39+
function isHoriz(fullData, fullLayout) {
40+
var stackOpts = fullLayout._scatterStackOpts || {};
41+
42+
for(var i = 0; i < fullData.length; i++) {
43+
var trace = fullData[i];
44+
var subplot = trace.xaxis + trace.yaxis;
45+
var subplotStackOpts = stackOpts[subplot] || {};
46+
var groupOpts = subplotStackOpts[trace.stackgroup] || {};
47+
48+
if(trace.orientation !== 'h' && groupOpts.orientation !== 'h') {
49+
return false;
50+
}
51+
}
52+
53+
return true;
54+
}

src/components/fx/layout_defaults.js

+5-35
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,22 @@
1111
var Lib = require('../../lib');
1212
var isUnifiedHover = require('./helpers').isUnifiedHover;
1313
var layoutAttributes = require('./layout_attributes');
14+
var handleHoverModeDefaults = require('./hovermode_defaults');
1415

1516
module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, fullData) {
1617
function coerce(attr, dflt) {
1718
return Lib.coerce(layoutIn, layoutOut, layoutAttributes, attr, dflt);
1819
}
1920

20-
var clickmode = coerce('clickmode');
21-
22-
var dragMode = coerce('dragmode');
23-
if(dragMode === 'select') coerce('selectdirection');
24-
25-
var hovermodeDflt;
26-
if(layoutOut._has('cartesian')) {
27-
if(clickmode.indexOf('select') > -1) {
28-
hovermodeDflt = 'closest';
29-
} else {
30-
// flag for 'horizontal' plots:
31-
// determines the state of the mode bar 'compare' hovermode button
32-
layoutOut._isHoriz = isHoriz(fullData, layoutOut);
33-
hovermodeDflt = layoutOut._isHoriz ? 'y' : 'x';
34-
}
35-
} else hovermodeDflt = 'closest';
36-
37-
var hoverMode = coerce('hovermode', hovermodeDflt);
21+
var hoverMode = handleHoverModeDefaults(layoutIn, layoutOut, fullData);
3822
if(hoverMode) {
3923
coerce('hoverdistance');
4024
coerce('spikedistance', isUnifiedHover(hoverMode) ? -1 : undefined);
4125
}
4226

27+
var dragMode = coerce('dragmode');
28+
if(dragMode === 'select') coerce('selectdirection');
29+
4330
// if only mapbox or geo subplots is present on graph,
4431
// reset 'zoom' dragmode to 'pan' until 'zoom' is implemented,
4532
// so that the correct modebar button is active
@@ -54,20 +41,3 @@ module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, fullData) {
5441
layoutOut.dragmode = 'pan';
5542
}
5643
};
57-
58-
function isHoriz(fullData, fullLayout) {
59-
var stackOpts = fullLayout._scatterStackOpts || {};
60-
61-
for(var i = 0; i < fullData.length; i++) {
62-
var trace = fullData[i];
63-
var subplot = trace.xaxis + trace.yaxis;
64-
var subplotStackOpts = stackOpts[subplot] || {};
65-
var groupOpts = subplotStackOpts[trace.stackgroup] || {};
66-
67-
if(trace.orientation !== 'h' && groupOpts.orientation !== 'h') {
68-
return false;
69-
}
70-
}
71-
72-
return true;
73-
}

0 commit comments

Comments
 (0)