1
/* This notice must be untouched at all times.
4
The latest version is available at
5
http://www.openjacob.org
7
Copyright (c) 2006 Andreas Herz. All rights reserved.
8
Created 5. 11. 2006 by Andreas Herz (Web: http://www.freegroup.de )
12
This library is free software; you can redistribute it and/or
13
modify it under the terms of the GNU Lesser General Public
14
License (LGPL) as published by the Free Software Foundation; either
15
version 2.1 of the License, or (at your option) any later version.
17
This library is distributed in the hope that it will be useful,
18
but WITHOUT ANY WARRANTY; without even the implied warranty of
19
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20
Lesser General Public License for more details.
22
You should have received a copy of the GNU Lesser General Public
23
License along with this library; if not, write to the Free Software
24
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA,
25
or see http://www.gnu.org/copyleft/lesser.html
28
draw2d.Drag=function()
33
* The item currently being dragged.
36
draw2d.Drag.current /*: Draggable */ = null;
37
draw2d.Drag.currentTarget /*:DropTarget */=null;
40
* Indicates whether or not an item is being dragged.
43
draw2d.Drag.dragging /*: boolean */ = false;
46
* Returns true if an item is being dragged.
48
* @return True if an item is being dragged, false if not.
51
draw2d.Drag.isDragging = function () /*: boolean */
57
* Sets the item being dragged.
59
* @param {Draggable} oDraggable The draggable item.
62
draw2d.Drag.setCurrent = function (oDraggable /*: Draggable */)
64
this.current = oDraggable;
69
* Returns the currently dragged item.
71
* @return The currently dragged item.
74
draw2d.Drag.getCurrent = function () /*: Draggable */
80
* Clears the currently dragged item from memory and sets the dragging
85
draw2d.Drag.clearCurrent = function ()
88
this.dragging = false;
92
* Encapsulates the functionality for a draggable element.
94
* @extends EventTarget
97
draw2d.Draggable=function(oElement, iConstraints)
101
* Inherit properties from EventTarget.
103
draw2d.EventTarget.call(this);
108
this.construct(oElement, iConstraints);
111
* The difference between the x cursor position and left edge of the element.
115
this.diffX /*: int */ = 0;
118
* The difference between the y cursor position and top edge of the element.
122
this.diffY /*: int */ = 0;
125
* Collection of drop targets for this item.
129
this.targets = new draw2d.ArrayList();
133
* Inherit methods from EventTarget.
135
draw2d.Draggable.prototype = new draw2d.EventTarget;
138
* Adds a new drop target to the draggable item.
140
* @param {DropTarget} oDropTarget The drop target to register for this item.
143
// draw2d.Draggable.prototype.addDropTarget = function (oDropTarget /*: DropTarget */)
145
// this.targets.add(oDropTarget);
149
* Creates a new instance based on the given element and the constraints.
152
* @param {HTMLElement} oElement The DOM element to make draggable.
153
* @param {int} iConstraints The rules for dragging.
155
draw2d.Draggable.prototype.construct = function (oElement /*: HTMLElement */, iConstraints /*: int */)
158
* The element to make draggable.
162
this.element /*: HTMLElement */ = oElement;
165
* The constraints indicating the rules for dragging.
169
this.constraints /*: int */ = iConstraints;
172
* Create a pointer to this object.
176
var dblTemp = function()
178
// Check if the user has made a "double click"
180
* Create a dragstart event and fire it.
182
var oDragStartEvent = new draw2d.DragDropEvent();
183
oDragStartEvent.initDragDropEvent("dblclick", true);
184
oThis.dispatchEvent(oDragStartEvent);
185
var oEvent = arguments[0] || window.event;
186
oEvent.cancelBubble = true;
187
oEvent.returnValue = false;
191
* Create a temporary function named fnTemp.
193
var fnTemp = function () {
196
* Get the event objects, which is either the first
197
* argument (for DOM-compliant browsers and Netscape 4.x)
198
* or window.event (for IE).
200
var oEvent = arguments[0] || window.event;
202
var oDragStartEvent = new draw2d.DragDropEvent();
203
// dispatch Event benötigt eventuel die x/y Koordinate um zu bestimmen
204
// ob dieses Even wirklich relevant ist. (z.b. Wo man in dem Object hinein geklickt hat)
206
var xOffset = oThis.node.workflow.getAbsoluteX();
207
var yOffset = oThis.node.workflow.getAbsoluteY();
208
var scrollLeft = oThis.node.workflow.getScrollLeft();
209
var scrollTop = oThis.node.workflow.getScrollTop();
210
oDragStartEvent.x = oEvent.clientX - oThis.element.offsetLeft+scrollLeft-xOffset;
211
oDragStartEvent.y = oEvent.clientY - oThis.element.offsetTop+scrollTop-yOffset;
217
oDragStartEvent.initDragDropEvent("contextmenu", true);
218
oThis.dispatchEvent(oDragStartEvent)
224
oDragStartEvent.initDragDropEvent("dragstart", true);
226
* If the event isn't cancelled, proceed.
228
if (oThis.dispatchEvent(oDragStartEvent))
231
* Get the difference between the clientX and clientY
232
* and the position of the element.
234
oThis.diffX = oEvent.clientX - oThis.element.offsetLeft;
235
oThis.diffY = oEvent.clientY - oThis.element.offsetTop;
238
* Set the currently dragged item.
240
draw2d.Drag.setCurrent(oThis);
242
// Error if the user drag the object outside the window and release the mouse button there.
243
// The object glues at the mose pointer.
244
if(oThis.isAttached==true)
245
oThis.detachEventHandlers();
248
* Add all DOM event handlers
250
oThis.attachEventHandlers();
254
oEvent.cancelBubble = true;
255
oEvent.returnValue = false;
259
* Create a temporary function named fnTemp.
261
var fnMouseMove = function ()
263
// Falls man gerade beim Drag&Drop ist, ist die
264
// MouseOver Anzeige ausgeschaltet
266
if(draw2d.Drag.getCurrent()==null)
269
* Get the event objects, which is either the first
270
* argument (for DOM-compliant browsers and Netscape 4.x)
271
* or window.event (for IE).
273
var oEvent = arguments[0] || window.event;
274
if(draw2d.Drag.currentHover!=null && oThis!=draw2d.Drag.currentHover)
276
// this codes will be called if you move the cursor from one figure (the current hover figure)
277
// to another figure without of "touch" the background.
279
var oDropEvent = new draw2d.DragDropEvent();
280
oDropEvent.initDragDropEvent("mouseleave", false, oThis);
281
draw2d.Drag.currentHover.dispatchEvent(oDropEvent);
283
if(oThis!=null && oThis!=draw2d.Drag.currentHover)
285
var oDropEvent = new draw2d.DragDropEvent();
286
oDropEvent.initDragDropEvent("mouseenter", false, oThis);
287
oThis.dispatchEvent(oDropEvent);
289
draw2d.Drag.currentHover = oThis;
293
// var oEvent = arguments[0] || window.event;
298
* Determine which method to use to add the event handler.
300
if (this.element.addEventListener) {
301
this.element.addEventListener("mousemove", fnMouseMove, false);
302
this.element.addEventListener("mousedown", fnTemp, false);
303
this.element.addEventListener("dblclick", dblTemp, false);
304
} else if (this.element.attachEvent) {
305
this.element.attachEvent("onmousemove", fnMouseMove);
306
this.element.attachEvent("onmousedown", fnTemp);
307
this.element.attachEvent("ondblclick", dblTemp);
309
throw new Error("Drag not supported in this browser.");
314
* Attaches event handlers for the mousemove and mouseup events.
319
draw2d.Draggable.prototype.attachEventHandlers = function () {
322
* Create a pointer to this object.
325
oThis.isAttached = true;
328
* Create a temporary function named tempMouseMove.
330
this.tempMouseMove = function () {
333
* Get the event objects, which is either the first
334
* argument (for DOM-compliant browsers and Netscape 4.x)
335
* or window.event (for IE).
337
var oEvent = arguments[0] || window.event;
340
* Get the new x and y coordinates for the dragged element by
341
* subtracting the difference in the x and y direction from
342
* the mouse position on the screen (clientX and clientY).
344
var newPos = new draw2d.Point(oEvent.clientX - oThis.diffX, oEvent.clientY - oThis.diffY);
346
// Adjust the new location if the object can snap to a helper
347
// like grid, geometry, ruler,...
349
if(oThis.node.getCanSnapToHelper())
351
newPos = oThis.node.getWorkflow().snapToHelper(oThis.node, newPos);
354
oThis.element.style.left = newPos.x+"px";
355
oThis.element.style.top = newPos.y+"px";
357
var scrollLeft = oThis.node.workflow.getScrollLeft();
358
var scrollTop = oThis.node.workflow.getScrollTop();
359
var xOffset = oThis.node.workflow.getAbsoluteX();
360
var yOffset = oThis.node.workflow.getAbsoluteY();
361
var oDropTarget = oThis.getDropTarget(oEvent.clientX+scrollLeft-xOffset, oEvent.clientY+scrollTop-yOffset);
362
var oCompartment = oThis.getCompartment(oEvent.clientX+scrollLeft-xOffset, oEvent.clientY+scrollTop-yOffset);
363
if(draw2d.Drag.currentTarget!=null && oDropTarget!=draw2d.Drag.currentTarget)
365
var oDropEvent = new draw2d.DragDropEvent();
366
oDropEvent.initDragDropEvent("dragleave", false, oThis);
367
draw2d.Drag.currentTarget.dispatchEvent(oDropEvent);
369
if(oDropTarget!=null && oDropTarget!=draw2d.Drag.currentTarget)
371
var oDropEvent = new draw2d.DragDropEvent();
372
oDropEvent.initDragDropEvent("dragenter", false, oThis);
373
oDropTarget.dispatchEvent(oDropEvent);
375
draw2d.Drag.currentTarget = oDropTarget;
378
if(draw2d.Drag.currentCompartment!=null && oCompartment!=draw2d.Drag.currentCompartment)
380
var oDropEvent = new draw2d.DragDropEvent();
381
oDropEvent.initDragDropEvent("figureleave", false, oThis);
382
draw2d.Drag.currentCompartment.dispatchEvent(oDropEvent);
384
if(oCompartment!=null && oCompartment.node!=oThis.node && oCompartment!=draw2d.Drag.currentCompartment)
386
var oDropEvent = new draw2d.DragDropEvent();
387
oDropEvent.initDragDropEvent("figureenter", false, oThis);
388
oCompartment.dispatchEvent(oDropEvent);
390
draw2d.Drag.currentCompartment = oCompartment;
393
* Create and fire a drag event.
395
var oDragEvent = new draw2d.DragDropEvent();
396
oDragEvent.initDragDropEvent("drag", false);
397
oThis.dispatchEvent(oDragEvent);
401
* Create a temporary function for the mouseup event.
403
oThis.tempMouseUp = function () {
406
* Detach all of the event handlers.
408
oThis.detachEventHandlers();
411
* Get the event object.
413
var oEvent = arguments[0] || window.event;
416
* Create and fire a dragend event.
418
var oDragEndEvent = new draw2d.DragDropEvent();
419
oDragEndEvent.initDragDropEvent("dragend", false);
420
oThis.dispatchEvent(oDragEndEvent);
423
* Determine if the mouse is over a drop target.
425
var scrollLeft = oThis.node.workflow.getScrollLeft();
426
var scrollTop = oThis.node.workflow.getScrollTop();
427
var xOffset = oThis.node.workflow.getAbsoluteX();
428
var yOffset = oThis.node.workflow.getAbsoluteY();
429
var oDropTarget = oThis.getDropTarget(oEvent.clientX+scrollLeft-xOffset, oEvent.clientY+scrollTop-yOffset);
430
var oCompartment= oThis.getCompartment(oEvent.clientX+scrollLeft-xOffset, oEvent.clientY+scrollTop-yOffset);
431
if (oDropTarget != null)
433
var oDropEvent = new draw2d.DragDropEvent();
434
oDropEvent.initDragDropEvent("drop", false, oThis);
435
oDropTarget.dispatchEvent(oDropEvent);
437
if (oCompartment != null && oCompartment.node != oThis.node)
439
var oDropEvent = new draw2d.DragDropEvent();
440
oDropEvent.initDragDropEvent("figuredrop", false, oThis);
441
oCompartment.dispatchEvent(oDropEvent);
444
if(draw2d.Drag.currentTarget!=null)
446
var oDropEvent = new draw2d.DragDropEvent();
447
oDropEvent.initDragDropEvent("dragleave", false, oThis);
448
draw2d.Drag.currentTarget.dispatchEvent(oDropEvent);
449
draw2d.Drag.currentTarget=null;
452
draw2d.Drag.currentCompartment=null;
453
draw2d.Drag.clearCurrent();
457
* Determine which method to use to add the event handlers for
458
* the mousemove and mouseup events.
460
if (document.body.addEventListener) {
461
document.body.addEventListener("mousemove", this.tempMouseMove, false);
462
document.body.addEventListener("mouseup", this.tempMouseUp, false);
463
} else if (document.body.attachEvent) {
464
document.body.attachEvent("onmousemove", this.tempMouseMove);
465
document.body.attachEvent("onmouseup", this.tempMouseUp);
467
throw new Error("Drag doesn't support this browser.");
473
* Detaches event handlers for the mousemove and mouseup events.
476
draw2d.Draggable.prototype.detachEventHandlers = function ()
478
this.isAttached = false;
480
* Determine the method for removing the event handlers for the
481
* mousemove and mouseup events.
483
if (document.body.removeEventListener) {
484
document.body.removeEventListener("mousemove", this.tempMouseMove, false);
485
document.body.removeEventListener("mouseup", this.tempMouseUp, false);
486
} else if (document.body.detachEvent) {
487
document.body.detachEvent("onmousemove", this.tempMouseMove);
488
document.body.detachEvent("onmouseup", this.tempMouseUp);
490
throw new Error("Drag doesn't support this browser.");
495
* Determines the drop target that the mouse is over.
497
* @param x The x-coordinate of the mouse.
498
* @param y The y-coordinate of the mouse.
499
* @return The drop target if the mouse is over one, null otherwise.
501
draw2d.Draggable.prototype.getDropTarget = function (/*:int*/ x ,/*:int*/ y )
503
for(var i=0;i<this.targets.getSize();i++)
505
var target = this.targets.get(i);
506
if (target.node.isOver(x, y) && target.node!=this.node)
515
* Determines the compartment target that the mouse is over.
517
* @param {int} x The x-coordinate of the mouse.
518
* @param {int} y The y-coordinate of the mouse.
519
* @return The drop target if the mouse is over one, null otherwise.
521
draw2d.Draggable.prototype.getCompartment = function (x /*: int */, y /*: int */) /*: DropTarget */
524
for(var i=0;i<this.node.workflow.compartments.getSize();i++)
526
var target = this.node.workflow.compartments.get(i);
527
if (target.isOver(x, y) && target!=this.node)
531
else if(result.getZOrder() < target.getZOrder())
535
return result==null?null:result.dropable;
540
* Returns the left coordinate of the element.
542
* @return The left coordinate of the element.
544
draw2d.Draggable.prototype.getLeft = function () /*: int */
546
return this.element.offsetLeft;
550
* Returns the top coordinate of the element.
552
* @return The top coordinate of the element.
554
draw2d.Draggable.prototype.getTop = function () /*: int */
556
return this.element.offsetTop;
560
* Encapsulates information about a drag drop event.
565
draw2d.DragDropEvent=function()
568
* Inherit properties from Event.
570
draw2d.Event.call(this);
574
* Inherit methods from Event.
576
draw2d.DragDropEvent.prototype = new draw2d.Event();
579
* Initializes the event object with information for the event.
581
* @param sType The type of event encapsulated by the object.
582
* @param bCancelable True if the event can be cancelled.
583
* @param oRelatedTarget The alternate target related to the event.
585
draw2d.DragDropEvent.prototype.initDragDropEvent = function(sType /*: String */,
586
bCancelable /*: boolean */,
587
oRelatedTarget /*: EventTarget */) {
589
* Call inherited method initEvent().
591
this.initEvent(sType, bCancelable);
594
* Assign related target (may be null).
596
this.relatedTarget = oRelatedTarget;
600
* A target for a Draggable to be dropped.
603
* @extends EventTarget
605
draw2d.DropTarget=function(oElement)
608
* Inherit properties from EventTarget.
610
draw2d.EventTarget.call(this);
615
this.construct(oElement);
619
* Inherit methods from EventTarget.
621
draw2d.DropTarget.prototype = new draw2d.EventTarget;
624
* Creates a new instance based on the given DOM element.
627
* @param oElement The DOM element to make into a drop target.
629
draw2d.DropTarget.prototype.construct = function (oElement /*: HTMLElement */)
632
* The DOM element to use as a drop target.
635
this.element = oElement;
639
* Returns the left coordinate of the drop target.
641
* @return The left coordinate of the drop target.
643
draw2d.DropTarget.prototype.getLeft = function () /*: int */
645
var el = this.element;
646
var ol=el.offsetLeft;
647
while((el=el.offsetParent) != null)
655
* Returns the top coordinate of the drop target.
657
* @return The top coordinate of the drop target.
659
draw2d.DropTarget.prototype.getTop = function () /*: int */
661
var el = this.element;
663
while((el=el.offsetParent) != null)
671
* Returns the height of the drop target.
673
* @return The height of the drop target.
675
draw2d.DropTarget.prototype.getHeight = function () /*: int */
677
return this.element.offsetHeight;
681
* Returns the width of the drop target.
683
* @return The width of the drop target.
685
draw2d.DropTarget.prototype.getWidth = function () /*: int */
687
return this.element.offsetWidth;
b'\\ No newline at end of file'