import "base";
import "../toolbar/button"
import "../utils/icons"


mpld3.BoxZoomPlugin = mpld3_BoxZoomPlugin;
mpld3.register_plugin("boxzoom", mpld3_BoxZoomPlugin);
mpld3_BoxZoomPlugin.prototype = Object.create(mpld3_Plugin.prototype);
mpld3_BoxZoomPlugin.prototype.constructor = mpld3_BoxZoomPlugin;
mpld3_BoxZoomPlugin.prototype.requiredProps = [];
mpld3_BoxZoomPlugin.prototype.defaultProps = {
    button: true,
    enabled: null
};

function mpld3_BoxZoomPlugin(fig, props) {
    mpld3_Plugin.call(this, fig, props);
    if (this.props.enabled === null){
        this.props.enabled = !(this.props.button);
    }

    var enabled = this.props.enabled;    
    if (this.props.button){
	// add a button to enable/disable box zoom
	var BoxZoomButton = mpld3.ButtonFactory({
	    buttonID: "boxzoom",
            sticky: true,
            actions: ["drag"],
	    onActivate: this.activate.bind(this),
	    onDeactivate: this.deactivate.bind(this),
            onDraw: function(){this.setState(enabled);},
	    icon: function(){return mpld3.icons["zoom"];},
	});
	this.fig.buttons.push(BoxZoomButton);
    }
    this.extentClass = "boxzoombrush";
}

mpld3_BoxZoomPlugin.prototype.activate = function(){
    if(this.enable) this.enable();
};

mpld3_BoxZoomPlugin.prototype.deactivate = function(){
    if(this.disable) this.disable();
};

mpld3_BoxZoomPlugin.prototype.draw = function(){
    mpld3.insert_css("#" + this.fig.figid + " rect.extent." + this.extentClass,
		     {"fill": "#fff",
                      "fill-opacity": 0,
                      "stroke": "#999"});
    
    // getBrush is a d3.svg.brush() object, set up for use on the figure.
    var brush = this.fig.getBrush();
    
    this.enable = function(){
        this.fig.showBrush(this.extentClass);
        brush.on("brushend", brushend.bind(this));
        this.enabled = true;
    }
    
    this.disable = function(){
        this.fig.hideBrush(this.extentClass);
        this.enabled = false;
    }
    
    this.toggle = function(){
	this.enabled ? this.disable() : this.enable();
    }
    
    function brushend(d){
	if(this.enabled){
	    var extent = brush.extent();
            if(!brush.empty()){
		d.set_axlim([extent[0][0], extent[1][0]],
		            [extent[0][1], extent[1][1]]);
            }
	}
	d.axes.call(brush.clear());
    }
    this.disable();
}


@@ -552,16 +552,17 @@
 == null)
+ 
 %7B%0A      
@@ -650,20 +650,16 @@
 enabled;
-    
 %0A    if 
@@ -677,19 +677,27 @@
 .button)
+ 
 %7B%0A
-%09
+        
 // add a
@@ -731,17 +731,24 @@
 ox zoom%0A
-%09
+        
 var BoxZ
@@ -781,17 +781,24 @@
 ctory(%7B%0A
-%09
+        
     butt
@@ -871,17 +871,24 @@
 drag%22%5D,%0A
-%09
+        
     onAc
@@ -921,17 +921,24 @@
 (this),%0A
-%09
+        
     onDe
@@ -1005,17 +1005,19 @@
 nction()
-%7B
+ %7B 
 this.set
@@ -1031,20 +1031,28 @@
 nabled);
+ 
 %7D,%0A
-%09
+        
     icon
@@ -1063,17 +1063,19 @@
 nction()
-%7B
+ %7B 
 return m
@@ -1097,17 +1097,32 @@
 m%22%5D;
+ 
 %7D,%0A
-%09%7D);%0A%09
+        %7D);%0A        
 this
@@ -1258,24 +1258,25 @@
 on()%7B%0A    if
+ 
 (this.enable
@@ -1357,16 +1357,17 @@
 %7B%0A    if
+ 
 (this.di
@@ -1463,16 +1463,25 @@
 ert_css(
+%0A        
 %22#%22 + th
@@ -1535,16 +1535,30 @@
 ss,%0A
-%09%09
+   
      %7B
+%0A            
 %22fil
@@ -1581,26 +1581,16 @@
         
-    
-      
 %22fill-op
@@ -1600,26 +1600,16 @@
 ty%22: 0,%0A
-          
         
@@ -1628,24 +1628,35 @@
 : %22#999%22
-%7D);
+,%0A        %7D
 %0A    
+);%0A
 %0A    // 
@@ -1759,20 +1759,16 @@
 rush();%0A
-    
 %0A    thi
@@ -1780,32 +1780,33 @@
 ble = function()
+ 
 %7B%0A        this.f
@@ -1915,36 +1915,32 @@
 d = true;%0A    %7D%0A
-    
 %0A    this.disabl
@@ -1949,24 +1949,25 @@
 = function()
+ 
 %7B%0A        th
@@ -2034,28 +2034,24 @@
 alse;%0A    %7D%0A
-    
 %0A    this.to
@@ -2067,19 +2067,27 @@
 nction()
+ 
 %7B%0A
-%09
+        
 this.ena
@@ -2131,20 +2131,16 @@
 ;%0A    %7D%0A
-    
 %0A    fun
@@ -2160,13 +2160,22 @@
 d(d)
+ 
 %7B%0A
-%09
+        
 if
+ 
 (thi
@@ -2184,19 +2184,27 @@
 enabled)
+ 
 %7B%0A
-%09
+        
     var 
@@ -2242,16 +2242,17 @@
       if
+ 
 (!brush.
@@ -2263,12 +2263,27 @@
 y())
+ 
 %7B%0A
-%09%09
+                
 d.se
@@ -2324,10 +2324,10 @@
 %5D%5D,%0A
-%09%09
+  
     
@@ -2334,16 +2334,18 @@
         
+  
 %5Bextent%5B
@@ -2385,12 +2385,26 @@
   %7D%0A
-%09%7D%0A%09
+        %7D%0A        
 d.ax
@@ -2425,32 +2425,33 @@
 clear());%0A    %7D%0A
+%0A
     this.disable

Fix indents/style in boxzoom.js