Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 313ef25

Browse files
committed
[poc] updatemenus split pushMargin from draw
1 parent d943619 commit 313ef25

File tree

2 files changed

+54
-71
lines changed

2 files changed

+54
-71
lines changed

src/components/updatemenus/draw.js

Lines changed: 52 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@ var svgTextUtils = require('../../lib/svg_text_utils');
1919
var arrayEditor = require('../../plot_api/plot_template').arrayEditor;
2020

2121
var LINE_SPACING = require('../../constants/alignment').LINE_SPACING;
22+
var FROM_TL = require('../../constants/alignment').FROM_TL;
23+
var FROM_BR = require('../../constants/alignment').FROM_BR;
2224

2325
var constants = require('./constants');
2426
var ScrollBox = require('./scrollbox');
2527

26-
module.exports = function draw(gd) {
28+
function draw(gd) {
2729
var fullLayout = gd._fullLayout;
2830
var menuData = Lib.filterVisible(fullLayout[constants.name]);
31+
var gs = fullLayout._size;
2932

3033
/* Update menu data is bound to the header-group.
3134
* The items in the header group are always present.
@@ -54,10 +57,6 @@ module.exports = function draw(gd) {
5457
* ...
5558
*/
5659

57-
function clearAutoMargin(menuOpts) {
58-
Plots.autoMargin(gd, autoMarginId(menuOpts));
59-
}
60-
6160
// draw update menu container
6261
var menus = fullLayout._menulayer
6362
.selectAll('g.' + constants.containerClassName)
@@ -67,15 +66,7 @@ module.exports = function draw(gd) {
6766
.classed(constants.containerClassName, true)
6867
.style('cursor', 'pointer');
6968

70-
menus.exit().each(function() {
71-
// Most components don't need to explicitly remove autoMargin, because
72-
// marginPushers does this - but updatemenu updates don't go through
73-
// a full replot so we need to explicitly remove it.
74-
// This is for removing *all* updatemenus, removing individuals is
75-
// handled below, in headerGroups.exit
76-
d3.select(this).selectAll('g.' + constants.headerGroupClassName)
77-
.each(clearAutoMargin);
78-
}).remove();
69+
menus.exit().remove();
7970

8071
// return early if no update menus are visible
8172
if(menuData.length === 0) return;
@@ -92,31 +83,35 @@ module.exports = function draw(gd) {
9283
s.style('pointer-events', 'all');
9384
});
9485

95-
// find dimensions before plotting anything (this mutates menuOpts)
96-
for(var i = 0; i < menuData.length; i++) {
97-
var menuOpts = menuData[i];
98-
findDimensions(gd, menuOpts);
99-
}
100-
10186
// setup scrollbox
10287
var scrollBoxId = 'updatemenus' + fullLayout._uid;
10388
var scrollBox = new ScrollBox(gd, gButton, scrollBoxId);
10489

105-
// remove exiting header, remove dropped buttons and reset margins
90+
// remove exiting header and remove dropped buttons
10691
if(headerGroups.enter().size()) {
10792
// make sure gButton is on top of all headers
10893
gButton.node().parentNode.appendChild(gButton.node());
10994
gButton.call(removeAllButtons);
11095
}
11196

112-
headerGroups.exit().each(function(menuOpts) {
97+
if(headerGroups.exit().size()) {
11398
gButton.call(removeAllButtons);
114-
clearAutoMargin(menuOpts);
115-
}).remove();
99+
}
100+
headerGroups.exit().remove();
116101

117102
// draw headers!
118103
headerGroups.each(function(menuOpts) {
119104
var gHeader = d3.select(this);
105+
var dims = menuOpts._dims;
106+
107+
dims.lx = Math.round(
108+
gs.l + gs.w * menuOpts.x -
109+
dims.paddedWidth * FROM_TL[Lib.getXanchor(menuOpts)]
110+
);
111+
dims.ly = Math.round(
112+
gs.t + gs.h * (1 - menuOpts.y) -
113+
dims.paddedHeight * FROM_TL[Lib.getYanchor(menuOpts)]
114+
);
120115

121116
var _gButton = menuOpts.type === 'dropdown' ? gButton : null;
122117
Plots.manageCommandObserver(gd, menuOpts, menuOpts.buttons, function(data) {
@@ -134,7 +129,7 @@ module.exports = function draw(gd) {
134129
drawButtons(gd, gHeader, null, null, menuOpts);
135130
}
136131
});
137-
};
132+
}
138133

139134
// Note that '_index' is set at the default step,
140135
// it corresponds to the menu index in the user layout update menu container.
@@ -469,7 +464,6 @@ function styleOnMouseOut(item, menuOpts) {
469464
.call(Color.fill, menuOpts.bgcolor);
470465
}
471466

472-
// find item dimensions (this mutates menuOpts)
473467
function findDimensions(gd, menuOpts) {
474468
var dims = menuOpts._dims = {
475469
width1: 0,
@@ -478,10 +472,10 @@ function findDimensions(gd, menuOpts) {
478472
widths: [],
479473
totalWidth: 0,
480474
totalHeight: 0,
475+
paddedWidth: 0,
476+
paddedHeight: 0,
481477
openWidth: 0,
482-
openHeight: 0,
483-
lx: 0,
484-
ly: 0
478+
openHeight: 0
485479
};
486480

487481
var fakeButtons = Drawing.tester.selectAll('g.' + constants.dropdownButtonClassName)
@@ -540,7 +534,6 @@ function findDimensions(gd, menuOpts) {
540534
dims.totalWidth -= constants.gapButton;
541535
}
542536

543-
544537
dims.headerWidth = dims.width1 + constants.arrowPadX;
545538
dims.headerHeight = dims.height1;
546539

@@ -556,50 +549,13 @@ function findDimensions(gd, menuOpts) {
556549

557550
fakeButtons.remove();
558551

559-
var paddedWidth = dims.totalWidth + menuOpts.pad.l + menuOpts.pad.r;
560-
var paddedHeight = dims.totalHeight + menuOpts.pad.t + menuOpts.pad.b;
561-
562-
var graphSize = gd._fullLayout._size;
563-
dims.lx = graphSize.l + graphSize.w * menuOpts.x;
564-
dims.ly = graphSize.t + graphSize.h * (1 - menuOpts.y);
565-
566-
var xanchor = 'left';
567-
if(Lib.isRightAnchor(menuOpts)) {
568-
dims.lx -= paddedWidth;
569-
xanchor = 'right';
570-
}
571-
if(Lib.isCenterAnchor(menuOpts)) {
572-
dims.lx -= paddedWidth / 2;
573-
xanchor = 'center';
574-
}
575-
576-
var yanchor = 'top';
577-
if(Lib.isBottomAnchor(menuOpts)) {
578-
dims.ly -= paddedHeight;
579-
yanchor = 'bottom';
580-
}
581-
if(Lib.isMiddleAnchor(menuOpts)) {
582-
dims.ly -= paddedHeight / 2;
583-
yanchor = 'middle';
584-
}
552+
dims.paddedWidth = dims.totalWidth + menuOpts.pad.l + menuOpts.pad.r;
553+
dims.paddedHeight = dims.totalHeight + menuOpts.pad.t + menuOpts.pad.b;
585554

586555
dims.totalWidth = Math.ceil(dims.totalWidth);
587556
dims.totalHeight = Math.ceil(dims.totalHeight);
588-
dims.lx = Math.round(dims.lx);
589-
dims.ly = Math.round(dims.ly);
590-
591-
Plots.autoMargin(gd, autoMarginId(menuOpts), {
592-
x: menuOpts.x,
593-
y: menuOpts.y,
594-
l: paddedWidth * ({right: 1, center: 0.5}[xanchor] || 0),
595-
r: paddedWidth * ({left: 1, center: 0.5}[xanchor] || 0),
596-
b: paddedHeight * ({top: 1, middle: 0.5}[yanchor] || 0),
597-
t: paddedHeight * ({bottom: 1, middle: 0.5}[yanchor] || 0)
598-
});
599-
}
600557

601-
function autoMarginId(menuOpts) {
602-
return constants.autoMarginIdRoot + menuOpts._index;
558+
return dims;
603559
}
604560

605561
// set item positions (mutates posOpts)
@@ -644,3 +600,29 @@ function removeAllButtons(gButton, newMenuIndexAttr) {
644600
.attr(constants.menuIndexAttrName, newMenuIndexAttr || '-1')
645601
.selectAll('g.' + constants.dropdownButtonClassName).remove();
646602
}
603+
604+
function pushMargin(gd) {
605+
var fullLayout = gd._fullLayout;
606+
var menuData = Lib.filterVisible(fullLayout[constants.name]);
607+
608+
for(var i = 0; i < menuData.length; i++) {
609+
var menuOpts = menuData[i];
610+
var xanchor = Lib.getXanchor(menuOpts);
611+
var yanchor = Lib.getYanchor(menuOpts);
612+
var dims = findDimensions(gd, menuOpts);
613+
614+
Plots.autoMargin(gd, constants.autoMarginIdRoot + menuOpts._index, {
615+
x: menuOpts.x,
616+
y: menuOpts.y,
617+
l: dims.paddedWidth * FROM_TL[xanchor],
618+
r: dims.paddedWidth * FROM_BR[xanchor],
619+
b: dims.paddedHeight * FROM_BR[yanchor],
620+
t: dims.paddedHeight * FROM_TL[yanchor]
621+
});
622+
}
623+
}
624+
625+
module.exports = {
626+
pushMargin: pushMargin,
627+
draw: draw
628+
};

src/components/updatemenus/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ module.exports = {
1717
layoutAttributes: require('./attributes'),
1818
supplyLayoutDefaults: require('./defaults'),
1919

20-
draw: require('./draw')
20+
pushMargin: require('./draw').pushMargin,
21+
draw: require('./draw').draw
2122
};

0 commit comments

Comments
 (0)