aboutsummaryrefslogtreecommitdiffstats
path: root/src/draggable.js
diff options
context:
space:
mode:
authorwout <wout@impinc.co.uk>2012-12-16 16:15:47 +0100
committerwout <wout@impinc.co.uk>2012-12-16 16:15:47 +0100
commitc6ac1246c271c66733366086f467e381c3fd65a8 (patch)
tree09ff273e5ffea615ba9555e38a441e5248e37190 /src/draggable.js
parent1fc78fe531ded4bc8a1ed5e176774600b897fcb1 (diff)
downloadsvg.js-c6ac1246c271c66733366086f467e381c3fd65a8.tar.gz
svg.js-c6ac1246c271c66733366086f467e381c3fd65a8.zip
Implemented core library
Diffstat (limited to 'src/draggable.js')
-rw-r--r--src/draggable.js46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/draggable.js b/src/draggable.js
index e69de29..b0deac7 100644
--- a/src/draggable.js
+++ b/src/draggable.js
@@ -0,0 +1,46 @@
+
+var bind = function(fn, me) {
+ return function() { return fn.apply(me, arguments); };
+};
+
+SVG.Draggable = function Draggable(e) {
+ this._windowMouseUp = bind(this._windowMouseUp, this);
+ this._windowMouseMove = bind(this._windowMouseMove, this);
+ this.draggable = bind(this.draggable, this);
+ this.element = e;
+ this.element.draggable = this.draggable;
+};
+
+SVG.Draggable.prototype.draggable = function() {
+ var self = this;
+ this.element.on('mousedown', function(e) {
+ self.startDragEvent = e;
+ self.startDragPosition = {
+ x: self.element.attributes.x || 0,
+ y: self.element.attributes.y || 0
+ };
+ window.addEventListener('mousemove', self._windowMouseMove);
+ window.addEventListener('mouseup', self._windowMouseUp);
+ return typeof self.element.dragstart === 'function' ? self.element.dragstart(e) : void 0;
+ });
+ return this.element;
+};
+
+SVG.Draggable.prototype._windowMouseMove = function(e) {
+ if (this.startDragEvent != null) {
+ var d = {
+ x: e.pageX - this.startDragEvent.pageX,
+ y: e.pageY - this.startDragEvent.pageY
+ };
+ this.element.move(this.startDragPosition.x + d.x, this.startDragPosition.y + d.y);
+ return typeof this.element.dragmove === 'function' ? this.element.dragmove(d, e) : void 0;
+ }
+};
+
+SVG.Draggable.prototype._windowMouseUp = function(e) {
+ this.startDragEvent = null;
+ this.startDragPosition = null;
+ window.removeEventListener('mousemove', this._windowMouseMove);
+ window.removeEventListener('mouseup', this._windowMouseUp);
+ return typeof this.element.dragend === 'function' ? this.element.dragend(e) : void 0;
+}; \ No newline at end of file