OpenLayers OpenLayers

Ticket #1325: DragFeature.js.diff

File DragFeature.js.diff, 5.6 kB (added by sbenthall, 7 months ago)

patch to DragFeature.js

  • DragFeature.js

    old new  
    6565    onComplete: function(feature, pixel) {}, 
    6666 
    6767    /** 
     68     * APIProperty: onCancel 
     69     * {Function} Define this function if you want to know when a feature's dragging has been 
     70     *     canceled, for example when the feature has been dragged within its tolerance level.  
     71     *     The function should expect to receive two arguments: 
     72     *     the feature that is being dragged and the pixel location of the 
     73     *     mouse. 
     74     * 
     75     * Parameters: 
     76     * feature - {<OpenLayers.Feature.Vector>} The feature that was dragged. 
     77     * pixel - {<OpenLayers.Pixel>} The pixel location of the mouse. 
     78     */ 
     79    onCancel: function(feature, pixel) {}, 
     80 
     81    /** 
    6882     * Property: layer 
    6983     * {<OpenLayers.Layer.Vector>} 
    7084     */ 
     
    107121    lastPixel: null, 
    108122 
    109123    /** 
     124     * Property: startPixel 
     125     * {<OpenLayers.Pixel>} 
     126     */ 
     127    startPixel: null, 
     128 
     129    /** 
     130     * Property: pixelTolerance 
     131     * {Number} 
     132     *  
     133     * Radius (in pixels) from the feature's original position where a drag 
     134     * should be interpreted as "accidental." This value will be ignored if 
     135     * the time it took to drag is greater than the timeTolerance below. 
     136     */ 
     137    pixelTolerance : 0, 
     138     
     139    /** 
     140     * Property: timeTolerance 
     141     * {Number} 
     142     *  
     143     * Number of milliseconds in which a drag will be interpreted as  
     144     * "accidental." If the user takes less time to drag the feature than 
     145     * given in the timeTolerance, the feature will return to its original 
     146     * position. 
     147     */ 
     148    timeTolerance: 400, 
     149     
     150    /** 
     151     * Property: dragStart 
     152     * {Number} 
     153     *  
     154     * The time, in milliseconds, in which the drag was started. 
     155     */ 
     156    dragStart: null, 
     157     
     158    /** 
     159     * Property: dragOffset 
     160     * {<OpenLayers.Pixel>} 
     161     *  
     162     * The offset from the feature's actual center lonlat that it was dragged. 
     163     * This causes the mouse to stay in the same position relative to the feautre's 
     164     * image while being dragged. 
     165     */ 
     166    dragOffset : null, 
     167 
     168    /** 
    110169     * Constructor: OpenLayers.Control.DragFeature 
    111170     * Create a new control to drag features. 
    112171     * 
     
    208267     */ 
    209268    downFeature: function(pixel) { 
    210269        this.lastPixel = pixel; 
    211         this.onStart(this.feature, pixel); 
     270        this.startPixel = pixel; 
     271         
     272        this.dragStart = (new Date()).getTime(); 
     273         
     274        var featureCenter = this.map.getViewPortPxFromLonLat( 
     275            this.feature.geometry.getBounds().getCenterLonLat()); 
     276         
     277        this.dragOffset = new OpenLayers.Pixel(featureCenter.x - this.startPixel.x,  
     278                                               featureCenter.y - this.startPixel.y); 
     279                                                
    212280    }, 
    213281 
    214282    /** 
     
    220288     * pixel - {<OpenLayers.Pixel>} Location of the mouse event. 
    221289     */ 
    222290    moveFeature: function(pixel) { 
    223         var res = this.map.getResolution(); 
    224         this.feature.geometry.move(res * (pixel.x - this.lastPixel.x), 
    225                                    res * (this.lastPixel.y - pixel.y)); 
    226         this.layer.drawFeature(this.feature); 
     291         
     292        if (!this.dragging) { 
     293            this.layer.bringToFront(this.feature); 
     294            this.onStart(this.feature, this.startPixel); 
     295            this.dragging = true; 
     296        } 
     297         
     298        // Translate where the feature should move based on the 
     299        // place in which the user clicked on the feature's image. 
     300        pixel.x += this.dragOffset.x; 
     301        pixel.y += this.dragOffset.y; 
     302         
     303        this.feature.move(pixel); 
    227304        this.lastPixel = pixel; 
    228305        this.onDrag(this.feature, pixel); 
    229306    }, 
     
    237314     * pixel - {<OpenLayers.Pixel>} Location of the mouse event. 
    238315     */ 
    239316    upFeature: function(pixel) { 
    240         if(!this.over) { 
    241             this.dragHandler.deactivate(); 
    242             this.feature = null; 
    243             // TBD replace with CSS classes 
    244             this.map.div.style.cursor = "default"; 
    245        
     317        //if(!this.over) { 
     318        //    this.dragHandler.deactivate(); 
     319        //    this.feature = null; 
     320        //    // TBD replace with CSS classes 
     321        //    this.map.div.style.cursor = "default"; 
     322        //
    246323    }, 
    247324 
    248325    /** 
     
    254331     *     came from a mouseout, this may not be in the map viewport. 
    255332     */ 
    256333    doneDragging: function(pixel) { 
    257         this.onComplete(this.feature, pixel); 
     334         
     335        // Check tolerance. 
     336        var passesTimeTolerance =  
     337            (new Date()).getTime() > this.dragStart + this.timeTolerance; 
     338         
     339        var xDiff = this.startPixel.x - pixel.x; 
     340        var yDiff = this.startPixel.y - pixel.y; 
     341         
     342        var passesPixelTolerance =  
     343            Math.sqrt(Math.pow(xDiff,2) + Math.pow(yDiff,2)) > this.pixelTolerance; 
     344         
     345        if(passesTimeTolerance || passesPixelTolerance){ 
     346            this.onComplete(this.feature, pixel);    
     347        } else { 
     348            var feature = this.feature; 
     349            var res = this.map.getResolution(); 
     350            this.feature.geometry.move(res * (this.startPixel.x - pixel.x), 
     351                                       res * (pixel.y - this.startPixel.y)); 
     352            this.layer.drawFeature(this.feature); 
     353            this.onCancel(feature,pixel); 
     354        } 
     355         
     356        this.dragging = false; 
    258357    }, 
    259358 
    260359    /**