3 /* HTMLElement interface */
4 var SOGODragAndDropSourceInterface = {
5 _removeGestureHandlers: function () {
6 window.removeEventListener("mousemove", this.dragGestureMouseMoveHandler, false);
7 window.removeEventListener("mouseup", this.dragGestureMouseUpHandler, false);
8 document._dragGestureStartPoint = null;
9 document._currentMouseGestureObject = null;
12 this.addEventListener("mousedown", this.dragGestureMouseDownHandler, false);
14 dragGestureMouseDownHandler: function (event) {
15 if (event.button == 0) {
16 document._dragGestureStartPoint = new Array(event.clientX,
18 document._currentMouseGestureObject = this;
19 window.addEventListener("mousemove", this.dragGestureMouseMoveHandler,
21 window.addEventListener("mouseup", this.dragGestureMouseUpHandler,
25 dragGestureMouseUpHandler: function (event) {
27 document._currentMouseGestureObject._removeGestureHandlers();
29 dragGestureMouseMoveHandler: function (event) {
30 // log("source mouse move (target: " + event.target + ")");
31 var deltaX = event.clientX - document._dragGestureStartPoint[0];
32 var deltaY = event.clientY - document._dragGestureStartPoint[1];
33 if (Math.sqrt((deltaX * deltaX) + (deltaY * deltaY)) > 10) {
34 // log("event: " + event);
35 // event.stopPropagation();
36 // event.preventDefault();
37 event.returnValue = true;
38 event.cancelBubble = true;
39 var object = document._currentMouseGestureObject;
40 var point = document._dragGestureStartPoint;
41 document.DNDManager.startDragging(object, point);
42 document._currentMouseGestureObject._removeGestureHandlers();
43 // var mouseup = document.createEvent("MouseEvent");
44 // mouseup.initEvent("mouseup", true, true);
45 // event.target.dispatchEvent(mouseup);
46 // var dragStart = document.createEvent("MouseEvents");
47 // dragStart.initMouseEvent("draggesture-hack", true, true, window,
48 // event.detail, event.screenX, event.screenY,
49 // event.clientX, event.clientY, event.ctrlKey,
50 // event.altKey, event.shiftKey, event.metaKey,
51 // event.button, null);
52 // this.dispatchEvent(dragStart);
58 document.DNDManager = {
62 destinations: new Array(),
63 registerSource: function (source) {
64 var id = source.getAttribute("id");
66 id = "_dndSource" + (this.lastSource + 1);
67 source.setAttribute("id", id);
69 this.sources[id] = source;
71 source.addInterface(SOGODragAndDropSourceInterface);
73 registerDestination: function (destination) {
74 var id = destination.getAttribute("id");
76 id = "_dndDestination" + (this.lastDestination + 1);
77 destination.setAttribute("id", id);
79 this.destinations[id] = destination;
80 this.lastDestination++;
82 _lookupSource: function (target) {
84 var id = target.getAttribute("id");
86 source = document.DNDManager.sources[id];
89 _lookupDestination: function (target) {
90 var destination = null;
91 var id = target.getAttribute("id");
93 destination = document.DNDManager.destinations[id];
96 startDragging: function (object, point) {
97 // log("source gesture intercepted (source: " + object + ")");
98 var source = document.DNDManager._lookupSource (object);
100 // log("source known");
101 document.DNDManager.currentDndOperation = new document.DNDOperation(source, point);
102 window.addEventListener("mouseup",
103 document.DNDManager.destinationDrop, false);
104 window.addEventListener("mouseover",
105 document.DNDManager.destinationEnter, false);
106 window.addEventListener("mousemove",
107 document.DNDManager.destinationOver, false);
108 window.addEventListener("mouseout",
109 document.DNDManager.destinationExit, false);
112 destinationEnter: function (event) {
113 var operation = document.DNDManager.currentDndOperation;
114 var destination = document.DNDManager._lookupDestination (event.target);
115 if (operation && destination && destination.dndAcceptType) {
116 // log("enter: " + event.target);
117 operation.type = null;
119 while (operation.type == null
120 && i < operation.types.length) {
121 if (destination.dndAcceptType(operation.types[i])) {
122 operation.type = operation.types[i];
123 operation.setDestination(destination);
124 if (destination.dndEnter)
125 destination.dndEnter(event, operation.source, operation.type);
132 destinationExit: function (event) {
133 var operation = document.DNDManager.currentDndOperation;
135 && operation.destination == event.target) {
136 // log("exit: " + event.target);
137 if (operation.destination.dndExit)
138 event.target.dndExit();
139 operation.setDestination(null);
142 destinationOver: function (event) {
143 // log("over: " + event.target);
144 // var operation = document.DNDManager.currentDndOperation;
146 // && operation.destination == event.target)
147 // log("over: " + event.target);
149 destinationDrop: function (event) {
151 var operation = document.DNDManager.currentDndOperation;
153 window.removeEventListener("mouseup",
154 document.DNDManager.destinationDrop, false);
155 window.removeEventListener("mouseover",
156 document.DNDManager.destinationEnter, false);
157 window.removeEventListener("mousemove",
158 document.DNDManager.destinationOver, false);
159 window.removeEventListener("mouseout",
160 document.DNDManager.destinationExit, false);
161 if (operation.destination == event.target) {
162 // log("drag / drop: " + operation.source + " to " + operation.destination);
163 if (operation.destination.dndExit) {
164 // log ("destination.dndExit...");
165 operation.destination.dndExit();
167 if (operation.destination.dndDrop) {
168 // log ("destination.dndDrop...");
170 // log ("optype: " + operation.type);
171 if (operation.source.dndDataForType)
172 data = operation.source.dndDataForType(operation.type);
173 // log ("data: " + data);
174 var result = operation.destination.dndDrop(data);
175 // log ("result: " + result);
176 if (operation.ghost) {
178 operation.bustGhost();
180 operation.chaseGhost();
185 operation.chaseGhost();
188 operation.chaseGhost();
190 document.DNDManager.currentDndOperation = null;
193 currentDndOperation: null,
197 document.DNDOperation = function (source, point) {
198 this.startPoint = point;
199 this.source = source;
200 if (source.dndTypes) {
201 this.types = source.dndTypes();
204 this.destination = null;
205 if (source.dndGhost) {
206 var ghost = source.dndGhost();
207 ghost.style.position = "absolute;";
208 ghost.style.zIndex = 10000;
209 ghost.style.MozOpacity = 0.8;
210 document.body.appendChild(ghost);
213 document.addEventListener("mousemove", this.moveGhost, false);
219 document.DNDOperation.prototype.setDestination = function(destination) {
220 this.destination = destination;
223 document.DNDOperation.prototype.moveGhost = function(event) {
224 var offsetX = event.clientX;
225 var offsetY = event.clientY;
226 if (document.DNDManager.currentDndOperation.ghost.ghostOffsetX)
227 offsetX += document.DNDManager.currentDndOperation.ghost.ghostOffsetX;
228 if (document.DNDManager.currentDndOperation.ghost.ghostOffsetY)
229 offsetY += document.DNDManager.currentDndOperation.ghost.ghostOffsetY;
231 document.DNDManager.currentDndOperation.ghost.style.left = offsetX + "px;";
232 document.DNDManager.currentDndOperation.ghost.style.top = offsetY + "px;";
235 document.DNDOperation.prototype.bustGhost = function() {
236 document._dyingOperation = this;
237 document.removeEventListener("mousemove", this.moveGhost, false);
238 this.ghost.bustStep = 10;
239 setTimeout("document._dyingOperation._fadeGhost();", 50);
242 document.DNDOperation.prototype.chaseGhost = function() {
243 document._dyingOperation = this;
244 document.removeEventListener("mousemove", this.moveGhost, false);
245 this.ghost.bustStep = 25;
246 this.ghost.chaseStep = 25;
247 this.ghost.chaseDeltaX = ((this.ghost.cascadeLeftOffset() - this.startPoint[0])
248 / this.ghost.chaseStep);
249 this.ghost.chaseDeltaY = ((this.ghost.cascadeTopOffset() - this.startPoint[1])
250 / this.ghost.chaseStep);
251 setTimeout("document._dyingOperation._chaseGhost();", 20);
254 document.DNDOperation.prototype._chaseGhost = function() {
255 if (this.ghost.chaseStep) {
256 var newLeft = this.ghost.cascadeLeftOffset() - this.ghost.chaseDeltaX;
257 var newTop = this.ghost.cascadeTopOffset() - this.ghost.chaseDeltaY;
258 this.ghost.style.MozOpacity = (0.04 * this.ghost.chaseStep);
259 this.ghost.style.left = newLeft + "px;";
260 this.ghost.style.top = newTop + "px;";
261 this.ghost.chaseStep--;
262 setTimeout("document._dyingOperation._chaseGhost();", 20);
265 document.body.removeChild(this.ghost);
270 document.DNDOperation.prototype._fadeGhost = function() {
271 if (this.ghost.bustStep) {
272 this.ghost.style.MozOpacity = (0.1 * this.ghost.bustStep);
273 this.ghost.bustStep--;
274 setTimeout("document._dyingOperation._fadeGhost();", 50);
277 document.body.removeChild(this.ghost);