-
Notifications
You must be signed in to change notification settings - Fork 1
/
svg.draggable.js
161 lines (127 loc) · 4.66 KB
/
svg.draggable.js
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// svg.draggable.js 0.12 - Copyright (c) 2013 Wout Fierens - Licensed under the MIT license
SVG.extend(SVG.Element, {
// Make element draggable
draggable: function(constraint) {
var start, drag, end
, element = this
, parent = this.parent._parent(SVG.Nested) || this._parent(SVG.Doc)
/* remove draggable if already present */
if (typeof this.fixed == 'function')
this.fixed()
/* ensure constraint object */
constraint = constraint || {}
//append tr at every drag
var $dradClass = this.node.id;
var $TABLE = $('#table');
var $clone = $TABLE.find('tr.hide').clone(true).removeClass('hide table-line').addClass($dradClass);
$TABLE.find('table').append($clone);
/* start dragging */
start = function(event) {
event = event || SVG.window.event
var box
/* invoke any callbacks */
if (element.beforedrag)
element.beforedrag(event)
/* get element bounding box */
box = element.bbox()
if (element instanceof SVG.G) {
box.x = element.trans.x
box.y = element.trans.y
} else if (element instanceof SVG.Nested) {
box = {
x: element.x()
, y: element.y()
, width: element.attr('width')
, height: element.attr('height')
}
}
/* store event */
element.startEvent = event
/* store start position */
element.startPosition = {
x: box.x
, y: box.y
, width: box.width
, height: box.height
, zoom: parent.viewbox().zoom
, rotation: element.transform('rotation') * Math.PI / 180
}
/* add while and end events to SVG.SVG.window */
SVG.on(SVG.window, 'mousemove', drag)
SVG.on(SVG.window, 'mouseup', end)
/* invoke any callbacks */
if (element.dragstart)
element.dragstart({ x: 0, y: 0, zoom: element.startPosition.zoom }, event)
/* prevent selection dragging */
event.preventDefault ? event.preventDefault() : event.returnValue = false
}
/* while dragging */
drag = function(event) {
event = event || SVG.window.event
if (element.startEvent) {
/* calculate move position */
var x, y
, rotation = element.startPosition.rotation
, width = element.startPosition.width
, height = element.startPosition.height
, delta = {
x: event.pageX - element.startEvent.pageX,
y: event.pageY - element.startEvent.pageY,
zoom: element.startPosition.zoom
}
/* caculate new position [with rotation correction] */
x = element.startPosition.x + (delta.x * Math.cos(rotation) + delta.y * Math.sin(rotation)) / element.startPosition.zoom
y = element.startPosition.y + (delta.y * Math.cos(rotation) + delta.x * Math.sin(-rotation)) / element.startPosition.zoom
/* recalculate any offset */
if (element._offset) {
x -= element._offset.x
y -= element._offset.y
}
/* keep element within constrained box */
if (constraint.minX != null && x < constraint.minX)
x = constraint.minX
else if (constraint.maxX != null && x > constraint.maxX - width)
x = constraint.maxX - width
if (constraint.minY != null && y < constraint.minY)
y = constraint.minY
else if (constraint.maxY != null && y > constraint.maxY - height)
y = constraint.maxY - height
/* move the element to its new position */
element.move(x, y)
/* invoke any callbacks */
if (element.dragmove)
element.dragmove(delta, event)
}
}
/* when dragging ends */
end = function(event) {
event = event || SVG.window.event
/* calculate move position */
var delta = {
x: event.pageX - element.startEvent.pageX,
y: event.pageY - element.startEvent.pageY,
zoom: element.startPosition.zoom
}
/* reset store */
element.startEvent = null
element.startPosition = null
/* remove while and end events to SVG.window */
SVG.off(SVG.window, 'mousemove', drag)
SVG.off(SVG.window, 'mouseup', end)
/* invoke any callbacks */
if (element.dragend)
element.dragend(delta, event)
}
/* bind mousedown event */
element.on('mousedown', start)
/* disable draggable */
element.fixed = function() {
element.off('mousedown', start)
SVG.off(SVG.window, 'mousemove', drag)
SVG.off(SVG.window, 'mouseup', end)
start = drag = end = null
return element
}
return this
}
})