diff --git a/platform/commonUI/general/bundle.json b/platform/commonUI/general/bundle.json index 0d68c0d9d4..816ba67987 100644 --- a/platform/commonUI/general/bundle.json +++ b/platform/commonUI/general/bundle.json @@ -49,6 +49,11 @@ "key": "mctContainer", "implementation": "MCTContainer.js", "depends": [ "containers[]" ] + }, + { + "key": "mctDrag", + "implementation": "MCTDrag.js", + "depends": [ "$document" ] } ], "containers": [ diff --git a/platform/commonUI/general/src/MCTDrag.js b/platform/commonUI/general/src/MCTDrag.js new file mode 100644 index 0000000000..dacc2a66f0 --- /dev/null +++ b/platform/commonUI/general/src/MCTDrag.js @@ -0,0 +1,61 @@ +/*global define*/ + +define( + [], + function () { + "use strict"; + + function MCTDrag($document) { + var body = $document.find('body'); + + function link(scope, element, attrs) { + var initialPosition, + currentPosition, + delta; + + function fireListener(name) { + scope.$eval(attrs[name], { delta: delta }); + } + + 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"); + } + + function endDrag(event) { + body.off("mouseup", endDrag); + body.off("mousemove", continueDrag); + + continueDrag(event); + + fireListener("mctDragUp"); + } + + function startDrag(event) { + body.on("mouseup", endDrag); + body.on("mousemove", continueDrag); + updatePosition(event); + fireListener("mctDragDown"); + fireListener("mctDrag"); + } + + element.on("mousedown", startDrag); + } + + return { + restrict: "A", + link: link + }; + } + + return MCTDrag; + } +); \ No newline at end of file