OpenLayers OpenLayers

Ticket #827: drag.2.patch

File drag.2.patch, 9.2 kB (added by tschaub, 1 year ago)

make drag handler easier to subclass

  • lib/OpenLayers/Handler/Drag.js

    old new  
    7777    }, 
    7878     
    7979    /** 
     80     * Method: activate 
     81     * Activate the handler. 
     82     *  
     83     * Return: 
     84     * {Boolean} The handler was successfully activated. 
     85     */ 
     86    activate: function() { 
     87        var activated = false; 
     88        if(OpenLayers.Handler.prototype.activate.apply(this, arguments)) { 
     89            this.dragging = false; 
     90            activated = true; 
     91        } 
     92        return activated; 
     93    }, 
     94 
     95    /** 
     96     * Method: deactivate  
     97     * Deactivate the handler. 
     98     *  
     99     * Return: 
     100     * {Boolean} The handler was successfully deactivated. 
     101     */ 
     102    deactivate: function() { 
     103        var deactivated = false; 
     104        if(OpenLayers.Handler.prototype.deactivate.apply(this, arguments)) { 
     105            this.dragging = false; 
     106            deactivated = true; 
     107        } 
     108        return deactivated; 
     109    }, 
     110 
     111    /** 
     112     * The four methods below (down, move, up, and out) are used by subclasses 
     113     *     to do their own processing related to these mouse events, or to 
     114     *     modify the handling of these events before the drag handler gets to 
     115     *     them. In subclasses, these methods should typically return true. 
     116     */ 
     117     
     118    /** 
     119     * Method: down 
     120     * This method is called prior to handling the mouse down event.  Subclasses 
     121     *     can do their own processing here and return true to continue with the 
     122     *     normal drag handling, or they can return false to do their own 
     123     *     handling. Note that this method should only be used/overridden by 
     124     *     subclasses; controls that wrap handlers should deal with callbacks 
     125     *     instead. 
     126     * 
     127     * Parameters: 
     128     * evt - {Event} The mouse down event 
     129     * 
     130     * Return: 
     131     * {Boolean} Let the handler deal with the mouse down event.  This should 
     132     *     typically return true unless a subclass wants to modify event 
     133     *     handling. 
     134     */ 
     135    down: function(evt) { 
     136        return true; 
     137    }, 
     138     
     139    /** 
     140     * Method: move 
     141     * This method is called prior to handling the mouse move event.  Subclasses 
     142     *     can do their own processing here and return true to continue with the 
     143     *     normal drag handling, or they can return false to do their own 
     144     *     handling. Note that this method should only be used/overridden by 
     145     *     subclasses; controls that wrap handlers should deal with callbacks 
     146     *     instead. 
     147     * 
     148     * Parameters: 
     149     * evt - {Event} The mouse move event 
     150     * 
     151     * Return: 
     152     * {Boolean} Let the handler deal with the mouse move event.  This should 
     153     *     typically return true unless a subclass wants to modify event 
     154     *     handling. 
     155     */ 
     156    move: function(evt) { 
     157        return true; 
     158    }, 
     159 
     160    /** 
     161     * Method: up 
     162     * This method is called prior to handling the mouse up event.  Subclasses 
     163     *     can do their own processing here and return true to continue with the 
     164     *     normal drag handling, or they can return false to do their own 
     165     *     handling. Note that this method should only be used/overridden by 
     166     *     subclasses; controls that wrap handlers should deal with callbacks 
     167     *     instead. 
     168     * 
     169     * Parameters: 
     170     * evt - {Event} The mouse up event 
     171     * 
     172     * Return: 
     173     * {Boolean} Let the handler deal with the mouse up event.  This should 
     174     *     typically return true unless a subclass wants to modify event 
     175     *     handling. 
     176     */ 
     177    up: function(evt) { 
     178        return true; 
     179    }, 
     180 
     181    /** 
     182     * Method: out 
     183     * This method is called prior to handling the mouse out event.  Subclasses 
     184     *     can do their own processing here and return true to continue with the 
     185     *     normal drag handling, or they can return false to do their own 
     186     *     handling. Note that this method should only be used/overridden by 
     187     *     subclasses; controls that wrap handlers should deal with callbacks 
     188     *     instead. 
     189     * 
     190     * Parameters: 
     191     * evt - {Event} The mouse out event 
     192     * 
     193     * Return: 
     194     * {Boolean} Let the handler deal with the mouse out event.  This should 
     195     *     typically return true unless a subclass wants to modify event 
     196     *     handling. 
     197     */ 
     198    out: function(evt) { 
     199        return true; 
     200    }, 
     201 
     202    /** 
    80203     * The methods below are part of the magic of event handling.  Because 
    81204     *     they are named like browser events, they are registered as listeners 
    82205     *     for the events they represent. 
     
    93216     * {Boolean} Let the event propagate. 
    94217     */ 
    95218    mousedown: function (evt) { 
     219        var propagate = true; 
    96220        if (this.checkModifiers(evt) && OpenLayers.Event.isLeftClick(evt)) { 
    97             this.started = true; 
    98             this.dragging = false; 
    99             this.start = evt.xy; 
    100             this.last = evt.xy; 
    101             // TBD replace with CSS classes 
    102             this.map.div.style.cursor = "move"; 
    103             this.callback("down", [evt.xy]); 
    104             OpenLayers.Event.stop(evt); 
    105             return false; 
     221            if(this.down(evt)) { 
     222                this.started = true; 
     223                this.dragging = false; 
     224                this.start = evt.xy; 
     225                this.last = evt.xy; 
     226                // TBD replace with CSS classes 
     227                this.map.div.style.cursor = "move"; 
     228                this.callback("down", [evt.xy]); 
     229                OpenLayers.Event.stop(evt); 
     230                propagate = false; 
     231            } 
    106232        } 
    107         return true; 
     233        return propagate; 
    108234    }, 
    109235 
    110236    /** 
     
    120246    mousemove: function (evt) { 
    121247        if (this.started) { 
    122248            if(evt.xy.x != this.last.x || evt.xy.y != this.last.y) { 
    123                 this.dragging = true; 
    124                 this.callback("move", [evt.xy]); 
    125                 if(!this.oldOnselectstart) { 
    126                     this.oldOnselectstart = document.onselectstart; 
    127                     document.onselectstart = function() {return false;} 
     249                if(this.move(evt)) { 
     250                    this.dragging = true; 
     251                    this.callback("move", [evt.xy]); 
     252                    if(!this.oldOnselectstart) { 
     253                        this.oldOnselectstart = document.onselectstart; 
     254                        document.onselectstart = function() {return false;} 
     255                    } 
     256                    this.last = evt.xy; 
    128257                } 
    129                 this.last = evt.xy; 
    130258            } 
    131259        } 
    132260        return true; 
     
    144272     */ 
    145273    mouseup: function (evt) { 
    146274        if (this.started) { 
    147             this.started = false; 
    148             this.dragging = false; 
    149             // TBD replace with CSS classes 
    150             this.map.div.style.cursor = ""; 
    151             this.callback("up", [evt.xy]); 
    152             this.callback("done", [evt.xy]); 
    153             document.onselectstart = this.oldOnselectstart; 
     275            if(this.up(evt)) { 
     276                this.started = false; 
     277                this.dragging = false; 
     278                // TBD replace with CSS classes 
     279                this.map.div.style.cursor = ""; 
     280                this.callback("up", [evt.xy]); 
     281                this.callback("done", [evt.xy]); 
     282                document.onselectstart = this.oldOnselectstart; 
     283            } 
    154284        } 
    155285        return true; 
    156286    }, 
     
    167297     */ 
    168298    mouseout: function (evt) { 
    169299        if (this.started && OpenLayers.Util.mouseLeft(evt, this.map.div)) { 
    170             this.started = false;  
    171             this.dragging = false; 
    172             // TBD replace with CSS classes 
    173             this.map.div.style.cursor = ""; 
    174             this.callback("out", []); 
    175             if(document.onselectstart) { 
    176                 document.onselectstart = this.oldOnselectstart; 
     300            if(this.out(evt)) { 
     301                this.started = false;  
     302                this.dragging = false; 
     303                // TBD replace with CSS classes 
     304                this.map.div.style.cursor = ""; 
     305                this.callback("out", []); 
     306                if(document.onselectstart) { 
     307                    document.onselectstart = this.oldOnselectstart; 
     308                } 
     309                this.callback("done", [evt.xy]) 
    177310            } 
    178             this.callback("done", [evt.xy]) 
    179311        } 
    180312        return true; 
    181313    }, 
     
    202334        return true; 
    203335    }, 
    204336 
    205     /** 
    206      * Method: activate 
    207      * Activate the handler. 
    208      *  
    209      * Return: 
    210      * {Boolean} The handler was successfully activated. 
    211      */ 
    212     activate: function() { 
    213         if(OpenLayers.Handler.prototype.activate.apply(this, arguments)) { 
    214             this.dragging = false; 
    215             return true; 
    216         } else { 
    217             return false; 
    218         } 
    219     }, 
    220  
    221     /** 
    222      * Method: deactivate  
    223      * Deactivate the handler. 
    224      *  
    225      * Return: 
    226      * {Boolean} The handler was successfully deactivated. 
    227      */ 
    228     deactivate: function() { 
    229         if(OpenLayers.Handler.prototype.deactivate.apply(this, arguments)) { 
    230             this.dragging = false; 
    231             return true; 
    232         } else { 
    233             return false; 
    234         } 
    235     }, 
    236  
    237337    CLASS_NAME: "OpenLayers.Handler.Drag" 
    238338});