/***** * * Dragger.es * * Authors: Kevin Lindsey, Antoine Quint * *****/ Dragger.VERSION = 0.1; Dragger.prototype = new EventTarget(false) Dragger.prototype.Class = Dragger; Dragger.Super = EventTarget.prototype; Dragger._MouseRegion = null; function Dragger() { this.Init(); } Dragger.prototype.Init = function() { this.Class.Super.Init.call(this); if ( this.Class._MouseRegion == null ) { var attributes = { xml: [ ['x', -32767], ['y', -32767], ['width', 65536], ['height', 65536] ], css: [ ['fill-opacity', 0], ['display', 'none'] ] }; var region = SVGUtil.CreateElement('rect', attributes); this.Class._MouseRegion = document.documentElement.appendChild(region); } this._Start = null; this._Last = null; this._Context = null; }; /***** Events *****/ Dragger.prototype.MakeEvent = function(type, e) { return new DragEvent( type, this, this._Start, this._Last, e ); }; Dragger.prototype.Drag = function(e) { // remember start position and init last position this._Last = this._Start = this._GetMouseCoords(e); // add event listeners this.Class._MouseRegion.addEventListener("mousemove", this, false); this.Class._MouseRegion.addEventListener("mouseup", this, false); // make sure _MouseRegion is on top document.documentElement.appendChild( this.Class._MouseRegion ); // show mouseRegion this.Class._MouseRegion.style.setProperty('display', 'inline', null); // send drag begin event if ( this.HasListener(DragEvent.Events.DRAGBEGIN) ) { this.DispatchEvent( this.MakeEvent(DragEvent.Events.DRAGBEGIN, e) ); } }; Dragger.prototype.Onmousemove = function(e) { // send drag event if ( this.HasListener(DragEvent.Events.DRAG) ) { this.DispatchEvent( this.MakeEvent(DragEvent.Events.DRAG, e) ); } // remember last position this._Last = this._GetMouseCoords(e); }; Dragger.prototype.Onmouseup = function(e) { // update position one last time this.Onmousemove(e); // remove event listeners from mouse region this.Class._MouseRegion.removeEventListener("mousemove", this, false); this.Class._MouseRegion.removeEventListener("mouseup", this, false); // hide mouse region this.Class._MouseRegion.style.setProperty('display', 'none', null); // send dragend event if ( this.HasListener(DragEvent.Events.DRAGEND) ) { this.DispatchEvent( this.MakeEvent(DragEvent.Events.DRAGEND, e) ); } }; Dragger.prototype._GetMouseCoords = function (e) { var coords; if (this._Context) { coords = SVGUtil.GetMouseCoordsInContext(e, this._Context); } else { coords = SVGUtil.GetMouseCoords(e); } return coords; }; Dragger.prototype.SetContext = function (context) { if (this._Context != context) { this._Context = context; } }; /***** * * DragEvent.es * * Authors: Kevin Lindsey, Antoine Quint * *****/ DragEvent.VERSION = 0.1; DragEvent.Events = { DRAGBEGIN : "dragbegin", DRAG : "drag", DRAGEND : "dragend" }; function DragEvent(type, dragger, start, last, mouseEvent) { this.Init(type, dragger, start, last, mouseEvent); } DragEvent.prototype.Init = function(type, dragger, start, last, mouseEvent) { this.type = type; this.Dragger = dragger; this.Start = start; this.Last = last; this.MouseEvent = mouseEvent; };