summaryrefslogtreecommitdiff
path: root/webapps/qooxdoo-0.6.3-sdk/frontend/framework/source/class/qx/event/handler/DragAndDropHandler.js
blob: d1f5ba4c22db3dad10c48bce868d1a66202d6c84 (plain)
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
/* ************************************************************************

   qooxdoo - the new era of web development

   http://qooxdoo.org

   Copyright:
     2004-2006 by 1&1 Internet AG, Germany, http://www.1and1.org

   License:
     LGPL 2.1: http://www.gnu.org/licenses/lgpl.html

   Authors:
     * Sebastian Werner (wpbasti)
     * Andreas Ecker (ecker)

************************************************************************ */

/* ************************************************************************

#module(ui_dragdrop)

************************************************************************ */

/**
 * This manager (singleton) manage all drag and drop handling of a qx.core.Init instance.
 *
 * @event dragdrop {qx.event.type.DragEvent}
 * @event dragout {qx.event.type.DragEvent}
 * @event dragover {qx.event.type.DragEvent}
 * @event dragmove {qx.event.type.DragEvent}
 * @event dragstart {qx.event.type.DragEvent}
 * @event dragend {qx.event.type.DragEvent}
 */
qx.OO.defineClass("qx.event.handler.DragAndDropHandler", qx.manager.object.ObjectManager,
function()
{
  qx.core.Target.call(this);

  this._data = {};
  this._actions = {};
  this._cursors = {};

  var vCursor;
  for (var vAction in this._actionNames)
  {
    vCursor = this._cursors[vAction] = new qx.ui.basic.Image(this._cursorPath + vAction + "." + this._cursorFormat);
    vCursor.setZIndex(1e8);
  }
});

qx.OO.addProperty({ name : "sourceWidget", type : "object" });
qx.OO.addProperty({ name : "destinationWidget", type : "object" });
qx.OO.addProperty({ name : "cursor", type : "object" });
qx.OO.addProperty({ name : "currentAction", type : "string" });

qx.Proto._actionNames =
{
  move : "move",
  copy : "copy",
  alias : "alias",
  nodrop : "nodrop"
}

qx.Proto._cursorPath = "widget/cursors/";
qx.Proto._cursorFormat = "gif";
qx.Proto._lastDestinationEvent = null;





/*
---------------------------------------------------------------------------
  COMMON MODIFIER
---------------------------------------------------------------------------
*/

qx.Proto._modifyDestinationWidget = function(propValue, propOldValue, propData)
{
  if (propValue)
  {
    propValue.dispatchEvent(new qx.event.type.DragEvent("dragdrop", this._lastDestinationEvent, propValue, this.getSourceWidget()));
    this._lastDestinationEvent = null;
  }

  return true;
}








/*
---------------------------------------------------------------------------
  DATA HANDLING
---------------------------------------------------------------------------
*/

/*!
Add data of mimetype.

#param vMimeType[String]: A valid mimetype
#param vData[Any]: Any value for the mimetype
*/
qx.Proto.addData = function(vMimeType, vData) {
  this._data[vMimeType] = vData;
}

qx.Proto.getData = function(vMimeType) {
  return this._data[vMimeType];
}

qx.Proto.clearData = function() {
  this._data = {};
}









/*
---------------------------------------------------------------------------
  MIME TYPE HANDLING
---------------------------------------------------------------------------
*/

qx.Proto.getDropDataTypes = function()
{
  var vDestination = this.getDestinationWidget();
  var vDropTypes = [];

  // If there is not any destination, simple return
  if (!vDestination) {
    return vDropTypes;
  }

  // Search for matching mimetypes
  var vDropDataTypes = vDestination.getDropDataTypes();

  for (var i=0, l=vDropDataTypes.length; i<l; i++) {
    if (vDropDataTypes[i] in this._data) {
      vDropTypes.push(vDropDataTypes[i]);
    }
  }

  return vDropTypes;
}







