@@ -20,6 +20,8 @@ var Fx = require('../fx');
20
20
var svgTextUtils = require ( '../../lib/svg_text_utils' ) ;
21
21
var setCursor = require ( '../../lib/setcursor' ) ;
22
22
var dragElement = require ( '../dragelement' ) ;
23
+ var arrayEditor = require ( '../../plot_api/plot_template' ) . arrayEditor ;
24
+
23
25
var drawArrowHead = require ( './draw_arrow_head' ) ;
24
26
25
27
// Annotations are stored in gd.layout.annotations, an array of objects
@@ -84,17 +86,21 @@ function drawRaw(gd, options, index, subplotId, xa, ya) {
84
86
var gs = gd . _fullLayout . _size ;
85
87
var edits = gd . _context . edits ;
86
88
87
- var className ;
88
- var annbase ;
89
+ var className , containerStr ;
89
90
90
91
if ( subplotId ) {
91
92
className = 'annotation-' + subplotId ;
92
- annbase = subplotId + '.annotations[' + index + '] ';
93
+ containerStr = subplotId + '.annotations' ;
93
94
} else {
94
95
className = 'annotation' ;
95
- annbase = 'annotations[' + index + '] ';
96
+ containerStr = 'annotations' ;
96
97
}
97
98
99
+ var editHelpers = arrayEditor ( gd . layout , containerStr , options ) ;
100
+ var modifyBase = editHelpers . modifyBase ;
101
+ var modifyItem = editHelpers . modifyItem ;
102
+ var getUpdateObj = editHelpers . getUpdateObj ;
103
+
98
104
// remove the existing annotation if there is one
99
105
fullLayout . _infolayer
100
106
. selectAll ( '.' + className + '[data-index="' + index + '"]' )
@@ -542,9 +548,7 @@ function drawRaw(gd, options, index, subplotId, xa, ya) {
542
548
. call ( Color . stroke , 'rgba(0,0,0,0)' )
543
549
. call ( Color . fill , 'rgba(0,0,0,0)' ) ;
544
550
545
- var update ,
546
- annx0 ,
547
- anny0 ;
551
+ var annx0 , anny0 ;
548
552
549
553
// dragger for the arrow & head: translates the whole thing
550
554
// (head/tail/text) all together
@@ -556,12 +560,11 @@ function drawRaw(gd, options, index, subplotId, xa, ya) {
556
560
557
561
annx0 = pos . x ;
558
562
anny0 = pos . y ;
559
- update = { } ;
560
563
if ( xa && xa . autorange ) {
561
- update [ xa . _name + '.autorange' ] = true ;
564
+ modifyBase ( xa . _name + '.autorange' , true ) ;
562
565
}
563
566
if ( ya && ya . autorange ) {
564
- update [ ya . _name + '.autorange' ] = true ;
567
+ modifyBase ( ya . _name + '.autorange' , true ) ;
565
568
}
566
569
} ,
567
570
moveFn : function ( dx , dy ) {
@@ -570,19 +573,19 @@ function drawRaw(gd, options, index, subplotId, xa, ya) {
570
573
ycenter = annxy0 [ 1 ] + dy ;
571
574
annTextGroupInner . call ( Drawing . setTranslate , xcenter , ycenter ) ;
572
575
573
- update [ annbase + '.x' ] = xa ?
576
+ modifyItem ( 'x' , xa ?
574
577
xa . p2r ( xa . r2p ( options . x ) + dx ) :
575
- ( options . x + ( dx / gs . w ) ) ;
576
- update [ annbase + '.y' ] = ya ?
578
+ ( options . x + ( dx / gs . w ) ) ) ;
579
+ modifyItem ( 'y' , ya ?
577
580
ya . p2r ( ya . r2p ( options . y ) + dy ) :
578
- ( options . y - ( dy / gs . h ) ) ;
581
+ ( options . y - ( dy / gs . h ) ) ) ;
579
582
580
583
if ( options . axref === options . xref ) {
581
- update [ annbase + '. ax'] = xa . p2r ( xa . r2p ( options . ax ) + dx ) ;
584
+ modifyItem ( ' ax', xa . p2r ( xa . r2p ( options . ax ) + dx ) ) ;
582
585
}
583
586
584
587
if ( options . ayref === options . yref ) {
585
- update [ annbase + '. ay'] = ya . p2r ( ya . r2p ( options . ay ) + dy ) ;
588
+ modifyItem ( ' ay', ya . p2r ( ya . r2p ( options . ay ) + dy ) ) ;
586
589
}
587
590
588
591
arrowGroup . attr ( 'transform' , 'translate(' + dx + ',' + dy + ')' ) ;
@@ -592,7 +595,7 @@ function drawRaw(gd, options, index, subplotId, xa, ya) {
592
595
} ) ;
593
596
} ,
594
597
doneFn : function ( ) {
595
- Registry . call ( 'relayout' , gd , update ) ;
598
+ Registry . call ( 'relayout' , gd , getUpdateObj ( ) ) ;
596
599
var notesBox = document . querySelector ( '.js-notes-box-panel' ) ;
597
600
if ( notesBox ) notesBox . redraw ( notesBox . selectedObj ) ;
598
601
}
@@ -604,8 +607,7 @@ function drawRaw(gd, options, index, subplotId, xa, ya) {
604
607
605
608
// user dragging the annotation (text, not arrow)
606
609
if ( editTextPosition ) {
607
- var update ,
608
- baseTextTransform ;
610
+ var baseTextTransform ;
609
611
610
612
// dragger for the textbox: if there's an arrow, just drag the
611
613
// textbox and tail, leave the head untouched
@@ -614,52 +616,54 @@ function drawRaw(gd, options, index, subplotId, xa, ya) {
614
616
gd : gd ,
615
617
prepFn : function ( ) {
616
618
baseTextTransform = annTextGroup . attr ( 'transform' ) ;
617
- update = { } ;
618
619
} ,
619
620
moveFn : function ( dx , dy ) {
620
621
var csr = 'pointer' ;
621
622
if ( options . showarrow ) {
622
623
if ( options . axref === options . xref ) {
623
- update [ annbase + '. ax'] = xa . p2r ( xa . r2p ( options . ax ) + dx ) ;
624
+ modifyItem ( ' ax', xa . p2r ( xa . r2p ( options . ax ) + dx ) ) ;
624
625
} else {
625
- update [ annbase + '. ax'] = options . ax + dx ;
626
+ modifyItem ( ' ax', options . ax + dx ) ;
626
627
}
627
628
628
629
if ( options . ayref === options . yref ) {
629
- update [ annbase + '. ay'] = ya . p2r ( ya . r2p ( options . ay ) + dy ) ;
630
+ modifyItem ( ' ay', ya . p2r ( ya . r2p ( options . ay ) + dy ) ) ;
630
631
} else {
631
- update [ annbase + '. ay'] = options . ay + dy ;
632
+ modifyItem ( ' ay', options . ay + dy ) ;
632
633
}
633
634
634
635
drawArrow ( dx , dy ) ;
635
636
}
636
637
else if ( ! subplotId ) {
638
+ var xUpdate , yUpdate ;
637
639
if ( xa ) {
638
- update [ annbase + '.x' ] = xa . p2r ( xa . r2p ( options . x ) + dx ) ;
640
+ xUpdate = xa . p2r ( xa . r2p ( options . x ) + dx ) ;
639
641
640
642
} else {
641
643
var widthFraction = options . _xsize / gs . w ,
642
644
xLeft = options . x + ( options . _xshift - options . xshift ) / gs . w -
643
645
widthFraction / 2 ;
644
646
645
- update [ annbase + '.x' ] = dragElement . align ( xLeft + dx / gs . w ,
647
+ xUpdate = dragElement . align ( xLeft + dx / gs . w ,
646
648
widthFraction , 0 , 1 , options . xanchor ) ;
647
649
}
648
650
649
651
if ( ya ) {
650
- update [ annbase + '.y' ] = ya . p2r ( ya . r2p ( options . y ) + dy ) ;
652
+ yUpdate = ya . p2r ( ya . r2p ( options . y ) + dy ) ;
651
653
} else {
652
654
var heightFraction = options . _ysize / gs . h ,
653
655
yBottom = options . y - ( options . _yshift + options . yshift ) / gs . h -
654
656
heightFraction / 2 ;
655
657
656
- update [ annbase + '.y' ] = dragElement . align ( yBottom - dy / gs . h ,
658
+ yUpdate = dragElement . align ( yBottom - dy / gs . h ,
657
659
heightFraction , 0 , 1 , options . yanchor ) ;
658
660
}
661
+ modifyItem ( 'x' , xUpdate ) ;
662
+ modifyItem ( 'y' , yUpdate ) ;
659
663
if ( ! xa || ! ya ) {
660
664
csr = dragElement . getCursor (
661
- xa ? 0.5 : update [ annbase + '.x' ] ,
662
- ya ? 0.5 : update [ annbase + '.y' ] ,
665
+ xa ? 0.5 : xUpdate ,
666
+ ya ? 0.5 : yUpdate ,
663
667
options . xanchor , options . yanchor
664
668
) ;
665
669
}
@@ -674,7 +678,7 @@ function drawRaw(gd, options, index, subplotId, xa, ya) {
674
678
} ,
675
679
doneFn : function ( ) {
676
680
setCursor ( annTextGroupInner ) ;
677
- Registry . call ( 'relayout' , gd , update ) ;
681
+ Registry . call ( 'relayout' , gd , getUpdateObj ( ) ) ;
678
682
var notesBox = document . querySelector ( '.js-notes-box-panel' ) ;
679
683
if ( notesBox ) notesBox . redraw ( notesBox . selectedObj ) ;
680
684
}
@@ -689,17 +693,16 @@ function drawRaw(gd, options, index, subplotId, xa, ya) {
689
693
options . text = _text ;
690
694
this . call ( textLayout ) ;
691
695
692
- var update = { } ;
693
- update [ annbase + '.text' ] = options . text ;
696
+ modifyItem ( 'text' , _text ) ;
694
697
695
698
if ( xa && xa . autorange ) {
696
- update [ xa . _name + '.autorange' ] = true ;
699
+ modifyBase ( xa . _name + '.autorange' , true ) ;
697
700
}
698
701
if ( ya && ya . autorange ) {
699
- update [ ya . _name + '.autorange' ] = true ;
702
+ modifyBase ( ya . _name + '.autorange' , true ) ;
700
703
}
701
704
702
- Registry . call ( 'relayout' , gd , update ) ;
705
+ Registry . call ( 'relayout' , gd , getUpdateObj ( ) ) ;
703
706
} ) ;
704
707
}
705
708
else annText . call ( textLayout ) ;
0 commit comments