@@ -46,13 +46,22 @@ function d3_this() {
4646d3 . functor = function ( v ) {
4747 return typeof v === "function" ? v : function ( ) { return v ; } ;
4848} ;
49- // A getter-setter method that preserves the appropriate `this` context.
50- d3 . rebind = function ( object , method ) {
49+ // Copies a variable number of methods from source to target.
50+ d3 . rebind = function ( target , source ) {
51+ var i = 1 , n = arguments . length , method ;
52+ while ( ++ i < n ) target [ method = arguments [ i ] ] = d3_rebind ( target , source , source [ method ] ) ;
53+ return target ;
54+ } ;
55+
56+ // Method is assumed to be a standard D3 getter-setter:
57+ // If passed with no arguments, gets the value.
58+ // If passed with arguments, sets the value and returns the target.
59+ function d3_rebind ( target , source , method ) {
5160 return function ( ) {
52- var x = method . apply ( object , arguments ) ;
53- return arguments . length ? object : x ;
61+ var value = method . apply ( source , arguments ) ;
62+ return arguments . length ? target : value ;
5463 } ;
55- } ;
64+ }
5665d3 . ascending = function ( a , b ) {
5766 return a < b ? - 1 : a > b ? 1 : a >= b ? 0 : NaN ;
5867} ;
@@ -494,7 +503,9 @@ d3_dispatch.prototype.on = function(type, listener) {
494503 type = type . substring ( 0 , i ) ;
495504 }
496505
497- this [ type ] . on ( name , listener ) ;
506+ return arguments . length < 2
507+ ? this [ type ] . on ( name )
508+ : ( this [ type ] . on ( name , listener ) , this ) ;
498509} ;
499510
500511function d3_dispatch_event ( ) {
@@ -512,6 +523,9 @@ function d3_dispatch_event() {
512523 dispatch . on = function ( name , listener ) {
513524 var l , i ;
514525
526+ // return the current listener, if any
527+ if ( arguments . length < 2 ) return ( l = listenerByName [ name ] ) && l . on ;
528+
515529 // remove the old listener, if any (with copy-on-write)
516530 if ( l = listenerByName [ name ] ) {
517531 l . on = null ;
@@ -2409,11 +2423,7 @@ function d3_scale_linear(domain, range, interpolate, clamp) {
24092423} ;
24102424
24112425function d3_scale_linearRebind ( scale , linear ) {
2412- scale . range = d3 . rebind ( scale , linear . range ) ;
2413- scale . rangeRound = d3 . rebind ( scale , linear . rangeRound ) ;
2414- scale . interpolate = d3 . rebind ( scale , linear . interpolate ) ;
2415- scale . clamp = d3 . rebind ( scale , linear . clamp ) ;
2416- return scale ;
2426+ return d3 . rebind ( scale , linear , "range" , "rangeRound" , "interpolate" , "clamp" ) ;
24172427}
24182428
24192429function d3_scale_linearNice ( dx ) {
@@ -4107,18 +4117,13 @@ d3.svg.brush = function() {
41074117 || ( y && extent [ 0 ] [ 1 ] === extent [ 1 ] [ 1 ] ) ;
41084118 } ;
41094119
4110- brush . on = function ( type , listener ) {
4111- event . on ( type , listener ) ;
4112- return brush ;
4113- } ;
4114-
41154120 d3 . select ( window )
41164121 . on ( "mousemove.brush" , d3_svg_brushMove )
41174122 . on ( "mouseup.brush" , d3_svg_brushUp )
41184123 . on ( "keydown.brush" , d3_svg_brushKeydown )
41194124 . on ( "keyup.brush" , d3_svg_brushKeyup ) ;
41204125
4121- return brush ;
4126+ return d3 . rebind ( brush , event , "on" ) ;
41224127} ;
41234128
41244129var d3_svg_brush ,
@@ -4303,12 +4308,7 @@ d3.behavior.drag = function() {
43034308 d3_behavior_dragDispatch ( "dragstart" ) ;
43044309 }
43054310
4306- drag . on = function ( type , listener ) {
4307- event . on ( type , listener ) ;
4308- return drag ;
4309- } ;
4310-
4311- return drag ;
4311+ return d3 . rebind ( drag , event , "on" ) ;
43124312} ;
43134313
43144314var d3_behavior_dragEvent ,
@@ -4448,12 +4448,7 @@ d3.behavior.zoom = function() {
44484448 return zoom ;
44494449 } ;
44504450
4451- zoom . on = function ( type , listener ) {
4452- event . on ( type , listener ) ;
4453- return zoom ;
4454- } ;
4455-
4456- return zoom ;
4451+ return d3 . rebind ( zoom , event , "on" ) ;
44574452} ;
44584453
44594454var d3_behavior_zoomDiv ,
0 commit comments