/*
---------------------------------------------------------------------------
  START DRAG
---------------------------------------------------------------------------
*/

/*!
This needed be called from any "dragstart" event to really start drag session.
*/
qx.Proto.startDrag = function()
{
  if (!this._dragCache) {
    throw new Error("Invalid usage of startDrag. Missing dragInfo!");
  }

  // Update status flag
  this._dragCache.dragHandlerActive = true;

  // Internal storage of source widget
  this.setSourceWidget(this._dragCache.sourceWidget);
}







/*
---------------------------------------------------------------------------
  FIRE IMPLEMENTATION FOR USER EVENTS
---------------------------------------------------------------------------
*/

qx.Proto._fireUserEvents = function(fromWidget, toWidget, e)
{
  if (fromWidget && fromWidget != toWidget && fromWidget.hasEventListeners("dragout")) {
    fromWidget.dispatchEvent(new qx.event.type.DragEvent("dragout", e, fromWidget, toWidget), true);
  }

  if (toWidget)
  {
    if (fromWidget != toWidget && toWidget.hasEventListeners("dragover")) {
      toWidget.dispatchEvent(new qx.event.type.DragEvent("dragover", e, toWidget, fromWidget), true);
    }

    if (toWidget.hasEventListeners("dragmove")) {
      toWidget.dispatchEvent(new qx.event.type.DragEvent("dragmove", e, toWidget, null), true);
    }
  }
}








/*
---------------------------------------------------------------------------
  HANDLER FOR MOUSE EVENTS
---------------------------------------------------------------------------
*/

/*!
This wraps the mouse events to custom handlers.
*/
qx.Proto.handleMouseEvent = function(e)
{
  switch (e.getType())
  {
    case "mousedown":
      return this._handleMouseDown(e);

    case "mouseup":
      return this._handleMouseUp(e);

    case "mousemove":
      return this._handleMouseMove(e);
  }
}

/*!
This starts the core drag and drop session.

To really get drag and drop working you need to define
a function which you attach to "dragstart"-event, which
invokes at least this.startDrag()
*/
qx.Proto._handleMouseDown = function(e)
{
  if (e.getDefaultPrevented()) {
    return;
  }

  // Store initial dragCache
  this._dragCache =
  {
    startScreenX : e.getScreenX(),
    startScreenY : e.getScreenY(),

    pageX : e.getPageX(),
    pageY : e.getPageY(),

    sourceWidget : e.getTarget(),
    sourceTopLevel : e.getTarget().getTopLevelWidget(),

    dragHandlerActive : false,
    hasFiredDragStart : false
  }
}


/*!
Handler for mouse move events
*/

qx.Proto._handleMouseMove = function(e)
{
  // Return if dragCache was not filled before
  if (!this._dragCache) {
    return;
  }

  /*
    Default handling if drag handler is activated
  */

  if (this._dragCache.dragHandlerActive)
  {
    // Update page coordinates
    this._dragCache.pageX = e.getPageX();
    this._dragCache.pageY = e.getPageY();

    // Get current target
    var currentDropTarget = this.getDropTarget(e);

    // Update action
    this.setCurrentAction(currentDropTarget ? this._evalNewAction(e.getShiftKey(), e.getCtrlKey(), e.getAltKey()) : null);

    // Fire user events
    this._fireUserEvents(this._dragCache.currentDropWidget, currentDropTarget, e);

    // Store current widget
    this._dragCache.currentDropWidget = currentDropTarget;

    // Update cursor icon
    this._renderCursor();
  }

  /*
    Initial activation and fire of dragstart
  */
  else if (!this._dragCache.hasFiredDragStart)
  {
    if (Math.abs(e.getScreenX() - this._dragCache.startScreenX) > 5 || Math.abs(e.getScreenY() - this._dragCache.startScreenY) > 5)
    {
      // Fire dragstart event to finally allow the above if to handle next events
      this._dragCache.sourceWidget.dispatchEvent(new qx.event.type.DragEvent("dragstart", e, this._dragCache.sourceWidget), true);

      // Update status flag
      this._dragCache.hasFiredDragStart = true;

      // Look if handler become active
      if (this._dragCache.dragHandlerActive)
      {
        // Fire first user events
        this._fireUserEvents(this._dragCache.currentDropWidget, this._dragCache.sourceWidget, e);

        // Update status flags
        this._dragCache.currentDropWidget = this._dragCache.sourceWidget;

        // Activate capture for clientDocument
        qx.ui.core.ClientDocument.getInstance().setCapture(true);
      }
    }
  }
}

