2014-06-11 12:41:55 +00:00
|
|
|
'use strict';
|
2014-04-14 13:11:02 +00:00
|
|
|
|
2014-06-11 12:41:55 +00:00
|
|
|
var $ = require('jquery');
|
2014-06-23 15:16:13 +00:00
|
|
|
var _ = require('lodash');
|
2014-04-14 13:11:02 +00:00
|
|
|
|
|
|
|
function MoveCanvas(events, canvas) {
|
2014-06-11 12:41:55 +00:00
|
|
|
|
2014-04-14 13:11:02 +00:00
|
|
|
var THRESHOLD = 20;
|
|
|
|
|
|
|
|
function init(element) {
|
|
|
|
|
2014-06-23 15:16:13 +00:00
|
|
|
var context = {};
|
2014-04-14 13:11:02 +00:00
|
|
|
|
|
|
|
function getPosition(e) {
|
|
|
|
return { x: e.clientX, y: e.clientY };
|
|
|
|
}
|
|
|
|
|
|
|
|
function getDelta(p1, p2) {
|
|
|
|
return {
|
|
|
|
x: p1.x - p2.x,
|
|
|
|
y: p1.y - p2.y
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function isThresholdReached(delta) {
|
|
|
|
return Math.abs(delta.x) > THRESHOLD || Math.abs(delta.y) > THRESHOLD;
|
|
|
|
}
|
|
|
|
|
|
|
|
function cursor(value) {
|
|
|
|
var current = document.body.style.cursor || '';
|
|
|
|
|
|
|
|
if (value !== undefined) {
|
|
|
|
document.body.style.cursor = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleMove(event) {
|
|
|
|
|
|
|
|
var position = getPosition(event);
|
|
|
|
|
|
|
|
|
2014-06-23 15:16:13 +00:00
|
|
|
if (!context.dragging && isThresholdReached(getDelta(context.start, position))) {
|
2014-04-14 13:11:02 +00:00
|
|
|
context.dragging = true;
|
|
|
|
|
|
|
|
context.cursor = cursor('move');
|
|
|
|
}
|
|
|
|
|
2014-06-23 15:16:13 +00:00
|
|
|
|
2014-04-14 13:11:02 +00:00
|
|
|
if (context.dragging) {
|
|
|
|
|
2014-06-23 15:16:13 +00:00
|
|
|
var lastPos = context.last || context.start;
|
|
|
|
|
|
|
|
var delta = getDelta(position, lastPos);
|
2014-04-14 13:11:02 +00:00
|
|
|
|
2014-06-23 15:16:13 +00:00
|
|
|
canvas.scroll({
|
|
|
|
dx: delta.x,
|
|
|
|
dy: delta.y
|
2014-04-14 13:11:02 +00:00
|
|
|
});
|
2014-06-23 15:16:13 +00:00
|
|
|
|
|
|
|
context.last = position;
|
2014-04-14 13:11:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// prevent select
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
|
2014-06-23 15:16:13 +00:00
|
|
|
|
2014-04-14 13:11:02 +00:00
|
|
|
function handleEnd(event) {
|
|
|
|
$(element).off('mousemove', handleMove);
|
|
|
|
$(document).off('mouseup', handleEnd);
|
|
|
|
|
|
|
|
if (context) {
|
|
|
|
cursor(context.cursor);
|
|
|
|
}
|
|
|
|
|
|
|
|
// prevent select
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleStart(event) {
|
|
|
|
|
|
|
|
var position = getPosition(event);
|
|
|
|
|
|
|
|
context = {
|
|
|
|
start: position
|
|
|
|
};
|
|
|
|
|
|
|
|
$(element).on('mousemove', handleMove);
|
|
|
|
$(document).on('mouseup', handleEnd);
|
|
|
|
|
|
|
|
// prevent select
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
$(element).on('mousedown', handleStart);
|
|
|
|
}
|
|
|
|
|
|
|
|
events.on('canvas.init', function(e) {
|
|
|
|
init(e.paper.node);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-11 12:41:55 +00:00
|
|
|
MoveCanvas.$inject = [ 'eventBus', 'canvas' ];
|
2014-04-14 13:11:02 +00:00
|
|
|
|
|
|
|
module.exports = MoveCanvas;
|