]> git.openstreetmap.org Git - rails.git/blobdiff - vendor/assets/leaflet/leaflet.locationfilter.js
Merge pull request #24 from zerebubuth/routing-merge
[rails.git] / vendor / assets / leaflet / leaflet.locationfilter.js
index f4e256858ccb946d78617587f421af297af13bbf..d120adc30f2e0baa85d6187d2d28ef6b5c28f269 100644 (file)
@@ -91,6 +91,8 @@ L.Control.ButtonContainer = L.Control.extend({
 });
 
 L.LocationFilter = L.Class.extend({
+    includes: L.Mixin.Events,
+
     options: {
         enableButton: {
             enableText: "Select area",
@@ -98,7 +100,8 @@ L.LocationFilter = L.Class.extend({
         },
         adjustButton: {
             text: "Select area within current zoom"
-        }
+        },
+        buttonPosition: 'topleft'
     },
 
     initialize: function(options) {
@@ -112,8 +115,10 @@ L.LocationFilter = L.Class.extend({
 
     onAdd: function(map) {
         this._map = map;
-        this._layer = new L.LayerGroup();
-        this._initializeButtonContainer();
+
+        if (this.options.enableButton || this.options.adjustButton) {
+            this._initializeButtonContainer();
+        }
 
         if (this.options.enable) {
             this.enable();
@@ -122,7 +127,9 @@ L.LocationFilter = L.Class.extend({
 
     onRemove: function(map) {
         this.disable();
-        this._buttonContainer.removeFrom(map);
+        if (this._buttonContainer) {
+            this._buttonContainer.removeFrom(map);
+        }
     },
 
     /* Get the current filter bounds */
@@ -135,8 +142,10 @@ L.LocationFilter = L.Class.extend({
         this._ne = bounds.getNorthEast();
         this._sw = bounds.getSouthWest();
         this._se = bounds.getSouthEast();
-        this._draw();
-        this._callCallback("onChange");
+        if (this.isEnabled()) {
+            this._draw();
+            this.fire("change", {bounds: bounds});
+        }
     },
 
     isEnabled: function() {
@@ -187,10 +196,10 @@ L.LocationFilter = L.Class.extend({
             var markerPos = that._moveMarker.getLatLng(),
                 latDelta = markerPos.lat-that._nw.lat,
                 lngDelta = markerPos.lng-that._nw.lng;
-            that._nw = new L.LatLng(that._nw.lat+latDelta, that._nw.lng+lngDelta);
-            that._ne = new L.LatLng(that._ne.lat+latDelta, that._ne.lng+lngDelta);
-            that._sw = new L.LatLng(that._sw.lat+latDelta, that._sw.lng+lngDelta);
-            that._se = new L.LatLng(that._se.lat+latDelta, that._se.lng+lngDelta);
+            that._nw = new L.LatLng(that._nw.lat+latDelta, that._nw.lng+lngDelta, true);
+            that._ne = new L.LatLng(that._ne.lat+latDelta, that._ne.lng+lngDelta, true);
+            that._sw = new L.LatLng(that._sw.lat+latDelta, that._sw.lng+lngDelta, true);
+            that._se = new L.LatLng(that._se.lat+latDelta, that._se.lng+lngDelta, true);
             that._draw();
         });
         this._setupDragendListener(this._moveMarker);
@@ -216,8 +225,8 @@ L.LocationFilter = L.Class.extend({
                 latMarker = options.moveAlong.lat,
                 lngMarker = options.moveAlong.lng;
             // Move follower markers when this marker is moved
-            latMarker.setLatLng(new L.LatLng(curPosition.lat, latMarker.getLatLng().lng));
-            lngMarker.setLatLng(new L.LatLng(lngMarker.getLatLng().lat, curPosition.lng));
+            latMarker.setLatLng(new L.LatLng(curPosition.lat, latMarker.getLatLng().lng, true));
+            lngMarker.setLatLng(new L.LatLng(lngMarker.getLatLng().lat, curPosition.lng, true));
             // Sort marker positions in nw, ne, sw, se order
             var corners = [that._nwMarker.getLatLng(), 
                            that._neMarker.getLatLng(), 
@@ -239,22 +248,13 @@ L.LocationFilter = L.Class.extend({
         this._setupDragendListener(marker);
     },
 
-    /* Call the callback (given by name) if it was supplied in options */
-    _callCallback: function(callbackName) {
-        if (this.options[callbackName]) {
-            this.options[callbackName](this.getBounds());
-        }
-    },
-
-    /* Call the onChange callback whenever dragend is triggered on the
+    /* Emit a change event whenever dragend is triggered on the
        given marker */
     _setupDragendListener: function(marker) {
-        if (this.options.onChange) {
-            var that = this;
-            marker.on('dragend', function(e) {
-                that._callCallback("onChange");
-            });
-        }
+        var that = this;
+        marker.on('dragend', function(e) {
+            that.fire("change", {bounds: that.getBounds()});
+        });
     },
 
     /* Create bounds for the mask rectangles and the location
@@ -263,9 +263,9 @@ L.LocationFilter = L.Class.extend({
         var mapBounds = this._map.getBounds(),
             outerBounds = new L.LatLngBounds(
                 new L.LatLng(mapBounds.getSouthWest().lat-0.1,
-                             mapBounds.getSouthWest().lng-0.1),
+                             mapBounds.getSouthWest().lng-0.1, true),
                 new L.LatLng(mapBounds.getNorthEast().lat+0.1,
-                             mapBounds.getNorthEast().lng+0.1)
+                             mapBounds.getNorthEast().lng+0.1, true)
             );
 
         // The south west and north east points of the mask */
@@ -273,10 +273,10 @@ L.LocationFilter = L.Class.extend({
         this._one = outerBounds.getNorthEast();
 
         // Bounds for the mask rectangles
-        this._northBounds = new L.LatLngBounds(new L.LatLng(this._ne.lat, this._osw.lng), this._one),
-        this._westBounds = new L.LatLngBounds(new L.LatLng(this._sw.lat, this._osw.lng), this._nw),
-        this._eastBounds = new L.LatLngBounds(this._se, new L.LatLng(this._ne.lat, this._one.lng)),
-        this._southBounds = new L.LatLngBounds(this._osw, new L.LatLng(this._sw.lat, this._one.lng));
+        this._northBounds = new L.LatLngBounds(new L.LatLng(this._ne.lat, this._osw.lng, true), this._one);
+        this._westBounds = new L.LatLngBounds(new L.LatLng(this._sw.lat, this._osw.lng, true), this._nw);
+        this._eastBounds = new L.LatLngBounds(this._se, new L.LatLng(this._ne.lat, this._one.lng, true));
+        this._southBounds = new L.LatLngBounds(this._osw, new L.LatLng(this._sw.lat, this._one.lng, true));
     },
 
     /* Initializes rectangles and markers */
@@ -285,6 +285,8 @@ L.LocationFilter = L.Class.extend({
             return;
         }
 
+        this._layer = new L.LayerGroup();
+
         // Calculate filter bounds
         this._calculateBounds();
 
@@ -294,7 +296,7 @@ L.LocationFilter = L.Class.extend({
         this._eastRect = this._drawRectangle(this._eastBounds);
         this._southRect = this._drawRectangle(this._southBounds);
         this._innerRect = this._drawRectangle(this.getBounds(), {
-            fillColor: "transparent",
+            fillOpacity: 0,
             stroke: true,
             color: "white",
             weight: 1,
@@ -378,7 +380,9 @@ L.LocationFilter = L.Class.extend({
             
 
         // Update buttons
-        this._buttonContainer.addClass("enabled");
+        if (this._buttonContainer) {
+            this._buttonContainer.addClass("enabled");
+        }
 
         if (this._enableButton) {
             this._enableButton.setText(this.options.enableButton.disableText);
@@ -411,8 +415,8 @@ L.LocationFilter = L.Class.extend({
 
         this._enabled = true;
         
-        // Call the enabled callback
-        this._callCallback("onEnabled");
+        // Fire the enabled event
+        this.fire("enabled");
     },
 
     /* Disable the location filter */
@@ -422,7 +426,9 @@ L.LocationFilter = L.Class.extend({
         }
 
         // Update buttons
-        this._buttonContainer.removeClass("enabled");
+        if (this._buttonContainer) {
+            this._buttonContainer.removeClass("enabled");
+        }
 
         if (this._enableButton) {
             this._enableButton.setText(this.options.enableButton.enableText);
@@ -440,8 +446,8 @@ L.LocationFilter = L.Class.extend({
 
         this._enabled = false;
 
-        // Call the disabled callback
-        this._callCallback("onDisabled");
+        // Fire the disabled event
+        this.fire("disabled");
     },
 
     /* Create a button that allows the user to adjust the location
@@ -454,7 +460,7 @@ L.LocationFilter = L.Class.extend({
             
             onClick: function(event) {
                 that._adjustToMap();
-                that._callCallback("onAdjustToZoomClick");
+                that.fire("adjustToZoomClick");
             }
         }).addTo(this._buttonContainer);
     },
@@ -463,7 +469,10 @@ L.LocationFilter = L.Class.extend({
        toggles the location filter */
     _initializeButtonContainer: function() {
         var that = this;
-        this._buttonContainer = new L.Control.ButtonContainer({className: "location-filter button-container"});
+        this._buttonContainer = new L.Control.ButtonContainer({
+           className: "location-filter button-container",
+           position: this.options.buttonPosition
+       });
 
         if (this.options.enableButton) {
             this._enableButton = new L.Control.Button({
@@ -474,11 +483,11 @@ L.LocationFilter = L.Class.extend({
                     if (!that._enabled) {
                         // Enable the location filter
                         that.enable();
-                        that._callCallback("onEnableClick");
+                        that.fire("enableClick");
                     } else {
                         // Disable the location filter
                         that.disable();
-                        that._callCallback("onDisableClick");
+                        that.fire("disableClick");
                     }
                 }
             }).addTo(this._buttonContainer);
@@ -486,4 +495,4 @@ L.LocationFilter = L.Class.extend({
 
         this._buttonContainer.addTo(this._map);
     }
-});
\ No newline at end of file
+});