/*!
Handle mouse up event. Normally this finalize the drag and drop event.
*/
qx.Proto._handleMouseUp = function(e)
{
  // Return if dragCache was not filled before
  if (!this._dragCache) {
    return;
  }

  if (this._dragCache.dragHandlerActive)
  {
    this._endDrag(this.getDropTarget(e), e);
  }
  else
  {
    // Clear drag cache
    this._dragCache = null;
  }
}







/*
---------------------------------------------------------------------------
  HANDLER FOR KEY EVENTS
---------------------------------------------------------------------------
*/

/*!
This wraps the key events to custom handlers.
*/
qx.Proto.handleKeyEvent = function(e)
{
  if (!this._dragCache) {
    return;
  }

  switch (e.getType())
  {
    case "keydown":
      this._handleKeyDown(e);
      return;

    case "keyup":
      this._handleKeyUp(e);
      return;
  }
}

qx.Proto._handleKeyDown = function(e)
{
  // Stop Drag on Escape
  if (e.getKeyIdentifier() == "Escape")
  {
    this.cancelDrag(e);
  }

  // Update cursor and action on press of modifier keys
  else if (this.getCurrentAction() != null)
  {
    // TODO this doesn't work in WebKit because WebKit doesn't fire keyevents for modifier keys
    switch(e.getKeyIdentifier())
    {
      case "Shift":
      case "Control":
      case "Alt":
        this.setAction(this._evalNewAction(e.getShiftKey(), e.getCtrlKey(), e.getAltKey()));
        this._renderCursor();

        e.preventDefault();
    }
  }
}

qx.Proto._handleKeyUp = function(e)
{
  // TODO this doesn't work in WebKit because WebKit doesn't fire keyevents for modifier keys
  var bShiftPressed = e.getKeyIdentifier() == "Shift";
  var bCtrlPressed = e.getKeyIdentifier() == "Control";
  var bAltPressed = e.getKeyIdentifier() == "Alt";

  if (bShiftPressed || bCtrlPressed || bAltPressed)
  {
    if (this.getCurrentAction() != null)
    {
      this.setAction(this._evalNewAction(!bShiftPressed && e.getShiftKey(), ! bCtrlPressed && e.getCtrlKey(), !bAltPressed && e.getAltKey()));
      this._renderCursor();

      e.preventDefault();
    }
  }
}









/*
---------------------------------------------------------------------------
  IMPLEMENTATION OF DRAG&DROP SESSION FINALISATION
---------------------------------------------------------------------------
*/

/*!
  Cancel current drag and drop session
*/
qx.Proto.cancelDrag = function(e) {
  this._endDrag(null, e);
}

qx.Proto.globalCancelDrag = function()
{
  if (this._dragCache && this._dragCache.dragHandlerActive) {
    this._endDragCore();
  }
}

/*!
  This will be called to the end of each drag and drop session
*/
qx.Proto._endDrag = function(currentDestinationWidget, e)
{
  // Use given destination widget
  if (currentDestinationWidget)
  {
    this._lastDestinationEvent = e;
    this.setDestinationWidget(currentDestinationWidget);
  }

  // Dispatch dragend event
  this.getSourceWidget().dispatchEvent(new qx.event.type.DragEvent("dragend", e, this.getSourceWidget(), currentDestinationWidget), true);

  // Fire dragout event
  this._fireUserEvents(this._dragCache && this._dragCache.currentDropWidget, null, e);

  // Call helper
  this._endDragCore();
}

