-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmove_handler.js
More file actions
92 lines (77 loc) · 2.72 KB
/
Copy pathmove_handler.js
File metadata and controls
92 lines (77 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//container - jquery object representing the div that will be allowed to be moved
//handle - jquery object representing the button or portion of the div that when clicked will initiate a
//move operation, think of this as the point on the div where you can click and drag the div around
var MoveHandler = function(container, handle){
//setup local variables
//element generated around the drag-handle so that the mouse
//movements are detected around the drag handle, indicating
//where the note should be moved to. Think if this as a rectangle
//around the grab handle that allows the grab handle to work
var grab_region = undefined;
//flag indicating whether a note is being moved
var moving = false;
//starting x and y (left and top) position/offset of the note prior to the move
var xOffset = 0;
var yOffset = 0;
//mouse start coordinates
var startX = 0;
var startY = 0;
//turns moving off if a movement operation
//was in progress
var reset = function(){
if(moving === true){
moving = false;
container.removeClass("moving-outer");
grab_region.remove();
}
};
//event handler that initializes a movement operation
//for a particular div
var onMoveButtonMouseDown = function(e){
// grab the mouse position
startX = e.clientX;
startY = e.clientY;
// grab the clicked element's position
xOffset = GetValue(container.css("left"));
yOffset = GetValue(container.css("top"));
grab_region = $(document.createElement("div"));
grab_region.addClass("drag-region");
container.append(grab_region);
container.addClass("moving-outer");
container.mousemove(onMouseMove);
container.mouseup(onMouseUp);
//set moving flag
moving=true;
//prevent default
//e.preventDefault();
};
//mouse move event handler for
//the grab region. The grab region is an
//element generated around the drag-handle so that the mouse
//movements are detected around the drag handle, indicating
//where the note should be moved to. Think if this as a rectangle
//around the grab handle that allows the grab handle to work
var onMouseMove = function(e){
if(moving){
// grab the mouse position
currentX = e.clientX;
currentY = e.clientY;
container.css("left",xOffset+(currentX-startX));
container.css("top",yOffset+(currentY-startY));
}
};
//mouse up event handler for the drag handle that ends
//a note move operation, the user let go of the note
var onMouseUp = function(e){
reset();
};
//sets up necessary event handler bindings
var setUp = function(){
handle.bind("mousedown",onMoveButtonMouseDown);
};
setUp();
//could possibly extend this API so return empty
//object for now
var moveHandler = {};
return moveHandler;
};