@@ -19,13 +19,16 @@ var svgTextUtils = require('../../lib/svg_text_utils');
19
19
var arrayEditor = require ( '../../plot_api/plot_template' ) . arrayEditor ;
20
20
21
21
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 ;
22
24
23
25
var constants = require ( './constants' ) ;
24
26
var ScrollBox = require ( './scrollbox' ) ;
25
27
26
- module . exports = function draw ( gd ) {
28
+ function draw ( gd ) {
27
29
var fullLayout = gd . _fullLayout ;
28
30
var menuData = Lib . filterVisible ( fullLayout [ constants . name ] ) ;
31
+ var gs = fullLayout . _size ;
29
32
30
33
/* Update menu data is bound to the header-group.
31
34
* The items in the header group are always present.
@@ -54,10 +57,6 @@ module.exports = function draw(gd) {
54
57
* ...
55
58
*/
56
59
57
- function clearAutoMargin ( menuOpts ) {
58
- Plots . autoMargin ( gd , autoMarginId ( menuOpts ) ) ;
59
- }
60
-
61
60
// draw update menu container
62
61
var menus = fullLayout . _menulayer
63
62
. selectAll ( 'g.' + constants . containerClassName )
@@ -67,15 +66,7 @@ module.exports = function draw(gd) {
67
66
. classed ( constants . containerClassName , true )
68
67
. style ( 'cursor' , 'pointer' ) ;
69
68
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 ( ) ;
79
70
80
71
// return early if no update menus are visible
81
72
if ( menuData . length === 0 ) return ;
@@ -92,31 +83,35 @@ module.exports = function draw(gd) {
92
83
s . style ( 'pointer-events' , 'all' ) ;
93
84
} ) ;
94
85
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
-
101
86
// setup scrollbox
102
87
var scrollBoxId = 'updatemenus' + fullLayout . _uid ;
103
88
var scrollBox = new ScrollBox ( gd , gButton , scrollBoxId ) ;
104
89
105
- // remove exiting header, remove dropped buttons and reset margins
90
+ // remove exiting header and remove dropped buttons
106
91
if ( headerGroups . enter ( ) . size ( ) ) {
107
92
// make sure gButton is on top of all headers
108
93
gButton . node ( ) . parentNode . appendChild ( gButton . node ( ) ) ;
109
94
gButton . call ( removeAllButtons ) ;
110
95
}
111
96
112
- headerGroups . exit ( ) . each ( function ( menuOpts ) {
97
+ if ( headerGroups . exit ( ) . size ( ) ) {
113
98
gButton . call ( removeAllButtons ) ;
114
- clearAutoMargin ( menuOpts ) ;
115
- } ) . remove ( ) ;
99
+ }
100
+ headerGroups . exit ( ) . remove ( ) ;
116
101
117
102
// draw headers!
118
103
headerGroups . each ( function ( menuOpts ) {
119
104
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
+ ) ;
120
115
121
116
var _gButton = menuOpts . type === 'dropdown' ? gButton : null ;
122
117
Plots . manageCommandObserver ( gd , menuOpts , menuOpts . buttons , function ( data ) {
@@ -134,7 +129,7 @@ module.exports = function draw(gd) {
134
129
drawButtons ( gd , gHeader , null , null , menuOpts ) ;
135
130
}
136
131
} ) ;
137
- } ;
132
+ }
138
133
139
134
// Note that '_index' is set at the default step,
140
135
// it corresponds to the menu index in the user layout update menu container.
@@ -469,7 +464,6 @@ function styleOnMouseOut(item, menuOpts) {
469
464
. call ( Color . fill , menuOpts . bgcolor ) ;
470
465
}
471
466
472
- // find item dimensions (this mutates menuOpts)
473
467
function findDimensions ( gd , menuOpts ) {
474
468
var dims = menuOpts . _dims = {
475
469
width1 : 0 ,
@@ -478,10 +472,10 @@ function findDimensions(gd, menuOpts) {
478
472
widths : [ ] ,
479
473
totalWidth : 0 ,
480
474
totalHeight : 0 ,
475
+ paddedWidth : 0 ,
476
+ paddedHeight : 0 ,
481
477
openWidth : 0 ,
482
- openHeight : 0 ,
483
- lx : 0 ,
484
- ly : 0
478
+ openHeight : 0
485
479
} ;
486
480
487
481
var fakeButtons = Drawing . tester . selectAll ( 'g.' + constants . dropdownButtonClassName )
@@ -540,7 +534,6 @@ function findDimensions(gd, menuOpts) {
540
534
dims . totalWidth -= constants . gapButton ;
541
535
}
542
536
543
-
544
537
dims . headerWidth = dims . width1 + constants . arrowPadX ;
545
538
dims . headerHeight = dims . height1 ;
546
539
@@ -556,50 +549,13 @@ function findDimensions(gd, menuOpts) {
556
549
557
550
fakeButtons . remove ( ) ;
558
551
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 ;
585
554
586
555
dims . totalWidth = Math . ceil ( dims . totalWidth ) ;
587
556
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
- }
600
557
601
- function autoMarginId ( menuOpts ) {
602
- return constants . autoMarginIdRoot + menuOpts . _index ;
558
+ return dims ;
603
559
}
604
560
605
561
// set item positions (mutates posOpts)
@@ -644,3 +600,29 @@ function removeAllButtons(gButton, newMenuIndexAttr) {
644
600
. attr ( constants . menuIndexAttrName , newMenuIndexAttr || '-1' )
645
601
. selectAll ( 'g.' + constants . dropdownButtonClassName ) . remove ( ) ;
646
602
}
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
+ } ;
0 commit comments