qx.Proto._endDragCore = function()
{
  // Remove cursor
  var oldCursor = this.getCursor();
  if (oldCursor)
  {
    oldCursor._style.display = "none";
    this.forceCursor(null);
  }

  // Reset drag cache for next drag and drop session
  if (this._dragCache)
  {
    this._dragCache.currentDropWidget = null;
    this._dragCache = null;
  }

  // Deactivate capture for clientDocument
  qx.ui.core.ClientDocument.getInstance().setCapture(false);

  // Cleanup data and actions
  this.clearData();
  this.clearActions();

  // Cleanup widgets
  this.setSourceWidget(null);
  this.setDestinationWidget(null);
}









/*
---------------------------------------------------------------------------
  IMPLEMENTATION OF CURSOR UPDATES
---------------------------------------------------------------------------
*/

/*!
  Select and setup the current used cursor
*/
qx.Proto._renderCursor = function()
{
  var vNewCursor;
  var vOldCursor = this.getCursor();

  switch(this.getCurrentAction())
  {
    case this._actionNames.move:
      vNewCursor = this._cursors.move;
      break;

    case this._actionNames.copy:
      vNewCursor = this._cursors.copy;
      break;

    case this._actionNames.alias:
      vNewCursor = this._cursors.alias;
      break;

    default:
      vNewCursor = this._cursors.nodrop;
  }

  // Hide old cursor
  if (vNewCursor != vOldCursor && vOldCursor != null) {
    vOldCursor._style.display = "none";
  }

  // Ensure that the cursor is created
  if (!vNewCursor._initialLayoutDone)
  {
    qx.ui.core.ClientDocument.getInstance().add(vNewCursor);
    qx.ui.core.Widget.flushGlobalQueues();
  }

  // Apply position with runtime style (fastest qooxdoo method)
  vNewCursor._applyRuntimeLeft(this._dragCache.pageX + 5);
  vNewCursor._applyRuntimeTop(this._dragCache.pageY + 15);

  // Finally show new cursor
  if (vNewCursor != vOldCursor) {
    vNewCursor._style.display = "";
  }

  // Store new cursor
  this.forceCursor(vNewCursor);
}








/*
---------------------------------------------------------------------------
  IMPLEMENTATION OF DROP TARGET VALIDATION
---------------------------------------------------------------------------
*/

qx.Proto.supportsDrop = function(vWidget)
{
  var vTypes = vWidget.getDropDataTypes();

  if (!vTypes) {
    return false;
  }

  for (var i=0; i<vTypes.length; i++)
  {
    if (vTypes[i] in this._data) {
      return true;
    }
  }

  return false;
}

/*!
#param e[qx.event.type.MouseEvent]: Current MouseEvent for dragdrop action
*/
if (qx.sys.Client.getInstance().isGecko())
{
  qx.Proto.getDropTarget = function(e)
  {
    var vCurrent = e.getTarget();

    // work around gecko bug (all other browsers are correct)
    // clicking on a free space and drag prohibit the get of
    // a valid event target. The target is always the element
    // which was the one with the mousedown event before.
    if (vCurrent == this._dragCache.sourceWidget)
    {
      // vCurrent = qx.event.handler.EventHandler.getTargetObject(qx.dom.ElementFromPoint.getElementFromPoint(e.getPageX(), e.getPageY()));

      // this is around 8-12 times faster as the above method
      vCurrent = this._dragCache.sourceTopLevel.getWidgetFromPoint(e.getPageX(), e.getPageY());
    }
    else
    {
      vCurrent = qx.event.handler.EventHandler.getTargetObject(null, vCurrent);
    }

    while (vCurrent != null && vCurrent != this._dragCache.sourceWidget)
    {
      if (!vCurrent.supportsDrop(this._dragCache)) {
        return null;
      }

      if (this.supportsDrop(vCurrent)) {
        return vCurrent;
      }

      vCurrent = vCurrent.getParent();
    }

    return null;
  }
}
else
{
  qx.Proto.getDropTarget = function(e)
  {
    var vCurrent = e.getTarget();

    while (vCurrent != null)
    {
      if (!vCurrent.supportsDrop(this._dragCache)) {
        return null;
      }

      if (this.supportsDrop(vCurrent)) {
        return vCurrent;
      }

      vCurrent = vCurrent.getParent();
    }

    return null;
  }
}









