2014-12-04 14:45:19 -08:00
|
|
|
/*global define*/
|
|
|
|
|
|
|
|
define(
|
|
|
|
[],
|
|
|
|
function () {
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
function MCTDrag($document) {
|
|
|
|
|
|
|
|
function link(scope, element, attrs) {
|
2014-12-05 14:20:50 -08:00
|
|
|
var body = $document.find('body'),
|
|
|
|
initialPosition,
|
2014-12-04 14:45:19 -08:00
|
|
|
currentPosition,
|
|
|
|
delta;
|
|
|
|
|
|
|
|
function fireListener(name) {
|
|
|
|
scope.$eval(attrs[name], { delta: delta });
|
2014-12-05 08:55:54 -08:00
|
|
|
|
|
|
|
// Trigger prompt digestion
|
|
|
|
scope.$apply();
|
2014-12-04 14:45:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
function updatePosition(event) {
|
|
|
|
currentPosition = [ event.pageX, event.pageY ];
|
|
|
|
initialPosition = initialPosition || currentPosition;
|
|
|
|
delta = currentPosition.map(function (v, i) {
|
|
|
|
return v - initialPosition[i];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function continueDrag(event) {
|
|
|
|
updatePosition(event);
|
|
|
|
fireListener("mctDrag");
|
2014-12-05 08:50:45 -08:00
|
|
|
|
|
|
|
// Don't show selection highlights, etc
|
|
|
|
event.preventDefault();
|
|
|
|
return false;
|
2014-12-04 14:45:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
function endDrag(event) {
|
|
|
|
body.off("mouseup", endDrag);
|
|
|
|
body.off("mousemove", continueDrag);
|
|
|
|
|
|
|
|
continueDrag(event);
|
|
|
|
|
|
|
|
fireListener("mctDragUp");
|
2014-12-05 08:50:45 -08:00
|
|
|
|
|
|
|
// Clear out start-of-drag position
|
|
|
|
initialPosition = undefined;
|
|
|
|
|
|
|
|
// Don't show selection highlights, etc
|
|
|
|
event.preventDefault();
|
|
|
|
return false;
|
2014-12-04 14:45:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
function startDrag(event) {
|
|
|
|
body.on("mouseup", endDrag);
|
|
|
|
body.on("mousemove", continueDrag);
|
2014-12-05 08:50:45 -08:00
|
|
|
|
2014-12-04 14:45:19 -08:00
|
|
|
updatePosition(event);
|
2014-12-05 08:50:45 -08:00
|
|
|
|
2014-12-04 14:45:19 -08:00
|
|
|
fireListener("mctDragDown");
|
|
|
|
fireListener("mctDrag");
|
2014-12-05 08:50:45 -08:00
|
|
|
|
|
|
|
// Don't show selection highlights, etc
|
|
|
|
event.preventDefault();
|
|
|
|
return false;
|
2014-12-04 14:45:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
element.on("mousedown", startDrag);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
restrict: "A",
|
|
|
|
link: link
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return MCTDrag;
|
|
|
|
}
|
|
|
|
);
|