- Overview
- Documents
This class sets up a DOM object to be draggable around the screen, similar to the V2 GDraggableObject. Has several extensions, such as the ability to restrict the object to vertical or horizontal only movement, move the object by a certain increment, or return the object to its original position before dragging started.
Simple Example
The simplest way to create a draggable element is to create an ExtDraggableObject and pass a DOM object as a parameter.
var draggableObj = new ExtDraggableObject(document.getElementById("draggableElement"));
Zoom Slider
This advanced example makes use of ExtDraggableObjectOption and events to create a custom zoom control in place of the default.
ExtDraggableObjectOptions aren't created directly but are instead passed as an object literal as the second parameter in the ExtDraggableObject constructor:
var zoomSlider = new ExtDraggableObject(document.getElementById("zoom"), {restrictX:true, intervalY:8, toleranceX:50, toleranceY:25, container:document.getElementById("zoomSlider")});
restrictX will prevent the slider from moving left and right, perfect for a zoom control.
intervalY will cause the object to only move the given number of pixels at a time along the y axis. In this case 8 is chosen so that the slider will snap to the dots on the zoom control.
toleranceX and toleranceY will snap the slider back to its starting position if the mouse moves the given number of pixels away from the slider. This is largely to emulate the normal behavior of scroll bars but it may have more applications as well.
Finally, container acts mostly as it did in the v2 DraggableObject. The object can't be dragged beyond the boundaries of the containing object. This isn't a perfect implementation (and neither was the v2 version). It's recommended that the container object be the parent object as well to get the best behavior.
There are a number of events exposed by ExtDraggableObject which can be seen in the reference. In our example we'll make use of dragend. This isn't the greatest example of a working control as the max zoom level is hardcoded in, but it does show the use of events:
var dragEndEvent = google.maps.event.addListener(zoomSlider, "dragend", function() { var val = 19 - zoomSlider.valueY(); if (map.getZoom() !== val) { map.setZoom(val); } });
valueX and valueY will return a position of the object based on the interval. This is simply the current position divided by the respective interval value. Likewise setValueX and setValueY will move the object based on the interval. Basically, value = position / interval.
Putting it all together:
function init() { var zoomSlider = new ExtDraggableObject(document.getElementById("zoom"), {restrictX:true, intervalY:8, toleranceX:50, toleranceY:25, container:document.getElementById("zoomSlider")}); var dragEndEvent = google.maps.event.addListener(zoomSlider, "dragend", function() { var val = 19 - zoomSlider.valueY(); if (map.getZoom() !== val) { map.setZoom(val); } }); var latlng = new google.maps.LatLng(37.313477473067, -121.880502070713); var map = new google.maps.Map(document.getElementById("map_canvas"), {zoom:8, center:latlng, mapTypeId:google.maps.MapTypeId.ROADMAP, disableDefaultUI:true}); zoomSlider.setValueY(11); var centerChanged = google.maps.event.addListener(map, "center_changed", function() { mapZoom.setCenter(map.getCenter()); }); var zoomChanged = google.maps.event.addListener(map, "zoom_changed", function() { zoomSlider.setValueY(19 - map.getZoom()); }); var zoomIn = google.maps.event.addDomListener(document.getElementById("zoomIn"), "click", function() { map.setZoom(map.getZoom() + 1); }); var zoomOut = google.maps.event.addDomListener(document.getElementById("zoomOut"), "click", function() { map.setZoom(map.getZoom() - 1); }); }
ExtDraggableObject
This library is used to make DOM objects draggable. For examples of how to use this library see the examples page.
class ExtDraggableObject
This class makes a DOM object draggable.
Constructor
Constructor | Description |
---|---|
ExtDraggableObject(src:Node, opt_drag:ExtDraggableObjectOptions) | Sets up event handlers so that the source object can be dragged. For a complete reference on the available options seeExtDraggableObjectOptions |
Methods
Method | Return Value | Description |
---|---|---|
moveTo(point:point) | None | Moves the draggable object to a given absolute position. The position is relative to the parent node. Note that point can be any object with an x and y property. google.maps.Point works for this but you can also simply pass an object literal with an x and y property. |
moveBy(size:size) | None | Moves the draggable object by the specified offset. Note that size can be any object with a width and height property. google.maps.Size works for this but you can also simply pass an object literal with a width and height property. |
setDraggableCursor(cursor:String) | None | Sets the cursor to use when the mouse is over the draggable object. |
setDraggingCursor(cursor:String) | None | Sets the cursor to use when the draggable object is being dragged. |
left() | Number | Returns the current left position of the draggable object. |
top() | Number | Returns the current top position of the draggable object. |
valueX() | Number | Returns the current number of times the draggable object has moved the amount specified by intervalX. If intervalX was not given in the constructor than valueX acts as if intervalX is set to 1. |
valueY() | Number | Returns the current number of times the draggable object has moved the amount specified by intervalY. If intervalY was not given in the constructor than valueY acts as if intervalY is set to 1. |
setValueX(value:Number) | None | Moves the draggable object by an amount equal to value multiplied by intervalX. If intervalX was not given in the constructor thansetValueX acts as if intervalX is set to 1. |
setValueY(value:Number) | None | Moves the draggable object by an amount equal to value multiplied by intervalY. If intervalY was not given in the constructor thansetValueY acts as if intervalY is set to 1. |
preventDefaultMovement() | None | Prevents the events setup by ExtDraggableObject from moving the draggable object by default. Note that mouse movement is still tracked and the position the object should be at is still calculated like normal, only the object isn't physically moved. Also, methods that allow the user to manually move the object still work. |
Events
Event | Arguments | Description |
---|---|---|
dragstart | DragObjectEvent | This event is fired when the mouse is first down and no dragging has taken place yet. |
drag | DragObjectEvent | This event is fired repeatedly as the mouse is moved by the user while dragstart has been triggered. |
dragend | DragObjectEvent | This event is fired when the user releases the mouse after dragstart has been triggered. |
mousedown | DOM Event | This event is fired when the user clicks and holds the mouse button down over the draggable object. |
mouseup | DOM Event | This event is fired when the user releases the mouse button after mousedown has been triggered. |
class ExtDraggableObjectOptions
This class represents optional arguments to the ExtDraggableObject constructor. It has no constructor but is instantiated as an object literal.
Properties
Property | Type | Description |
---|---|---|
left | Number | The left starting position of the object. |
top | Number | The top starting position of the object. |
container | Node | A DOM element that will act as the bounding box for the draggable object. |
draggableCursor | String | The cursor to show on mouseover. |
draggingCursor | String | The cursor to show while dragging. |
intervalX | Number | The number of pixels the draggable object will move along the X-axis while dragging. |
intervalY | Number | The number of pixels the draggable object will move along the Y-axis while dragging. |
toleranceX | Number | The number of pixels the mouse can move away from the object along the X-axis before the object is snapped back to the position it was at dragstart. |
toleranceY | Number | The number of pixels the mouse can move away from the object along the Y-axis before the object is snapped back to the position it was at dragstart. |
restrictX | Number | Prevents the draggable object from moving along the X-axis if set to true. |
restrictY | Number | Prevents the draggable object from moving along the Y-axis if set to true. |
class DragObjectEvent
This class represents arguments returned by ExtDraggableObject events. It has no constructor or interfaces and is simply returned by events as an object literal.
Properties
Property | Type | Description |
---|---|---|
mouseX | Number | The X-axis position of the mouse on the page. |
mouseY | Number | The Y-axis position of the mouse on the page. |
startLeft | Number | The left position the draggable object was at on dragstart. |
startTop | Number | The top position the draggable object was at on dragstart. |
event | DOM Event | The DOM Event object associated with the underlying mouse event. |