/*
---------------------------------------------------------------------------
  ACTION HANDLING
---------------------------------------------------------------------------
*/

qx.Proto.addAction = function(vAction, vForce)
{
  this._actions[vAction] = true;

  // Defaults to first added action
  if (vForce || this.getCurrentAction() == null) {
    this.setCurrentAction(vAction);
  }
}

qx.Proto.clearActions = function()
{
  this._actions = {};
  this.setCurrentAction(null);
}

qx.Proto.removeAction = function(vAction)
{
  delete this._actions[vAction];

  // Reset current action on remove
  if (this.getCurrentAction() == vAction) {
    this.setCurrentAction(null);
  }
}

qx.Proto.setAction = function(vAction)
{
  if (vAction != null && !(vAction in this._actions)) {
    this.addAction(vAction, true);
  }
  else
  {
    this.setCurrentAction(vAction);
  }
}

qx.Proto._evalNewAction = function(vKeyShift, vKeyCtrl, vKeyAlt)
{
  if (vKeyShift && vKeyCtrl && this._actionNames.alias in this._actions)
  {
    return this._actionNames.alias;
  }
  else if (vKeyShift && vKeyAlt && this._actionNames.copy in this._actions)
  {
    return this._actionNames.copy;
  }
  else if (vKeyShift && this._actionNames.move in this._actions)
  {
    return this._actionNames.move;
  }
  else if (vKeyAlt && this._actionNames.alias in this._actions)
  {
    return this._actionNames.alias;
  }
  else if (vKeyCtrl && this._actionNames.copy in this._actions)
  {
    return this._actionNames.copy;
  }
  else
  {
    // Return the first action found
    for (var vAction in this._actions) {
      return vAction;
    }
  }

  return null;
}









/*
---------------------------------------------------------------------------
  DISPOSER
---------------------------------------------------------------------------
*/

qx.Proto.dispose = function()
{
  if (this.getDisposed()) {
    return;
  }

  // Reset drag cache for next drag and drop session
  if (this._dragCache)
  {
    this._dragCache.currentDropWidget = null;
    this._dragCache = null;
  }

  // Cleanup data and actions
  this._data = null;
  this._actions = null;
  this._actionNames = null;

  this._lastDestinationEvent = null;

  if (this._cursors)
  {
    if (this._cursors.move)
    {
      this._cursors.move.dispose();
      delete this._cursors.move;
    }

    if (this._cursors.copy)
    {
      this._cursors.copy.dispose();
      delete this._cursors.copy;
    }

    if (this._cursors.alias)
    {
      this._cursors.alias.dispose();
      delete this._cursors.alias;
    }

    if (this._cursors.nodrop)
    {
      this._cursors.nodrop.dispose();
      delete this._cursors.nodrop;
    }

    this._cursors = null;
  }

  return qx.manager.object.ObjectManager.prototype.dispose.call(this);
}








/*
---------------------------------------------------------------------------
  DEFER SINGLETON INSTANCE
---------------------------------------------------------------------------
*/

/**
 * Singleton Instance Getter
 */
qx.Class.getInstance = qx.util.Return.returnInstance;